Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ 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)
{
base.StartClientSide(api);

client = new Client(api);

patcher = new Patcher("WaypointTogetherContinued");
patcher.PatchAll(api);
}

public override void StartServerSide(ICoreServerAPI api)
Expand Down
2 changes: 1 addition & 1 deletion src/ModConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public Config()
}
public Config(Config previousConfig)
{
DeafultSharing = previousConfig.DeafultSharing;
DeafultSharing = previousConfig?.DeafultSharing ?? false;
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/Patcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
23 changes: 11 additions & 12 deletions src/client/ClientNetwork.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using HarmonyLib;
using System;
using System.Linq;
using Vintagestory.API.Client;
using Vintagestory.GameContent;

Expand All @@ -18,19 +19,18 @@ public ClientNetwork(ICoreClientAPI api)

channel = api.Network.RegisterChannel("malin.waypointtogethercontinued");
channel.RegisterMessageType<ShareWaypointPacket>();
channel.RegisterMessageType<ShareWaypointPacketFromServer>();
channel.SetMessageHandler<ShareWaypointPacketFromServer>(this.HandlePacket);
channel.SetMessageHandler<ShareWaypointPacket>(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)
{
Expand All @@ -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<WorldMapManager>().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)
{
Expand All @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions src/network/packets/ShareWaypoint.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ProtoBuf;
using Vintagestory.GameContent;

[ProtoContract]
class ShareWaypointPacket
Expand All @@ -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;
}
}
13 changes: 6 additions & 7 deletions src/patches/DialogPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -39,7 +38,7 @@ public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructio
{
yield return new CodeInstruction(OpCodes.Ldloca_S, 0);
yield return new CodeInstruction(OpCodes.Ldloca_S, 1);
yield return new CodeInstruction(OpCodes.Call, typeof(AddWaypointShareSwitchPatch).GetMethod("AddShareComponent", BindingFlags.Static | BindingFlags.Public));
yield return new CodeInstruction(OpCodes.Call, AddShareComponentMethod);

found = true;
}
Expand Down
35 changes: 26 additions & 9 deletions src/patches/WaypointAdd.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using HarmonyLib;
Expand All @@ -18,7 +19,8 @@ public static void BroadcastWaypoint(ICoreClientAPI capi, string message, GuiDia
if (instance.SingleComposer.GetSwitch(Settings.ShouldShareSwitchName).On)
{
WaypointTogetherContinued.Core mod = capi.ModLoader.GetModSystem<WaypointTogetherContinued.Core>();
mod.client.network.ShareWaypoint(message, capi.World.Player.PlayerUID);
var waypoint = Traverse.Create(instance).Field("waypoint").GetValue<Waypoint>();
mod.client?.network.ShareWaypoint(message, waypoint);
string messageToTheUser = Lang.Get("waypointtogethercontinued:waypoint-shared");
capi.ShowChatMessage(messageToTheUser);
}
Expand All @@ -27,20 +29,35 @@ public static void BroadcastWaypoint(ICoreClientAPI capi, string message, GuiDia
}
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
List<CodeInstruction> 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");
}
}
}
40 changes: 27 additions & 13 deletions src/patches/WaypointEdit.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
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;

[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)
{
if (capi != null)
{
if (instance.SingleComposer.GetSwitch(Settings.ShouldShareSwitchName).On)
{
Core mod = capi.ModLoader.GetModSystem<Core>();
var maplayers = capi.ModLoader.GetModSystem<WorldMapManager>().MapLayers;
var mapLayer = (maplayers.Find(x => x is WaypointMapLayer) as WaypointMapLayer);
WaypointTogetherContinued.Core mod = capi.ModLoader.GetModSystem<WaypointTogetherContinued.Core>();
var waypoint = Traverse.Create(instance).Field("waypoint").GetValue<Waypoint>();
mod.client.network.ShareWaypoint(message, waypoint.Guid);
mod.client?.network.ShareWaypoint(message, waypoint);
string messageToTheUser = Lang.Get("waypointtogethercontinued:waypoint-shared");
capi.ShowChatMessage(messageToTheUser);
}
Expand All @@ -32,21 +31,36 @@ public static void BroadcastWaypoint(ICoreClientAPI capi, string message, GuiDia
}
public static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
List<CodeInstruction> 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");
}
}
}
}
36 changes: 1 addition & 35 deletions src/server/ServerNetwork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,13 @@ public ServerNetwork(ICoreServerAPI api)
{
channel = api.Network.RegisterChannel("malin.waypointtogethercontinued");
channel.RegisterMessageType<ShareWaypointPacket>();
channel.RegisterMessageType<ShareWaypointPacketFromServer>();
channel.SetMessageHandler<ShareWaypointPacket>(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<WorldMapManager>().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);
}
}