Skip to content

Commit

Permalink
Fix new GMHS heart color being incorrect in overworld journal poem page
Browse files Browse the repository at this point in the history
  • Loading branch information
maddie480 committed Mar 30, 2021
1 parent 37250e1 commit 709d3e1
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions GrandmasterHeartSideHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ static class GrandmasterHeartSideHelper {
private static ILHook hookLobbyJournal;
private static ILHook hookOverworldJournal;
private static Hook hookLevelExitToLobby;
private static ILHook hookPoemColors;

public static void Load() {
Assembly collabUtils = typeof(CollabUtils2.CollabModule).Assembly;
Expand All @@ -39,6 +40,19 @@ public static void Load() {

IL.Celeste.Level.CompleteArea_bool_bool_bool += modLevelComplete;
IL.Celeste.OuiChapterPanel.Render += renderOldGMHSCompletionStamp;

// we are looking for a lambda in Celeste.Mod.CollabUtils2.LobbyHelper.modJournalPoemHeartColors...
// except it is located in Celeste.Mod.CollabUtils2.LobbyHelper.<>c.<modJournalPoemHeartColors>b__{someRandomNumber}_0.
// find it by bruteforcing it a bit.
Type innerType = collabUtils.GetType("Celeste.Mod.CollabUtils2.LobbyHelper").GetNestedType("<>c", BindingFlags.NonPublic);
for (int i = 0; i < 100; i++) {
MethodInfo innerMethod = innerType.GetMethod($"<modJournalPoemHeartColors>b__{i}_0", BindingFlags.NonPublic | BindingFlags.Instance);
if (innerMethod != null) {
// found it!
hookPoemColors = new ILHook(innerMethod, modifyGMHSeartColor);
break;
}
}
}

public static void Unload() {
Expand All @@ -53,6 +67,9 @@ public static void Unload() {

IL.Celeste.Level.CompleteArea_bool_bool_bool -= modLevelComplete;
IL.Celeste.OuiChapterPanel.Render -= renderOldGMHSCompletionStamp;

hookPoemColors?.Dispose();
hookPoemColors = null;
}

private static bool modIsHeartSide(Func<string, bool> orig, string sid) {
Expand Down Expand Up @@ -173,5 +190,22 @@ private static void renderOldGMHSCompletionStamp(ILContext il) {
});
}
}

private static void modifyGMHSeartColor(ILContext il) {
ILCursor cursor = new ILCursor(il);

while (cursor.TryGotoNext(MoveType.After, instr => instr.MatchLdstr("_ZZ_HeartSide_A"), instr => instr.MatchCall<string>("Concat"))) {
Logger.Log("SpringCollab2020/GrandmasterHeartSideHelper", $"Modifying new GMHS heart color at {cursor.Index} in IL for {il.Method.FullName}");

cursor.Emit(OpCodes.Ldarg_2); // "poem" argument
cursor.EmitDelegate<Func<string, string, string>>((orig, poem) => {
// alter the check so that it matches on the new GMHS like it does for old GMHS.
if (orig == "poem_SpringCollab2020/5-Grandmaster_ZZ_HeartSide_A" && poem == Dialog.Clean("poem_SpringCollab2020_5_Grandmaster_ZZ_NewHeartSide_A")) {
return "poem_SpringCollab2020_5_Grandmaster_ZZ_NewHeartSide_A";
}
return orig;
});
}
}
}
}

0 comments on commit 709d3e1

Please sign in to comment.