Skip to content

Commit

Permalink
Null Ref fix
Browse files Browse the repository at this point in the history
  • Loading branch information
killzoms committed Dec 15, 2023
1 parent 8e2ebf0 commit beab7d5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
11 changes: 10 additions & 1 deletion NitroxClient/MonoBehaviours/NitroxEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,17 @@ public static Dictionary<NitroxId, GameObject> GetObjectsFrom(HashSet<NitroxId>

public static bool TryGetObjectFrom(NitroxId id, out GameObject gameObject)
{
if (id != null && gameObjectsById.TryGetValue(id, out gameObject))
{
if (gameObject)
{
return true;
}
gameObjectsById.Remove(id); // Stale Reference
}

gameObject = null;
return id != null && gameObjectsById.TryGetValue(id, out gameObject) && gameObject;
return false;
}

public static bool TryGetComponentFrom<T>(NitroxId id, out T component)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public sealed partial class FreezeRigidbodyWhenFar_FixedUpdate_Patch : NitroxPat
{
public static readonly MethodInfo TARGET_METHOD = Reflect.Method((FreezeRigidbodyWhenFar t) => t.FixedUpdate());

public static IEnumerable<CodeInstruction> Transpiler(MethodBase original, IEnumerable<CodeInstruction> instructions, ILGenerator generator)
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
List<CodeInstruction> instructionList = instructions.ToList();
for (int i = 0; i < instructionList.Count; i++)
Expand All @@ -23,21 +23,23 @@ public static IEnumerable<CodeInstruction> Transpiler(MethodBase original, IEnum
if (instruction.opcode == OpCodes.Call && instruction.operand.Equals(Reflect.Method((Component c) => c.GetComponent<Rigidbody>())))
{
yield return instruction;
yield return instructionList[i + 1];
yield return instructionList[i + 1]; // Yield the found instruction and the instruction after it
object jmpLabel = null;

for (int j = i; j < instructionList.Count; j++) // search for branch instruction
{
if (instructionList[j].opcode == OpCodes.Ble_Un)
{
jmpLabel = instructionList[j].operand;
jmpLabel = instructionList[j].operand; // Copy original label
break;
}
}
yield return new CodeInstruction(OpCodes.Ldarg_0);
yield return new CodeInstruction(OpCodes.Call, Reflect.Property((Component c) => c.gameObject).GetGetMethod());
yield return new CodeInstruction(OpCodes.Call, Reflect.Method(() => IsMoving(default)));
yield return new CodeInstruction(OpCodes.Brtrue, jmpLabel);

yield return new(OpCodes.Ldarg_0);
yield return new(OpCodes.Call, Reflect.Property((Component c) => c.gameObject).GetGetMethod());
yield return new(OpCodes.Call, Reflect.Method(() => IsMoving(default)));
yield return new(OpCodes.Brtrue, jmpLabel); // Put down original jmp label

i = i + 1;
continue;
}
Expand Down

0 comments on commit beab7d5

Please sign in to comment.