forked from Buttys/DevProLauncher
/
Program.cs
348 lines (303 loc) · 12.1 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
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
using System.Threading;
using System.Windows.Forms;
using System;
using System.Net;
using System.IO;
using System.Diagnostics;
using YGOPro_Launcher.Config;
using YGOPro_Launcher.Login;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using System.Collections.Generic;
using YGOPro_Launcher.Chat;
using YGOPro_Launcher.FileManager;
namespace YGOPro_Launcher
{
static class Program
{
public const string Version = "183700";
public static Configuration Config;
public static LanguageManager LanguageManager;
public static NetClient ServerConnection;
public static ChatClient ChatServer = new ChatClient();
public static UserData UserInfo;
public const string ConfigurationFilename = "launcher.conf";
public static Login_frm LoginWindow;
public static Authenticator LoginService;
public static List<Server> ServerList = new List<Server>();
public static Main_frm MainForm;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
bool forcelogin = false;
foreach(string arg in args)
{
if (arg == "-r")
{
int timeout = 0;
while (LauncherHelper.checkInstance())
{
if (timeout == 3)
{
if (MessageBox.Show(LanguageManager.Translation.pmsbProgRun) == DialogResult.OK)
{
return;
}
}
Thread.Sleep(500);
timeout++;
}
}
if (arg == "-l")
{
forcelogin = true;
}
}
Config = new Configuration();
LoadConfig(Program.ConfigurationFilename);
#if DEBUG
Config.DebugMode = true;
#endif
//if (Config.ConfigReset181000)
//{
// if (MessageBox.Show("Major changes were made this update and requires user settings to be reset.") == DialogResult.OK)
// {
// Config = new Configuration();
// Config.ConfigReset181000 = false;
// SaveConfig(ConfigurationFilename, Config);
// Process process = new Process();
// ProcessStartInfo startInfos = new ProcessStartInfo(Application.ExecutablePath, "-r");
// process.StartInfo = startInfos;
// process.Start();
// Application.Exit();
// }
//}
//new update server - Forced change to prevent resting a users config
Config.UpdaterAddress = "http://ygopro.de/launcher/checkversion.php";
Config.ServerInfoAddress = "http://ygopro.de/launcher/serverinfo.php";
if (!Config.DebugMode)
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
if (forcelogin)
{
Config.AutoLogin = false;
}
LanguageManager = new LanguageManager();
//LanguageManager.Save("English");
LanguageManager.Load(Config.Language);
if (LauncherHelper.checkInstance())
if (MessageBox.Show(LanguageManager.Translation.pmsbProgRun) == DialogResult.OK)
return;
UserInfo = new UserData();
ServerConnection = new NetClient();
if (!Config.DebugMode)
{
if (CheckUpdates())
return;
CheckServerInfo();
}
#if DEBUG
ServerList.Add(new Server() { ServerName = "Debug", ServerAddress = "86.0.24.143", ServerPort = 7922, GamePort = 7911, ChatAddress = "86.0.24.143", ChatPort = 6666 });
#endif
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (Program.Config.NewUpdate)
{
LauncherHelper.CardManager = new CardDatabase.CardsManager();
LauncherHelper.CardManager.Init();
if (LauncherHelper.CardManager.Loaded)
{
FileCleaner cleaner = new FileCleaner();
cleaner.ShowDialog();
}
}
Server serverinfo = null;
foreach (Server server in ServerList)
{
if (server.ServerName == Config.DefaultServer)
{
serverinfo = server;
Config.ServerAddress = serverinfo.ServerAddress; Config.GamePort = serverinfo.GamePort; Config.ServerPort = serverinfo.ServerPort;
Config.ChatServerAddress = serverinfo.ChatAddress; Config.ChatPort = serverinfo.ChatPort; Config.ServerName = serverinfo.ServerName;
}
}
LoginService = new Authenticator(Config.DefaultUsername, Config.Password, ServerConnection, UserInfo);
if (serverinfo != null)
{
if (!ServerConnection.Connect(Config.ServerName,Config.ServerAddress, Config.ServerPort))
{
MessageBox.Show(LanguageManager.Translation.pMsbErrorToServer);
}
if (Config.AutoLogin && Config.DefaultUsername.Length < 15)
{
LoginService.Authenticate();
Thread.Sleep(2000);
}
}
if (UserInfo.Username == "" && UserInfo.LoginKey == "")
{
LoginWindow = new Login_frm(Config, ServerConnection, LoginService);
if (LoginWindow.ShowDialog() != DialogResult.OK)
{
return;
}
}
if (!ServerConnection.IsConnected)
{
return;
}
if (UserInfo.Username != "" && UserInfo.LoginKey != "")
{
if(forcelogin)
LoadConfig(ConfigurationFilename);
MainForm = new Main_frm();
Application.Run(MainForm);
}
else
{
Config.AutoLogin = false;
SaveConfig(ConfigurationFilename, Config);
MessageBox.Show(LanguageManager.Translation.pMsbBadLog);
}
}
public static void ChangeUsername(string username)
{
UserInfo.Username = username;
MainForm.UpdateUsername(username);
}
public static bool CheckUpdates()
{
string updateLink = Config.UpdaterAddress;
const string updaterName = "YgoUpdater.exe";
const string dllName = "ICSharpCode.SharpZipLib.dll";
WebClient client = new WebClient { Proxy = null };
string result = "";
try
{
result= client.DownloadString(updateLink + "?v=" + Version);
}
catch
{
Console.WriteLine("Unable to connect to update server");
return false;
}
if (result.Equals("OK"))
return false;
if (result.Equals("KO"))
{
MessageBox.Show(LanguageManager.Translation.pMsbOldVers,
"DevPro - Update", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
if (result == "")
return false;
if (result.Length > 70)
return false;
if (result.Contains("|"))
{
Config.NewUpdate = true;
SaveConfig(ConfigurationFilename, Config);
string[] data = result.Split(new string[] { "|" },StringSplitOptions.RemoveEmptyEntries);
File.WriteAllBytes(Path.Combine(Application.StartupPath, updaterName), Properties.Resources.YgoUpdater);
File.WriteAllBytes(Path.Combine(Application.StartupPath, dllName), Properties.Resources.ICSharpCode_SharpZipLib);
Process updateProcess = new Process();
updateProcess.StartInfo.FileName = Path.Combine(Application.StartupPath, updaterName);
updateProcess.StartInfo.Arguments = data[1] + " " + Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().Location);
updateProcess.Start();
}
return true;
}
public static bool CheckServerInfo()
{
string updateLink = Config.ServerInfoAddress;
WebClient client = new WebClient { Proxy = null };
string result = "";
try
{
result = client.DownloadString(updateLink + "?v=" + Version);
}
catch
{
ServerList.Add(new Server()
{ ServerName = Config.ServerName, ServerAddress = Config.ServerAddress, ServerPort = Config.ServerPort,
GamePort = Config.GamePort, ChatAddress = Config.ChatServerAddress, ChatPort = Config.ChatPort });
return false;
}
if (result == "KO")
return false;
if (result == "")
return false;
try
{
string[] servers = result.Split(new string[]{";"},StringSplitOptions.RemoveEmptyEntries);
foreach (string server in servers)
{
string[] infos = server.Split(',');
if (infos.Length > 6)
return false;
ServerList.Add(new Server()
{ ServerName = infos[0], ServerAddress = infos[1], ServerPort = Convert.ToInt32(infos[2]),
GamePort = Convert.ToInt32(infos[3]), ChatAddress = infos[5], ChatPort = Convert.ToInt32(infos[4]) });
}
}
catch
{
MessageBox.Show("Incorrect server string format");
return false;
}
return true;
}
private static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
Exception exception = e.ExceptionObject as Exception ?? new Exception();
File.WriteAllText("crash_" + DateTime.UtcNow.ToString("yyyy-MM-dd_HH-mm-ss") + ".txt", exception.ToString());
ErrorReport_frm error = new ErrorReport_frm(exception);
error.ShowDialog();
Console.WriteLine(exception.ToString());
Process.GetCurrentProcess().Kill();
}
public static void SaveConfig(string filename, Configuration config)
{
try
{
XmlSerializer serializer = new XmlSerializer(typeof(Configuration));
TextWriter textWriter = new StreamWriter(filename);
serializer.Serialize(textWriter, config);
textWriter.Close();
}
catch (Exception)
{
MessageBox.Show("Error Saving " + filename);
}
}
public static void LoadConfig(string filename)
{
if (!File.Exists(filename))
{
SaveConfig(filename, new Configuration());
}
try
{
XmlSerializer deserializer = new XmlSerializer(typeof(Configuration));
TextReader textReader = new StreamReader(filename);
Program.Config = (Configuration)deserializer.Deserialize(textReader);
textReader.Close();
}
catch (Exception)
{
MessageBox.Show("Error Loading " + filename);
}
}
}
public class Server
{
public string ServerName;
public string ServerAddress;
public int ServerPort;
public int GamePort;
public string ChatAddress;
public int ChatPort;
}
}