forked from Nifyr/Imposters-Ordeal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlatformUtils.cs
148 lines (133 loc) · 4.63 KB
/
PlatformUtils.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace ImpostersOrdeal
{
internal class PlatformUtils
{
public static string[] GetPathList()
{
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
var pathString = Environment.GetEnvironmentVariable("PATH");
return pathString?.Split(';');
}
else if (Environment.OSVersion.Platform == PlatformID.Unix)
{
var pathString = Environment.GetEnvironmentVariable("PATH");
return pathString?.Split(':');
}
else
{
return null;
}
}
public static bool UsingMsys()
{
var msystemString = Environment.GetEnvironmentVariable("MSYSTEM");
return msystemString == "MINGW64" || msystemString == "MINGW32";
}
public static string[] FindPythonExePaths(string[] pathList)
{
HashSet<string> pythonNames;
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
pythonNames = new() { "python.exe", "python3.exe", "py.exe", "py3.exe" };
}
else if (Environment.OSVersion.Platform == PlatformID.Unix)
{
pythonNames = new() { "python3", "python" };
}
else
{
return null;
}
var pythonExePaths = new List<string>();
foreach (var path in pathList)
{
foreach (var name in pythonNames)
{
var possiblePythonExePath = Path.Combine(path, name);
if (File.Exists(possiblePythonExePath))
{
pythonExePaths.Add(possiblePythonExePath);
}
}
}
return pythonExePaths.ToArray();
}
public static (int, int) GetPythonVersion()
{
var commandLine = "import sys; print(sys.version_info.major, sys.version_info.minor, 'kwgood')";
var processStartInfo = new ProcessStartInfo
{
FileName = "python",
Arguments = $"-c \"{commandLine}\"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
var process = Process.Start(processStartInfo);
if (process == null || !process.WaitForExit(5000))
{
return (0, 0);
}
var result = process.StandardOutput.ReadToEnd().Trim();
var splits = result.Split(' ');
if (splits.Length != 3 || splits[2] != "kwgood")
{
return (0, 0);
}
if (!int.TryParse(splits[0], out var majorVer) || !int.TryParse(splits[1], out var minorVer))
{
return (0, 0);
}
return (majorVer, minorVer);
}
public static string FindPythonLibPath(string[] pathList)
{
var exePaths = FindPythonExePaths(pathList);
if (exePaths.Length == 0)
{
return null;
}
foreach (var path in exePaths)
{
var (majorVer, minorVer) = GetPythonVersion();
if (majorVer == 0 && minorVer == 0)
{
continue;
}
var dir = Path.GetDirectoryName(path);
if (dir == null)
{
continue;
}
string pythonLibName;
if (Environment.OSVersion.Platform == PlatformID.Win32NT && !UsingMsys())
{
pythonLibName = Path.Combine(dir, $"python{majorVer}{minorVer}.dll");
}
else if (Environment.OSVersion.Platform == PlatformID.Unix || UsingMsys())
{
pythonLibName = Path.Combine(dir, $"libpython{majorVer}.{minorVer}.so");
}
else
{
// shouldn't happen
return null;
}
foreach (var searchPath in pathList)
{
var possiblePythonExePath = Path.Combine(searchPath, pythonLibName);
if (File.Exists(possiblePythonExePath))
{
return possiblePythonExePath;
}
}
}
return null;
}
}
}