From ec206dc35952cd5f93f625a8907517a4dac70915 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Mail=C3=A4nder?= Date: Mon, 14 Jul 2025 20:17:27 +0200 Subject: [PATCH] Fix serialisation from dictionary. --- OpenRA.Game/FieldLoader.cs | 12 ------------ OpenRA.Game/FieldSaver.cs | 18 ++++++++++-------- 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/OpenRA.Game/FieldLoader.cs b/OpenRA.Game/FieldLoader.cs index c65623d8c9..d142567dca 100644 --- a/OpenRA.Game/FieldLoader.cs +++ b/OpenRA.Game/FieldLoader.cs @@ -790,7 +790,6 @@ namespace OpenRA public string YamlName; public string Loader; public bool FromYamlKey; - public bool DictionaryFromYamlKey; public bool Required; public bool AllowEmptyEntries; @@ -828,17 +827,6 @@ namespace OpenRA } } - // Special-cases FieldFromYamlKeyAttribute for use with Dictionary. - [AttributeUsage(AttributeTargets.Field)] - public sealed class DictionaryFromYamlKeyAttribute : FieldLoader.SerializeAttribute - { - public DictionaryFromYamlKeyAttribute() - { - FromYamlKey = true; - DictionaryFromYamlKey = true; - } - } - // Mirrors DescriptionAttribute from System.ComponentModel but we don't want to have to use that everywhere. [AttributeUsage(AttributeTargets.All)] public sealed class DescAttribute(params string[] lines) : Attribute diff --git a/OpenRA.Game/FieldSaver.cs b/OpenRA.Game/FieldSaver.cs index b787f6ae15..d3ffa79604 100644 --- a/OpenRA.Game/FieldSaver.cs +++ b/OpenRA.Game/FieldSaver.cs @@ -27,23 +27,25 @@ namespace OpenRA var nodes = new List(); string root = null; - foreach (var info in FieldLoader.GetTypeLoadInfo(o.GetType(), includePrivateByDefault)) + foreach (var fieldInfo in FieldLoader.GetTypeLoadInfo(o.GetType(), includePrivateByDefault)) { - if (info.Attribute.DictionaryFromYamlKey) + if (fieldInfo.Field.FieldType.IsGenericType && fieldInfo.Field.FieldType.IsAssignableTo(typeof(System.Collections.IDictionary))) { - var dict = (System.Collections.IDictionary)info.Field.GetValue(o); + var dict = (System.Collections.IDictionary)fieldInfo.Field.GetValue(o); + var dictNodes = new List(); foreach (var kvp in dict) { var key = ((System.Collections.DictionaryEntry)kvp).Key; var value = ((System.Collections.DictionaryEntry)kvp).Value; - - nodes.Add(new MiniYamlNode(FormatValue(key), FormatValue(value))); + dictNodes.Add(new MiniYamlNode(FormatValue(key), FormatValue(value))); } + + nodes.Add(new MiniYamlNode(fieldInfo.YamlName, "", dictNodes)); } - else if (info.Attribute.FromYamlKey) - root = FormatValue(o, info.Field); + else if (fieldInfo.Attribute.FromYamlKey) + root = FormatValue(o, fieldInfo.Field); else - nodes.Add(new MiniYamlNode(info.YamlName, FormatValue(o, info.Field))); + nodes.Add(new MiniYamlNode(fieldInfo.YamlName, FormatValue(o, fieldInfo.Field))); } return new MiniYaml(root, nodes);