Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Putting and taking items from belts synchronization #163

Merged
merged 2 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NebulaClient/NebulaClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
<Compile Include="PacketProcessors\Factory\Assembler\AssemblerRecipeEventProcessor.cs" />
<Compile Include="PacketProcessors\Factory\Assembler\AssemblerUpdateProducesProcessor.cs" />
<Compile Include="PacketProcessors\Factory\Assembler\AssemblerUpdateStorageProcessor.cs" />
<Compile Include="PacketProcessors\Factory\Belt\BeltUpdatePickupItemsProcessor.cs" />
<Compile Include="PacketProcessors\Factory\Belt\BeltUpdatePutItemOnProcessor.cs" />
<Compile Include="PacketProcessors\Factory\Entity\CreatePrebuildsRequestProcessor.cs" />
<Compile Include="PacketProcessors\Factory\Ejector\EjectorOrbitUpdateProcessor.cs" />
<Compile Include="PacketProcessors\Factory\Ejector\EjectorStorageUpdateProcessor.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using NebulaModel.Attributes;
using NebulaModel.Networking;
using NebulaModel.Packets.Belt;
using NebulaModel.Packets.Processors;

namespace NebulaClient.PacketProcessors.Factory.Belt
{
[RegisterPacketProcessor]
class BeltUpdatePickupItemsProcessor : IPacketProcessor<BeltUpdatePickupItemsPacket>
{
public void ProcessPacket(BeltUpdatePickupItemsPacket packet, NebulaConnection conn)
{
if (GameMain.data.factories[packet.FactoryIndex]?.cargoTraffic != null)
{
//Iterate though belt updates and remove target items
for (int i = 0; i < packet.BeltUpdates.Length; i++)
{
CargoTraffic traffic = GameMain.data.factories[packet.FactoryIndex].cargoTraffic;
CargoPath cargoPath = traffic.GetCargoPath(traffic.beltPool[packet.BeltUpdates[i].BeltId].segPathId);
cargoPath.TryPickItem(packet.BeltUpdates[i].SegId - 4 - 1, 12);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using NebulaModel.Attributes;
using NebulaModel.Networking;
using NebulaModel.Packets.Belt;
using NebulaModel.Packets.Processors;
using NebulaWorld.Factory;

namespace NebulaClient.PacketProcessors.Factory.Belt
{
[RegisterPacketProcessor]
class BeltUpdatePutItemOnProcessor : IPacketProcessor<BeltUpdatePutItemOnPacket>
{
public void ProcessPacket(BeltUpdatePutItemOnPacket packet, NebulaConnection conn)
{
if (GameMain.data.factories[packet.FactoryIndex]?.cargoTraffic != null)
{
FactoryManager.EventFromServer = true;
GameMain.data.factories[packet.FactoryIndex].cargoTraffic.PutItemOnBelt(packet.BeltId, packet.ItemId);
FactoryManager.EventFromServer = false;
}
}
}
}
2 changes: 2 additions & 0 deletions NebulaHost/NebulaHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
<Compile Include="PacketProcessors\Factory\Assembler\AssemblerRecipeEventProcessor.cs" />
<Compile Include="PacketProcessors\Factory\Assembler\AssemblerUpdateProducesProcessor.cs" />
<Compile Include="PacketProcessors\Factory\Assembler\AssemblerUpdateStorageProcessor.cs" />
<Compile Include="PacketProcessors\Factory\Belt\BeltUpdatePickupItemsProcessor.cs" />
<Compile Include="PacketProcessors\Factory\Belt\BeltUpdatePutItemOnProcessor.cs" />
<Compile Include="PacketProcessors\Factory\Entity\CreatePrebuildsRequestProcessor.cs" />
<Compile Include="PacketProcessors\Factory\Ejector\EjectorOrbitUpdateProcessor.cs" />
<Compile Include="PacketProcessors\Factory\Ejector\EjectorStorageUpdateProcessor.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using NebulaModel.Attributes;
using NebulaModel.Networking;
using NebulaModel.Packets.Belt;
using NebulaModel.Packets.Processors;

namespace NebulaHost.PacketProcessors.Factory.Belt
{
[RegisterPacketProcessor]
class BeltUpdatePickupItemsProcessor : IPacketProcessor<BeltUpdatePickupItemsPacket>
{
public void ProcessPacket(BeltUpdatePickupItemsPacket packet, NebulaConnection conn)
{
//Iterate though belt updates and remove target items
for (int i = 0; i < packet.BeltUpdates.Length; i++)
{
CargoTraffic traffic = GameMain.data.factories[packet.FactoryIndex].cargoTraffic;
CargoPath cargoPath = traffic.GetCargoPath(traffic.beltPool[packet.BeltUpdates[i].BeltId].segPathId);
cargoPath.TryPickItem(packet.BeltUpdates[i].SegId - 4 - 1, 12);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using NebulaModel.Attributes;
using NebulaModel.Networking;
using NebulaModel.Packets.Belt;
using NebulaModel.Packets.Processors;
using NebulaWorld.Factory;

namespace NebulaHost.PacketProcessors.Factory.Belt
{
[RegisterPacketProcessor]
class BeltUpdatePutItemOnProcessor : IPacketProcessor<BeltUpdatePutItemOnPacket>
{
public void ProcessPacket(BeltUpdatePutItemOnPacket packet, NebulaConnection conn)
{
FactoryManager.EventFromClient = true;
GameMain.data.factories[packet.FactoryIndex].cargoTraffic.PutItemOnBelt(packet.BeltId, packet.ItemId);
FactoryManager.EventFromClient = true;
}
}
}
2 changes: 2 additions & 0 deletions NebulaModel/NebulaModel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
<Compile Include="Networking\BinaryUtils.cs" />
<Compile Include="Networking\DelayedPacket.cs" />
<Compile Include="Networking\NebulaConnection.cs" />
<Compile Include="Packets\Belt\BeltUpdatePickupItemsPacket.cs" />
<Compile Include="Packets\Belt\BeltUpdatePutItemOnPacket.cs" />
<Compile Include="Packets\Factory\Assembler\AssemblerRecipeEventPacket.cs" />
<Compile Include="Packets\Factory\Assembler\AssemblerUpdateProducesPacket.cs" />
<Compile Include="Packets\Factory\Assembler\AssemblerUpdateStoragePacket.cs" />
Expand Down
51 changes: 51 additions & 0 deletions NebulaModel/Packets/Belt/BeltUpdatePickupItemsPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using NebulaModel.Attributes;
using NebulaModel.Networking.Serialization;

namespace NebulaModel.Packets.Belt
{
public class BeltUpdatePickupItemsPacket
{
public int FactoryIndex { get; set; }
public BeltUpdate[] BeltUpdates { get; set; }

public BeltUpdatePickupItemsPacket() { }

public BeltUpdatePickupItemsPacket(BeltUpdate[] beltUpdates, int factoryIndex)
{
BeltUpdates = beltUpdates;
FactoryIndex = factoryIndex;
}
}

[RegisterNestedType]
public struct BeltUpdate : INetSerializable
{
public int ItemId { get; set; }
public int Count { get; set; }
public int BeltId { get; set; }
public int SegId { get; set; }
public BeltUpdate(int itemId, int count, int beltId, int segId)
{
SegId = segId;
ItemId = itemId;
Count = count;
BeltId = beltId;
}

public void Serialize(NetDataWriter writer)
{
writer.Put(ItemId);
writer.Put(Count);
writer.Put(BeltId);
writer.Put(SegId);
}

public void Deserialize(NetDataReader reader)
{
ItemId = reader.GetInt();
Count = reader.GetInt();
BeltId = reader.GetInt();
SegId = reader.GetInt();
}
}
}
16 changes: 16 additions & 0 deletions NebulaModel/Packets/Belt/BeltUpdatePutItemOnPacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace NebulaModel.Packets.Belt
{
public class BeltUpdatePutItemOnPacket
{
public int BeltId { get; set; }
public int ItemId { get; set; }
public int FactoryIndex { get; set; }
public BeltUpdatePutItemOnPacket() { }
public BeltUpdatePutItemOnPacket(int beltId, int itemId, int factoryIndex)
{
BeltId = beltId;
ItemId = itemId;
FactoryIndex = factoryIndex;
}
}
}
2 changes: 2 additions & 0 deletions NebulaPatcher/NebulaPatcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<Compile Include="Logger\BepInExLogger.cs" />
<Compile Include="MonoBehaviours\NebulaBootstrapper.cs" />
<Compile Include="NebulaPlugin.cs" />
<Compile Include="Patches\Dynamic\CargoTraffic_Patch.cs" />
<Compile Include="Patches\Dynamic\FactoryProductionStat_Patch.cs" />
<Compile Include="Patches\Dynamic\GameHistoryData_Patch.cs" />
<Compile Include="Patches\Dynamic\GameMain_Patch.cs" />
Expand Down Expand Up @@ -81,6 +82,7 @@
<Compile Include="Patches\Dynamic\UITankWindow_Patch.cs" />
<Compile Include="Patches\Dynamic\UIStarmap_Patch.cs" />
<Compile Include="Patches\Dynamic\VFInput_Patch.cs" />
<Compile Include="Patches\Transpilers\CargoTraffic_Patch.cs" />
<Compile Include="Patches\Transpilers\PlanetFactory_Patch.cs" />
<Compile Include="Patches\Transpilers\PlayerAction_Build_Patch.cs" />
<Compile Include="Patches\Transpilers\PlayerAction_Mine_Patch.cs" />
Expand Down
41 changes: 41 additions & 0 deletions NebulaPatcher/Patches/Dynamic/CargoTraffic_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using HarmonyLib;
using NebulaModel.Packets.Belt;
using NebulaWorld;
using NebulaWorld.Factory;

namespace NebulaPatcher.Patches.Dynamic
{
[HarmonyPatch(typeof(CargoTraffic))]
class CargoTraffic_Patch
{
[HarmonyPrefix]
[HarmonyPatch("PickupBeltItems")]
public static void PickupBeltItems_Prefix()
{
if (SimulatedWorld.Initialized)
{
BeltManager.BeltPickupStarted();
}
}

[HarmonyPostfix]
[HarmonyPatch("PickupBeltItems")]
public static void PickupBeltItems_Postfix()
{
if (SimulatedWorld.Initialized)
{
BeltManager.BeltPickupEnded();
}
}

[HarmonyPrefix]
[HarmonyPatch("PutItemOnBelt")]
public static void PutItemOnBelt_Prefix(int beltId, int itemId)
{
if (SimulatedWorld.Initialized && !FactoryManager.EventFromServer && !FactoryManager.EventFromClient)
{
LocalPlayer.SendPacketToLocalStar(new BeltUpdatePutItemOnPacket(beltId, itemId, GameMain.data.localPlanet.factoryIndex));
}
}
}
}
51 changes: 51 additions & 0 deletions NebulaPatcher/Patches/Transpilers/CargoTraffic_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using HarmonyLib;
using NebulaWorld.Factory;
using System.Collections.Generic;
using System.Reflection.Emit;

namespace NebulaPatcher.Patches.Transpiler
{
/* Change this:
if (num3 > 0)
{
UIItemup.Up(itemId, num3);
}
* To this:
if (num3 > 0)
{
BeltManager.RegisterBeltPickupUpdate(itemId, count, beltId, segId);
UIItemup.Up(itemId, num3);
}
*/
[HarmonyPatch(typeof(CargoTraffic))]
class CargoTraffic_Patch
{
[HarmonyTranspiler]
[HarmonyPatch("PickupBeltItems")]
static IEnumerable<CodeInstruction> PickupBeltItems_Transpiler(ILGenerator gen, IEnumerable<CodeInstruction> instructions)
{
var codes = new List<CodeInstruction>(instructions);
for (int i = 0; i < codes.Count; i++)
{
if (codes[i].opcode == OpCodes.Ble &&
codes[i - 1].opcode == OpCodes.Ldc_I4_0 &&
codes[i - 2].opcode == OpCodes.Ldloc_S &&
codes[i - 3].opcode == OpCodes.Stloc_S &&
codes[i - 4].opcode == OpCodes.Callvirt &&
codes[i - 5].opcode == OpCodes.Ldfld)
{
codes.InsertRange(i + 1, new CodeInstruction[] {
new CodeInstruction(OpCodes.Ldloc_S, 4),
new CodeInstruction(OpCodes.Ldloc_S, 5),
new CodeInstruction(OpCodes.Ldarg_2),
new CodeInstruction(OpCodes.Ldloc_3),
new CodeInstruction(OpCodes.Call, AccessTools.Method(typeof(BeltManager), "RegisterBeltPickupUpdate", new System.Type[] { typeof(int), typeof(int), typeof(int), typeof(int)})),
});
break;
}
}
return codes;
}
}
}
26 changes: 26 additions & 0 deletions NebulaWorld/Factory/BeltManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using NebulaModel.Packets.Belt;
using System.Collections.Generic;

namespace NebulaWorld.Factory
{
public static class BeltManager
{
public static List<BeltUpdate> BeltUpdates = new List<BeltUpdate>();
public static void BeltPickupStarted()
{
BeltUpdates.Clear();
}
public static void RegisterBeltPickupUpdate(int itemId, int count, int beltId, int segId)
{
if (SimulatedWorld.Initialized)
{
BeltUpdates.Add(new BeltUpdate(itemId, count, beltId, segId));
}
}
public static void BeltPickupEnded()
{
LocalPlayer.SendPacketToLocalStar(new BeltUpdatePickupItemsPacket(BeltUpdates.ToArray(), GameMain.data.localPlanet.factoryIndex));
BeltUpdates.Clear();
}
}
}
1 change: 1 addition & 0 deletions NebulaWorld/NebulaWorld.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Factory\BeltManager.cs" />
<Compile Include="Factory\FactoryManager.cs" />
<Compile Include="Factory\StorageManager.cs" />
<Compile Include="GameDataHistory\GameDataHistoryManager.cs" />
Expand Down