Skip to content

Commit

Permalink
Merge pull request #459 from PhantomGamers/patch-ach_broadcaststar
Browse files Browse the repository at this point in the history
Fix NRE in ACH_BroadcastStar.OnGameTick()
  • Loading branch information
PhantomGamers authored Oct 4, 2021
2 parents 2a0cdac + 91ff54f commit ca1861d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
## Changelog

0.6.3:
0.7.0:

- Fixed instance where error would trigger by loading saves made on earlier Nebula versions. **WARNING: All previous client inventory and position data will be lost!** (should be for the last time!)
- Fixed error that was triggered by the client loading a planet after traveling to a different planetary system
- @phantomgamers: Fixed instance where error would trigger by loading saves made on earlier Nebula versions. **WARNING: All previous client inventory and position data will be lost!** (should be for the last time!)
- @phantomgamers: Fixed error that was triggered by the client loading a planet after traveling to a different planetary system
- @phantomgamers: Fixed error that was triggered by the client warping outside of a planetary system
- @starfi5h: Added syncing of solar sails and rockets when client does not have the planet they originated from loaded.
- @sp00ktober: Implemented smooth loading of factories for clients (fixed clients phasing through planet when flying too fast)

0.6.2:

Expand Down
66 changes: 66 additions & 0 deletions NebulaPatcher/Patches/Transpilers/ACH_BroadcastStar_Transpiler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using HarmonyLib;
using NebulaWorld;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Reflection.Emit;

namespace NebulaPatcher.Patches.Transpilers
{
[HarmonyPatch(typeof(ACH_BroadcastStar))]
internal class ACH_BroadcastStar_Transpiler
{
/*
* Returns early if
* instance.gameData.factories[factoryId]?.powerSystem?.genPool[generatorId] == null || instance.gameData.factories[factoryId].index == -1
* while in a multiplayer session as a client
*/
[HarmonyTranspiler]
[HarmonyPatch(nameof(ACH_BroadcastStar.OnGameTick))]
private static IEnumerable<CodeInstruction> OnGameTick_Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator il)
{
try
{
CodeMatcher codeMatcher = new CodeMatcher(instructions, il)
.MatchForward(false,
new CodeMatch(OpCodes.Ldarg_0),
new CodeMatch(i => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "gameData"),
new CodeMatch(i => i.opcode == OpCodes.Ldfld && ((FieldInfo)i.operand).Name == "factories"),
new CodeMatch(i => i.IsLdloc()),
new CodeMatch(OpCodes.Ldelem_Ref)
);

CodeInstruction factoryIdInstruction = codeMatcher.InstructionAt(3);
CodeInstruction generatorIdInstruction = codeMatcher.InstructionAt(7);

return codeMatcher
.CreateLabel(out Label label)
.InsertAndAdvance(new CodeInstruction(OpCodes.Ldarg_0))
.InsertAndAdvance(factoryIdInstruction)
.InsertAndAdvance(generatorIdInstruction)
.InsertAndAdvance(HarmonyLib.Transpilers.EmitDelegate<Func<ACH_BroadcastStar, int, int, bool>>((instance, factoryId, generatorId) =>
{
if (!Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost)
{
return true;
}

if (instance.gameData.factories[factoryId]?.powerSystem?.genPool[generatorId] == null || instance.gameData.factories[factoryId].index == -1)
{
return false;
}

return true;
}))
.InsertAndAdvance(new CodeInstruction(OpCodes.Brtrue, label))
.InsertAndAdvance(new CodeInstruction(OpCodes.Ret))
.InstructionEnumeration();
}
catch
{
NebulaModel.Logger.Log.Error("ACH_BroadcastStar.OnGameTick_Transpiler failed. Mod version not compatible with game version.");
return instructions;
}
}
}
}
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "0.6.3",
"version": "0.7.0",
"assemblyVersion": {
"precision": "build"
},
Expand Down

0 comments on commit ca1861d

Please sign in to comment.