-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cs
117 lines (100 loc) · 3.48 KB
/
Main.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
using DDSS_MelonSettings.Components;
using DDSS_MelonSettings.GUI;
using HarmonyLib;
using MelonLoader;
using Semver;
using System;
using System.Reflection;
namespace DDSS_MelonSettings
{
internal class MelonMain : MelonMod
{
private static bool _isDisabled;
internal static MelonLogger.Instance _logger;
public override void OnInitializeMelon()
{
// Static Cache Logger
_logger = LoggerInstance;
// Check for ModHelper
MelonMod modHelper = FindModHelper();
if (modHelper != null)
{
MakeModHelperAware(modHelper);
// Check for Legacy ModHelpers
if (IsUsingLegacyModHelper(modHelper, out SemVersion modHelperVersion))
{
_logger.Msg($"ModHelper {modHelperVersion.ToString()} includes a Mod Settings GUI already!");
_logger.Msg($"Temporarily disabling {Properties.BuildInfo.Name}...");
_isDisabled = true;
return;
}
}
// Register Custom Types
ManagedEnumerator.Register();
// Apply Patches
ApplyPatches();
// Log Success
_logger.Msg("Initialized!");
}
public override void OnSceneWasInitialized(int buildIndex, string sceneName)
{
if (_isDisabled)
return;
if (sceneName == "MainMenuScene") // Main Menu
{
ModSettingsManager.MainMenuInit();
}
else if (sceneName != "LobbyScene") // In-Game
{
ModSettingsManager.GameInit();
}
}
private void ApplyPatches()
{
Assembly melonAssembly = typeof(MelonMain).Assembly;
foreach (Type type in melonAssembly.GetValidTypes())
{
// Check Type for any Harmony Attribute
if (type.GetCustomAttribute<HarmonyPatch>() == null)
continue;
// Apply
try
{
if (MelonDebug.IsEnabled())
LoggerInstance.Msg($"Applying {type.Name}");
HarmonyInstance.PatchAll(type);
}
catch (Exception e)
{
LoggerInstance.Error($"Exception while attempting to apply {type.Name}: {e}");
}
}
}
private MelonMod FindModHelper()
{
MelonMod modHelper = null;
foreach (var mod in RegisteredMelons)
if (mod.Info.Name == "ModHelper")
{
modHelper = mod;
break;
}
return modHelper;
}
private bool IsUsingLegacyModHelper(MelonMod modHelper, out SemVersion ver)
{
ver = modHelper.Info.SemanticVersion;
return ver <= "1.2.0";
}
private void MakeModHelperAware(MelonMod modHelper)
{
Type modFilterType = modHelper.MelonAssembly.Assembly.GetType("DDSS_ModHelper.Utils.RequirementFilter");
if (modFilterType == null)
return;
MethodInfo method = modFilterType.GetMethod("AddOptionalMelon", BindingFlags.Public | BindingFlags.Static);
if (method == null)
return;
method.Invoke(null, [this]);
}
}
}