From 6a6016e1daa16d7435d3d8e15ddb37165f26dedb Mon Sep 17 00:00:00 2001 From: starfish <50672801+starfi5h@users.noreply.github.com> Date: Sat, 23 Dec 2023 01:36:57 +0800 Subject: [PATCH] Refactor OnNewSetInserterPickTarget & OnNewSetInserterInsertTarget --- NebulaAPI/GameState/IFactoryManager.cs | 4 -- .../Transpilers/PlanetFactory_Transpiler.cs | 52 ++++++++++++------- NebulaWorld/Factory/FactoryManager.cs | 21 -------- 3 files changed, 32 insertions(+), 45 deletions(-) diff --git a/NebulaAPI/GameState/IFactoryManager.cs b/NebulaAPI/GameState/IFactoryManager.cs index 63bba542a..3b5d9ea2b 100644 --- a/NebulaAPI/GameState/IFactoryManager.cs +++ b/NebulaAPI/GameState/IFactoryManager.cs @@ -44,8 +44,4 @@ public interface IFactoryManager : IDisposable int GetNextPrebuildId(int planetId); int GetNextPrebuildId(PlanetFactory factory); - - void OnNewSetInserterPickTarget(int objId, int otherObjId, int inserterId, int offset, Vector3 pointPos); - - void OnNewSetInserterInsertTarget(int objId, int otherObjId, int inserterId, int offset, Vector3 pointPos); } diff --git a/NebulaPatcher/Patches/Transpilers/PlanetFactory_Transpiler.cs b/NebulaPatcher/Patches/Transpilers/PlanetFactory_Transpiler.cs index b3e888364..bf9deb52c 100644 --- a/NebulaPatcher/Patches/Transpilers/PlanetFactory_Transpiler.cs +++ b/NebulaPatcher/Patches/Transpilers/PlanetFactory_Transpiler.cs @@ -8,6 +8,7 @@ using HarmonyLib; using NebulaModel.Logger; using NebulaModel.Packets.Factory.Belt; +using NebulaModel.Packets.Factory.Inserter; using NebulaWorld; using UnityEngine; @@ -23,6 +24,33 @@ internal class PlanetFactory_Transpiler public static readonly List CheckPopupPresent = []; public static readonly Dictionary> FaultyVeins = []; + private static void OnNewSetInserterPickTarget(int inserterId, int otherObjId, int offset, int objId, Vector3 pointPos) + { + // 1. Client receives the build request from itself 2. Host sends the build request + if (Multiplayer.IsActive) + { + var factoryManager = Multiplayer.Session.Factories; + if ((Multiplayer.Session.LocalPlayer.IsHost && !factoryManager.IsIncomingRequest.Value) || Multiplayer.Session.LocalPlayer.Id == factoryManager.PacketAuthor) + { + Multiplayer.Session.Network.SendPacketToLocalStar(new NewSetInserterPickTargetPacket(objId, otherObjId, inserterId, + offset, pointPos, GameMain.localPlanet?.id ?? -1)); + } + } + } + + private static void OnNewSetInserterInsertTarget(int inserterId, int otherObjId, int offset, int objId, Vector3 pointPos) + { + if (Multiplayer.IsActive) + { + var factoryManager = Multiplayer.Session.Factories; + if ((Multiplayer.Session.LocalPlayer.IsHost && !factoryManager.IsIncomingRequest.Value) || Multiplayer.Session.LocalPlayer.Id == factoryManager.PacketAuthor) + { + Multiplayer.Session.Network.SendPacketToLocalStar(new NewSetInserterInsertTargetPacket(objId, otherObjId, + inserterId, offset, pointPos, GameMain.localPlanet?.id ?? -1)); + } + } + } + [HarmonyTranspiler] [HarmonyPatch(nameof(PlanetFactory.OnBeltBuilt))] private static IEnumerable OnBeltBuilt_Transpiler(IEnumerable instructions, @@ -30,7 +58,7 @@ private static IEnumerable OnBeltBuilt_Transpiler(IEnumerable OnBeltBuilt_Transpiler(IEnumerable>( - (inserterId, pickTarget, offset, objId, pointPos) => - { - if (Multiplayer.IsActive) - { - Multiplayer.Session.Factories.OnNewSetInserterPickTarget(objId, pickTarget, inserterId, offset, - pointPos); - } - })); + .InsertAndAdvance(new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(PlanetFactory_Transpiler), nameof(OnNewSetInserterPickTarget)))); /* * Calls - * Multiplayer.Session.Factories.OnNewSetInserterInsertTarget(objId, pickTarget, inserterId, offset, pointPos); + * OnNewSetInserterInsertTarget(objId, pickTarget, inserterId, offset, pointPos); * After * this.factorySystem.SetInserterInsertTarget(inserterId, num9, num5 - num10); */ @@ -95,15 +115,7 @@ private static IEnumerable OnBeltBuilt_Transpiler(IEnumerable>( - (inserterId, pickTarget, offset, objId, pointPos) => - { - if (Multiplayer.IsActive) - { - Multiplayer.Session.Factories.OnNewSetInserterInsertTarget(objId, pickTarget, inserterId, offset, - pointPos); - } - })); + .InsertAndAdvance(new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(PlanetFactory_Transpiler), nameof(OnNewSetInserterInsertTarget)))); return codeMatcher.InstructionEnumeration(); } diff --git a/NebulaWorld/Factory/FactoryManager.cs b/NebulaWorld/Factory/FactoryManager.cs index 86aa42159..c82c10a7e 100644 --- a/NebulaWorld/Factory/FactoryManager.cs +++ b/NebulaWorld/Factory/FactoryManager.cs @@ -204,27 +204,6 @@ public int GetNextPrebuildId(PlanetFactory factory) return prebuildRecycleCursor <= 0 ? factory.prebuildCursor + 1 : prebuildRecycle[prebuildRecycleCursor - 1]; } - public void OnNewSetInserterPickTarget(int objId, int otherObjId, int inserterId, int offset, Vector3 pointPos) - { - // 1. Client receives the build request from itself 2. Host sends the build request - if (Multiplayer.IsActive && (Multiplayer.Session.LocalPlayer.Id == PacketAuthor || - Multiplayer.Session.LocalPlayer.IsHost && !IsIncomingRequest.Value)) - { - Multiplayer.Session.Network.SendPacketToLocalStar(new NewSetInserterPickTargetPacket(objId, otherObjId, inserterId, - offset, pointPos, GameMain.localPlanet?.id ?? -1)); - } - } - - public void OnNewSetInserterInsertTarget(int objId, int otherObjId, int inserterId, int offset, Vector3 pointPos) - { - if (Multiplayer.IsActive && (Multiplayer.Session.LocalPlayer.Id == PacketAuthor || - Multiplayer.Session.LocalPlayer.IsHost && !IsIncomingRequest.Value)) - { - Multiplayer.Session.Network.SendPacketToLocalStar(new NewSetInserterInsertTargetPacket(objId, otherObjId, - inserterId, offset, pointPos, GameMain.localPlanet?.id ?? -1)); - } - } - private Locker GetPrebuildRequests(out Dictionary prebuildRequests) { return threadSafe.PrebuildRequests.GetLocked(out prebuildRequests);