-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormMods.cs
391 lines (357 loc) · 17.1 KB
/
FormMods.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
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace BotM
{
public partial class FormMods : Form
{
private bool hacking = false;
public FormMods()
{
InitializeComponent();
CenterToScreen();
listBoxInstalled.DataSource = new BindingList<ModListing>();
listBoxNotInstalled.DataSource = new BindingList<ModListing>();
UpdateModList();
var feh = new System.IO.FileSystemEventHandler(OnFileEvent);
var feh2 = new System.IO.RenamedEventHandler(OnFileEvent);
System.IO.FileSystemWatcher watcher1 = new System.IO.FileSystemWatcher
{
Path = MainForm.Settings.NotInstalledModsDirectory,
NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName,
Filter = "*.botm.zip"
};
watcher1.Created += feh;
watcher1.Changed += feh;
watcher1.Renamed += feh2;
watcher1.Deleted += feh;
watcher1.EnableRaisingEvents = true;
System.IO.FileSystemWatcher watcher2 = new System.IO.FileSystemWatcher
{
Path = MainForm.Settings.NotInstalledModsDirectory,
NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName,
Filter = "*.botm.zip"
};
watcher2.Created += feh;
watcher2.Deleted += feh;
watcher2.EnableRaisingEvents = true;
}
private void OnFileEvent(object n, EventArgs e)
{
UpdateModList();
}
private void UpdateModList()
{
if (hacking) return;
hacking = true;
BindingList<ModListing> niMods = (BindingList<ModListing>)listBoxNotInstalled.DataSource;
niMods.Clear();
foreach (var f in Directory.GetFiles(MainForm.Settings.NotInstalledModsDirectory, "*.botm.zip"))
{
Mod m = ReadModMeta(f);
if (m == null)
{
MessageBox.Show(this, "Mod ignored- Appears to be invalid:\n\n" + f, "Invalid!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
continue;
}
niMods.Add(new ModListing { m = m, ModName = m.ModName, ModAuthor = m.ModAuthor, ModHelp = m.ModHelp, HasIcon = (m.ModIcon != null), Icon = m.ModIcon?.bmp });
}
BindingList<ModListing> iMods = (BindingList<ModListing>)listBoxInstalled.DataSource;
iMods.Clear();
foreach (var f in Directory.GetFiles(MainForm.Settings.InstalledModsDirectory, "*.botm.zip"))
{
Mod m = ReadModMeta(f);
if (m == null)
{
MessageBox.Show(this, "Mod ignored- Appears to be invalid:\n\n" + f, "Invalid!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
continue;
}
iMods.Add(new ModListing { m = m, ModName = m.ModName, ModAuthor = m.ModAuthor, ModHelp = m.ModHelp, HasIcon = (m.ModIcon != null), Icon = m.ModIcon?.bmp });
}
listBoxInstalled.Invoke(new Action(listBoxInstalled.ListHack));
listBoxNotInstalled.Invoke(new Action(listBoxNotInstalled.ListHack));
hacking = false;
}
private Mod ReadModMeta(string filepath)
{
if (!File.Exists(filepath)) return null;
try
{
using (var fs = new FileStream(filepath, FileMode.Open, FileAccess.Read))
{
using (var zf = new ZipFile(fs))
{
Mod m;
var ze = zf.GetEntry("mod.json");
if (ze == null)
{
if (MainForm.Settings.DeveloperMode) MessageBox.Show(this, "Mod read error:\n\n" + filepath + "\n\nMissing mod.json !", "Mod Read Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
return null;
}
using (var s = zf.GetInputStream(ze))
{
using (StreamReader reader = new StreamReader(s))
{
m = Newtonsoft.Json.JsonConvert.DeserializeObject<Mod>(reader.ReadToEnd());
}
}
m.ModFile = filepath;
var ze2 = zf.GetEntry("mod.png");
if(ze2 != null)
{
//this mod has an icon
using (var s = zf.GetInputStream(ze2))
{
m.ModIcon = new ModIcon(new Bitmap(s));
}
}
var ze3 = zf.GetEntry("readme.txt");
if (ze3 != null)
{
//this mod has a readme
using (var s = zf.GetInputStream(ze3))
{
using (StreamReader reader = new StreamReader(s))
{
m.ModHelp = reader.ReadToEnd();
}
}
} else
{
m.ModHelp = null;
}
return m;
}
}
} catch (Exception e)
{
if (MainForm.Settings.DeveloperMode)
{
MessageBox.Show(this, "Mod read error:\n\n" + filepath + "\n\n" + e.Message, "Mod Read Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return null;
}
}
private void ButtonOK_Click(object sender, EventArgs e)
{
Close();
}
private void Button1_Click(object sender, EventArgs e)
{
OpenFileDialog fdlg = new OpenFileDialog
{
Title = "Add Mod from File",
InitialDirectory = Environment.SpecialFolder.MyDocuments.ToString(),
Filter = "BotM Mod (*.botm.zip)|*.botm.zip|All files (*.*)|*.*",
FilterIndex = 1,
RestoreDirectory = true
};
if (fdlg.ShowDialog() == DialogResult.OK)
{
string fn = System.IO.Path.GetFileName(fdlg.FileName);
try
{
System.IO.File.Copy(fdlg.FileName, System.IO.Path.Combine(MainForm.Settings.NotInstalledModsDirectory, fn));
MessageBox.Show("Mod successfully imported.");
} catch(Exception ex)
{
MessageBox.Show(this, "Mod import failed:\n\n" + ex.Message,"Mod Import Error!",MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void Button2_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(MainForm.Settings.NotInstalledModsDirectory);
}
private void OnModClick(object sender, EventArgs e)
{
ListBoxMods lbm = (ListBoxMods)sender;
if(lbm.SelectedItem != null)
{
string help = ((ModListing)lbm.SelectedItem).ModHelp;
if (!string.IsNullOrWhiteSpace(help))
{
textBoxHelp.Text = ((ModListing)lbm.SelectedItem).ModHelp;
textBoxHelp.Visible = true;
} else
{
textBoxHelp.Text = "";
textBoxHelp.Visible = false;
}
}
}
private void button3_Click(object sender, EventArgs e)
{
//install mod
if (listBoxNotInstalled.SelectedItem == null) return;
try
{
ModListing ml = (ModListing)listBoxNotInstalled.SelectedItem;
//do install
List<InstallAction> actions = ml.m.InstallActions;
if(actions != null)
{
foreach(var a in actions)
{
switch (a.Action)
{
case "Overwrite":
if(a.Parameters.Length != 2)
{
MessageBox.Show("Wrong number of parameters for install action: " + a.Action);
continue;
}
Overwrite(ml.m, a.Parameters[0], a.Parameters[1]);
break;
default:
MessageBox.Show("Unknown install action:\n" + a.Action);
continue;
}
}
}
File.Move(ml.m.ModFile, Path.Combine(MainForm.Settings.InstalledModsDirectory, Path.GetFileName(ml.m.ModFile)));
MessageBox.Show("Mod successfully installed.");
}
catch(Exception ex) {
MessageBox.Show("Error occured while installing mod:\n" + ex.Message);
}
}
public void UnOverwrite(Mod m, string destinationFile)
{
if (!File.Exists(m.ModFile)) throw new Exception("Mod file does not exist!");
string baseFile = destinationFile;
//let's find the destination
if (destinationFile.StartsWith("update\\"))
{
destinationFile = destinationFile.Substring(7);
destinationFile = MainForm.Settings.UpdatePath + Path.DirectorySeparatorChar + destinationFile;
}
else if (destinationFile.StartsWith("game\\"))
{
destinationFile = destinationFile.Substring(5);
destinationFile = MainForm.Settings.GamePath + Path.DirectorySeparatorChar + destinationFile;
}
string backupFile = Path.Combine(MainForm.Settings.BackupDirectory, Path.GetFileNameWithoutExtension(m.ModFile), Path.GetFileName(destinationFile));
if (File.Exists(backupFile))
{
DialogResult dr = MessageBox.Show(this, "Would you like to restore the backup of: " + destinationFile, "Restore Backup?", MessageBoxButtons.YesNo);
if (dr == DialogResult.No) return;
if(MainForm.ReplacedFiles.ContainsKey(baseFile)) MainForm.ReplacedFiles.Remove(baseFile);
File.Copy(backupFile, destinationFile, true);
}
else
{
DialogResult dr = MessageBox.Show(this, "No backup available for:\n" + destinationFile + "\nSelect 'Yes' to leave the file where it is,\nor 'No' to remove it (not recommended!)", "No Backup!", MessageBoxButtons.YesNo);
if (dr == DialogResult.Yes) return;
else
{
File.Delete(destinationFile);
}
}
}
public void Overwrite(Mod m, string sourceFile, string destinationFile)
{
if (!File.Exists(m.ModFile)) throw new Exception("Mod file does not exist!");
using (var fs = new FileStream(m.ModFile, FileMode.Open, FileAccess.Read))
{
using (var zf = new ZipFile(fs))
{
var ze = zf.GetEntry(sourceFile.Replace('\\', '/'));
if (ze == null)
{
if (MainForm.Settings.DeveloperMode) MessageBox.Show(this, "Mod read error:\n\n" + m.ModName + "\n\nMissing file: " + sourceFile, "Mod Read Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
using (var s = zf.GetInputStream(ze))
{
byte[] output;
using (MemoryStream ms = new MemoryStream())
{
s.CopyTo(ms);
output = ms.ToArray();
}
//we have the source file
//let's find the destination
string baseFile = destinationFile;
if (destinationFile.StartsWith("update\\"))
{
destinationFile = destinationFile.Substring(7);
destinationFile = MainForm.Settings.UpdatePath + Path.DirectorySeparatorChar + destinationFile;
} else if(destinationFile.StartsWith("game\\"))
{
destinationFile = destinationFile.Substring(5);
destinationFile = MainForm.Settings.GamePath + Path.DirectorySeparatorChar + destinationFile;
}
if (File.Exists(destinationFile))
{
DialogResult dr = MessageBox.Show(this, "The mod would like to replace this file: " + destinationFile + "\n\nAllow this operation?", "Overwrite", MessageBoxButtons.YesNo);
if (dr == DialogResult.No) return;
if (MainForm.ReplacedFiles.ContainsKey(baseFile))
{
DialogResult dr3 = MessageBox.Show(this, "Another mod (" + MainForm.ReplacedFiles[baseFile] + ") has already replaced this file: " + destinationFile + "\n\nContinue anyway? (not recommended)", "Mod Collision", MessageBoxButtons.YesNo);
if (dr3 == DialogResult.No) return;
}
//create backup
try
{
Directory.CreateDirectory(Path.Combine(MainForm.Settings.BackupDirectory, Path.GetFileNameWithoutExtension(m.ModFile)));
File.Copy(destinationFile, Path.Combine(MainForm.Settings.BackupDirectory, Path.GetFileNameWithoutExtension(m.ModFile), Path.GetFileName(destinationFile)), true);
} catch(Exception e)
{
DialogResult dr2 = MessageBox.Show(this, "Failed to backup file: " + destinationFile + "\n" + e.Message + "\nContinue anyway? (not recommended!)", "Backup Failed!", MessageBoxButtons.YesNo);
if (dr2 == DialogResult.No) return;
}
} else
{
DialogResult dr = MessageBox.Show(this, "Tried to replace this file:\n" + destinationFile + "\n\nbut it doesn't seem to exist.\nPlease make sure directories are set correctly.\n\nPlace file here anyway?", "Overwrite", MessageBoxButtons.YesNo);
if(dr == DialogResult.No) return;
}
MainForm.ReplacedFiles[baseFile] = Path.GetFileNameWithoutExtension(m.ModFile);
File.WriteAllBytes(destinationFile, output);
}
}
}
}
private void button4_Click(object sender, EventArgs e)
{
//uninstall mod
if (listBoxInstalled.SelectedItem == null) return;
try
{
ModListing ml = (ModListing)listBoxInstalled.SelectedItem;
//do uninstall
List<InstallAction> actions = ml.m.InstallActions;
if (actions != null)
{
foreach (var a in actions)
{
switch (a.Action)
{
case "Overwrite":
if (a.Parameters.Length != 2)
{
MessageBox.Show("Wrong number of parameters for install action: " + a.Action);
continue;
}
UnOverwrite(ml.m, a.Parameters[1]);
break;
default:
MessageBox.Show("Unknown install action:\n" + a.Action);
continue;
}
}
}
File.Move(ml.m.ModFile, Path.Combine(MainForm.Settings.NotInstalledModsDirectory, Path.GetFileName(ml.m.ModFile)));
MessageBox.Show("Mod successfully uninstalled.");
}
catch (Exception ex)
{
MessageBox.Show("Error occured while uninstalling mod:\n" + ex.Message);
}
}
}
}