forked from CnCNet/dta-mg-client-launcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
260 lines (216 loc) · 7.49 KB
/
Program.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
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
using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
namespace DTALauncherStub
{
class Program
{
private const string RESOURCES = "Resources";
private const int ERROR_CANCELLED_CODE = 1223;
private const int NET_FRAMEWORK_45_RELEASE_KEY = 378389;
private static void Main(string[] args)
{
var osVersion = GetOperatingSystemVersion();
char dsc = Path.DirectorySeparatorChar;
if (args != null)
{
foreach (string arg in args)
{
if (arg.ToUpper() == "-XNA")
{
RunXNA();
return;
}
else if (arg.ToUpper() == "-OGL")
{
RunOGL();
return;
}
else if (arg.ToUpper() == "-DX")
{
RunDX();
return;
}
}
}
switch (osVersion)
{
case OSVersion.WINXP:
case OSVersion.WINVISTA:
if (!IsNetFramework4Installed())
{
Application.Run(new NETFramework4MissingMessageForm());
break;
}
RunXNA();
break;
case OSVersion.WIN7:
case OSVersion.WIN810:
W7And10Autorun();
break;
case OSVersion.UNIX:
case OSVersion.UNKNOWN:
RunOGL();
break;
}
}
private static void RunXNA()
{
if (!IsXNAFramework4Installed())
{
Application.Run(new XNAFrameworkMissingMessageForm());
return;
}
StartProcess(RESOURCES + Path.DirectorySeparatorChar + "clientxna.exe");
}
private static void RunOGL()
{
StartProcess(RESOURCES + Path.DirectorySeparatorChar + "clientogl.exe");
}
private static void RunDX()
{
if (GetOperatingSystemVersion() == OSVersion.WIN7)
{
if (!IsNetFramework45Installed())
{
Application.Run(new NETFramework45MissingMessageForm());
return;
}
}
StartProcess(RESOURCES + Path.DirectorySeparatorChar + "clientdx.exe");
}
private static void W7And10Autorun()
{
string basePath = Environment.CurrentDirectory +
Path.DirectorySeparatorChar + "Client" + Path.DirectorySeparatorChar;
string dxFailFilePath = basePath + ".dxfail";
if (File.Exists(dxFailFilePath))
{
if (IsXNAFramework4Installed())
{
RunXNA();
return;
}
DialogResult dr = new IncompatibleGPUMessageForm().ShowDialog();
if (dr == DialogResult.No)
{
File.Delete(dxFailFilePath);
RunDX();
}
else if (dr == DialogResult.Yes)
{
RunXNA();
}
return;
}
RunDX();
}
private static void StartProcess(string relativePath)
{
string completeFilePath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + relativePath;
if (!File.Exists(completeFilePath))
{
MessageBox.Show("Main client executable (" + relativePath + ") not found!",
"Client Launcher Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
Process.Start(completeFilePath);
}
catch (Win32Exception ex)
{
if (ex.NativeErrorCode == ERROR_CANCELLED_CODE)
{
MessageBox.Show("Unable to launch the main client. It could be blocked by Windows SmartScreen."
+ Environment.NewLine + Environment.NewLine +
"Please try to launch the following file manually: " + relativePath
+ Environment.NewLine + Environment.NewLine +
"If the client still doesn't run, please contact the mod's authors for support.",
"Client Launcher Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
private static OSVersion GetOperatingSystemVersion()
{
Version osVersion = Environment.OSVersion.Version;
if (Environment.OSVersion.Platform == PlatformID.Win32Windows)
return OSVersion.WIN9X;
if (Environment.OSVersion.Platform == PlatformID.Win32NT)
{
if (osVersion.Major < 5)
return OSVersion.UNKNOWN;
if (osVersion.Major == 5)
return OSVersion.WINXP;
if (osVersion.Minor > 1)
return OSVersion.WIN810;
else if (osVersion.Minor == 0)
return OSVersion.WINVISTA;
return OSVersion.WIN7;
}
int p = (int)Environment.OSVersion.Platform;
if ((p == 4) || (p == 6) || (p == 128))
{
return OSVersion.UNIX;
}
return OSVersion.UNKNOWN;
}
private static bool IsNetFramework4Installed()
{
try
{
RegistryKey ndpKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full");
string installValue = ndpKey.GetValue("Install").ToString();
if (installValue == "1")
return true;
}
catch
{
}
return false;
}
private static bool IsNetFramework45Installed()
{
try
{
RegistryKey ndpKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\NET Framework Setup\\NDP\\v4\\Full");
string installValue = ndpKey.GetValue("Release").ToString();
// https://docs.microsoft.com/en-us/dotnet/framework/migration-guide/how-to-determine-which-versions-are-installed#net_d
if (Convert.ToInt32(installValue) >= NET_FRAMEWORK_45_RELEASE_KEY)
return true;
}
catch
{
}
return false;
}
private static bool IsXNAFramework4Installed()
{
try
{
RegistryKey xnaKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\XNA\\Framework\\v4.0");
string installValue = xnaKey.GetValue("Installed").ToString();
if (installValue == "1")
return true;
}
catch
{
}
return false;
}
}
enum OSVersion
{
UNKNOWN,
WIN9X,
WINXP,
WINVISTA,
WIN7,
WIN810,
UNIX
}
}