-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
355 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
using System.Linq; | ||
using Winch.Util; | ||
|
||
namespace Winch.Data.Shop; | ||
|
||
public class ModdedShopData : ShopData | ||
{ | ||
public string id = string.Empty; | ||
|
||
public GridKey gridKey = GridKey.NONE; | ||
|
||
internal void Populate() | ||
{ | ||
foreach (var itemData in alwaysInStock.Concat(phaseLinkedShopData.SelectMany(pl => pl.itemData)).Concat(dialogueLinkedShopData.SelectMany(pl => pl.itemData))) | ||
{ | ||
if (itemData is ModdedShopItemData moddedItemData) | ||
moddedItemData.Populate(); | ||
} | ||
} | ||
|
||
public static implicit operator ShopRestocker.ShopDataGridConfig(ModdedShopData shopData) => new ShopRestocker.ShopDataGridConfig | ||
{ | ||
shopData = shopData, | ||
gridKey = shopData.gridKey, | ||
}; | ||
|
||
public class ModdedShopItemData : ShopItemData | ||
{ | ||
public new string itemData = string.Empty; | ||
|
||
public SpatialItemData ItemData => ItemUtil.GetSpatialItemData(itemData); | ||
|
||
internal void Populate() | ||
{ | ||
base.itemData = ItemData; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using HarmonyLib; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using UnityEngine.ResourceManagement.AsyncOperations; | ||
using Winch.Core.API; | ||
using Winch.Util; | ||
using static ShopRestocker; | ||
|
||
namespace Winch.Patches.API; | ||
|
||
[HarmonyPatch(typeof(ShopRestocker))] | ||
[HarmonyPatch(nameof(ShopRestocker.Awake))] | ||
internal static class ShopLoadPatcher | ||
{ | ||
public static void Postfix(ShopRestocker __instance) | ||
{ | ||
ShopUtil.AddModdedShopData(__instance); | ||
ShopUtil.PopulateShopData(__instance.shopDataGridConfigs); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
Winch/Serialization/Shop/DialogueLinkedShopDataConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Newtonsoft.Json.Linq; | ||
using System.Collections.Generic; | ||
using static ShopData; | ||
using static ShopData.DialogueLinkedShopData; | ||
|
||
namespace Winch.Serialization.Shop; | ||
|
||
public class DialogueLinkedShopDataConverter : DredgeTypeConverter<DialogueLinkedShopData> | ||
{ | ||
private readonly Dictionary<string, FieldDefinition> _definitions = new() | ||
{ | ||
{ "itemData", new(new List<ShopItemData>(), o=>DredgeTypeHelpers.ParseShopItemDataList((JArray)o)) }, | ||
{ "dialogueNodes", new( new List<string>(), o=>DredgeTypeHelpers.ParseStringList((JArray)o)) }, | ||
{ "requireMode", new( RequireMode.ANY, o => DredgeTypeHelpers.GetEnumValue<RequireMode>(o)) } | ||
}; | ||
|
||
public DialogueLinkedShopDataConverter() | ||
{ | ||
AddDefinitions(_definitions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Newtonsoft.Json.Linq; | ||
using System.Collections.Generic; | ||
using static ShopData; | ||
|
||
namespace Winch.Serialization.Shop; | ||
|
||
public class PhaseLinkedShopDataConverter : DredgeTypeConverter<PhaseLinkedShopData> | ||
{ | ||
private readonly Dictionary<string, FieldDefinition> _definitions = new() | ||
{ | ||
{ "itemData", new(new List<ShopItemData>(), o=>DredgeTypeHelpers.ParseShopItemDataList((JArray)o)) }, | ||
{ "phase", new(0, o=>int.Parse(o.ToString())) }, | ||
}; | ||
|
||
public PhaseLinkedShopDataConverter() | ||
{ | ||
AddDefinitions(_definitions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using Newtonsoft.Json.Linq; | ||
using System.Collections.Generic; | ||
using Winch.Data.Shop; | ||
using static ShopData; | ||
|
||
namespace Winch.Serialization.Shop; | ||
|
||
public class ShopDataConverter : DredgeTypeConverter<ModdedShopData> | ||
{ | ||
private readonly Dictionary<string, FieldDefinition> _definitions = new() | ||
{ | ||
{ "id", new(string.Empty, null) }, | ||
{ "alwaysInStock", new(new List<ShopItemData>(), o=>DredgeTypeHelpers.ParseShopItemDataList((JArray)o)) }, | ||
{ "phaseLinkedShopData", new(new List<PhaseLinkedShopData>(), o=>DredgeTypeHelpers.ParsePhaseLinkedShopDataList((JArray)o)) }, | ||
{ "dialogueLinkedShopData", new(new List<DialogueLinkedShopData>(), o=>DredgeTypeHelpers.ParseDialogueLinkedShopDataList((JArray)o)) }, | ||
{ "itemSubtypesFromResearchPool", new(new List<ItemSubtype>(), o=>DredgeTypeHelpers.GetEnumValues<ItemSubtype>((JArray)o)) }, | ||
{ "countOfEachItemFromResearchPool", new(0, o=>int.Parse(o.ToString())) }, | ||
{ "gridKey", new(GridKey.NONE, o=>DredgeTypeHelpers.GetEnumValue<GridKey>(o)) }, | ||
}; | ||
|
||
public ShopDataConverter() | ||
{ | ||
AddDefinitions(_definitions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System.Collections.Generic; | ||
using Winch.Data.Shop; | ||
|
||
namespace Winch.Serialization.Shop; | ||
|
||
public class ShopItemDataConverter : DredgeTypeConverter<ModdedShopData.ModdedShopItemData> | ||
{ | ||
private readonly Dictionary<string, FieldDefinition> _definitions = new() | ||
{ | ||
{ "itemData", new(string.Empty, null) }, | ||
{ "count", new(1, o=>int.Parse(o.ToString())) }, | ||
{ "chance", new(1f, o=>float.Parse(o.ToString())) }, | ||
}; | ||
|
||
public ShopItemDataConverter() | ||
{ | ||
AddDefinitions(_definitions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using AeLa.EasyFeedback.APIs; | ||
using Winch.Core; | ||
using Winch.Data.Shop; | ||
using Winch.Serialization.Shop; | ||
using static ShopData; | ||
using static ShopRestocker; | ||
using static Winch.Data.Shop.ModdedShopData; | ||
|
||
namespace Winch.Util; | ||
|
||
public static class ShopUtil | ||
{ | ||
private static ShopDataConverter ShopDataConverter = new ShopDataConverter(); | ||
private static ShopItemDataConverter ShopItemDataConverter = new ShopItemDataConverter(); | ||
private static PhaseLinkedShopDataConverter PhaseLinkedShopDataConverter = new PhaseLinkedShopDataConverter(); | ||
private static DialogueLinkedShopDataConverter DialogueLinkedShopDataConverter = new DialogueLinkedShopDataConverter(); | ||
|
||
internal static bool PopulateShopDataFromMetaWithConverter(ModdedShopData data, Dictionary<string, object> meta) | ||
{ | ||
return UtilHelpers.PopulateObjectFromMeta(data, meta, ShopDataConverter); | ||
} | ||
|
||
internal static bool PopulateShopItemDataFromMetaWithConverter(ModdedShopData.ModdedShopItemData data, Dictionary<string, object> meta) | ||
{ | ||
return UtilHelpers.PopulateObjectFromMeta(data, meta, ShopItemDataConverter); | ||
} | ||
|
||
internal static bool PopulatePhaseLinkedShopDataFromMetaWithConverter(PhaseLinkedShopData data, Dictionary<string, object> meta) | ||
{ | ||
return UtilHelpers.PopulateObjectFromMeta(data, meta, PhaseLinkedShopDataConverter); | ||
} | ||
|
||
internal static bool PopulateDialogueLinkedShopDataFromMetaWithConverter(DialogueLinkedShopData data, Dictionary<string, object> meta) | ||
{ | ||
return UtilHelpers.PopulateObjectFromMeta(data, meta, DialogueLinkedShopDataConverter); | ||
} | ||
|
||
internal static List<string> KeepInStockItems = new(); | ||
internal static Dictionary<GridKey, ShopDataGridConfig> AllShopDataGridConfigDict = new(); | ||
internal static Dictionary<string, ModdedShopData> ModdedShopDataDict = new(); | ||
internal static Dictionary<string, ShopData> AllShopDataDict = new(); | ||
|
||
public static void RegisterKeepInStockItem(string itemID) => KeepInStockItems.Add(itemID); | ||
|
||
public static ModdedShopData GetModdedShopData(string id) | ||
{ | ||
if (string.IsNullOrWhiteSpace(id)) | ||
return null; | ||
|
||
if (ModdedShopDataDict.TryGetValue(id, out ModdedShopData shopData)) | ||
return shopData; | ||
else | ||
return null; | ||
} | ||
|
||
public static ShopData GetShopData(string id) | ||
{ | ||
if (string.IsNullOrWhiteSpace(id)) | ||
return null; | ||
|
||
if (AllShopDataDict.TryGetValue(id, out var shop)) | ||
return shop; | ||
|
||
if (ModdedShopDataDict.TryGetValue(id, out var moddedShop)) | ||
return moddedShop; | ||
|
||
return null; | ||
} | ||
|
||
internal static void AddModdedShopData(ShopRestocker restocker) | ||
{ | ||
restocker.itemIdsToKeep.AddRange(KeepInStockItems); | ||
foreach (var shopData in ModdedShopDataDict.Values) | ||
{ | ||
shopData.Populate(); | ||
restocker.shopDataGridConfigs.Add(shopData); | ||
} | ||
} | ||
|
||
internal static void PopulateShopData(IEnumerable<ShopDataGridConfig> result) | ||
{ | ||
foreach (var shopDataGridConfig in result) | ||
{ | ||
AllShopDataGridConfigDict.SafeAdd(shopDataGridConfig.gridKey, shopDataGridConfig); | ||
WinchCore.Log.Debug($"Added shop data {shopDataGridConfig.gridKey} to AllShopDataGridConfigDict"); | ||
AllShopDataDict.SafeAdd(shopDataGridConfig.shopData.name, shopDataGridConfig.shopData); | ||
WinchCore.Log.Debug($"Added shop data {shopDataGridConfig.shopData.name} to AllShopDataDict"); | ||
} | ||
} | ||
|
||
internal static void ClearShopData() | ||
{ | ||
AllShopDataGridConfigDict.Clear(); | ||
AllShopDataDict.Clear(); | ||
} | ||
|
||
internal static void AddShopDataFromMeta(string metaPath) | ||
{ | ||
var meta = UtilHelpers.ParseMeta(metaPath); | ||
if (meta == null) | ||
{ | ||
WinchCore.Log.Error($"Meta file {metaPath} is empty"); | ||
return; | ||
} | ||
var shopData = UtilHelpers.GetScriptableObjectFromMeta<ModdedShopData>(meta, metaPath); | ||
if (shopData == null) | ||
{ | ||
WinchCore.Log.Error($"Couldn't create ModdedShopData"); | ||
return; | ||
} | ||
var id = (string)meta["id"]; | ||
if (ModdedShopDataDict.ContainsKey(id)) | ||
{ | ||
WinchCore.Log.Error($"Duplicate shop data {id} at {metaPath} failed to load"); | ||
return; | ||
} | ||
if (PopulateShopDataFromMetaWithConverter(shopData, meta)) | ||
{ | ||
ModdedShopDataDict.Add(id, shopData); | ||
} | ||
else | ||
{ | ||
WinchCore.Log.Error($"No shop data converter found"); | ||
} | ||
} | ||
|
||
public static ShopData[] GetAllShopData() | ||
{ | ||
return AllShopDataDict.Values.ToArray(); | ||
} | ||
} |