Skip to content

Commit

Permalink
Merge pull request #642 from sp00ktober/master
Browse files Browse the repository at this point in the history
fix NRE in UpdateDrones due to leftover drones from BattleBases
  • Loading branch information
sp00ktober authored Jan 11, 2024
2 parents f2f4bcb + 39aadfa commit c2f08e4
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions NebulaPatcher/Patches/Transpilers/ConstructionSystem_Transpiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace NebulaPatcher.Patches.Transpilers;

internal delegate bool IsOwnerAMecha(int owner);

internal delegate bool OwnerAndIdMatchMecha(int id, int owner);
internal delegate bool OwnerAndIdMatchMecha(ConstructionModuleComponent constructionModuleComponent, int owner);
internal delegate bool AreDronesEnabled(bool droneEnabled, ref DroneComponent drone);

[HarmonyPatch(typeof(ConstructionSystem))]
Expand Down Expand Up @@ -158,7 +158,7 @@ public static IEnumerable<CodeInstruction> UpdateDrones_Transpiler2(IEnumerable<
owner <= 0; // we set owner to negative values in ConstructionModuleComponent_Transpiler to mark drones from other players.
}));
matcher
.MatchForward(true,
.MatchForward(false,
new CodeMatch(OpCodes.Ldloc_S),
new CodeMatch(i => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "id"),
new CodeMatch(OpCodes.Ldloc_S),
Expand All @@ -175,13 +175,20 @@ public static IEnumerable<CodeInstruction> UpdateDrones_Transpiler2(IEnumerable<
// change: if (constructionModuleComponent.id != ptr.owner || ptr2.id != ptr.craftId)
// to: if (ptr2.id != ptr.craftId)
matcher
.Advance(1)
.Set(OpCodes.Nop, null) // dont grab id but pass whole object, as it can be null in certain situations
.Advance(3)
.InsertAndAdvance(
HarmonyLib.Transpilers.EmitDelegate<OwnerAndIdMatchMecha>((id, owner) =>
HarmonyLib.Transpilers.EmitDelegate<OwnerAndIdMatchMecha>((constructionModuleComponent, owner) =>
{
if (!Multiplayer.IsActive)
{
return
id != owner; // game does exit when id does not match owner, so we do too when multiplayer is inactive
return constructionModuleComponent.id != owner; // game does exit when id does not match owner, so we do too when multiplayer is inactive
}
if (constructionModuleComponent == null)
{
// this might happen when battle bases are removed while drones are still around
return true; // this will stop rendering of those drones
}

return false; // this might be a bit too open but will render any drone regardless of the games checks.
Expand Down

0 comments on commit c2f08e4

Please sign in to comment.