-
-
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.
- Loading branch information
Showing
5 changed files
with
489 additions
and
0 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
NebulaModel/Packets/Statistics/StatisticsReferenceSpeedTipPacket.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,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; } | ||
} |
50 changes: 50 additions & 0 deletions
50
NebulaNetwork/PacketProcessors/Statistics/StatisticsReferenceSpeedTipProcessor.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,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
47
NebulaPatcher/Patches/Dynamic/UIReferenceSpeedTip_Patch.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,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>())); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
NebulaPatcher/Patches/Transpilers/UIReferenceSpeedTip_Transpiler.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,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; | ||
} | ||
} | ||
} |
Oops, something went wrong.