Skip to content

Commit

Permalink
Make shorter
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaPiggy committed Jul 17, 2024
1 parent bace02c commit 1851e76
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 30 deletions.
12 changes: 12 additions & 0 deletions Winch/Serialization/DredgeTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,16 @@ protected void AddReroutes(Dictionary<string, string> 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;
}
}
10 changes: 10 additions & 0 deletions Winch/Serialization/DredgeTypeHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ public static List<Vector2Int> ParseDimensions(JArray dimensions)
return parsed;
}

public static HarvestableType[] ParseHarvestableTypes(JArray values)
{
List<HarvestableType> types = new();
foreach (object type in values)
{
types.Add(DredgeTypeHelpers.GetEnumValue<HarvestableType>(type));
};
return types.ToArray();
}

public static CellGroupConfiguration ParseCellGroupConfiguration(JToken cellGroupConfiguration)
{
var config = new UnstructedCellGroupConfiguration();
Expand Down
12 changes: 1 addition & 11 deletions Winch/Serialization/Item/HarvesterItemDataConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class HarvesterItemDataConverter : SpatialItemDataConverter
private readonly Dictionary<string, FieldDefinition> _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) },
Expand All @@ -19,14 +19,4 @@ public HarvesterItemDataConverter()
{
AddDefinitions(_definitions);
}

public static HarvestableType[] ParseHarvestableTypes(JArray values)
{
List<HarvestableType> types = new();
foreach (object type in values)
{
types.Add(DredgeTypeHelpers.GetEnumValue<HarvestableType>(type));
};
return types.ToArray();
}
}
24 changes: 7 additions & 17 deletions Winch/Serialization/Item/ItemDataConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public class ItemDataConverter : DredgeTypeConverter<ItemData>
private readonly Dictionary<string, FieldDefinition> _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<ItemType>(o)) },
{ "itemSubtype", new(ItemSubtype.GENERAL, o => DredgeTypeHelpers.GetEnumValue<ItemSubtype>(o)) },
{ "tooltipTextColor", new(new Color(0.4902f, 0.3843f, 0.2667f, 255f), o => DredgeTypeHelpers.GetColorFromJsonObject(o)) },
Expand All @@ -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);
}
2 changes: 1 addition & 1 deletion Winch/Serialization/Item/MessageItemDataConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class MessageItemDataConverter : NonSpatialItemDataConverter
private readonly Dictionary<string, FieldDefinition> _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<MessagesSet>(o) )}
};

Expand Down
2 changes: 1 addition & 1 deletion Winch/Serialization/Item/ResearchableItemDataConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class ResearchableItemDataConverter : NonSpatialItemDataConverter
{
private readonly Dictionary<string, FieldDefinition> _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<ResearchBenefitType>(o)) },
{ "researchBenefitValue", new(0.05f, o => float.Parse(o.ToString())) },
Expand Down

0 comments on commit 1851e76

Please sign in to comment.