Skip to content

Commit cfa4611

Browse files
committed
Optim: Union Mod Reload pipeline
1 parent 28c9ba2 commit cfa4611

4 files changed

Lines changed: 43 additions & 48 deletions

File tree

services/ModReloadService.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@
66

77
namespace NeoModLoader.services;
88

9-
internal static class ModReloadService
9+
/// <summary>
10+
/// Coordinates mod hot-reload workflows.
11+
/// </summary>
12+
public static class ModReloadService
1013
{
14+
/// <summary>
15+
/// Recompiles and patches a reloadable mod.
16+
/// </summary>
1117
public static bool HotfixMethods(IReloadable pMod, ModDeclare pModDeclare)
1218
{
1319
if (!ModReloadUtils.Prepare(pMod, pModDeclare)) return false;
@@ -16,6 +22,30 @@ public static bool HotfixMethods(IReloadable pMod, ModDeclare pModDeclare)
1622
return true;
1723
}
1824

25+
/// <summary>
26+
/// Recompiles, patches and invokes the reload callback of a mod.
27+
/// </summary>
28+
public static bool ReloadMod(IReloadable pMod, ModDeclare pModDeclare)
29+
{
30+
if (!HotfixMethods(pMod, pModDeclare)) return false;
31+
if (pMod is IMod mod)
32+
{
33+
try
34+
{
35+
if (!ReloadResources(mod)) return false;
36+
ReloadLocales(mod);
37+
}
38+
catch
39+
{
40+
return false;
41+
}
42+
}
43+
return ModReloadUtils.Reload();
44+
}
45+
46+
/// <summary>
47+
/// Rebuilds mod resources from disk.
48+
/// </summary>
1949
public static bool ReloadResources(IMod pMod)
2050
{
2151
MasterBuilder Builder = new();
@@ -26,9 +56,12 @@ public static bool ReloadResources(IMod pMod)
2656
Builder.AddBuilders(builders);
2757
Builder.AddBuilders(builders2);
2858
Builder.BuildAll();
29-
return false;
59+
return true;
3060
}
3161

62+
/// <summary>
63+
/// Reloads locale files from disk and applies them.
64+
/// </summary>
3265
public static void ReloadLocales(IMod pMod)
3366
{
3467
ModCompileLoadService.LoadLocales(pMod, pMod.GetDeclaration(), true, true);

ui/ModListWindow.cs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -425,30 +425,11 @@ public override void Setup(IMod mod)
425425
reload_button.onClick.RemoveAllListeners();
426426
reload_button.onClick.AddListener(() =>
427427
{
428-
if (!ModReloadUtils.Prepare(reloadable, mod_declare))
429-
{
430-
LogService.LogWarning($"Failed to prepare mod {mod_declare.Name} for reloading.");
431-
return;
432-
}
433-
434-
if (!ModReloadUtils.CompileNew())
435-
{
436-
LogService.LogWarning($"Failed to compile new mod {mod_declare.Name} for reloading.");
437-
return;
438-
}
439-
440-
if (!ModReloadUtils.PatchHotfixMethodsNT())
441-
{
442-
LogService.LogWarning(
443-
$"Failed to patch hotfix methods of mod {mod_declare.Name} for reloading.");
444-
return;
445-
}
446-
447-
if (!ModReloadUtils.Reload())
428+
if (!ModReloadService.ReloadMod(reloadable, mod_declare))
448429
{
449430
LogService.LogWarning($"Failed to reload mod {mod_declare.Name}.");
450431
}
451432
});
452433
}
453434
}
454-
}
435+
}

ui/NewModListWindow.cs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -324,26 +324,7 @@ private void ReloadSelectedMod()
324324
{
325325
if (mod.GetDeclaration() == CurrentSelected && mod is IReloadable reloadable)
326326
{
327-
if (!ModReloadUtils.Prepare(reloadable, CurrentSelected))
328-
{
329-
LogService.LogWarning($"Failed to prepare mod {CurrentSelected.Name} for reloading.");
330-
return;
331-
}
332-
333-
if (!ModReloadUtils.CompileNew())
334-
{
335-
LogService.LogWarning($"Failed to compile new mod {CurrentSelected.Name} for reloading.");
336-
return;
337-
}
338-
339-
if (!ModReloadUtils.PatchHotfixMethodsNT())
340-
{
341-
LogService.LogWarning(
342-
$"Failed to patch hotfix methods of mod {CurrentSelected.Name} for reloading.");
343-
return;
344-
}
345-
346-
if (!ModReloadUtils.Reload())
327+
if (!ModReloadService.ReloadMod(reloadable, CurrentSelected))
347328
{
348329
LogService.LogWarning($"Failed to reload mod {CurrentSelected.Name}.");
349330
}
@@ -377,4 +358,4 @@ private enum DisplayType
377358
Mod,
378359
Resource
379360
}
380-
}
361+
}

utils/ModReloadUtils.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ internal static class ModReloadUtils
4141
private static readonly List<object> _generated_wrapper_delegates = new();
4242
private static readonly List<Type> _generated_wrapper_types = new();
4343

44-
public static bool Prepare(IReloadable pMod, ModDeclare pModDeclare)
44+
internal static bool Prepare(IReloadable pMod, ModDeclare pModDeclare)
4545
{
4646
_mod = pMod;
4747
_mod_declare = pModDeclare;
@@ -91,7 +91,7 @@ public static bool Prepare(IReloadable pMod, ModDeclare pModDeclare)
9191
return true;
9292
}
9393

94-
public static bool CompileNew()
94+
internal static bool CompileNew()
9595
{
9696
if (!ModCompileLoadService.TryCompileModAtRuntime(_mod_declare, true)) return false;
9797
foreach (var type in EnumerateTypesRecursive(_old_assembly_definition.MainModule.Types))
@@ -950,7 +950,7 @@ private static void InitializeOpcodeMap()
950950
_op_code_map.Add(Mono.Cecil.Cil.OpCodes.Tail, OpCodes.Tailcall);
951951
}
952952

953-
public static bool PatchHotfixMethodsNT()
953+
internal static bool PatchHotfixMethodsNT()
954954
{
955955
if (_op_code_map.Count == 0)
956956
{
@@ -1550,7 +1550,7 @@ private static void EmitMethodBody(MethodDefinition methodDefinition, ILGenerato
15501550
}
15511551
}
15521552

1553-
public static bool Reload()
1553+
internal static bool Reload()
15541554
{
15551555
try
15561556
{

0 commit comments

Comments
 (0)