-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix "spawn" command and sync "sub" command
- Loading branch information
1 parent
5c48f8c
commit 43fc3c8
Showing
7 changed files
with
125 additions
and
53 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
Nitrox.Test/Patcher/Patches/Dynamic/SpawnConsoleCommand_OnConsoleCommand_PatchTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using HarmonyLib; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using NitroxTest.Patcher; | ||
|
||
namespace NitroxPatcher.Patches.Dynamic; | ||
|
||
[TestClass] | ||
public class SpawnConsoleCommand_OnConsoleCommand_PatchTest | ||
{ | ||
[TestMethod] | ||
public void Sanity() | ||
{ | ||
IEnumerable<CodeInstruction> originalIl = PatchTestHelper.GetInstructionsFromMethod(SpawnConsoleCommand_OnConsoleCommand_Patch.TARGET_METHOD); | ||
IEnumerable<CodeInstruction> transformedIl = SpawnConsoleCommand_OnConsoleCommand_Patch.Transpiler(originalIl); | ||
transformedIl.Count().Should().Be(originalIl.Count() + 2); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
Nitrox.Test/Patcher/Patches/Dynamic/SubConsoleCommand_OnConsoleCommand_sub_PatchTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using FluentAssertions; | ||
using HarmonyLib; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using NitroxTest.Patcher; | ||
|
||
namespace NitroxPatcher.Patches.Dynamic; | ||
|
||
[TestClass] | ||
public class SubConsoleCommand_OnConsoleCommand_sub_PatchTest | ||
{ | ||
[TestMethod] | ||
public void Sanity() | ||
{ | ||
IEnumerable<CodeInstruction> originalIl = PatchTestHelper.GetInstructionsFromMethod(SubConsoleCommand_OnConsoleCommand_sub_Patch.TARGET_METHOD); | ||
IEnumerable<CodeInstruction> transformedIl = SubConsoleCommand_OnConsoleCommand_sub_Patch.Transpiler(originalIl); | ||
transformedIl.Count().Should().Be(originalIl.Count()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 30 additions & 8 deletions
38
NitroxPatcher/Patches/Dynamic/SubConsoleCommand_OnConsoleCommand_sub_Patch.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,41 @@ | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Reflection.Emit; | ||
using HarmonyLib; | ||
using NitroxClient.GameLogic; | ||
using NitroxModel.Helper; | ||
using UnityEngine; | ||
|
||
namespace NitroxPatcher.Patches.Dynamic; | ||
|
||
/// <summary> | ||
/// Called whenever a Cyclops or Seamoth is spawned. Nitrox already has its own command to spawn vehicles. | ||
/// This patch is only meant to block the method from executing, causing two vehicles to be spawned instead of one | ||
/// </summary> | ||
public sealed partial class SubConsoleCommand_OnConsoleCommand_sub_Patch : NitroxPatch, IDynamicPatch | ||
{ | ||
private static readonly MethodInfo TARGET_METHOD = Reflect.Method((SubConsoleCommand t) => t.OnConsoleCommand_sub(default)); | ||
internal static readonly MethodInfo TARGET_METHOD = Reflect.Method((SubConsoleCommand t) => t.OnConsoleCommand_sub(default)); | ||
|
||
public static bool Prefix() | ||
/* | ||
* REPLACE: | ||
* LightmappedPrefabs.main.RequestScenePrefab(text, new LightmappedPrefabs.OnPrefabLoaded(this.OnSubPrefabLoaded)); | ||
* BY: | ||
* LightmappedPrefabs.main.RequestScenePrefab(text, new LightmappedPrefabs.OnPrefabLoaded(SubConsoleCommand_OnConsoleCommand_sub_Patch.WrappedCallback)); | ||
*/ | ||
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions) | ||
{ | ||
Log.InGame(Language.main.Get("Nitrox_CommandNotAvailable")); | ||
return false; | ||
return new CodeMatcher(instructions).MatchEndForward([ | ||
new CodeMatch(OpCodes.Stfld), | ||
new CodeMatch(OpCodes.Ldsfld), | ||
new CodeMatch(OpCodes.Ldloc_0), | ||
new CodeMatch(OpCodes.Ldarg_0) | ||
]) | ||
.SetOpcodeAndAdvance(OpCodes.Ldnull) | ||
.SetOperandAndAdvance(Reflect.Method(() => WrappedCallback(default))) | ||
.InstructionEnumeration(); | ||
} | ||
|
||
public static void WrappedCallback(GameObject prefab) | ||
{ | ||
SubConsoleCommand instance = SubConsoleCommand.main; | ||
// Call the original callback and then get the object it created to broadcast its creation | ||
instance.OnSubPrefabLoaded(prefab); | ||
Resolve<NitroxConsole>().Spawn(instance.lastCreatedSub); | ||
} | ||
} |