Skip to content

Commit 23474ff

Browse files
committed
Fix mod loader mod enable/disable functionality.
1 parent 26de723 commit 23474ff

File tree

3 files changed

+22
-33
lines changed

3 files changed

+22
-33
lines changed

Source/Mod/Mod.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public bool Loaded
3131
}
3232

3333
protected bool forceloaded = false;
34-
private bool preloaded = true;
34+
internal bool preloaded = true;
3535
internal bool initialized = false;
3636

3737
public bool NextState
@@ -64,6 +64,7 @@ public Mod(Info.ModInfo info, Manifest.ModManifest manifest, global::Mod instanc
6464
this.Manifest = manifest;
6565
this.instance = instance;
6666
this.modDisableState = modDisableState;
67+
this.initialized = true;
6768
}
6869

6970
public EModDisableState GetModDisableState()
@@ -81,7 +82,7 @@ public EModDisableState GetModDisableState()
8182

8283
public string GetModDisableStateReason()
8384
{
84-
switch (this.modDisableState)
85+
switch (this.GetModDisableState())
8586
{
8687
case EModDisableState.Allowed:
8788
return "";

Source/Mod/ModLoader.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ public void SetSelfAndDetectMods(global::Mod _modInstance)
108108
}
109109
}
110110

111-
public bool IsModEnabled(Mod mod)
111+
public bool IsModEnabled(global::Mod mod)
112112
{
113-
return !disabledModNames.Contains(mod.Info.Name);
113+
return !disabledModNames.Contains(mod.Name);
114114
}
115115

116116
public Mod GetModFromInstance(global::Mod instance)
@@ -136,6 +136,7 @@ public bool SaveModChanges()
136136
if (mod.NextState)
137137
{
138138
lines.RemoveAll(modName => mod.Info.Name.EqualsCaseInsensitive(modName));
139+
disabledModNames.Remove(mod.Info.Name);
139140
mod.Load();
140141
}
141142
else if(!lines.Contains(mod.Info.Name))

Source/Mod/ModLoaderPatches.cs

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,47 +4,34 @@ namespace CustomModManager.Mod
44
{
55
internal sealed class ModLoaderPatches
66
{
7-
// Flag to prevent mod DLLs loading after Mod Manager does to prevent disabled mods from loading automatically.
8-
public static bool CAN_INIT_MOD_CODE = false;
9-
107
[HarmonyPatch(typeof(global::Mod))]
118
[HarmonyPatch(nameof(global::Mod.InitModCode))]
129
private sealed class Mod_InitModCode_Patch
1310
{
14-
private static int PRE_INIT_COUNT = 1; // 1 includes 7 Days To Die itself.
15-
16-
private static bool Prefix()
11+
private static bool Prefix(global::Mod __instance)
1712
{
18-
if (!CAN_INIT_MOD_CODE)
19-
PRE_INIT_COUNT++;
13+
Mod modInstance = ModLoader.Instance.GetModFromInstance(__instance);
14+
modInstance.preloaded = false;
2015

21-
return CAN_INIT_MOD_CODE;
22-
}
16+
bool enabled = ModLoader.Instance.IsModEnabled(__instance);
17+
modInstance.initialized = enabled;
2318

24-
private static int MOD_COUNT
25-
{
26-
get
27-
{
28-
return ModLoader.Instance.GetMods(false).Count;
29-
}
19+
return enabled;
3020
}
21+
}
3122

32-
private static void Postfix()
23+
[HarmonyPatch(typeof(global::ModManager))]
24+
[HarmonyPatch(nameof(global::ModManager.ModLoaded))]
25+
private sealed class ModManager_ModLoaded_Patch
26+
{
27+
private static void Postfix(string _modName, ref bool __result)
3328
{
34-
if (CAN_INIT_MOD_CODE)
35-
return;
36-
37-
int modCount = MOD_COUNT;
38-
CAN_INIT_MOD_CODE = PRE_INIT_COUNT == modCount;
39-
40-
if(CAN_INIT_MOD_CODE)
29+
if(__result == false)
4130
{
42-
foreach(var mod in ModLoader.Instance.GetMods(false))
43-
{
44-
if(ModLoader.Instance.IsModEnabled(mod))
45-
mod.Load();
46-
}
31+
return;
4732
}
33+
34+
__result = ModLoader.Instance.IsModEnabled(ModManager.loadedMods.dict[_modName]);
4835
}
4936
}
5037
}

0 commit comments

Comments
 (0)