Skip to content

Commit

Permalink
Add KK_Fix_PartyCardCompatibility code
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Jun 14, 2019
1 parent e3dac8b commit 3c11311
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
44 changes: 44 additions & 0 deletions KK_Fix_PartyCardCompatibility/FixPartyCardCompatibility.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection.Emit;
using BepInEx;
using BepInEx.Logging;
using Common;
using Harmony;

Expand All @@ -10,5 +13,46 @@ namespace KK_Fix_PartyCardCompatibility
public class FixPartyCardCompatibility : BaseUnityPlugin
{
public const string Guid = "KK_Fix_PartyCardCompatibility";

private void Awake()
{
HarmonyInstance.Create(Guid).PatchAll(typeof(FixPartyCardCompatibility));
}

/// <summary>
/// Needs to return false if the condition passes, true if it fails (this replaces the string != operand)
/// </summary>
private static bool CustomCardTokenCompare(string read, string expected)
{
if (read == null) return true;

// Other card types add letters to the end of the string before closing bracket
var trimmedExpected = expected.Substring(0, expected.Length - 1);
return !read.StartsWith(trimmedExpected, StringComparison.OrdinalIgnoreCase);
}

[HarmonyTranspiler, HarmonyPatch(typeof(ChaFile), "LoadFile", new[] { typeof(BinaryReader), typeof(bool), typeof(bool) })]
public static IEnumerable<CodeInstruction> ChaFileLoadFileTranspiler(IEnumerable<CodeInstruction> instructions)
{
var patchNext = false;
foreach (var instruction in instructions)
{
if (patchNext)
{
if (instruction.opcode == OpCodes.Call)
instruction.operand = AccessTools.Method(typeof(FixPartyCardCompatibility), nameof(CustomCardTokenCompare));
else
Logger.Log(LogLevel.Error, $"[{Guid}] Failed to hook ChaFile.LoadFile, unexpected IL opcode");

patchNext = false;
}
else if (instruction.operand is string s && s == "【KoiKatuChara】")
{
patchNext = true;
}

yield return instruction;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,14 @@
<HintPath>..\lib\0Harmony.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Assembly-CSharp">
<HintPath>..\lib\Assembly-CSharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="BepInEx">
<HintPath>..\lib\BepInEx.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="UnityEngine">
<HintPath>..\lib\UnityEngine.dll</HintPath>
Expand Down

0 comments on commit 3c11311

Please sign in to comment.