Skip to content

Commit

Permalink
Refactor OnNewSetInserterPickTarget & OnNewSetInserterInsertTarget
Browse files Browse the repository at this point in the history
  • Loading branch information
starfi5h committed Dec 22, 2023
1 parent 441e007 commit 6a6016e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 45 deletions.
4 changes: 0 additions & 4 deletions NebulaAPI/GameState/IFactoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
52 changes: 32 additions & 20 deletions NebulaPatcher/Patches/Transpilers/PlanetFactory_Transpiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using HarmonyLib;
using NebulaModel.Logger;
using NebulaModel.Packets.Factory.Belt;
using NebulaModel.Packets.Factory.Inserter;
using NebulaWorld;
using UnityEngine;

Expand All @@ -23,14 +24,41 @@ internal class PlanetFactory_Transpiler
public static readonly List<int> CheckPopupPresent = [];
public static readonly Dictionary<int, List<int>> 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<CodeInstruction> OnBeltBuilt_Transpiler(IEnumerable<CodeInstruction> instructions,
ILGenerator iLGenerator)
{
/*
* Calls
* Multiplayer.Session.Factories.OnNewSetInserterPickTarget(objId, pickTarget, inserterId, offset, pointPos);
* OnNewSetInserterPickTarget(objId, pickTarget, inserterId, offset, pointPos);
* After
* this.factorySystem.SetInserterPickTarget(inserterId, num6, num5 - num7);
*/
Expand All @@ -57,19 +85,11 @@ private static IEnumerable<CodeInstruction> OnBeltBuilt_Transpiler(IEnumerable<C
.InsertAndAdvance(setInserterTargetInsts.ToArray())
.InsertAndAdvance(objIdInst)
.InsertAndAdvance(pointPosInsts.ToArray())
.InsertAndAdvance(HarmonyLib.Transpilers.EmitDelegate<Action<int, int, int, int, Vector3>>(
(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);
*/
Expand All @@ -95,15 +115,7 @@ private static IEnumerable<CodeInstruction> OnBeltBuilt_Transpiler(IEnumerable<C
.InsertAndAdvance(setInserterTargetInsts.ToArray())
.InsertAndAdvance(objIdInst)
.InsertAndAdvance(pointPosInsts.ToArray())
.InsertAndAdvance(HarmonyLib.Transpilers.EmitDelegate<Action<int, int, int, int, Vector3>>(
(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();
}
Expand Down
21 changes: 0 additions & 21 deletions NebulaWorld/Factory/FactoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PrebuildOwnerKey, ushort> prebuildRequests)
{
return threadSafe.PrebuildRequests.GetLocked(out prebuildRequests);
Expand Down

0 comments on commit 6a6016e

Please sign in to comment.