forked from Buttys/DevProLauncher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
190 lines (158 loc) · 6.31 KB
/
Program.cs
File metadata and controls
190 lines (158 loc) · 6.31 KB
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
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;
namespace YGOPro_Launcher
{
static class Program
{
public const string Version = "131000";
public static Configuration Config;
public static LanguageManager LanguageManager;
public static NetClient ServerConnection;
public static UserData UserInfo;
public const string ConfigurationFilename = "launcher.conf";
public static Login_frm LoginWindow;
public static Authenticator LoginService;
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
Config = new Configuration();
Config.Load(Program.ConfigurationFilename);
if (File.Exists("ygopro_vs.exe") && !File.Exists("devpro.dll"))
{
File.Copy("ygopro_vs.exe", "devpro.dll");
File.Delete("ygopro_vs.exe");
Config.GameExe = "devpro.dll";
Config.Save(ConfigurationFilename);
}
LanguageManager = new LanguageManager();
//LanguageManager.Save("English");
LanguageManager.Load(Config.Language);
if (LauncherHelper.checkInstance() != null)
if (MessageBox.Show(LanguageManager.Translation.pmsbProgRun) == DialogResult.OK)
return;
UserInfo = new UserData();
ServerConnection = new NetClient();
if (!Config.DebugMode)
{
if (CheckUpdates())
return;
CheckServerInfo();
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
LoginService = new Authenticator(Config.DefaultUsername, Config.Password, ServerConnection, UserInfo);
if(!ServerConnection.Connect(Config.ServerAddress, Config.ServerPort))
{
MessageBox.Show(LanguageManager.Translation.pMsbErrorToServer);
}
if (Config.AutoLogin)
{
LoginService.Authenticate();
Thread.Sleep(2000);
}
if (UserInfo.Username == "" && UserInfo.LoginKey == "")
{
LoginWindow = new Login_frm(Config, ServerConnection, LoginService);
if (LoginWindow.ShowDialog() != DialogResult.OK)
{
return;
}
Thread.Sleep(2000);
}
if (!ServerConnection.IsConnected)
{
return;
}
if (UserInfo.Username != "" && UserInfo.LoginKey != "")
Application.Run(new Main_frm());
else
MessageBox.Show(LanguageManager.Translation.pMsbBadLog);
}
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.Contains("|"))
{
string[] data = result.Split('|');
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
{
Console.WriteLine("Unable to connect to update server");
return false;
}
if (result == "KO")
return false;
try
{
string[] infos = result.Split(',');
Config.ServerName = infos[0];
Config.ServerAddress = infos[1];
Config.ServerPort = Convert.ToInt32(infos[2]);
Config.GamePort = Convert.ToInt32(infos[3]);
}
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();
MessageBox.Show(LanguageManager.Translation.pMsbException);
File.WriteAllText("crash_" + DateTime.UtcNow.ToString("yyyy-MM-dd_HH-mm-ss") + ".txt", exception.ToString());
Console.WriteLine(exception.ToString());
Process.GetCurrentProcess().Kill();
}
}
}