Skip to content

Commit

Permalink
Fix markers for added/removed ships
Browse files Browse the repository at this point in the history
  • Loading branch information
schlangguru committed Aug 14, 2021
1 parent ee570fd commit ea40a01
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 23 deletions.
12 changes: 8 additions & 4 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ public class Main : BaseUnityPlugin

private Harmony _harmony = null;

public static List<string> shipTypes = new List<string>() {"Raft", "Karve", "Longship"};
public static Dictionary<ZDOID, ShipMarkerData> _shipMarkers = new Dictionary<ZDOID, ShipMarkerData>();
public static List<ShipType> ShipTypes = new List<ShipType>() {
new ShipType("Raft", "$ship_raft"),
new ShipType("Karve", "$ship_karve"),
new ShipType("VikingShip", "$ship_longship")
};
public static Dictionary<ZDOID, ShipMarkerData> ShipMarkers = new Dictionary<ZDOID, ShipMarkerData>();


void Awake()
Expand All @@ -29,14 +33,14 @@ void Awake()
private void OnDestroy()
{
this._harmony?.UnpatchSelf();
foreach (ShipMarkerData data in _shipMarkers.Values)
foreach (ShipMarkerData data in ShipMarkers.Values)
{
if (data.Marker != null)
{
UnityEngine.Object.Destroy(data.Marker);
}
}
_shipMarkers.Clear();
ShipMarkers.Clear();
}
}
}
6 changes: 3 additions & 3 deletions MiniMap_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ class Minimap_Patch
[HarmonyPostfix]
private static void UpdatePins(float ___m_largeZoom)
{
if (Main._shipMarkers.Count > 0)
if (Main.ShipMarkers.Count > 0)
{
RawImage rawImage = Minimap.instance.m_largeRoot.activeSelf ? Minimap.instance.m_mapImageLarge : Minimap.instance.m_mapImageSmall;
float markerSize = Minimap.instance.m_largeRoot.activeSelf ? Minimap.instance.m_pinSizeLarge : Minimap.instance.m_pinSizeSmall;
RectTransform rectTransform = Minimap.instance.m_largeRoot.activeSelf ? Minimap.instance.m_pinRootLarge : Minimap.instance.m_pinRootSmall;
foreach (ShipMarkerData data in Main._shipMarkers.Values)
foreach (ShipMarkerData data in Main.ShipMarkers.Values)
{
Vector3 shipPos = data.ZDO.GetPosition();
if (IsPointVisible(shipPos, rawImage) && !IsShipControlledByPlayer(data.ZDO))
Expand Down Expand Up @@ -81,7 +81,7 @@ private static void DrawShipMarker(ShipMarkerData data, float size, RectTransfor
if (Minimap.instance.m_largeRoot.activeSelf && largeMapZoom < Minimap.instance.m_showNamesZoom)
{
text.gameObject.SetActive(true);
text.text = Localization.instance.Localize("$ship_" + data.Type.ToLower());
text.text = Localization.instance.Localize(data.Type.Name);
}
else
{
Expand Down
4 changes: 1 addition & 3 deletions ShipMarkerData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ namespace VH_Ship_Marker_Mod

public class ShipMarkerData
{

public string Type
public ShipType Type
{ get; set; }

public GameObject Marker
Expand All @@ -16,5 +15,4 @@ public ZDO ZDO
{ get; set; }

}

}
15 changes: 15 additions & 0 deletions ShipType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace VH_Ship_Marker_Mod
{

public class ShipType
{
public string Prefab { get; }
public string Name { get; }

public ShipType(string prefab, string name) {
this.Prefab = prefab;
this.Name = name;
}
}

}
36 changes: 23 additions & 13 deletions ZDOMan_Patch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,52 @@ class ZDOMan_Patch
[HarmonyPostfix]
private static void Loaded()
{
Main._shipMarkers.Clear();
Main.ShipMarkers.Clear();
List<ZDO> shipZDOs = new List<ZDO>();
foreach (string prefab in Main.shipTypes)
foreach (ShipType shipType in Main.ShipTypes)
{
ZDOMan.instance.GetAllZDOsWithPrefab(prefab, shipZDOs);
ZDOMan.instance.GetAllZDOsWithPrefab(shipType.Prefab, shipZDOs);
foreach (ZDO zdo in shipZDOs)
{
ShipMarkerData markerData = new ShipMarkerData();
markerData.ZDO = zdo;
markerData.Type = prefab;
Main._shipMarkers.Add(zdo.m_uid, markerData);
markerData.Type = shipType;
Main.ShipMarkers.Add(zdo.m_uid, markerData);
}
shipZDOs.Clear();
}
}

[HarmonyPatch(typeof(ZNetScene), "OnZDODestroyed")]
[HarmonyPostfix]
[HarmonyPrefix]
private static void ZDODestroyed(ZDO zdo)
{
if (Main._shipMarkers.ContainsKey(zdo.m_uid))
if (Main.ShipMarkers.ContainsKey(zdo.m_uid))
{
Main._shipMarkers.Remove(zdo.m_uid);
ShipMarkerData data = Main.ShipMarkers[zdo.m_uid];
data.ZDO = null;
data.Type = null;
if (data.Marker != null) {
UnityEngine.Object.Destroy(data.Marker);
}
Main.ShipMarkers.Remove(zdo.m_uid);
}
}

[HarmonyPatch(typeof(ZNetScene), "AddInstance")]
[HarmonyPostfix]
private static void ZDOAdded(ZDO zdo)
{
IEnumerable<int> prefabHashes = Main.shipTypes.Select(str => str.GetStableHashCode());
if (prefabHashes.Contains(zdo.GetPrefab()))
foreach (ShipType type in Main.ShipTypes)
{
ShipMarkerData markerData = new ShipMarkerData();
markerData.ZDO = zdo;
Main._shipMarkers.Add(zdo.m_uid, markerData);
if (zdo.GetPrefab() == type.Prefab.GetStableHashCode() && !Main.ShipMarkers.ContainsKey(zdo.m_uid))
{
ShipMarkerData markerData = new ShipMarkerData();
markerData.ZDO = zdo;
markerData.Type = type;
Main.ShipMarkers.Add(zdo.m_uid, markerData);
break;
}
}
}
}
Expand Down

0 comments on commit ea40a01

Please sign in to comment.