Skip to content

Commit

Permalink
Sync UIBeltWindow.OnReverseButtonClick
Browse files Browse the repository at this point in the history
  • Loading branch information
starfi5h committed Feb 1, 2023
1 parent a0f232b commit 439d302
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
15 changes: 15 additions & 0 deletions NebulaModel/Packets/Factory/Belt/BeltReversePacket.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace NebulaModel.Packets.Factory.Belt
{
public class BeltReversePacket
{
public int BeltId { get; set; }
public int PlanetId { get; set; }

public BeltReversePacket() { }
public BeltReversePacket(int beltId, int planetId)
{
BeltId = beltId;
PlanetId = planetId;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using NebulaAPI;
using NebulaModel.Packets.Factory.Belt;

namespace NebulaNetwork.PacketProcessors.Factory.Belt
{
[RegisterPacketProcessor]
internal class BeltReverseProcessor : BasePacketProcessor<BeltReversePacket>
{
public override void ProcessPacket(BeltReversePacket packet, INebulaConnection conn)
{
PlanetFactory factory = GameMain.galaxy.PlanetById(packet.PlanetId).factory;
if (factory == null)
return;

using (NebulaModAPI.MultiplayerSession.Factories.IsIncomingRequest.On())
{
NebulaModAPI.MultiplayerSession.Factories.EventFactory = factory;
NebulaModAPI.MultiplayerSession.Factories.TargetPlanet = packet.PlanetId;
if (NebulaModAPI.MultiplayerSession.LocalPlayer.IsHost)
{
// Load planet model
NebulaModAPI.MultiplayerSession.Factories.AddPlanetTimer(packet.PlanetId);
}

UIBeltWindow beltWindow = UIRoot.instance.uiGame.beltWindow;
beltWindow._Close(); // close the window first to avoid changing unwant variable when setting beltId
PlanetFactory tmpFactory = beltWindow.factory;
int tmpBeltId = beltWindow.beltId;
beltWindow.factory = factory;
beltWindow.beltId = packet.BeltId;
beltWindow.OnReverseButtonClick(0);
beltWindow.factory = tmpFactory;
beltWindow.beltId = tmpBeltId;

NebulaModAPI.MultiplayerSession.Factories.EventFactory = null;
NebulaModAPI.MultiplayerSession.Factories.TargetPlanet = NebulaModAPI.PLANET_NONE;
}
}
}
}
21 changes: 21 additions & 0 deletions NebulaPatcher/Patches/Dynamic/UIBeltWindow_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using HarmonyLib;
using NebulaModel.Packets.Factory.Belt;
using NebulaWorld;

namespace NebulaPatcher.Patches.Dynamic
{
[HarmonyPatch(typeof(UIBeltWindow))]
internal class UIBeltWindow_Patch
{
[HarmonyPostfix]
[HarmonyPatch(typeof(UIBeltWindow), nameof(UIBeltWindow.OnReverseButtonClick))]
public static void OnReverseButtonClick_Postfix(UIBeltWindow __instance)
{
// Notify others about belt direction reverse
if (Multiplayer.IsActive && !Multiplayer.Session.Factories.IsIncomingRequest.Value)
{
Multiplayer.Session.Network.SendPacketToLocalStar(new BeltReversePacket(__instance.beltId, __instance.factory.planetId));
}
}
}
}

0 comments on commit 439d302

Please sign in to comment.