-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHSMod.cs
More file actions
410 lines (391 loc) · 17.4 KB
/
HSMod.cs
File metadata and controls
410 lines (391 loc) · 17.4 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
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
using System;
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
namespace HalfSwordModInstaller
{
public class HSMod : HSInstallable
{
public override bool IsInstalled
{
get
{
var modInstallPath = Path.Combine(HSUtils.HSUE4SSPath, RelativePath);
if (Directory.Exists(modInstallPath))
{
// TODO this is a very crude way of detecting a development setup with symlinks, but works for now
var attrs = File.GetAttributes(modInstallPath);
if ((attrs & FileAttributes.ReparsePoint) == FileAttributes.ReparsePoint)
{
//HSUtils.Log($"[WARNING] Development setup with symlinks detected for mod \"{this.Name}\"");
_developmentSetup = true;
}
var mainLua = Path.Combine(modInstallPath, "scripts\\main.lua");
if (File.Exists(mainLua))
{
// Proper version detection in the lua variable mod_version
var luaTextLines = File.ReadLines(mainLua).ToList();
var versionLine = luaTextLines.Find(line => line.StartsWith("local mod_version"));
if (versionLine != null)
{
foreach (Match match in Regex.Matches(versionLine, @"local mod_version\s+=\s+""(\d+\.\d+)"""))
{
if (match.Success && match.Groups.Count > 0)
{
InstalledVersion = "v" + match.Groups[1].Value;
break;
}
}
}
else
{
// Fallback method, parse the first line of main.lua to get the version from the comment e.g. "-- lolmod v0.1"
var firstLine = luaTextLines.First();
foreach (Match match in Regex.Matches(firstLine, @"\s(v\d+(\.\d+)+)"))
{
if (match.Success && match.Groups.Count > 0)
{
InstalledVersion = match.Groups[1].Value;
break;
}
}
}
if (HasLogicMods)
{
if (Directory.Exists(HSUtils.HSLogicModsPath))
{
// TODO properly compare the actual files with distribution ZIP maybe?
if (Directory.GetFiles(HSUtils.HSLogicModsPath, "*", SearchOption.AllDirectories).Length == 0)
{
// No files in LogicMods means a broken installation == no installation
HSUtils.Log($"[WARNING] Empty LogicMods folder for mod \"{this.Name}\", assuming it is not installed");
_isInstalled = false;
}
else
{
_isInstalled = true;
}
}
else
{
// TODO broken install, as LogicMods folder does not exist
// For now return as if not installed
HSUtils.Log($"[WARNING] Missing LogicMods folder for mod \"{this.Name}\", assuming it is not installed");
_isInstalled = false;
}
}
else
{
_isInstalled = true;
}
}
else
{
// TODO broken install, folder exists but the actual mod not there
// For now return as if not installed
HSUtils.Log($"[WARNING] Empty mod folder for mod \"{this.Name}\", assuming it is not installed");
_isInstalled = false;
}
}
else
{
_isInstalled = false;
}
return _isInstalled;
}
set
{
_isInstalled = value;
}
}
public override bool IsEnabled
{
get
{
if (File.Exists(HSUtils.HSUE4SSModsTxt))
{
var lines = File.ReadAllLines(HSUtils.HSUE4SSModsTxt).ToList();
int thisModIndex = lines.FindIndex(line => line.Contains(Name));
if (thisModIndex < 0)
{
_isEnabled = false;
}
else
{
int thisModIndexEnabled = lines.FindIndex(line => line.Trim().Equals($"{Name} : 1"));
if (thisModIndexEnabled < 0)
{
_isEnabled = false;
}
else
{
if (HasLogicMods)
{
int BPMLIndexEnabled = lines.FindIndex(line => line.Trim().Equals($"{HSUtils.BPMLName} : 1"));
if (BPMLIndexEnabled < 0)
{
// Lua part of our mod is enabled, but BPModLoaderMod is not enabled,
// so we consider our mod to be not enabled as well
_isEnabled = false;
}
else
{
_isEnabled = true;
}
}
else
{
_isEnabled = true;
}
}
}
}
else
{
_isEnabled = false;
}
return _isEnabled;
}
set
{
_isEnabled = value;
}
}
protected bool HasLogicMods = false;
// TODO implement the proper state machine for the mod
/*
* +----------------------------------+
* | |
* V |
* Unknown -----> VersionKnown -----> Downloaded <-----> Installed -----> Enabled
* | | ^ | | ^ |
* | | | | | | |
* | | | +-----+ | | |
* | | | / V | |
* | | +----------- Disabled <---------+ |
* | | / | |
* | | +----+ | |
* | | | | |
* | V V V |
* +--------------> Broken <-------------+----------------+
**/
/*
public enum ModState
{
Unknown, // Latest version is unknown yet, nothing on disk
VersionKnown, // Retrieved the version, but not downloaded the mod successfully yet
Downloaded, // Downloaded successfully, but not installed
Installed, // Installed, but not enabled (no mods.txt record)
Enabled, // Installed and enabled
Disabled, // Installed and disabled (record in mods.txt)
Broken // Unusable, broken (downloaded and can't be installed, or installed and unusable)
}
*/
// Development setup for mods is done with symlinks from the game mods folder to somewhere else (e.g. a git repo)
protected bool _developmentSetup = false;
public bool DevelopmentSetup
{
get
{
return _developmentSetup;
}
set
{
_developmentSetup |= value;
}
}
public HSMod(string name, string url, HSUtils.HSGameType compatibleGameType, bool hasLogicMods, List<HSInstallable> dependencyGraph) : base(name, url, compatibleGameType, dependencyGraph)
{
this.HasLogicMods = hasLogicMods;
// TODO handle game type/isExperimental
this.RelativePath = $"Mods\\{Name}";
}
public new void LogMe()
{
HSUtils.Log($"Mod=\"{Name}\", Url=\"{Url}\", LatestVersion=\"{LatestVersion}\", " +
$"Downloaded={IsDownloaded}, Installed={IsInstalled}, " +
$"InstalledVersion={(string.IsNullOrEmpty(InstalledVersion) ? "null" : "\"" + InstalledVersion + "\"")}, " +
$"Experimental={IsExperimental}, " +
$"CompatibleGameType=\"{CompatibleGameType}\", " +
$"Enabled={IsEnabled}, " +
$"HasLogicMods={HasLogicMods}");
}
public override void Install()
{
Install(false);
}
public override void InstallAll()
{
Install(true);
}
public override void Install(bool forceInstallDependencies = false)
{
HSUtils.Log($"Installing mod \"{Name}\" from \"{LocalZipPath}\"...");
if (this.dependencyGraph?.Count > 0)
{
// TODO should we throw an exception here? Or should we simply install the dependency?
foreach (var dependency in this.dependencyGraph)
{
if (forceInstallDependencies)
{
dependency.Install(true);
}
else if (!dependency.IsInstalled)
{
HSUtils.Log($"[ERROR] Cannot install mod \"{Name}\", dependency \"{dependency.Name}\" is not installed.");
return;
}
}
}
if (!IsDownloaded)
{
Download();
}
string unzipFolder = Path.Combine(HSUtils.HSUE4SSPath, "Mods");
if (!Directory.Exists(unzipFolder))
{
// TODO the installation is broken. Should probably bail out.
HSUtils.Log($"[ERROR] Cannot install mod \"{Name}\", missing \"Mods\" folder");
return;
}
try
{
HSUtils.ForceExtractToDirectory(LocalZipPath, unzipFolder);
// Clean up by removing readme and license, yes, this is bad. Sorry.
string readmePath = Path.Combine(unzipFolder, "README.md");
if (File.Exists(readmePath)) { File.Delete(readmePath); }
string licensePath = Path.Combine(unzipFolder, "LICENSE");
if (File.Exists(licensePath)) { File.Delete(licensePath); }
// TODO this does not account for which exact files this mod has in LogicMods
if (HasLogicMods)
{
var tempLogicMods = Path.Combine(unzipFolder, "LogicMods");
// TODO This is all horrible and must be rewritten
Directory.CreateDirectory(HSUtils.HSLogicModsPath);
foreach (var file in new DirectoryInfo(tempLogicMods).GetFiles())
{
var newFname = Path.Combine(HSUtils.HSLogicModsPath, file.Name);
if (File.Exists(newFname))
{
HSUtils.Log($"[WARNING] Overwriting file {newFname}");
File.Delete(newFname);
}
file.MoveTo(newFname);
}
Directory.Delete(tempLogicMods);
}
}
catch (Exception ex)
{
HSUtils.Log($"[ERROR] An error occurred while installing mod \"{Name}\": {ex.Message}");
HSUtils.Log(ex.StackTrace);
return;
}
InstalledVersion = LatestVersion;
HSUtils.Log($"Installed mod \"{Name}\" from \"{LocalZipPath}\" to \"{unzipFolder}\"");
}
public override void Uninstall()
{
var uninstallPath = Path.Combine(HSUtils.HSUE4SSPath, RelativePath);
HSUtils.Log($"Uninstalling mod \"{Name}\" from \"{uninstallPath}\"...");
try
{
// TODO not sure if we need to erase the mod line from mods.txt or not at this moment
// Only disable it for now, leave the line in mods.txt
if (IsEnabled)
{
SetEnabled(false);
}
Directory.Delete(uninstallPath, true);
// TODO DANGER this does not account for which exact files this mod has in LogicMods
// TODO DANGER We just delete all of them
if (HasLogicMods)
{
foreach (FileInfo file in new DirectoryInfo(HSUtils.HSLogicModsPath).EnumerateFiles())
{
file.Delete();
}
}
}
catch (Exception ex)
{
HSUtils.Log($"[ERROR] An error occurred while uninstalling mod \"{Name}\": {ex.Message}");
HSUtils.Log(ex.StackTrace);
return;
}
HSUtils.Log($"Uninstalled mod \"{Name}\"");
}
public override void Download()
{
if (IsExperimental)
{
DownloadLatestBranch();
}
else
{
base.Download();
}
}
public override void SetEnabled(bool isEnabled = true)
{
EditModsTxt(Name, isEnabled);
if (HasLogicMods)
{
// We force-enable the BPModLoaderMod
EditModsTxt(HSUtils.BPMLName, isEnabled);
}
if (isEnabled)
{
HSUtils.Log($"Enabled \"{Name}\"");
}
else
{
HSUtils.Log($"Disabled \"{Name}\"");
}
}
public override bool GetEnabled()
{
return IsEnabled;
}
public static void EditModsTxt(string modName, bool enabled)
{
string value = enabled ? "1" : "0";
if (!File.Exists(HSUtils.HSUE4SSModsTxt))
{
// TODO should probably abort hard here, installation is broken
HSUtils.Log($"[ERROR] Mods.txt is missing, cannot {(enabled ? "enable" : "disable")} mod {modName}");
return;
}
var lines = File.ReadAllLines(HSUtils.HSUE4SSModsTxt).ToList();
int keybindsIndex = lines.FindIndex(line => line.Contains("Keybinds"));
int keyIndex = lines.FindIndex(line => line.StartsWith(modName));
// If the key already exists, just edit it
if (keyIndex != -1)
{
lines[keyIndex] = modName + " : " + value;
}
else
{
// If the key doesn't exist, add it before existing "Keybinds" to make sure the hotkeys work
if (keybindsIndex != -1)
{
var insertIndex = keybindsIndex - 1;
lines.Insert(insertIndex, "");
lines.Insert(insertIndex, modName + " : " + value);
lines.Insert(insertIndex, "");
}
else
{
// Add missing Keybinds at the end, they are mandatory for hotkeys
lines.Add(modName + " : " + value);
lines.Add("");
lines.Add("Keybinds : 1");
}
}
File.WriteAllLines(HSUtils.HSUE4SSModsTxt, lines);
}
}
}