Skip to content

Commit

Permalink
conversion layer for manual pins coming from other mods
Browse files Browse the repository at this point in the history
  • Loading branch information
nbusseneau committed Jun 8, 2024
1 parent 9f8ba59 commit 83857b1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Add further compatibility for sharing private pins manually added by other mods.

## [0.5.3] - 2024-06-08

### Fixed
Expand Down
1 change: 1 addition & 0 deletions src/Extensions/PinData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace BetterCartographyTable.Extensions;
public static class PinDataExtensions
{
public static bool IsPrivate(this PinData pin) => (pin is SharablePinData sharablePin && sharablePin.IsPrivate) || pin is not SharablePinData;
public static bool IsSharable(this PinData pin) => SharablePinData.IsSharable(pin);
public static void ToggleChecked(this PinData pin)
{
pin.m_checked = !pin.m_checked;
Expand Down
19 changes: 19 additions & 0 deletions src/Model/Managers/MinimapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,23 @@ private static PinData GetClosestPinToMouse()
var radius = instance.m_removeRadius * (instance.m_largeZoom * 2f);
return instance.GetClosestPin(pos, radius);
}

/// <summary>
/// Try to replace given PinData with a new SharablePinData copy, if it is compatible. This internally removes the
/// existing pin and re-adds a new one, so be mindful of the implications.
/// </summary>
public static bool TryConvert(PinData pin, out SharablePinData sharablePin)
{
// safeguard against other mods potentially subclassing PinData, no point in converting if it will mess them up
if (pin.GetType() != typeof(PinData))
{
sharablePin = null;
return false;
}

sharablePin = new(pin);
instance.RemovePin(pin);
instance.AddPin(sharablePin);
return true;
}
}
4 changes: 3 additions & 1 deletion src/Model/PinClickHandlers/PrivatePinClickHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public static void Dispatch(ClickType clickType, PinData pin)
/// </summary>
public static void LeftClickPlusModifier(PinData pin)
{
if (!MapTableManager.IsTableValid || pin is not SharablePinData sharablePin || !sharablePin.IsSharable) return;
if (!MapTableManager.IsTableValid || !pin.IsSharable()) return;
if (pin is not SharablePinData sharablePin && !MinimapManager.TryConvert(pin, out sharablePin)) return;

sharablePin.SharingMode = MapTableManager.CurrentTable.SharingMode;
instance.m_pinUpdateRequired = true;
MapTableManager.CurrentTable.SendPinEvent(sharablePin, PinEvent.Add);
Expand Down
24 changes: 18 additions & 6 deletions src/Model/SharablePinData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
namespace BetterCartographyTable.Model;

/// <summary>
/// Subclass of <c>PinData</c> intended for use as a transparent replacement in
/// <c>Minimap.instance.m_pins</c>. We try to ensure that all pins on the map are actually
/// <c>SharablePinData</c> objects and not <c>PinData</c> objects via a Harmony patch on
/// <c>Minimap.AddPin(...)</c> (though this may not always be the case as other mods may still
/// manually add <c>PinData</c> objects without calling <c>Minimap.AddPin(...)</c>).
/// Subclass of <c>PinData</c> intended for use as a transparent replacement in <c>Minimap.instance.m_pins</c>.
/// We try to ensure that all pins on the map are actually <c>SharablePinData</c> objects and not <c>PinData</c> objects
/// via a Harmony patch on <c>Minimap.AddPin(...)</c>, though this may not always be the case as other mods may still
/// manually add <c>PinData</c> objects without calling <c>Minimap.AddPin(...)</c>.
/// If this happens, <c>MinimapManager.TryConvert(...)</c> may be used to try and convert pins of a compatible type.
/// </summary>
public class SharablePinData : PinData, IEquatable<PinData>
{
Expand All @@ -26,13 +26,25 @@ public class SharablePinData : PinData, IEquatable<PinData>
PinType.Hildir2,
PinType.Hildir3,
];
public static bool IsSharable(PinData pin) => s_sharablePinTypes.Contains(pin.m_type);

public SharingMode SharingMode { get; set; } = SharingMode.Private;
public bool IsSharable => s_sharablePinTypes.Contains(this.m_type);
public bool IsPrivate => this.SharingMode == SharingMode.Private;
public bool IsShared => !this.IsPrivate;
public bool IsPublic => this.SharingMode == SharingMode.Public;
public bool IsGuild => this.SharingMode == SharingMode.Guild;

public SharablePinData() { }
public SharablePinData(PinData pin)
{
this.m_name = pin.m_name;
this.m_type = pin.m_type;
this.m_pos = pin.m_pos;
this.m_ownerID = pin.m_ownerID;
this.m_author = pin.m_author;
this.m_checked = pin.m_checked;
}

public override string ToString() => $"{this.SharingMode} {m_name} {m_pos} {(m_checked ? "⦻" : "⭘")} {m_author} {m_ownerID}L {Enum.GetName(typeof(PinType), m_type)}";

public void SetVisibility(bool isVisible)
Expand Down

0 comments on commit 83857b1

Please sign in to comment.