Skip to content

Instantly share code, notes, and snippets.

@grapefrukt
Created January 13, 2014 22:09
Show Gist options
  • Save grapefrukt/8409089 to your computer and use it in GitHub Desktop.
Save grapefrukt/8409089 to your computer and use it in GitHub Desktop.
Due to incompatibilities I needed a way to detect the version of Windows my game was running on. Sys.systemName() helpfully returns "Windows" and nothing more, so I had to get creative.
package com.grapefrukt.utils;
/**
* Retrieves the version of Windows the application is running under
* @author Martin Jonasson, m@grapefrukt.com
*/
class WindowsVersion {
private static var inited:Bool = false;
private static var _major:Int = 0;
private static var _minor:Int = 0;
public static var major(get, never):Int;
public static var minor(get, never):Int;
public static var isWindows(get, never):Bool;
private static function init() {
inited = true;
#if cpp
// if we're not on windows, this won't work at all
if (!isWindows) return;
// call out to cmd.exe to get the version (it's listed when it starts up)
var process = new sys.io.Process(Sys.getEnv("windir") + '\\system32\\cmd.exe', []);
var output = "";
// try to read the stdout, this crashes if there's nothing to read, hence the try/catch
try {
output = process.stdout.readLine();
} catch (e:Dynamic) {
// call failed, not much to do about it
}
// regex out the values
var r = ~/Version (\d+)\.(\d+)/g;
if (r.match(output)) {
_major = Std.parseInt(r.matched(1));
_minor = Std.parseInt(r.matched(2));
}
// finally, close the process, it should be closed already, but just to make sure
process.close();
#end
}
private static function get_major() {
if (!inited) init();
return _major;
}
private static function get_minor() {
if (!inited) init();
return _minor;
}
private static function get_isWindows() {
return Sys.systemName() == "Windows";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment