-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
480 lines (398 loc) · 15 KB
/
MainForm.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//Guild Wars MultiLaunch - Safe and efficient way to launch multiple GWs.
//The Guild Wars executable is never modified, keeping you inline with the tos.
//
//Copyright (C) 2010 IMKey@GuildWarsGuru
//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
//along with this program. If not, see <http://www.gnu.org/licenses/>.
//comment out for 64 bit builds
#define BUILD32
using System;
using System.IO;
using System.Collections.Generic;
using System.Windows.Forms;
namespace GWMultiLaunch
{
public partial class MainForm : Form
{
private int mSelectedIndex = -1;
private bool mSwitchingArgs = false;
public MainForm()
{
InitializeComponent();
InitUI();
PlatformAlert();
}
private void InitUI()
{
//Populate listbox with copies
foreach (SettingsManager.Profile p in Program.settings.Profiles)
{
profilesListBox.Items.Add(p.Path);
}
//Select copies
foreach (int i in Program.settings.SelectedIndices)
{
profilesListBox.SelectedIndices.Add(i);
}
//Avoids springing up alert box with setting it to checked state
if (Program.settings.ForceUnlock == false)
{
forceUnlockCheckBox.CheckBoxControl.Checked = false;
}
UpdateArgumentsTextBox();
UpdateButtonStates();
}
private void PlatformAlert()
{
#if (BUILD32)
if (Environment.GetEnvironmentVariable("ProgramFiles(x86)") != null)
{
MessageBox.Show(@"Warning: You are running the 32-bit build" +
" of Guild Wars Multi-Launch under a 64-bit operating system. Functionality " +
"will be affected! Use the 64-bit version of GWML.",
"Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
this.Text += " (32-bit)";
#else
this.Text += " (64-bit)";
#endif
}
#region Button Click Events
private void addButton_Click(object sender, EventArgs e)
{
AddCopy();
}
private void removeButton_Click(object sender, EventArgs e)
{
RemoveCopies();
}
private void copyButton_Click(object sender, EventArgs e)
{
MakeCopy();
}
private void launchButton_Click(object sender, EventArgs e)
{
LaunchCopies();
}
private void shortcutButton_Click(object sender, EventArgs e)
{
CreateShortcut(GetSelectedInstalls());
}
private void masterShortcutButton_Click(object sender, EventArgs e)
{
CreateMasterShortcut();
}
private void editArgButton_Click(object sender, EventArgs e)
{
ArgumentsWizard argForm = new ArgumentsWizard();
argForm.Arguments = argumentsTextBox.Text;
argForm.StartPosition = FormStartPosition.CenterParent;
if (argForm.ShowDialog() == DialogResult.OK)
{
argumentsTextBox.Text = argForm.Arguments;
}
}
private void setPathButton_Click(object sender, EventArgs e)
{
RegistryManager.SetGWRegPath(GetSelectedInstall());
}
private void killMutexButton_Click(object sender, EventArgs e)
{
HandleManager.ClearMutex();
}
private void startTexModButton_Click(object sender, EventArgs e)
{
RegistryManager.SetGWRegPath(GetSelectedInstall());
HandleManager.ClearMutex();
StartTexMod();
}
private void exitButton_Click(object sender, EventArgs e)
{
Application.Exit();
}
#endregion
#region Other Events
private void profilesListBox_DragDrop(object sender, DragEventArgs e)
{
string[] droppedFilenames = (string[])e.Data.GetData(DataFormats.FileDrop, false);
AddCopies(droppedFilenames);
}
private void profilesListBox_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.All;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void profilesListBox_MouseDoubleClick(object sender, MouseEventArgs e)
{
int clickedIndex = profilesListBox.IndexFromPoint(e.X, e.Y);
if (clickedIndex != ListBox.NoMatches)
{
LaunchGame(clickedIndex);
}
}
private void profilesListBox_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateSelectedPath();
UpdateArgumentsTextBox();
UpdateButtonStates();
}
private void argumentsTextBox_TextChanged(object sender, EventArgs e)
{
//don't update profile when textbox is being populated with data from profiles
if (mSwitchingArgs == false)
{
Program.settings.UpdateProfile(mSelectedIndex, argumentsTextBox.Text);
}
}
private void forceUnlockCheckBox_CheckedChanged(object sender, EventArgs e)
{
VerifyForceUnlock();
}
#endregion
#region Helpers
private int AddCopies(string[] pathsToAdd)
{
int copiesAdded = 0;
foreach (string pathToAdd in pathsToAdd)
{
if (AddCopy(pathToAdd))
copiesAdded++;
}
return copiesAdded;
}
private bool AddCopy()
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Guild Wars (*.exe, *.lnk)|*.exe;*.lnk";
if (dlg.ShowDialog() == DialogResult.OK)
{
return AddCopy(dlg.FileName);
}
return false;
}
private bool AddCopy(string pathToAdd)
{
string gwPath = pathToAdd;
string arguments = string.Empty;
string fileExt = Path.GetExtension(pathToAdd);
if (fileExt.Equals(".lnk", StringComparison.OrdinalIgnoreCase))
{
gwPath = ShortcutCreator.GetShortcutTarget(pathToAdd);
arguments = ShortcutCreator.GetShortcutArguments(pathToAdd).Trim();
}
if (arguments == string.Empty)
{
return AddCopy(gwPath, Program.DEFAULT_ARGUMENT);
}
return AddCopy(gwPath, arguments);
}
private bool AddCopy(string gwPath, string arguments)
{
bool success = false;
//add to file
Program.settings.AddProfile(gwPath, arguments);
//add to list
profilesListBox.Items.Add(gwPath);
//deselect all
profilesListBox.SelectedIndex = -1;
success = true;
return success;
}
private void RemoveCopies()
{
if (ConfirmRemoval() == false)
{
return;
}
int[] selectedInstalls = GetSelectedInstalls();
//we want it in reverse order, remove highest index values first
Array.Reverse(selectedInstalls);
foreach (int selectedInstall in selectedInstalls)
{
//remove from file
Program.settings.RemoveProfile(selectedInstall);
//remove from list
mSelectedIndex = -1;
profilesListBox.Items.RemoveAt(selectedInstall);
}
}
private bool ConfirmRemoval()
{
DialogResult removeCopies =
MessageBox.Show("Are you sure you want to remove selected copies?", "Remove Copies?",
MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (removeCopies == DialogResult.Yes)
{
return true;
}
return false;
}
private void MakeCopy()
{
string newCopy = MakeCopy(GetSelectedInstall());
if (File.Exists(newCopy))
{
AddCopy(newCopy);
}
}
private void VerifyForceUnlock()
{
if (forceUnlockCheckBox.CheckBoxControl.Checked)
{
DialogResult t = MessageBox.Show("This option is very experimental! Forcing an unlock may corrupt gw.dat. \n" +
"Guild Wars may crash. Only do a forced launch after first copy has fully loaded an area (item art including). " +
"Any graphics or text that have not been loaded into memory will not show up once gw.dat is detached. " +
"Use this option if you want to mule between characters in your guild hall and do not " +
"have the disk space to make an extra copy of Guild Wars.\n\n" +
"Are you sure you want to enable this option?",
"Experimental Option!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
if (t == DialogResult.No)
{
forceUnlockCheckBox.CheckBoxControl.Checked = false;
}
else
{
Program.settings.ForceUnlock = true;
}
}
else
{
Program.settings.ForceUnlock = false;
}
}
private void LaunchCopies()
{
foreach (int copy in GetSelectedInstalls())
{
LaunchGame(copy);
}
}
private void LaunchGame(int index)
{
string gwPath = Program.settings.GetPath(index);
string gwArgs = Program.settings.GetArguments(index);
//check if the install exists
if (!File.Exists(gwPath))
{
MessageBox.Show("The path: " + gwPath + " does not exist!");
}
else
{
bool forced = forceUnlockCheckBox.CheckBoxControl.Checked;
if (forced)
{
HandleManager.ClearDatLock(Directory.GetParent(gwPath).FullName);
}
//attempt to launch
if (LaunchGame(gwPath, gwArgs, forced))
{
//give time for gw to read path before it gets changed again.
System.Threading.Thread.Sleep(Program.settings.RegistryCooldown);
}
}
}
private string GetSelectedInstall()
{
object selectedItem = profilesListBox.SelectedItem;
if (selectedItem != null)
{
string selectedInstall = profilesListBox.SelectedItem.ToString();
if (selectedInstall.Length > 0)
{
return selectedInstall;
}
}
return string.Empty;
}
private int[] GetSelectedInstalls()
{
int[] selectedItems = new int[profilesListBox.SelectedItems.Count];
profilesListBox.SelectedIndices.CopyTo(selectedItems, 0);
return selectedItems;
}
private void UpdateSelectedPath()
{
if (profilesListBox.SelectedIndices.Count == 1)
{
//one item selected
mSelectedIndex = profilesListBox.SelectedIndex;
}
else
{
//multiple items or nothing selected
mSelectedIndex = -1;
}
//update selected indices in profiles
int[] indicesArr = new int[profilesListBox.SelectedIndices.Count];
profilesListBox.SelectedIndices.CopyTo(indicesArr, 0);
Program.settings.SelectedIndices = indicesArr;
}
private void UpdateArgumentsTextBox()
{
if (profilesListBox.SelectedIndices.Count == 1)
{
//one item selected
argumentsTextBox.Enabled = true;
mSwitchingArgs = true;
argumentsTextBox.Text = Program.settings.GetArguments(mSelectedIndex);
mSwitchingArgs = false;
}
else
{
//multiple items or nothing selected
argumentsTextBox.Enabled = false;
mSwitchingArgs = true;
argumentsTextBox.Text = string.Empty;
mSwitchingArgs = false;
}
}
private void UpdateButtonStates()
{
if (profilesListBox.SelectedIndices.Count > 1)
{
removeButton.Enabled = true;
copyButton.Enabled = false;
launchButton.Enabled = true;
shortcutButton.Enabled = true;
setPathButton.Enabled = false;
startTexModButton.Enabled = false;
editArgButton.Enabled = false;
}
else if (profilesListBox.SelectedIndices.Count == 1)
{
removeButton.Enabled = true;
copyButton.Enabled = true;
launchButton.Enabled = true;
shortcutButton.Enabled = true;
setPathButton.Enabled = true;
startTexModButton.Enabled = true;
editArgButton.Enabled = true;
}
else
{
removeButton.Enabled = false;
copyButton.Enabled = false;
launchButton.Enabled = false;
shortcutButton.Enabled = false;
setPathButton.Enabled = false;
startTexModButton.Enabled = false;
editArgButton.Enabled = false;
}
}
#endregion
}
}