diff --git a/Source/Client/Syncing/Game/SyncMethods.cs b/Source/Client/Syncing/Game/SyncMethods.cs index 7199000de..a3d5fbd9a 100644 --- a/Source/Client/Syncing/Game/SyncMethods.cs +++ b/Source/Client/Syncing/Game/SyncMethods.cs @@ -869,6 +869,39 @@ public static void TryDirtyCurrentPawnTable(object instance = null, object[] arg table.SetDirty(); } } + [HarmonyPatch(typeof(FlyShipLeaving), nameof(FlyShipLeaving.LeaveMap))] + static class FlyShipLeaving_LeaveMap_Patch + { + //When launch transporter, should try to use pawn inside's faction instead of Faction.OfPlayer + static IEnumerable Transpiler(IEnumerable insts) + { + var ofPlayerGetter = AccessTools.PropertyGetter(typeof(Faction), nameof(Faction.OfPlayer)); + var replacement = AccessTools.Method(typeof(FlyShipLeaving_LeaveMap_Patch), + nameof(GetFactionFromContents)); + + foreach (var ci in insts) + { + if (ci.Calls(ofPlayerGetter)) + { + yield return new CodeInstruction(OpCodes.Ldarg_0); + ci.opcode = OpCodes.Call; + ci.operand = replacement; + } + yield return ci; + } + } + + static Faction GetFactionFromContents(FlyShipLeaving flyShip) + { + foreach (Thing t in flyShip.Contents.innerContainer) + { + Pawn pawn = t as Pawn; + if (pawn?.Faction != null && pawn.Faction.IsPlayer) + return pawn.Faction; + } + return Faction.OfPlayer; // fallback + } + } [HarmonyPatch(typeof(ITab_ContentsBooks), nameof(ITab_ContentsBooks.DoRow))] static class ITab_ContentsBooks_DoRow_Patch