-
Notifications
You must be signed in to change notification settings - Fork 33
/
SystemData.hx
418 lines (382 loc) · 10.5 KB
/
SystemData.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
package crashdumper;
#if sys
import sys.io.Process;
#end
#if flash
import flash.system.Capabilities;
#end
/**
* A simple data structure that records data about the user's system.
*
* usage: var s = new SystemData();
*
* @author larsiusprime
*/
class SystemData
{
public var os:String; //simple constant matching the haxedef -- "windows", "mac", "linux", etc
public var osRaw:String; //raw output from command-line OS identifier -- "Microsoft Windows [Version 6.1.7601]"
public var osName:String; //common product name of OS -- "Windows 7", "Ubuntu 14.10", "OSX Snow Leopard", "Android KitKat"
public var osVersion:String; //version number of OS -- "6.1.7601, 14.10, 10.6, 4.4" (for the above 4 examples)
public var totalMemory:Int; //total visible memory, in kilobytes, except for mac where it's in gigabytes
public var cpuName:String; //the name of your cpu, -- "Intel(R) Core(TM)2 Duo CPU E7400 @ 2.80GHz"
public var gpuName:String; //the name of your gpu, -- "ATI Radeon HD 4800 Series"
public var gpuDriverVersion:String; //version number of gpu driver, -- "8.970.100.1100"
#if flash
public var playerVersion:String; //version number of the flash player, ie "WIN 12,0,0,77"
public var playerType:String;
#end
public function new()
{
#if windows
os = "windows";
#elseif mac
os = "mac";
#elseif linux
os = "linux";
#elseif android
os = "android";
#elseif ios
os = "ios";
#elseif flash
osName = flash.system.Capabilities.os + " (flash)";
playerType = flash.system.Capabilities.playerType;
playerVersion = flash.system.Capabilities.version;
cpuName = flash.system.Capabilities.cpuArchitecture;
totalMemory = 0;
gpuName = "unknown";
gpuDriverVersion = "unknown";
#end
try {
#if windows
runProcess("crashdumper/os.bat", [], processOS);
runProcess("crashdumper/memory.bat", [], processMemory);
runProcess("crashdumper/cpu.bat", [], processCPU);
runProcess("crashdumper/gpu.bat", [], processGPU);
#elseif linux
// must set file to executable first
runProcess("chmod", [ "a+x","crashdumper/os.sh"], dummy);
runProcess("chmod", [ "a+x","crashdumper/memory.sh"], dummy);
runProcess("chmod", [ "a+x","crashdumper/cpu.sh"], dummy);
runProcess("chmod", [ "a+x","crashdumper/gpu.sh"], dummy);
runProcess("crashdumper/os.sh", [], processOS);
runProcess("crashdumper/memory.sh", [], processMemory);
runProcess("crashdumper/cpu.sh", [], processCPU);
runProcess("crashdumper/gpu.sh", [], processGPU);
#elseif mac
// must set file to executable first
runProcess("chmod", [ "a+x","crashdumper/os.sh"], dummy);
runProcess("chmod", [ "a+x","crashdumper/memory.sh"], dummy);
runProcess("chmod", [ "a+x","crashdumper/cpu.sh"], dummy);
runProcess("chmod", [ "a+x","crashdumper/gpu.sh"], dummy);
runProcess("crashdumper/os.sh", [], processOS);
runProcess("crashdumper/memory.sh", [], processMemory);
runProcess("crashdumper/cpu.sh", [], processCPU);
runProcess("crashdumper/gpu.sh", [], processGPU);
#end
}
catch (msg:Dynamic)
{
trace("error creating SystemData : " + msg);
}
}
private function dummy(line:String):Void
{
// this is because runprocess() must accept function
}
public function summary():String
{
#if flash
return "SystemData" + endl() +
"{" + endl() +
" OS: " + osName + endl() +
"FLASH: " + playerType + " v. " + playerVersion + endl() +
" CPU: " + cpuName + endl() +
"}";
#else
return "SystemData" + endl() +
"{" + endl() +
" OS : " + osName + endl() +
" RAM: " + totalMemory + " KB (" + toGBStr(totalMemory) + " GB)" + endl() +
" CPU: " + cpuName + endl() +
" GPU: " + gpuName + ", driver v. " + gpuDriverVersion + endl() +
"}";
#end
}
public function toString():String
{
return "SystemData" + "\n"+
"{" + endl() +
" os: " + os + "\n" +
" osRaw: " + osRaw + "\n" +
" osName: " + osName + "\n" +
" osVersion: " + osVersion + "\n" +
#if flash
" playerType: " + playerType + "\n" +
" playerVersion: " + playerVersion + "\n" +
#end
" totalMemory: " + toGBStr(totalMemory) + "\n" +
" cpuName: " + cpuName + "\n" +
" gpuName: " + gpuName + "\n" +
" gpuDriverVersion: " + gpuDriverVersion + "\n" +
"}";
}
private function toMB(kilobytes:Int):Float
{
return kilobytes / (1024);
}
private function toGB(kilobytes:Int):Float
{
return kilobytes / (1024 * 1024);
}
private function toGBStr(kilobytes:Int):String
{
var gb:Float = toGB(kilobytes);
gb = Math.round(gb * 100) / 100;
return Std.string(gb);
}
private function runProcess(commandStr:String, commandArgs:Array<String>, processFunc:String->Void):Void
{
#if sys
var p:Process = null;
try
{
p = new Process(commandStr, commandArgs);
}
catch (msg:String)
{
p = null;
}
if (p != null)
{
p.exitCode();
var str:String = p.stdout.readAll().toString();
p.close();
processFunc(str);
}
#end
}
private function isOneOfThese(char:String,arr:Array<String>):Bool
{
for (str in arr) {
if (char == str) {
return true;
}
}
return false;
}
private function processOS(line:String):Void
{
if (line == null)
{
line = "unknown"; //avoid null error when parsing
}
#if windows
//ver returns something like this: "Microsoft Windows [Version 6.1.7601]", localized
//we wanna strip away everything but the number
line = stripEndLines(line);
osRaw = line;
line = line.toLowerCase();
line = stripWhiteSpace(line);
//chomp away everything before the "[" and after the "]"
if (line.indexOf("[") != -1)
{
while (line.charAt(0) != "[")
{
line = line.substr(1, line.length - 1);
}
}
if (line.indexOf("]") != -1)
{
while (line.charAt(line.length - 1) != "]")
{
line = line.substr(0, line.length - 1);
}
}
//now we have something like this: "[versionX.Y.Z]"
//where X.Y.Z are numbers and "version" is locale-specific
//strip the "[]" chars
line = stripWord(line, "[");
line = stripWord(line, "]");
var numAndDot:Array<String> = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "."];
//strip away all non-number and non-dot characters
if (line.length > 0 && !isOneOfThese(line.charAt(0), numAndDot))
{
while (line.length > 0 && !isOneOfThese(line.charAt(0), numAndDot))
{
line = line.substr(1, line.length - 1);
}
}
//now we have a string we can safely compare against known values
osVersion = line;
//check for windows 10
if (osVersion.substr(0, 2) == "10")
{
osName = checkWindows10Version(osVersion);
}
else
{
switch(line)
{
case "3.10.103": osName = "Windows 3.1";
case "4.00.950": osName = "Windows 95";
case "4.00.1111": osName = "Windows 95 OSR2";
case "4.00.1381": osName = "Windows NT 4.0";
case "4.10.1998": osName = "Windows 98";
case "4.10.2222": osName = "Windows 98 SE";
case "4.90.3000": osName = "Windows ME";
case "5.00.2195": osName = "Windows 2000";
case "5.1.2600": osName = "Windows XP";
case "6.0.6000": osName = "Windows Vista";
case "6.0.6002": osName = "Windows Vista SP2";
case "6.1.7600": osName = "Windows 7";
case "6.1.7601": osName = "Windows 7 SP1";
case "6.2.9200": osName = "Windows 8";
case "6.3.9600": osName = "Windows 8.1";
default: osName = "Windows (unknown version)";
}
}
#elseif linux
var temp = line.split("\n");
if (temp != null && temp.length >= 2)
{
osName = temp[0] + " (" + temp[1] + ")";
}
#elseif mac
line = stripEndLines(line);
osName = line;
#end
}
private function checkWindows10Version(line:String):String
{
//the specific windows 10 version is determined by the last number in the string
var versionNumber:Int = Std.parseInt(line.substr(line.lastIndexOf(".") + 1));
//all versions under 10240 are preview releases
if (versionNumber < 10240)
{
return "Windows 10 (Pre-release)";
}
//10240 is the original release version
else if (versionNumber == 10240)
{
return "Windows 10 (TH1)";
}
//versions up until 10586 are part of the second wave of updates
else if (versionNumber > 10240 && versionNumber <= 10586)
{
return "Windows 10 (TH2)";
}
else
{
return "Windows 10 (Unknown Specific Version)";
}
}
private function processMemory(line:String):Void
{
#if windows
line = stripWhiteSpace(line);
if (line.indexOf("TotalVisibleMemorySize=") != -1)
{
line = stripWord(line,"TotalVisibleMemorySize=");
totalMemory = Std.parseInt(line);
}
#elseif linux
totalMemory = Std.parseInt(line);
#elseif mac
totalMemory = Std.parseInt(line) * 1024 * 1024;
#end
}
private function processCPU(line:String):Void
{
#if windows
line = stripEndLines(line);
if (line != null && line.indexOf("Name=") != -1)
{
cpuName = stripWord(line, "Name=");
}
else
{
cpuName = "unknown";
}
#elseif linux
cpuName = stripWord(line,"\n");
#elseif mac
cpuName = stripEndLines(line);
#end
}
private function processGPU(line:String):Void
{
#if windows
gpuName = "unknown";
gpuDriverVersion = "unknown";
var arr:Array<String> = line.split(",");
if (arr != null && arr.length == 2)
{
for (str in arr)
{
str = stripEndLines(str);
if (str.indexOf("Name=") != -1)
{
gpuName = stripWord(str, "Name=");
}
else if (str.indexOf("DriverVersion=") != -1)
{
gpuDriverVersion = stripWord(str, "DriverVersion=");
}
}
}
#elseif linux
gpuName = line;
gpuDriverVersion = "unknown";
#elseif mac
gpuName = line;
gpuDriverVersion = "unknown";
#end
}
public static function endl():String
{
#if windows
return "\r\n";
#end
return "\n";
}
public static function slash():String
{
#if windows
return "\\";
#end
return "/";
}
public static function replaceWord(line:String, word:String, replace:String):String
{
if (word == replace)
{
return line;
}
while (line.indexOf(word) != -1)
{
line = StringTools.replace(line, word, replace);
}
return line;
}
public static function stripWord(line:String, word:String):String
{
while (line.indexOf(word) != -1)
{
line = StringTools.replace(line, word, "");
}
return line;
}
public static function stripEndLines(str:String):String
{
str = stripWord(str, "\n");
str = stripWord(str, "\r");
return str;
}
public static function stripWhiteSpace(str:String):String
{
str = stripWord(str, " ");
str = stripWord(str, "\t");
return str;
}
}