Skip to content

Commit

Permalink
Map markers
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaPiggy committed Sep 3, 2024
1 parent 504a1e3 commit 70cfd47
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"portraitSprite": "Steve",
"smallPortraitSprite": "SteveSmall",
"alwaysAvailable": true,
"yarnRootNode": "ExampleItems_Steve_Root"
"yarnRootNode": "ExampleItems_Steve_Chat"
}
4 changes: 4 additions & 0 deletions Winch.Examples/ExampleItems/Assets/Dialogues/Steve.yarn
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
title: ExampleItems_Steve_Root
---
<<AddMapMarker exampleitems.mapmarker false>>
===
title: ExampleItems_Steve_Chat
---
exampleitems.steve: Test #line:ex78383
===
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"x": 366.0,
"z": -344.0
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"y": 0.0,
"z": -344.0
},
"conversationNodeName": "ExampleItems_Steve_Root",
"isOneTimeOnly": "false"
"conversationNodeName": "ExampleItems_Steve_Chat",
"isOneTimeOnly": "false",
"mapMarkerData": "exampleitems.mapmarker"
}
19 changes: 19 additions & 0 deletions Winch/Core/AssetLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ private static void LoadAssetFolder(string path)
string poiFolderpath = Path.Combine(path, "POI");
string harvestZoneFolderpath = Path.Combine(path, "HarvestZones");
string vibrationFolderpath = Path.Combine(path, "Vibrations");
string mapMarkerFolderpath = Path.Combine(path, "MapMarkers");
string abilityFolderpath = Path.Combine(path, "Abilities");
string worldEventFolderpath = Path.Combine(path, "WorldEvents");
string dialogueFolderpath = Path.Combine(path, "Dialogues");
Expand All @@ -72,6 +73,7 @@ private static void LoadAssetFolder(string path)
if(Directory.Exists(gridConfigFolderpath)) LoadGridConfigFiles(gridConfigFolderpath);
if(Directory.Exists(itemFolderPath)) LoadItemFiles(itemFolderPath);
if(Directory.Exists(vibrationFolderpath)) LoadVibrationFiles(vibrationFolderpath);
if(Directory.Exists(mapMarkerFolderpath)) LoadMapMarkerFiles(mapMarkerFolderpath);
if(Directory.Exists(poiFolderpath)) LoadPoiFiles(poiFolderpath);
if(Directory.Exists(harvestZoneFolderpath)) LoadHarvestZoneFiles(harvestZoneFolderpath);
if(Directory.Exists(abilityFolderpath)) LoadAbilityFiles(abilityFolderpath);
Expand Down Expand Up @@ -191,6 +193,23 @@ private static void LoadVibrationFiles(string vibrationFolderPath)
}
}


private static void LoadMapMarkerFiles(string mapMarkerFolderPath)
{
string[] mapMarkerFiles = Directory.GetFiles(mapMarkerFolderPath);
foreach (string file in mapMarkerFiles)
{
try
{
MapMarkerUtil.AddMapMarkerDataFromMeta(file);
}
catch (Exception ex)
{
WinchCore.Log.Error($"Failed to load map marker data from {file}: {ex}");
}
}
}

private static void LoadPoiFilesOfType<T>(string poiFolderPath) where T : CustomPOI
{
string[] poiFiles = Directory.GetFiles(poiFolderPath);
Expand Down
6 changes: 6 additions & 0 deletions Winch/Data/POI/CustomPOI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ public class CustomPOI : SerializedScriptableObject
/// </summary>
[SerializeField]
public Vector3 interactPointTarget = Vector3.zero;

/// <summary>
/// ID of this POI's map marker data
/// </summary>
[SerializeField]
public string mapMarkerData = string.Empty;
}


15 changes: 15 additions & 0 deletions Winch/Patches/API/MapMarkerClearPatcher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using HarmonyLib;
using Winch.Util;

namespace Winch.Patches.API
{
[HarmonyPatch(typeof(DataLoader))]
[HarmonyPatch(nameof(DataLoader.OnGameEnded))]
internal static class MapMarkerClearPatcher
{
public static void Postfix(DataLoader __instance)
{
MapMarkerUtil.ClearMapMarkerData();
}
}
}
3 changes: 3 additions & 0 deletions Winch/Patches/API/MapMarkerLoadPatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using UnityEngine.ResourceManagement.AsyncOperations;
using Winch.Core.API;
using Winch.Util;

namespace Winch.Patches.API
{
Expand All @@ -13,13 +14,15 @@ public static void Prefix(DataLoader __instance, AsyncOperationHandle<IList<MapM
{
if (handle.Result == null || handle.Status != AsyncOperationStatus.Succeeded) return;

MapMarkerUtil.AddModdedMapMarkerData(handle.Result);
DredgeEvent.AddressableEvents.MapMarkersLoaded.Trigger(__instance, handle, true);
}

public static void Postfix(DataLoader __instance, AsyncOperationHandle<IList<MapMarkerData>> handle)
{
if (handle.Result == null || handle.Status != AsyncOperationStatus.Succeeded) return;

MapMarkerUtil.PopulateMapMarkerData(handle.Result);
DredgeEvent.AddressableEvents.MapMarkersLoaded.Trigger(__instance, handle, false);
}
}
Expand Down
24 changes: 24 additions & 0 deletions Winch/Serialization/MapMarker/MapMarkerDataConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Winch.Util;

namespace Winch.Serialization.MapMarker;

public class MapMarkerDataConverter : DredgeTypeConverter<MapMarkerData>
{
private readonly Dictionary<string, FieldDefinition> _definitions = new()
{
{ "x", new(0f, o => float.Parse(o.ToString())) },
{ "z", new(0f, o => float.Parse(o.ToString())) },
{ "mapMarkerType", new(MapMarkerType.SIDE, o=> DredgeTypeHelpers.GetEnumValue<MapMarkerType>(o) )},
};

public MapMarkerDataConverter()
{
AddDefinitions(_definitions);
}
}
3 changes: 2 additions & 1 deletion Winch/Serialization/POI/CustomPOIConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ public class CustomPOIConverter : DredgeTypeConverter<CustomPOI>
{ "location", new( Vector3.zero, o=> DredgeTypeHelpers.ParseVector3(o)) },
{ "canBeGhostWindTarget", new( false, o=> bool.Parse(o.ToString())) },
{ "ghostWindTarget", new( Vector3.zero, o=> DredgeTypeHelpers.ParseVector3(o)) },
{ "interactPointTarget", new( Vector3.zero, o=> DredgeTypeHelpers.ParseVector3(o)) }
{ "interactPointTarget", new( Vector3.zero, o=> DredgeTypeHelpers.ParseVector3(o)) },
{ "mapMarkerData", new( string.Empty, null) }
};

public CustomPOIConverter()
Expand Down
101 changes: 101 additions & 0 deletions Winch/Util/MapMarkerUtil.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System.Collections.Generic;
using System.Linq;
using Winch.Core;
using Winch.Serialization.MapMarker;

namespace Winch.Util;

public static class MapMarkerUtil
{
private static MapMarkerDataConverter MapMarkerDataConverter = new MapMarkerDataConverter();

internal static bool PopulateMapMarkerDataFromMetaWithConverter(MapMarkerData data, Dictionary<string, object> meta)
{
return UtilHelpers.PopulateObjectFromMeta(data, meta, MapMarkerDataConverter);
}

internal static Dictionary<string, MapMarkerData> ModdedMapMarkerDataDict = new();
internal static Dictionary<string, MapMarkerData> AllMapMarkerDataDict = new();

public static MapMarkerData GetModdedMapMarkerData(string id)
{
if (string.IsNullOrWhiteSpace(id))
return null;

if (ModdedMapMarkerDataDict.TryGetValue(id, out MapMarkerData mapMarkerData))
return mapMarkerData;
else
return null;
}

public static MapMarkerData GetMapMarkerData(string id)
{
if (string.IsNullOrWhiteSpace(id))
return null;

if (AllMapMarkerDataDict.TryGetValue(id, out var mapMarker))
return mapMarker;

if (ModdedMapMarkerDataDict.TryGetValue(id, out var moddedMapMarker))
return moddedMapMarker;

return null;
}

internal static void AddModdedMapMarkerData(IList<MapMarkerData> list)
{
foreach (var mapMarkerData in ModdedMapMarkerDataDict.Values)
{
list.SafeAdd(mapMarkerData);
}
}

internal static void PopulateMapMarkerData(IList<MapMarkerData> result)
{
foreach (var mapMarkerData in result)
{
AllMapMarkerDataDict.SafeAdd(mapMarkerData.name, mapMarkerData);
WinchCore.Log.Debug($"Added map marker data {mapMarkerData.name} to AllMapMarkerDataDict");
}
}

internal static void ClearMapMarkerData()
{
AllMapMarkerDataDict.Clear();
}

internal static void AddMapMarkerDataFromMeta(string metaPath)
{
var meta = UtilHelpers.ParseMeta(metaPath);
if (meta == null)
{
WinchCore.Log.Error($"Meta file {metaPath} is empty");
return;
}
var mapMarkerData = UtilHelpers.GetScriptableObjectFromMeta<MapMarkerData>(meta, metaPath);
if (mapMarkerData == null)
{
WinchCore.Log.Error($"Couldn't create MapMarkerData");
return;
}
var id = (string)meta["id"];
if (ModdedMapMarkerDataDict.ContainsKey(id))
{
WinchCore.Log.Error($"Duplicate map marker data {id} at {metaPath} failed to load");
return;
}
if (PopulateMapMarkerDataFromMetaWithConverter(mapMarkerData, meta))
{
ModdedMapMarkerDataDict.Add(id, mapMarkerData);
}
else
{
WinchCore.Log.Error($"No map marker data converter found");
}
}

public static MapMarkerData[] GetAllMapMarkerData()
{
return AllMapMarkerDataDict.Values.ToArray();
}
}
7 changes: 7 additions & 0 deletions Winch/Util/PoiUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,13 @@ internal static (GameObject, T) CreateGenericPoiFromCustomPoi<T>(CustomPOI custo
poi.interactPointTargetTransform = interactPointTarget.transform;
}

if (!string.IsNullOrWhiteSpace(customPoi.mapMarkerData))
{
var mapMarkerLocation = customPoiObject.AddComponent<MapMarkerLocation>();
mapMarkerLocation.mapMarkerData = MapMarkerUtil.GetMapMarkerData(customPoi.mapMarkerData);
mapMarkerLocation.SetMapMarkerData();
}

customPoiObject.layer = Layer.POI;
return (customPoiObject, poi);
}
Expand Down

0 comments on commit 70cfd47

Please sign in to comment.