Skip to content

Commit

Permalink
added support for custom c-side unlock postcards
Browse files Browse the repository at this point in the history
  • Loading branch information
nhruo123 committed Jun 12, 2023
1 parent dbc5e22 commit 52621d5
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions Celeste.Mod.mm/Patches/OverworldLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using Mono.Cecil.Cil;
using MonoMod.Cil;
using MonoMod.Utils;
using System.Linq;


namespace Celeste {
class patch_OverworldLoader : OverworldLoader {
Expand All @@ -24,7 +26,23 @@ public patch_OverworldLoader(Overworld.StartMode startMode, HiresSnow snow = nul

[MonoModIgnore] // don't change anything in the method...
[PatchTotalHeartGemCSidePostcard] // except for replacing TotalHeartGems with TotalHeartGemsInVanilla through MonoModRules
[PatchCSidePostcardText] // and checking for a custom C-side unlock postcard through MonoModRules
private extern IEnumerator Routine(Session session);


/// <summary>
/// A helper function that is called from <c>Routine</c> to determent which c-side unlock postcard to display
/// </summary>
private static string GetCSidePostcard(Session session) {
AreaData areaData = AreaData.Get(session);
string customLevelCSidePostcardDialog = $"{areaData.Name}_CSIDES_POSTCARD";

if (areaData.GetLevelSet() == "Celeste" || !Dialog.Has(customLevelCSidePostcardDialog)) {
return "POSTCARD_CSIDES";
} else {
return customLevelCSidePostcardDialog;
}
}
}
}

Expand All @@ -35,6 +53,12 @@ namespace MonoMod {
[MonoModCustomMethodAttribute(nameof(MonoModRules.PatchTotalHeartGemCSidePostcard))]
class PatchTotalHeartGemCSidePostcardAttribute : Attribute { }

/// <summary>
/// Patch Routine to get the dialog for the C-side unlock instead of the constant "POSTCARD_CSIDES"
/// </summary>
[MonoModCustomMethodAttribute(nameof(MonoModRules.PatchCSidePostcardText))]
class PatchCSidePostcardTextAttribute : Attribute { }

static partial class MonoModRules {

public static void PatchTotalHeartGemCSidePostcard(MethodDefinition method, CustomAttribute attrib) {
Expand All @@ -60,5 +84,22 @@ public static void PatchTotalHeartGemCSidePostcard(MethodDefinition method, Cust
});
}

public static void PatchCSidePostcardText(MethodDefinition method, CustomAttribute attrib) {
MethodDefinition m_OverworldLoader_GetCSidePostcard = method.Module.GetType("Celeste.OverworldLoader").FindMethod("System.String GetCSidePostcard(Celeste.Session)");

method = method.GetEnumeratorMoveNext();

FieldDefinition f_session = method.DeclaringType.Fields.FirstOrDefault(f => f.FieldType.Name == "Session");

new ILContext(method).Invoke(il => {
ILCursor cursor = new ILCursor(il);

cursor.GotoNext(MoveType.Before, instr => instr.MatchLdstr("POSTCARD_CSIDES"));
cursor.Remove();
cursor.Emit(OpCodes.Ldarg_0);
cursor.Emit(OpCodes.Ldfld, f_session);
cursor.Emit(OpCodes.Call, m_OverworldLoader_GetCSidePostcard);
});
}
}
}

0 comments on commit 52621d5

Please sign in to comment.