Skip to content

Commit

Permalink
Sync UIReferenceSpeedTip
Browse files Browse the repository at this point in the history
  • Loading branch information
starfi5h committed Dec 8, 2024
1 parent d8fb4b5 commit 97fbe79
Show file tree
Hide file tree
Showing 5 changed files with 489 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace NebulaModel.Packets.Statistics;

public class StatisticsReferenceSpeedTipPacket
{
public StatisticsReferenceSpeedTipPacket() { }

public StatisticsReferenceSpeedTipPacket(int itemId, int astroFilter, int itemCycle, int productionProtoId, byte[] binaryData)
{
ItemId = itemId;
AstroFilter = astroFilter;
ItemCycle = itemCycle;
ProductionProtoId = productionProtoId;
BinaryData = binaryData;
}

public int ItemId { get; set; }
public int AstroFilter { get; set; }
public int ItemCycle { get; set; }
public int ProductionProtoId { get; set; }
public byte[] BinaryData { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#region

using System;
using System.IO;
using NebulaAPI.Packets;
using NebulaModel.Logger;
using NebulaModel.Networking;
using NebulaModel.Packets;
using NebulaModel.Packets.Statistics;
using NebulaWorld;

#endregion

namespace NebulaNetwork.PacketProcessors.Statistics;

[RegisterPacketProcessor]
internal class StatisticsReferenceSpeedTipProcessor : PacketProcessor<StatisticsReferenceSpeedTipPacket>
{
protected override void ProcessPacket(StatisticsReferenceSpeedTipPacket packet, NebulaConnection conn)
{
if (IsHost)
{
try
{
using var writer = new BinaryUtils.Writer();
Multiplayer.Session.Statistics.GetReferenceSpeedTip(writer.BinaryWriter, packet.ItemId, packet.AstroFilter, packet.ItemCycle, packet.ProductionProtoId);
packet.BinaryData = writer.CloseAndGetBytes();
conn.SendPacket(packet);
}
catch (Exception ex)
{
Log.Warn("StatisticsReferenceSpeedTipPacket request error!");
Log.Warn(ex);
}
}
if (IsClient)
{
try
{
using var reader = new BinaryUtils.Reader(packet.BinaryData);
Multiplayer.Session.Statistics.SetReferenceSpeedTip(reader.BinaryReader, packet.ItemId, packet.AstroFilter, packet.ItemCycle, packet.ProductionProtoId);
}
catch (Exception ex)
{
Log.Warn("StatisticsReferenceSpeedTipPacket response error!");
Log.Warn(ex);
}
}
}
}
47 changes: 47 additions & 0 deletions NebulaPatcher/Patches/Dynamic/UIReferenceSpeedTip_Patch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#region

using System;
using HarmonyLib;
using NebulaModel.Packets.Statistics;
using NebulaWorld;
#pragma warning disable IDE0301 // Simplify collection initialization

#endregion

namespace NebulaPatcher.Patches.Dynamic;

[HarmonyPatch(typeof(UIReferenceSpeedTip))]
internal class UIReferenceSpeedTip_Patch
{
[HarmonyPrefix]
[HarmonyPatch(nameof(UIReferenceSpeedTip.AddEntryDataWithFactory))]
public static bool AddEntryDataWithFactory_Prefix()
{
// Client will use server response to update loadedEntryDatas and loadedSubTipDatas
return !Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost;
}

[HarmonyPrefix]
[HarmonyPatch(nameof(UIReferenceSpeedTip.SetTip))]
public static void SetTip_Prefix(UIReferenceSpeedTip __instance, int _itemId, int _astroFilter, UIReferenceSpeedTip.EItemCycle _itemCycle)
{
if (!Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost) return;

// Client: Send request to server when setting a new tip
if (__instance.itemId == _itemId && __instance.astroFilter == _astroFilter && __instance.itemCycle == _itemCycle) return;
Multiplayer.Session.Network.SendPacket(new StatisticsReferenceSpeedTipPacket(
_itemId, _astroFilter, (int)_itemCycle, 0, Array.Empty<byte>()));
}

[HarmonyPrefix]
[HarmonyPatch(nameof(UIReferenceSpeedTip.SetSubTip))]
public static void SetSubTip_Prefix(UIReferenceSpeedTip __instance, int _productionProtoId)
{
if (!Multiplayer.IsActive || Multiplayer.Session.LocalPlayer.IsHost) return;

// Client: Send request to server when setting a valid subtip
if (_productionProtoId == 0) return;
Multiplayer.Session.Network.SendPacket(new StatisticsReferenceSpeedTipPacket(
__instance.itemId, __instance.astroFilter, (int)__instance.itemCycle, _productionProtoId, Array.Empty<byte>()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#region

using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using HarmonyLib;
using NebulaModel.Logger;

#endregion

namespace NebulaPatcher.Patches.Transpilers;

[HarmonyPatch(typeof(UIReferenceSpeedTip))]
public static class UIReferenceSpeedTip_Transpiler
{
[HarmonyTranspiler]
[HarmonyPatch(nameof(UIReferenceSpeedTip.RefreshSubEntries))]
private static IEnumerable<CodeInstruction> RefreshSubEntries_Transpiler(IEnumerable<CodeInstruction> instructions)
{
//Remove planetData.factory != null condiction check
//Change: if (planetData != null && planetData.factory != null && this.loadedSubTipDatas[planetData.astroId].astroId == planetData.astroId)
//To: if (planetData != null && this.loadedSubTipDatas[planetData.astroId].astroId == planetData.astroId)
var codeInstructions = instructions as CodeInstruction[] ?? instructions.ToArray();
try
{
return new CodeMatcher(instructions)
.MatchForward(true,
new CodeMatch(OpCodes.Ldfld,
AccessTools.Field(typeof(PlanetData), nameof(PlanetData.factory))),
new CodeMatch(OpCodes.Brfalse)
)
.Repeat(matcher => matcher
.SetAndAdvance(OpCodes.Pop, null)
)
.InstructionEnumeration();
}
catch
{
Log.Error("Transpiler UIReferenceSpeedTip.RefreshSubEntries failed. Reference speed tip may not work properly.");
return codeInstructions;
}
}
}
Loading

0 comments on commit 97fbe79

Please sign in to comment.