cseelhoff / RimThreaded

RimThreaded is a RimWorld mod designed to enable RimWorld to utilize multiple threads.
MIT License
118 stars 21 forks source link

[Humanoid Alien Races] Compatbility issue with Humanoid alien race #642

Closed brustmopf closed 2 years ago

brustmopf commented 2 years ago

https://github.com/cseelhoff/RimThreaded/blob/273033faf108caba82b0818406ed7a7433a9d60c/Source/HediffSet_Patch.cs#L147

This method overwrites the original rimworld method (destructive prefix). with an almost identical method However, Humanoid alien races patches the original method to change how the field Raceprops.body is referenced. The rimthread patch destroys that HAR patch by using the previous reference to raceprops.body, so we need to "patch the patch" by applying the HAR transpiler to the RT patch if HAR is present:

Suggested patch to add to RT:

using System; using System.Collections.Generic; using System.Reflection; using System.Reflection.Emit; using Verse; using HarmonyLib; using static HarmonyLib.AccessTools; using static RimThreaded.RimThreadedHarmony;

namespace RimThreaded.Mod_Patches { class AlienRace_Patch {

    public static void Patch()
    {
        Type ARHarmonyPatches = TypeByName("AlienRace.HarmonyPatches");
        if (ARHarmonyPatches != null)
        {

            string methodName = nameof(HediffSet_Patch.AddDirect);
            Log.Message("RimThreaded is patching " + typeof(HediffSet_Patch).FullName + " " + methodName);
            Transpile(typeof(HediffSet_Patch), typeof(AlienRace_Patch), methodName);

            methodName = nameof(HediffSet_Patch.CacheMissingPartsCommonAncestors);
            Log.Message("RimThreaded is patching " + typeof(HediffSet_Patch).FullName + " " + methodName);
            Transpile(typeof(HediffSet_Patch), typeof(AlienRace_Patch), methodName);

        }
    }

    public static IEnumerable<CodeInstruction> AddDirect(IEnumerable<CodeInstruction> instructions, ILGenerator iLGenerator)
    {

        Type ARHarmonyPatches = TypeByName("AlienRace.HarmonyPatches");
        return (IEnumerable<CodeInstruction>)ARHarmonyPatches.GetMethod("BodyReferenceTranspiler").Invoke(null, new object[] { instructions, iLGenerator });
    }
    public static IEnumerable<CodeInstruction> CacheMissingPartsCommonAncestors(IEnumerable<CodeInstruction> instructions, ILGenerator iLGenerator)
    {

        Type ARHarmonyPatches = TypeByName("AlienRace.HarmonyPatches");
        return (IEnumerable<CodeInstruction>)ARHarmonyPatches.GetMethod("BodyReferenceTranspiler").Invoke(null, new object[] { instructions, iLGenerator });
    }
}

}

Niilo007 commented 2 years ago

643