-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoonoMod.cs
370 lines (322 loc) · 16.2 KB
/
MoonoMod.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
// This file is part of MoonoMod and is licenced under the GNU GPL v3.0.
// See LICENSE file for full text.
// Copyright © 2024 Michael Ripley
// Uncomment the following define to enable my debugging features. These include certain things I do not want in the base mod, as they're either logspam, inefficient, or just plain cheating.
#define DEBUG
using System;
using System.Collections.Generic;
using System.Reflection.Emit;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using HarmonyLib;
using UnityEngine;
using UnityEngine.SceneManagement;
namespace MoonoMod
{
[BepInPlugin(GUID, MOD_NAME, MOD_VERSION)]
public class MoonoMod : BaseUnityPlugin
{
internal const string GUID = "dev.zkxs.moonomod";
internal const string MOD_NAME = "MoonoMod";
internal const string MOD_VERSION = "1.1.1";
private readonly static string EXPECTED_LUNACID_VERSION = "1.1.2";
private readonly static int SCALING_TYPE_MOON = 1;
internal static new ManualLogSource? Logger;
internal static ConfigEntry<bool>? fullMoon;
internal static ConfigEntry<bool>? skipWaits;
internal static ConfigEntry<bool>? christmas;
internal static ConfigEntry<bool>? summer;
internal static ConfigEntry<bool>? fixAllSpellCheck;
internal static ConfigEntry<bool>? fixAllWeaponCheck;
internal static ConfigEntry<bool>? allLoot;
internal static ConfigEntry<bool>? debugLogs;
internal static ConfigEntry<bool>? verboseLogs;
internal static ConfigEntry<bool>? debugInventory;
internal static ConfigEntry<bool>? debugLoot;
private void Awake()
{
try
{
Logger = base.Logger; // this lets us access the logger from static contexts later: namely our patches.
fullMoon = Config.Bind("General", "Force Full Moon", true, "Force full moon exclusive objects to appear on level load, and maximize the moon multiplier.");
skipWaits = Config.Bind("General", "Skip Waits", false, "Force all checks to see if the player has waited some duration of time (sometimes minutes, somtimes months) to pass.");
christmas = Config.Bind("General", "Force Christmas", false, "Force Christmas exclusive objects to appear on level load, and allow the Jingle Bells spell to be cast.");
summer = Config.Bind("General", "Force Summer", false, "Force Summer exclusive objects to appear on level load.");
fixAllSpellCheck = Config.Bind("Bugfixes", "Fix All-Spell Check", true, "Fix the all-spell check to not include normally unobtainable spells in your total.");
fixAllWeaponCheck = Config.Bind("Bugfixes", "Fix All-Weapon Check", true, "Fix the all-weapon check to not not break if your Shadow/Shining blade has nonzero weapon XP.");
#if DEBUG
allLoot = Config.Bind("Cheats", "Drop All Loot", false, "Enemies drop everything in their loot table when killed.");
debugLogs = Config.Bind("Developer", "Enable Debug Logs", false, "Emit BepInEx logs when certain time checks are detected. Useful for figuring out which levels contain which checks.");
verboseLogs = Config.Bind("Developer", "Enable Verbose Logs", false, "Emit BepInEx logs in a higher level of verbosity."); // currently unused
debugInventory = Config.Bind("Developer", "Enable Inventory Debug Logs", false, "Emit BepInEx logs that contain inventory state information.");
debugLoot = Config.Bind("Developer", "Enable Loot Debug Logs", false, "Emit BepInEx logs that contain drop table information on enemy kill.");
#endif
if (Application.version != EXPECTED_LUNACID_VERSION)
{
Logger.LogWarning($"Lunacid is on version {Application.version}, but {MOD_NAME} was built for Lunacid {EXPECTED_LUNACID_VERSION}. While {MOD_NAME} was designed to be as future-proof as reasonably possible, it may behave in unintended ways as I can't forsee every change Kira might make.");
}
Harmony harmony = new Harmony(GUID);
AllSpells.Init();
AllWeapons.Init();
harmony.PatchAll();
Logger.LogInfo("You're about to hack time, are you sure?"); // kung fury quote
}
catch (Exception e)
{
base.Logger.LogError($"Something has gone terribly wrong:\n{e}");
throw e;
}
}
// the date passed to the Christmas spell's time check.
public static DateTime ChristmasDate()
{
// this is intentionally not logged, as it'd get spammed every time you try casting the Christmas spell.
if (christmas!.Value)
{
// This date counts as Christmas, obviously
return new(2023, 12, 25);
}
else
{
return DateTime.Now;
}
}
// the date passed to SimpleMoon's time check
public static DateTime FullMoonDate()
{
// this is intentionally not logged, as the game makes a LOT of SimpleMoon components.
if (fullMoon!.Value)
{
// This date ALSO counts as a full moon, somewhat less obviously.
return new(2023, 12, 25);
}
else
{
return DateTime.Now;
}
}
[HarmonyPatch]
private static class HarmonyPatches
{
// Enable the March-August timed content by skipping the check.
// This is used to put some flowers on Patchouli's branch-horn-things.
[HarmonyPrefix]
[HarmonyPatch(typeof(Season_Con), "OnEnable")]
private static bool SeasonCon(Season_Con __instance)
{
if (debugLogs?.Value ?? false)
{
Logger!.LogInfo($"Level {SceneManager.GetActiveScene().name} contains a Summer check.");
}
if (!summer!.Value)
{
return true; // run original method
}
__instance.transform.GetChild(0).gameObject.SetActive(true);
return false; // skip original method
}
// Skip the wait-a-month skeleton egg check. Normally it requires the month to change.
// Also note that the vanilla code isn't making you wait a month... it's making you wait for the current month to CHANGE.
[HarmonyPrefix]
[HarmonyPatch(typeof(WaitAMonth), "Start")]
private static bool WaitAMonth(WaitAMonth __instance)
{
if (__instance.Setter)
{
return true; // run original method it it's a Setter call, because why not?
}
else
{
if (debugLogs?.Value ?? false)
{
Logger!.LogInfo($"Level {SceneManager.GetActiveScene().name} contains a wait-a-month check.");
}
if (!skipWaits!.Value)
{
return true; // run original method
}
// if it's a Getter call just skip the check and run the SetActive code.
__instance.transform.GetChild(0).gameObject.SetActive(true);
return false; // skip original method
}
}
// Skip the real time duration that certain things require you to wait for.
// Note that Kira's code is buggy and doesn't track time correctly. There are not 600 minutes in a day, Kira.
[HarmonyPrefix]
[HarmonyPatch(typeof(Real_Timer), "OnEnable")]
private static bool RealTimer(Real_Timer __instance)
{
if (__instance.Begin)
{
return true; // run original method it it's a Begin call, because why not?
}
else
{
if (debugLogs?.Value ?? false)
{
Logger!.LogInfo($"Level {SceneManager.GetActiveScene().name} contains a {__instance.length} minutes wait check.");
}
if (!skipWaits!.Value)
{
return true; // run original method
}
__instance.ACT?.SetActive(true);
return false; // skip original method
}
}
// Always enable objects that are Christmas-exclusive. This is mostly decorations, but also the Christmas spell.
[HarmonyPrefix]
[HarmonyPatch(typeof(CRIMPUS), "OnEnable")]
private static bool Christmas(WaitAMonth __instance)
{
if (debugLogs?.Value ?? false)
{
Logger!.LogInfo($"Level {SceneManager.GetActiveScene().name} contains a Christmas check.");
}
if (!christmas!.Value)
{
return true; // run original method
}
__instance.transform.GetChild(0).gameObject.SetActive(true);
return false; // skip original method
}
// Make the Christmas spell always work. This is achieved by hijacking one of the DateTime.Now calls Magic_scr.Cast() makes.
[HarmonyTranspiler]
[HarmonyPatch(typeof(Magic_scr), "Cast")]
private static IEnumerable<CodeInstruction> ChristmasCast(IEnumerable<CodeInstruction> instructions)
{
var codes = new List<CodeInstruction>(instructions);
var nowMethod = AccessTools.DeclaredPropertyGetter(typeof(DateTime), nameof(DateTime.Now));
var fakeMethod = AccessTools.DeclaredMethod(typeof(MoonoMod), nameof(ChristmasDate));
bool santaCast = false;
for (var index = 0; index < codes.Count; index += 1)
{
if (santaCast)
{
// we've found the santa string, so now search for the next DateTime.Now call
if (codes[index].Calls(nowMethod))
{
// replace with a call to our faked DateTime.Now()
codes[index] = new CodeInstruction(OpCodes.Call, fakeMethod);
return codes;
}
}
else
{
// search for first santa cast
if (codes[index].opcode == OpCodes.Ldstr && (string)codes[index].operand == "SANTA_CAST")
{
santaCast = true;
}
}
}
throw new TranspilerException("could not find DateTime.Now call after LDSTR \"SANTA_CAST\" to patch");
}
// Make it always a full moon. This is achieved by hijacking all DateTime.Now calls SimpleMoon makes.
[HarmonyTranspiler]
[HarmonyPatch(typeof(SimpleMoon), "Start")]
private static IEnumerable<CodeInstruction> FullMoon(IEnumerable<CodeInstruction> instructions)
{
var codes = new List<CodeInstruction>(instructions);
var nowMethod = AccessTools.DeclaredPropertyGetter(typeof(DateTime), nameof(DateTime.Now));
var fakeMethod = AccessTools.DeclaredMethod(typeof(MoonoMod), nameof(FullMoonDate));
bool replacedAny = false;
for (int index = 0; index < codes.Count; index += 1)
{
if (codes[index].Calls(nowMethod))
{
// replace with a call to our faked DateTime.Now()
codes[index] = new CodeInstruction(OpCodes.Call, fakeMethod);
replacedAny = true;
}
}
if (replacedAny)
{
return codes;
}
else
{
throw new TranspilerException("could not find any DateTime.Now calls to patch");
}
}
#if DEBUG
// log levels that contain full moon exclusive objects
[HarmonyPrefix]
[HarmonyPatch(typeof(Spawn_if_moon), "OnEnable")]
private static void LogMoonCheck(Spawn_if_moon __instance)
{
if (debugLogs?.Value ?? false)
{
bool passed = __instance.MOON.MOON_MULT > 9.0;
Logger!.LogInfo($"Level {SceneManager.GetActiveScene().name} contains a full moon check. MOON_MULT = {__instance.MOON.MOON_MULT}. Pass = {passed}.");
}
}
#endif
#if DEBUG
// log levels that contain materials or lights affected by MOON_MULT
[HarmonyPrefix]
[HarmonyPatch(typeof(Moon_Light), "OnEnable")]
private static void LogMoonLight(Moon_Light __instance)
{
if (debugLogs?.Value ?? false)
{
Logger!.LogInfo($"Level {SceneManager.GetActiveScene().name} contains {__instance.Mats.Length} materials and {__instance.Lights.Length} lights affected by MOON_MULT ({__instance.MOON.MOON_MULT}).");
}
}
#endif
#if DEBUG
// log levels that enemies with moon-based health scaling
[HarmonyPrefix]
[HarmonyPatch(typeof(NPC_Scaling), "Scale_NPC")]
private static void LogMoonHealth(NPC_Scaling __instance)
{
if ((debugLogs?.Value ?? false) && __instance.Scaling_Type == SCALING_TYPE_MOON)
{
var scale_factor = Mathf.Lerp(1f, __instance.scale_str, __instance.MOON.MOON_MULT / 8f);
Logger!.LogInfo($"Level {SceneManager.GetActiveScene().name} contains NPC {__instance.AI.gameObject.name} with health scaled by {scale_factor} to {__instance.AI.health_max} by MOON_MULT ({__instance.MOON.MOON_MULT}).");
}
}
#endif
#if DEBUG
// drop all possible loot from an enemy (instead of just one item from the loot table). THIS IS CHEATING! I used this to debug the all weapons achievement check, because ain't no way I was gonna get both an Obsidian Cursebrand and an Obsidian Posisonguard naturally.
[HarmonyPrefix]
[HarmonyPatch(typeof(Loot_scr), "OnEnable")]
private static bool AllLoot(Loot_scr __instance)
{
if (debugLoot?.Value ?? false)
{
Logger!.LogInfo($"{__instance.gameObject?.name} loot table:");
int totalChance = 0;
foreach (var reward in __instance.LOOTS)
{
totalChance += reward.CHANCE;
}
foreach (var reward in __instance.LOOTS)
{
string? name = reward.ITEM?.name ?? "null";
double chance = 100d * reward.CHANCE / totalChance;
Logger!.LogInfo($"* {chance,6:0.00}%: {name}");
}
}
if (!(allLoot?.Value ?? false))
{
return true; // run original method
}
for (int index = 0; index < __instance.LOOTS.Length; index += 1)
{
if (__instance.LOOTS[index].ITEM != null)
{
GameObject gameObject = Instantiate(__instance.LOOTS[index].ITEM, __instance.transform.position, Quaternion.identity);
gameObject.SetActive(false);
gameObject.AddComponent<Place_on_Ground>();
gameObject.GetComponent<Place_on_Ground>().LOOTED = true;
gameObject.SetActive(true);
}
}
return false; // skip original method
}
#endif
}
}
}