Skip to content

Commit

Permalink
Merge pull request NebulaModTeam#719 from starfi5h/pr-tip
Browse files Browse the repository at this point in the history
Sync UIReferenceSpeedTip and bump to v0.9.12
  • Loading branch information
starfi5h authored Dec 9, 2024
2 parents d8fb4b5 + bb31f4c commit 1da704e
Show file tree
Hide file tree
Showing 8 changed files with 502 additions and 5 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## Changelog

0.9.12:
- Compatible with game version 0.10.31.24697
- @starfi5h: Client can now use metadata to unlock tech. Sandbox unlock by client is now synced
- @starfi5h: Remove metadata requirement for blueprint tech due to client can't get metadata in MP
- @starfi5h: The right part of logistics control panel (I) is available for loaded planet
- @starfi5h: Sync Reference rate, Import/Export storage, Storage amount in production statistics panel

0.9.11:
- @starfi5h: Fix half-growth dark fog bases keep regenerating
- @starfi5h: Fix combat drones doesn't increase ground base threat
Expand Down
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 1da704e

Please sign in to comment.