Fix serialisation from dictionary.

This commit is contained in:
Matthias Mailänder
2025-07-14 20:17:27 +02:00
committed by Gustas Kažukauskas
parent 25be2149c1
commit ec206dc359
2 changed files with 10 additions and 20 deletions

View File

@@ -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<K,V>.
[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

View File

@@ -27,23 +27,25 @@ namespace OpenRA
var nodes = new List<MiniYamlNode>();
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<MiniYamlNode>();
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);