This repository has been archived by the owner on Apr 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathMain.cs
249 lines (220 loc) · 8.19 KB
/
Main.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
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Security.Principal;
using System.Threading;
using System.Windows.Forms;
using EnhancedMap.Core;
using EnhancedMap.Core.Network;
using EnhancedMap.GUI;
using Microsoft.Win32;
namespace EnhancedMap
{
public static class MainCore
{
private static Assembly _assembly;
private static DateTime _expireDate = new DateTime(2018, 6, 1);
public static bool IsRunning { get; private set; }
public static Version MapVersion => _assembly.GetName().Version;
[STAThread]
private static void Main(string[] args)
{
if (!IsUserAdministrator())
{
MessageBox.Show("EnhancedMap requires administrator privileges to run.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (!CheckNet45Installed())
{
if (MessageBox.Show("EnhancedMap requires .Net Framework 4.6.2 to run.\r\nPress OK to download.", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
Process.Start("https://www.microsoft.com/en-us/download/details.aspx?id=53344");
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
#if !DEBUG
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
#endif
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
IsRunning = true;
_assembly = Assembly.GetEntryAssembly();
#if BETA
string text = "";
string a = "";
int processId = -1;
for (int i = 0; i < args.Length; i++)
{
if (args[i] == "--source" && i < args.Length - 1)
{
text = args[i + 1];
}
if (args[i] == "--action" && i < args.Length - 1)
{
a = args[i + 1];
}
if (args[i] == "--pid" && i < args.Length - 1)
{
processId = int.Parse(args[i + 1]);
}
}
if (a == "update")
{
try
{
Process.GetProcessById(processId);
Thread.Sleep(1000);
Process.GetProcessById(processId).Kill();
}
catch
{
}
DirectoryInfo dir = new DirectoryInfo(Path.GetDirectoryName(text));
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
try
{
//File.Delete(file.FullName);
if (file.Extension == ".exe" || file.Extension == ".dll" || file.Extension == ".lua")
file.Delete();
}
catch (Exception e)
{
}
//File.Copy(Application.ExecutablePath, file.FullName);
}
dir = new DirectoryInfo(Application.StartupPath);
files = dir.GetFiles();
foreach (FileInfo file in files)
{
if (file.Extension == ".exe" || file.Extension == ".dll" || file.Extension == ".lua")
File.Copy(file.FullName, Path.Combine(Path.GetDirectoryName(text), file.Name));
}
/*foreach (FileInfo file in fromTempPath.GetFiles())
{
}
*/
/*try
{
File.Delete(text);
}
catch
{
}
File.Copy(Application.ExecutablePath, text);
*/
new Process
{
StartInfo =
{
FileName = text,
UseShellExecute = false,
Arguments =
$"--source \"{Application.ExecutablePath}\" --pid {Process.GetCurrentProcess().Id} --action cleanup"
}
}.Start();
return;
}
if (a == "cleanup")
{
try
{
Process.GetProcessById(processId);
Thread.Sleep(1000);
Process.GetProcessById(processId).Kill();
}
catch (Exception e)
{
}
try
{
DirectoryInfo directory = new DirectoryInfo(text);
directory.Delete(true);
//File.Delete(text);
}
catch (Exception e)
{
}
}
/*if (_expireDate <= DateTime.Now)
{
MessageBox.Show("BETA version is expired...");
return;
}*/
#endif
try
{
Update.CheckUpdates();
}
catch
{
}
FilesManager.Load();
Application.Run(new MainWindow());
}
}
private static bool IsUserAdministrator()
{
try
{
var user = WindowsIdentity.GetCurrent();
var principal = new WindowsPrincipal(user);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
catch (UnauthorizedAccessException e)
{
}
catch (Exception e)
{
}
return false;
}
private static bool CheckNet45Installed()
{
const string subkey = @"SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\";
using (RegistryKey ndpKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry32).OpenSubKey(subkey))
{
if (ndpKey != null && ndpKey.GetValue("Release") != null)
{
int version = (int) ndpKey.GetValue("Release");
if (version >= 394802)
{
Console.WriteLine(".NET Framework found: {0}", version);
return true;
}
}
else
Console.WriteLine(".NET Framework Version 4.6.2 is not detected.");
}
return false;
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
IsRunning = false;
try
{
if (e.IsTerminating)
{
new MessageDialog("Unhandled Exception", !e.IsTerminating, e.ExceptionObject.ToString()).ShowDialog(Global.MainWindow);
if (!(e.ExceptionObject is Exception exception) || exception is ThreadAbortException)
return;
using (var txt = new StreamWriter("Crash.log", true))
{
txt.AutoFlush = true;
txt.WriteLine("Exception @ {0}", DateTime.Now.ToString("MM-dd-yy HH:mm:ss.ffff"));
txt.WriteLine(exception.ToString());
txt.WriteLine("");
txt.WriteLine("");
}
}
}
catch (Exception ex)
{
Application.Exit();
}
}
}
}