-
-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #163 from Baldie-dev/belt-sync
Putting and taking items from belts synchronization
- Loading branch information
Showing
14 changed files
with
282 additions
and
0 deletions.
There are no files selected for viewing
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
25 changes: 25 additions & 0 deletions
25
NebulaClient/PacketProcessors/Factory/Belt/BeltUpdatePickupItemsProcessor.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,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); | ||
} | ||
} | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
NebulaClient/PacketProcessors/Factory/Belt/BeltUpdatePutItemOnProcessor.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,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; | ||
} | ||
} | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
NebulaHost/PacketProcessors/Factory/Belt/BeltUpdatePickupItemsProcessor.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,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); | ||
} | ||
} | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
NebulaHost/PacketProcessors/Factory/Belt/BeltUpdatePutItemOnProcessor.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,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; | ||
} | ||
} | ||
} |
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
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(); | ||
} | ||
} | ||
} |
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,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; | ||
} | ||
} | ||
} |
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
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)); | ||
} | ||
} | ||
} | ||
} |
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,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; | ||
} | ||
} | ||
} |
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,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(); | ||
} | ||
} | ||
} |
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