forked from Buttys/DevProLauncher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
174 lines (144 loc) · 5.69 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
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;
namespace YGOPro_Launcher
{
static class Program
{
public static string Version = "250000";
public static Configuration Config;
public static NetClient ServerConnection;
public static UserData UserInfo;
public static string ConfigurationFilename = "launcher.conf";
//public static Main MainForm;
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;
if (LauncherHelper.checkInstance() != null)
if (MessageBox.Show("Program already running") == DialogResult.OK)
return;
Config = new Configuration();
Config.Load(Program.ConfigurationFilename);
UserInfo = new UserData();
ServerConnection = new NetClient();
if (CheckUpdates())
return;
if(!Config.DebugMode)
CheckServerInfo();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
LoginService = new Authenticator(Config.DefaultUsername, Config.Password, ServerConnection, UserInfo);
if(!ServerConnection.Connect(Config.ServerAddress, Config.ServerPort))
{
MessageBox.Show("Error Connecting to server");
}
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("Bad Login");
}
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("You have a too old version of the launcher. Please reinstall it.",
"TDOANE - 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("Oooops! Something bad happened! Software will now exit!");
File.WriteAllText("crash_" + DateTime.UtcNow.ToString("yyyy-MM-dd_HH-mm-ss") + ".txt", exception.ToString());
Console.WriteLine(exception.ToString());
Process.GetCurrentProcess().Kill();
}
}
}