From eeee58a6bb7ec2dc4e70d71aa4e504ad60e45922 Mon Sep 17 00:00:00 2001 From: thakyZ <950594+thakyZ@users.noreply.github.com> Date: Sun, 16 Feb 2025 12:20:19 -0800 Subject: [PATCH] update: to 1.20.3 --- src/Core.cs | 6 ++--- src/ModConfig.cs | 2 +- src/Patcher.cs | 4 ++- src/client/ClientNetwork.cs | 23 ++++++++-------- src/network/packets/ShareWaypoint.cs | 9 ++++--- src/patches/DialogPatch.cs | 13 +++++---- src/patches/WaypointAdd.cs | 35 +++++++++++++++++------- src/patches/WaypointEdit.cs | 40 +++++++++++++++++++--------- src/server/ServerNetwork.cs | 36 +------------------------ 9 files changed, 83 insertions(+), 85 deletions(-) diff --git a/src/Core.cs b/src/Core.cs index b2603a5..b1d01d9 100644 --- a/src/Core.cs +++ b/src/Core.cs @@ -17,9 +17,6 @@ public override void Start(ICoreAPI api) { base.Start(api); ModConfig.ReadConfig(api); - - patcher = new Patcher("WaypointTogetherContinued"); - patcher.PatchAll(api); } public override void StartClientSide(ICoreClientAPI api) @@ -27,6 +24,9 @@ public override void StartClientSide(ICoreClientAPI api) base.StartClientSide(api); client = new Client(api); + + patcher = new Patcher("WaypointTogetherContinued"); + patcher.PatchAll(api); } public override void StartServerSide(ICoreServerAPI api) diff --git a/src/ModConfig.cs b/src/ModConfig.cs index 79f34f9..31bfe5d 100644 --- a/src/ModConfig.cs +++ b/src/ModConfig.cs @@ -11,7 +11,7 @@ public Config() } public Config(Config previousConfig) { - DeafultSharing = previousConfig.DeafultSharing; + DeafultSharing = previousConfig?.DeafultSharing ?? false; } } diff --git a/src/Patcher.cs b/src/Patcher.cs index 4407f16..e2463c6 100644 --- a/src/Patcher.cs +++ b/src/Patcher.cs @@ -20,7 +20,9 @@ public void PatchAll(ICoreAPI api) catch (Exception e) { api.Logger.Error(e.ToString()); - api.Logger.Error(e.InnerException.ToString()); + if (e.InnerException != null) { + api.Logger.Error(e.InnerException.ToString()); + } throw; } } diff --git a/src/client/ClientNetwork.cs b/src/client/ClientNetwork.cs index 02e4829..70a5cd9 100644 --- a/src/client/ClientNetwork.cs +++ b/src/client/ClientNetwork.cs @@ -1,5 +1,6 @@ using HarmonyLib; using System; +using System.Linq; using Vintagestory.API.Client; using Vintagestory.GameContent; @@ -18,19 +19,18 @@ public ClientNetwork(ICoreClientAPI api) channel = api.Network.RegisterChannel("malin.waypointtogethercontinued"); channel.RegisterMessageType(); - channel.RegisterMessageType(); - channel.SetMessageHandler(this.HandlePacket); + channel.SetMessageHandler(this.HandlePacket); } - public void ShareWaypoint(string message, string byUser) + public void ShareWaypoint(string message, Waypoint wayPoint) { - if (message != null && message != "") + if (!string.IsNullOrEmpty(message)) { - channel.SendPacket(new ShareWaypointPacket(message, byUser)); + channel.SendPacket(new ShareWaypointPacket(message, wayPoint)); } } - private void HandlePacket(ShareWaypointPacketFromServer packet) + private void HandlePacket(ShareWaypointPacket packet) { if (lastMessage == packet.Message) { @@ -46,11 +46,11 @@ private void HandlePacket(ShareWaypointPacketFromServer packet) string color = split[3]; string icon = split[4]; string pinned = split[5]; - string name = split[6]; + string name = string.Join(' ', split.Skip(6)); var maplayers = api.ModLoader.GetModSystem().MapLayers; var waypointLayer = (maplayers.Find(x => x is WaypointMapLayer) as WaypointMapLayer); - Waypoint existing = packet.ExistingWaypoint; + Waypoint existing = packet.Waypoint; int myExistingId = -1; if (waypointLayer != null && waypointLayer.ownWaypoints != null) { @@ -64,10 +64,9 @@ private void HandlePacket(ShareWaypointPacketFromServer packet) } else { - int worldLen = api.World.Config.GetAsInt("worldLength") / 2; - double x = existing.Position.X - worldLen; - double y = existing.Position.Y - worldLen; - double z = existing.Position.Z - worldLen; + double x = existing.Position.X - (api.World.BlockAccessor.MapSizeX / 2); + double y = existing.Position.Y - api.World.MapSizeY; + double z = existing.Position.Z - (api.World.BlockAccessor.MapSizeZ / 2); // we want /waypoint addati [icon] [x] [y] [z] [pinned] [color] [title] string message = $"/waypoint addati {icon} {x} {y} {z} {pinned} {color} {name}"; api.SendChatMessage(message); diff --git a/src/network/packets/ShareWaypoint.cs b/src/network/packets/ShareWaypoint.cs index f361a1f..6a24f2d 100644 --- a/src/network/packets/ShareWaypoint.cs +++ b/src/network/packets/ShareWaypoint.cs @@ -1,4 +1,5 @@ using ProtoBuf; +using Vintagestory.GameContent; [ProtoContract] class ShareWaypointPacket @@ -7,17 +8,17 @@ class ShareWaypointPacket public string Message { get; set; } [ProtoMember(2)] - public string WaypointGuid { get; set; } + public Waypoint Waypoint { get; set; } public ShareWaypointPacket() { Message = ""; - WaypointGuid = ""; + Waypoint = null; } - public ShareWaypointPacket(string message, string waypointGuid) + public ShareWaypointPacket(string message, Waypoint waypointGuid) { Message = message; - WaypointGuid = waypointGuid; + Waypoint = waypointGuid; } } diff --git a/src/patches/DialogPatch.cs b/src/patches/DialogPatch.cs index 3ae87c5..623aafe 100644 --- a/src/patches/DialogPatch.cs +++ b/src/patches/DialogPatch.cs @@ -8,22 +8,21 @@ using Vintagestory.API.Client; using Vintagestory.API.Config; using Vintagestory.GameContent; - using GuiComposerHelpers = Vintagestory.API.Client.GuiComposerHelpers; public static class WaypointShareSwitchPatch { - static void OnShareSwitch(bool on) { } + public static readonly MethodInfo AddShareComponentMethod = AccessTools.Method(typeof(WaypointShareSwitchPatch), nameof(AddShareComponent)); public static GuiComposer AddShareComponent(GuiComposer composer, ref ElementBounds textBounds, ref ElementBounds toggleBounds) { - if (GuiComposerHelpers.GetSwitch(composer, Settings.ShouldShareSwitchName) == null) + if (composer.GetSwitch(Settings.ShouldShareSwitchName) == null) { string shareString = Lang.Get("waypointtogethercontinued:share"); composer = composer.AddStaticText(shareString, CairoFont.WhiteSmallText(), textBounds = textBounds.BelowCopy(0, 9, 0, 0)); - GuiComposer c = GuiComposerHelpers.AddSwitch(composer, OnShareSwitch, toggleBounds = toggleBounds.BelowCopy(0, 5, 0, 0).WithFixedWidth(200), Settings.ShouldShareSwitchName); - var sw = GuiComposerHelpers.GetSwitch(composer, Settings.ShouldShareSwitchName); - sw.On = ModConfig.ClientConfig.DeafultSharing; + GuiComposer c = composer.AddSwitch((bool _) => { }, toggleBounds = toggleBounds.BelowCopy(0, 5, 0, 0).WithFixedWidth(200), Settings.ShouldShareSwitchName); + var sw = composer.GetSwitch(Settings.ShouldShareSwitchName); + sw.On = ModConfig.ClientConfig?.DeafultSharing ?? false; return c; } @@ -39,7 +38,7 @@ public static IEnumerable Transpiler(IEnumerable(); - mod.client.network.ShareWaypoint(message, capi.World.Player.PlayerUID); + var waypoint = Traverse.Create(instance).Field("waypoint").GetValue(); + mod.client?.network.ShareWaypoint(message, waypoint); string messageToTheUser = Lang.Get("waypointtogethercontinued:waypoint-shared"); capi.ShowChatMessage(messageToTheUser); } @@ -27,20 +29,35 @@ public static void BroadcastWaypoint(ICoreClientAPI capi, string message, GuiDia } public static IEnumerable Transpiler(IEnumerable instructions) { - List list = new(); - foreach (var instruction in instructions) + var cloned = instructions.ToList(); + var found = 0; + for (int i = 0; i < cloned.Count; i++) { - if (instruction.opcode == OpCodes.Callvirt && (MethodInfo)instruction.operand == Settings.SendChatMessageMethod) + var instruction = cloned[i]; + CodeInstruction nextInstruction = null; + if (i < cloned.Count - 1) { + nextInstruction = cloned[i + 1]; + } + if (found == 0 && instruction.opcode == OpCodes.Ldnull && nextInstruction?.opcode == OpCodes.Callvirt && nextInstruction.operand is MethodInfo method && method == Settings.SendChatMessageMethod) + { + found = 1; + } + else if (found == 1) { - list.RemoveAt(list.Count - 1); // remove 'ldnull' - list.Add(new CodeInstruction(OpCodes.Ldarg_0)); // load 'this' - list.Add(new CodeInstruction(OpCodes.Call, toReplaceWith)); + // ORIGINAL: list.RemoveAt(list.Count - 1); // remove 'ldnull' + yield return new CodeInstruction(OpCodes.Ldarg_0); // load 'this' + yield return new CodeInstruction(OpCodes.Call, toReplaceWith); + found = 2; } else { - list.Add(instruction); + yield return instruction; } } - return list; + + if (found != 2) + { + throw new ArgumentException("Cannot find `ldnull` before `callvirt` in GuiDialogAddWayPoint.onSave"); + } } } diff --git a/src/patches/WaypointEdit.cs b/src/patches/WaypointEdit.cs index 521f10e..2c2a0a3 100644 --- a/src/patches/WaypointEdit.cs +++ b/src/patches/WaypointEdit.cs @@ -1,9 +1,11 @@ namespace WaypointTogetherContinued { using HarmonyLib; + using System; using System.Collections.Generic; using System.Reflection.Emit; using System.Reflection; + using System.Linq; using Vintagestory.GameContent; using Vintagestory.API.Client; using Vintagestory.API.Config; @@ -11,7 +13,6 @@ [HarmonyPatch(typeof(GuiDialogEditWayPoint), "onSave")] class Patch_GuiDialogEditWayPoint_onSave { - public static readonly MethodInfo toReplaceWith = AccessTools.Method(typeof(Patch_GuiDialogEditWayPoint_onSave), nameof(BroadcastWaypoint)); public static void BroadcastWaypoint(ICoreClientAPI capi, string message, GuiDialogEditWayPoint instance) { @@ -19,11 +20,9 @@ public static void BroadcastWaypoint(ICoreClientAPI capi, string message, GuiDia { if (instance.SingleComposer.GetSwitch(Settings.ShouldShareSwitchName).On) { - Core mod = capi.ModLoader.GetModSystem(); - var maplayers = capi.ModLoader.GetModSystem().MapLayers; - var mapLayer = (maplayers.Find(x => x is WaypointMapLayer) as WaypointMapLayer); + WaypointTogetherContinued.Core mod = capi.ModLoader.GetModSystem(); var waypoint = Traverse.Create(instance).Field("waypoint").GetValue(); - mod.client.network.ShareWaypoint(message, waypoint.Guid); + mod.client?.network.ShareWaypoint(message, waypoint); string messageToTheUser = Lang.Get("waypointtogethercontinued:waypoint-shared"); capi.ShowChatMessage(messageToTheUser); } @@ -32,21 +31,36 @@ public static void BroadcastWaypoint(ICoreClientAPI capi, string message, GuiDia } public static IEnumerable Transpiler(IEnumerable instructions) { - List list = new(); - foreach (var instruction in instructions) + var cloned = instructions.ToList(); + var found = 0; + for (int i = 0; i < cloned.Count; i++) { - if (instruction.opcode == OpCodes.Callvirt && (MethodInfo)instruction.operand == Settings.SendChatMessageMethod) + var instruction = cloned[i]; + CodeInstruction nextInstruction = null; + if (i < cloned.Count - 1) { + nextInstruction = cloned[i + 1]; + } + if (found == 0 && instruction.opcode == OpCodes.Ldnull && nextInstruction?.opcode == OpCodes.Callvirt && nextInstruction.operand is MethodInfo method && method == Settings.SendChatMessageMethod) + { + found = 1; + } + else if (found == 1) { - list.RemoveAt(list.Count - 1); // remove 'ldnull' - list.Add(new CodeInstruction(OpCodes.Ldarg_0)); // load 'this' - list.Add(new CodeInstruction(OpCodes.Call, toReplaceWith)); + // ORIGINAL: list.RemoveAt(list.Count - 1); // remove 'ldnull' + yield return new CodeInstruction(OpCodes.Ldarg_0); // load 'this' + yield return new CodeInstruction(OpCodes.Call, toReplaceWith); + found = 2; } else { - list.Add(instruction); + yield return instruction; } } - return list; + + if (found != 2) + { + throw new ArgumentException("Cannot find `ldnull` before `callvirt` in GuiDialogEditWayPoint.onSave"); + } } } } diff --git a/src/server/ServerNetwork.cs b/src/server/ServerNetwork.cs index b090440..6257a79 100644 --- a/src/server/ServerNetwork.cs +++ b/src/server/ServerNetwork.cs @@ -18,47 +18,13 @@ public ServerNetwork(ICoreServerAPI api) { channel = api.Network.RegisterChannel("malin.waypointtogethercontinued"); channel.RegisterMessageType(); - channel.RegisterMessageType(); channel.SetMessageHandler(this.HandlePacket); this.api = api; } - public void ShareWaypoint(string message, string byPlayer) - { - if (message != null && message != "") - { - channel.SendPacket(new ShareWaypointPacket(message, byPlayer)); - } - } - private void HandlePacket(IServerPlayer player, ShareWaypointPacket packet) { - var maplayers = api.ModLoader.GetModSystem().MapLayers; - var waypointLayer = (maplayers.Find(x => x is WaypointMapLayer) as WaypointMapLayer); - Waypoint existing = waypointLayer.Waypoints.Find(x => x.Guid == packet.WaypointGuid); - var newPacket = new ShareWaypointPacketFromServer(packet.Message, existing); - channel.BroadcastPacket(newPacket, player); - } -} - -[ProtoContract] -public class ShareWaypointPacketFromServer -{ - [ProtoMember(1)] - public string Message { get; set; } - - [ProtoMember(2)] - public Waypoint ExistingWaypoint { get; set; } - - public ShareWaypointPacketFromServer() - { - Message = ""; - } - - public ShareWaypointPacketFromServer(string message, Waypoint existingWaypoint) - { - Message = message; - ExistingWaypoint = existingWaypoint; + channel.BroadcastPacket(packet, player); } }