This repository has been archived by the owner on May 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
MainWindow.xaml.cs
334 lines (282 loc) · 10.7 KB
/
MainWindow.xaml.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
namespace Verdant
{
public partial class MainWindow : Window
{
public string PathToCookies = Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "login.vdt");
public string QuickStart = null;
public NaverAccount Account = null;
public MapleGame Maple;
private bool otherIdsLoaded = false;
public MainWindow()
{
InitializeComponent();
Account = new NaverAccount(PathToCookies);
toggleUi(false);
Tools.TryPromptUpdate();
}
private void toggleUi(bool status)
{
changeMapleIdButton.IsEnabled = otherIdsLoaded ? false : status;
startButton.IsEnabled = status;
tespiaCheckBox.IsEnabled = status;
}
private async Task checkAccountLogin()
{
try
{
await Account.EnsureLoggedIn();
}
catch (NaverAccount.LoginSessionExpiredException)
{
// session invalid.
MessageBox.Show("Your Naver login has expired. A new login is required.");
File.Delete(PathToCookies);
}
}
private async void Window_Loaded(object sender, RoutedEventArgs e)
{
mapleIdLabel.Content = "Loading...";
string[] args = Environment.GetCommandLineArgs();
bool loadCustom = false;
if (args.Length > 1)
{
// custom form
if (args[1].EndsWith(".dll"))
loadCustom = true;
// custom cookie file
else if (args[1].EndsWith(".vdt"))
PathToCookies = args[1];
else
QuickStart = args[1];
}
// NAVER LOGIN
// check for preexist login data, and verify if session still valid
// or, if new, show the dialog!
// we need to run the async function synchronously - not the greatest but we'll just do this for now
if (Account.Preloaded)
{
await checkAccountLogin();
if (!Account.LoggedIn)
(new LoginNew(Account)).ShowDialog();
}
else
(new LoginNew(Account)).ShowDialog();
if (!Account.LoggedIn)
{
Close();
return;
}
statusLabel.Content = "Logged in.";
// custom form post-login load
if (loadCustom)
{
Assembly asm = Assembly.LoadFile(Path.GetFullPath(args[1]));
Type t = asm.GetExportedTypes().First(a => typeof(Window).IsAssignableFrom(a));
Window w = (Window)Activator.CreateInstance(t, Account);
w.Title += " (powered by Verdant)";
w.Show();
Close();
return;
}
// MAPLE LOGIN/CHANNEL
if (Process.GetProcessesByName("MapleStory").Length > 0
|| Process.GetProcessesByName("NGM").Length > 0)
{
var mbr = MessageBox.Show("You are already playing MapleStory! Continue?", "Verdant", MessageBoxButton.YesNo, MessageBoxImage.Exclamation);
if (mbr == MessageBoxResult.No)
{
Close();
return;
}
}
Maple = new MapleGame(Account);
try
{
await Maple.Init();
}
catch (VerdantException.GameNotFoundException)
{
MessageBox.Show("Looks like we couldn't find your installation of MapleStory! Make sure you have KMS + NGM installed properly!");
Close();
return;
}
catch (VerdantException.ChannelingRequiredException)
{
mapleIdLabel.Content = "Loading... (channeling)";
try
{
await Maple.Channel();
}
catch
{
MessageBox.Show("Error channeling with Nexon and your Naver account. Please try again later.");
Close();
return;
}
}
catch
{
MessageBox.Show("Error connecting to Maple right now, or you have not made a Maple ID! Please try again later.");
Close();
return;
}
if (QuickStart != null)
{
mapleIdLabel.Content = "Quickstarting...";
startGame();
return;
}
mapleIdLabel.Content = "Web Main Character (대표 캐릭터): " + Maple.MainCharName;
if (Maple.CharacterImageUrl != null)
charImage.Source = Tools.UrlToXamlImage(Maple.CharacterImageUrl);
toggleUi(true);
}
private bool noAuth = false;
private void startButton_Click(object sender, RoutedEventArgs e)
{
startGame();
}
private async Task startGame()
{
toggleUi(false);
try
{
await Maple.Start((bool)tespiaCheckBox.IsChecked);
}
catch (VerdantException.NoAuthException)
{
if (noAuth)
{
MessageBox.Show("We failed to auth. Login data will be deleted, and Verdant will be closed. Please try logging in completely again.");
File.Delete(PathToCookies);
Close();
return;
}
// last ditch
noAuth = true;
await mapleIdSelection(true);
startButton.IsEnabled = false;
if (Maple.MapleIds.Count == 1)
{
mapleIdBox.Text = Maple.MapleIds[0];
await switchMapleId();
Close();
}
else if (Maple.MapleIds.Contains(QuickStart))
{
Debug.WriteLine("quickstart id last ditch: " + QuickStart);
mapleIdBox.Text = QuickStart;
await switchMapleId();
Close();
}
else
{
MessageBox.Show("There was an error starting the game, but... we have one last trick. Please select the Maple ID in the dropdown box below that you would like to login to, and we can try again!", "Verdant");
mapleIdBox.Focus();
mapleIdBox.IsDropDownOpen = true;
}
return;
}
catch (Exception ex)
{
MessageBox.Show("Error starting game...\n\n" + ex.ToString());
}
Close();
}
private void changeMapleIdButton_Click(object sender, RoutedEventArgs e)
{
mapleIdSelection();
}
private async Task mapleIdSelection(bool inStart = false)
{
toggleUi(false);
await Maple.GetMapleIds();
otherIdsLoaded = true;
if (!inStart && Maple.MapleIds.Count < 2)
{
MessageBox.Show("You only have 1 maple id...");
return;
}
Maple.MapleIds.ForEach(x => mapleIdBox.Items.Add(x));
mapleIdBox.IsEnabled = true;
toggleUi(true);
}
private void mapleIdBox_DropDownClosed(object sender, EventArgs e)
{
switchMapleId();
}
private async Task switchMapleId()
{
if (String.IsNullOrEmpty(mapleIdBox.Text) || !Maple.MapleIds.Contains(mapleIdBox.Text))
return;
charImage.Source = null;
toggleUi(false);
mapleIdLabel.Content = "Switching...";
try
{
await Maple.SwitchMapleId(mapleIdBox.Text);
}
catch (Exception ex)
{
MessageBox.Show("Error swiching IDs...\n\n" + ex.ToString());
}
mapleIdLabel.Content = "Web Main Character (대표 캐릭터): " + Maple.MainCharName;
if (Maple.CharacterImageUrl != null)
charImage.Source = Tools.UrlToXamlImage(Maple.CharacterImageUrl);
tespiaCheckBox.IsChecked = false;
if (noAuth)
{
Account.SaveCookies();
await startGame();
return;
}
toggleUi(true);
}
private void mapleIdBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
// https://social.msdn.microsoft.com/Forums/vstudio/en-US/5e14706e-308b-44b1-8d74-aadd89e4c940/disable-up-and-down-arrow-access-key-of-combobox?forum=wpf
if (e.Key == Key.Down || e.Key == Key.Up)
{
e.Handled = true;
return; // do not call the base class method OnPreviewKeyDown()
}
base.OnPreviewKeyDown(e);
}
private void logoutButton_Click(object sender, RoutedEventArgs e)
{
Account.UiLogout();
}
private async void tespiaCheckBox_Checked(object sender, RoutedEventArgs e)
{
toggleUi(false);
try
{
await Maple.UseTespia();
}
catch (MapleGame.TespiaNotEnabled)
{
var mbr = MessageBox.Show("You have not signed up for the Test Server yet. Click yes to open up your browser to the signup page, signup, then try again.", "Verdant", MessageBoxButton.YesNo, MessageBoxImage.Error);
if (mbr == MessageBoxResult.Yes)
Process.Start("https://maplestory.nexon.game.naver.com/MyMaple/TestWorld/Apply");
tespiaCheckBox.IsChecked = false;
}
toggleUi(true);
}
}
}