This repository was archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathProgram.cs
246 lines (214 loc) · 8.31 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
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevProLauncher.Windows;
using DevProLauncher.Network;
using DevProLauncher.Network.Data;
using DevProLauncher.Config;
using System.IO;
using System.Xml.Serialization;
using System.Net;
using System.Diagnostics;
using DevProLauncher.Windows.MessageBoxs;
using DevProLauncher.Helpers;
namespace DevProLauncher
{
static class Program
{
public const string Version = "199700";
public static Configuration Config;
public static LanguageManager LanguageManager;
public static ChatClient ChatServer;
public static UserData UserInfo;
public static int LoginKey = 0;
public const string ConfigurationFilename = "launcher.conf";
public static Dictionary<string, ServerInfo> ServerList = new Dictionary<string, ServerInfo>();
public static MainFrm MainForm;
public static ServerInfo Server;
public static Dictionary<string,ServerInfo> CheckmateServerList = new Dictionary<string,ServerInfo>();
public static Random Rand = new Random();
[STAThread]
static void Main()
{
LoadConfig(ConfigurationFilename);
#if !DEBUG
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
#endif
//new update server - Forced change to prevent reseting a users config
Config.UpdaterAddress = "/launcher/version.php";
Config.ServerInfoAddress = "/launcher/server.php";
LanguageManager = new LanguageManager();
LanguageManager.Load(Config.Language);
if (LauncherHelper.CheckInstance())
if (MessageBox.Show(LanguageManager.Translation.pmsbProgRun) == DialogResult.OK)
return;
ChatServer = new ChatClient();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
CheckmateServerList.Add("Checkmate USA+CN", new ServerInfo("Checkmate USA+CN", "173.224.211.158", 21001));
CheckmateServerList.Add("Checkmate EU",new ServerInfo("Checkmate EU", "94.247.40.146", 7980));
if (LauncherHelper.TestConnection())
{
#if !DEBUG
if (NewUpdateCheck())
return;
if (!CheckServerInfo("http://91.250.87.52"))
CheckServerInfo("http://ygopro.de");
#endif
}
else MessageBox.Show("An internet connection is required to play online.");
#if DEBUG
Config.ServerAddress = "127.0.0.1";
Config.ChatPort = 8933;
Server = new ServerInfo("DevPro", "127.0.0.1", 3333);
#endif
MainForm = new MainFrm();
Application.Run(MainForm);
}
public static void SaveConfig(string filename, Configuration config)
{
try
{
var 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))
{
Config = new Configuration();
SaveConfig(filename, Config);
}
else
{
try
{
var deserializer = new XmlSerializer(typeof(Configuration));
TextReader textReader = new StreamReader(filename);
Config = (Configuration)deserializer.Deserialize(textReader);
textReader.Close();
}
catch (Exception)
{
MessageBox.Show("Error Loading " + filename);
}
}
}
public static bool NewUpdateCheck()
{
switch(CheckUpdates("http://91.250.87.52"))
{
case 0:
return (CheckUpdates("http://ygopro.de") == 2);
case 2:
return true;
default:
return false;
}
}
public static int CheckUpdates(string url)
{
string updateLink = Config.UpdaterAddress;
const string updaterName = "YgoUpdater.exe";
const string dllName = "ICSharpCode.SharpZipLib.dll";
var client = new WebClient { Proxy = null };
string result;
try
{
result = client.DownloadString(url + updateLink + "?v=" + Version);
}
catch
{
return 0;
}
if (result.Equals("OK"))
return 1;
if (result.Equals("KO"))
{
MessageBox.Show(LanguageManager.Translation.pMsbOldVers,
"DevPro - Update", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
if (!result.StartsWith("KO"))
{
return 0;
}
if (result.Contains("|"))
{
string updaterPath = Path.GetTempPath();
Config.NewUpdate = true;
SaveConfig(ConfigurationFilename, Config);
string[] data = result.Split(new [] { "|" }, StringSplitOptions.RemoveEmptyEntries);
File.WriteAllBytes(Path.Combine(updaterPath, updaterName), Properties.Resources.YgoUpdater);
File.WriteAllBytes(Path.Combine(updaterPath, dllName), Properties.Resources.ICSharpCode_SharpZipLib);
var updateProcess = new Process
{
StartInfo =
{
FileName = Path.Combine(updaterPath, updaterName),
Arguments =
data[1] + " " +
System.Reflection.Assembly.GetExecutingAssembly().Location
}
};
updateProcess.Start();
}
return 2;
}
public static bool CheckServerInfo(string url)
{
string updateLink = Config.ServerInfoAddress;
var client = new WebClient { Proxy = null };
string result;
try
{
result = client.DownloadString(url + updateLink + "?v=" + Version);
}
catch
{
return false;
}
if (result.Equals("KO"))
return false;
if (!result.StartsWith("OK"))
return false;
try
{
string[] serverinfo = result.Split(new [] { "|" }, StringSplitOptions.RemoveEmptyEntries);
Config.ServerAddress = serverinfo[1];
Config.ChatPort = int.Parse(serverinfo[2]);
Config.GamePort = int.Parse(serverinfo[3]);
Server = new ServerInfo("DevPro",serverinfo[1],int.Parse(serverinfo[4]));
//CheckmateServerList.Clear();
//if(!CheckmateServerList.ContainsKey("Checkmate"))
// CheckmateServerList.Add("Checkmate",new ServerInfo("Checkmate", serverinfo[5], int.Parse(serverinfo[6])));
}
catch
{
MessageBox.Show("Incorrect server string format");
return false;
}
return true;
}
public static void ChangeUsername(string username)
{
UserInfo.username = username;
MainForm.UpdateUsername();
}
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());
var error = new ErrorReportFrm(exception);
error.ShowDialog();
Console.WriteLine(exception.ToString());
Process.GetCurrentProcess().Kill();
}
}
}