From 1851e764c8e7a211b35905589455fc98480baa4f Mon Sep 17 00:00:00 2001 From: Noah Pilarski Date: Wed, 17 Jul 2024 15:44:26 -0400 Subject: [PATCH] Make shorter --- Winch/Serialization/DredgeTypeConverter.cs | 12 ++++++++++ Winch/Serialization/DredgeTypeHelpers.cs | 10 ++++++++ .../Item/HarvesterItemDataConverter.cs | 12 +--------- Winch/Serialization/Item/ItemDataConverter.cs | 24 ++++++------------- .../Item/MessageItemDataConverter.cs | 2 +- .../Item/ResearchableItemDataConverter.cs | 2 +- 6 files changed, 32 insertions(+), 30 deletions(-) diff --git a/Winch/Serialization/DredgeTypeConverter.cs b/Winch/Serialization/DredgeTypeConverter.cs index 1f00cb22..b55f3808 100644 --- a/Winch/Serialization/DredgeTypeConverter.cs +++ b/Winch/Serialization/DredgeTypeConverter.cs @@ -105,4 +105,16 @@ protected void AddReroutes(Dictionary reroutes) this.Reroutes.Add(reroute.Key, reroute.Value); } } + + protected static LocalizedString CreateLocalizedString(string key, string value) + { + var keyValueTuple = (key, value); + if (StringDefinitionCache.TryGetValue(keyValueTuple, out LocalizedString cached)) + { + return cached; + } + var localizedString = new LocalizedString(key, value); + StringDefinitionCache.Add(keyValueTuple, localizedString); + return localizedString; + } } diff --git a/Winch/Serialization/DredgeTypeHelpers.cs b/Winch/Serialization/DredgeTypeHelpers.cs index af7e8510..d30ae36a 100644 --- a/Winch/Serialization/DredgeTypeHelpers.cs +++ b/Winch/Serialization/DredgeTypeHelpers.cs @@ -122,6 +122,16 @@ public static List ParseDimensions(JArray dimensions) return parsed; } + public static HarvestableType[] ParseHarvestableTypes(JArray values) + { + List types = new(); + foreach (object type in values) + { + types.Add(DredgeTypeHelpers.GetEnumValue(type)); + }; + return types.ToArray(); + } + public static CellGroupConfiguration ParseCellGroupConfiguration(JToken cellGroupConfiguration) { var config = new UnstructedCellGroupConfiguration(); diff --git a/Winch/Serialization/Item/HarvesterItemDataConverter.cs b/Winch/Serialization/Item/HarvesterItemDataConverter.cs index 52f18043..0213b3c7 100644 --- a/Winch/Serialization/Item/HarvesterItemDataConverter.cs +++ b/Winch/Serialization/Item/HarvesterItemDataConverter.cs @@ -8,7 +8,7 @@ public class HarvesterItemDataConverter : SpatialItemDataConverter private readonly Dictionary _definitions = new() { { "itemType", new(ItemType.EQUIPMENT, null) }, - { "harvestableTypes", new(new HarvestableType[]{}, o => ParseHarvestableTypes((JArray)o)) }, + { "harvestableTypes", new(new HarvestableType[]{}, o => DredgeTypeHelpers.ParseHarvestableTypes((JArray)o)) }, { "aberrationBonus", new(0f, o => float.Parse(o.ToString())) }, { "showAlertOnDiscardHold", new(true, null) }, { "discardHoldTimeOverride", new(true, null) }, @@ -19,14 +19,4 @@ public HarvesterItemDataConverter() { AddDefinitions(_definitions); } - - public static HarvestableType[] ParseHarvestableTypes(JArray values) - { - List types = new(); - foreach (object type in values) - { - types.Add(DredgeTypeHelpers.GetEnumValue(type)); - }; - return types.ToArray(); - } } \ No newline at end of file diff --git a/Winch/Serialization/Item/ItemDataConverter.cs b/Winch/Serialization/Item/ItemDataConverter.cs index 608a16be..927ccdb1 100644 --- a/Winch/Serialization/Item/ItemDataConverter.cs +++ b/Winch/Serialization/Item/ItemDataConverter.cs @@ -13,13 +13,13 @@ public class ItemDataConverter : DredgeTypeConverter private readonly Dictionary _definitions = new() { { "id", new( Guid.NewGuid().ToString(), null) }, - { "itemNameKey", new(LocalizationUtil.Empty, o=> CreateLocalizedString(ItemTableDefinition, o.ToString())) }, - { "itemDescriptionKey", new(LocalizationUtil.Empty, o=> CreateLocalizedString(ItemTableDefinition, o.ToString())) }, - { "itemInsaneTitleKey", new(LocalizationUtil.Empty, o=> CreateLocalizedString(ItemTableDefinition, o.ToString())) }, - { "itemInsaneDescriptionKey", new(LocalizationUtil.Empty, o=> CreateLocalizedString(ItemTableDefinition, o.ToString())) }, + { "itemNameKey", new(LocalizationUtil.Empty, o=> CreateLocalizedString(o.ToString())) }, + { "itemDescriptionKey", new(LocalizationUtil.Empty, o=> CreateLocalizedString(o.ToString())) }, + { "itemInsaneTitleKey", new(LocalizationUtil.Empty, o=> CreateLocalizedString(o.ToString())) }, + { "itemInsaneDescriptionKey", new(LocalizationUtil.Empty, o=> CreateLocalizedString(o.ToString())) }, { "hasAdditionalNote", new(false, o=> bool.Parse(o.ToString())) }, - { "additionalNoteKey", new (LocalizationUtil.Empty, o=> CreateLocalizedString(ItemTableDefinition, o.ToString())) }, - { "dialogueNodeSpecificDescription", new(LocalizationUtil.Empty, o=> CreateLocalizedString(ItemTableDefinition, o.ToString())) }, + { "additionalNoteKey", new (LocalizationUtil.Empty, o=> CreateLocalizedString(o.ToString())) }, + { "dialogueNodeSpecificDescription", new(LocalizationUtil.Empty, o=> CreateLocalizedString(o.ToString())) }, { "itemType", new(ItemType.GENERAL, o => DredgeTypeHelpers.GetEnumValue(o)) }, { "itemSubtype", new(ItemSubtype.GENERAL, o => DredgeTypeHelpers.GetEnumValue(o)) }, { "tooltipTextColor", new(new Color(0.4902f, 0.3843f, 0.2667f, 255f), o => DredgeTypeHelpers.GetColorFromJsonObject(o)) }, @@ -43,15 +43,5 @@ public ItemDataConverter() AddReroutes(_reroutes); } - internal static LocalizedString CreateLocalizedString(string key, string value) - { - var keyValueTuple = (key, value); - if (StringDefinitionCache.TryGetValue(keyValueTuple, out LocalizedString cached)) - { - return cached; - } - var localizedString = new LocalizedString(key, value); - StringDefinitionCache.Add(keyValueTuple, localizedString); - return localizedString; - } + protected static LocalizedString CreateLocalizedString(string value) => CreateLocalizedString(ItemTableDefinition, value); } diff --git a/Winch/Serialization/Item/MessageItemDataConverter.cs b/Winch/Serialization/Item/MessageItemDataConverter.cs index 86f2fa88..b0518d2b 100644 --- a/Winch/Serialization/Item/MessageItemDataConverter.cs +++ b/Winch/Serialization/Item/MessageItemDataConverter.cs @@ -8,7 +8,7 @@ public class MessageItemDataConverter : NonSpatialItemDataConverter private readonly Dictionary _definitions = new() { { "chronologicalOrder", new(0, o=> int.Parse(o.ToString())) }, - { "messageBodyKey", new(LocalizationUtil.Empty, o=> CreateLocalizedString(ItemTableDefinition, o.ToString())) }, + { "messageBodyKey", new(LocalizationUtil.Empty, o=> CreateLocalizedString(o.ToString())) }, { "set", new( (int)MessagesSet.MESSAGES, o=> (int)DredgeTypeHelpers.GetEnumValue(o) )} }; diff --git a/Winch/Serialization/Item/ResearchableItemDataConverter.cs b/Winch/Serialization/Item/ResearchableItemDataConverter.cs index 68013101..e764b5a7 100644 --- a/Winch/Serialization/Item/ResearchableItemDataConverter.cs +++ b/Winch/Serialization/Item/ResearchableItemDataConverter.cs @@ -7,7 +7,7 @@ public class ResearchableItemDataConverter : NonSpatialItemDataConverter { private readonly Dictionary _definitions = new() { - { "completedDescriptionKey", new(LocalizationUtil.Empty, o=> CreateLocalizedString(ItemTableDefinition, o.ToString())) }, + { "completedDescriptionKey", new(LocalizationUtil.Empty, o=> CreateLocalizedString(o.ToString())) }, { "daysToResearch", new( 1f, o => float.Parse(o.ToString())) }, { "researchBenefitType", new(ResearchBenefitType.MOVEMENT_SPEED, o=> DredgeTypeHelpers.GetEnumValue(o)) }, { "researchBenefitValue", new(0.05f, o => float.Parse(o.ToString())) },