Add Dictionary<,> support to FieldSaver
This commit is contained in:
@@ -648,6 +648,7 @@ namespace OpenRA
|
|||||||
public string YamlName;
|
public string YamlName;
|
||||||
public string Loader;
|
public string Loader;
|
||||||
public bool FromYamlKey;
|
public bool FromYamlKey;
|
||||||
|
public bool DictionaryFromYamlKey;
|
||||||
public bool Required;
|
public bool Required;
|
||||||
|
|
||||||
public SerializeAttribute(bool serialize = true, bool required = false)
|
public SerializeAttribute(bool serialize = true, bool required = false)
|
||||||
@@ -710,6 +711,17 @@ 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 dont want to have to use that everywhere.
|
// mirrors DescriptionAttribute from System.ComponentModel but we dont want to have to use that everywhere.
|
||||||
[AttributeUsage(AttributeTargets.All)]
|
[AttributeUsage(AttributeTargets.All)]
|
||||||
public sealed class DescAttribute : Attribute
|
public sealed class DescAttribute : Attribute
|
||||||
|
|||||||
@@ -28,7 +28,18 @@ namespace OpenRA
|
|||||||
|
|
||||||
foreach (var info in FieldLoader.GetTypeLoadInfo(o.GetType(), includePrivateByDefault))
|
foreach (var info in FieldLoader.GetTypeLoadInfo(o.GetType(), includePrivateByDefault))
|
||||||
{
|
{
|
||||||
if (info.Attribute.FromYamlKey)
|
if (info.Attribute.DictionaryFromYamlKey)
|
||||||
|
{
|
||||||
|
var dict = (System.Collections.IDictionary)info.Field.GetValue(o);
|
||||||
|
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, key.GetType()), FormatValue(value, value.GetType())));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (info.Attribute.FromYamlKey)
|
||||||
root = FormatValue(o, info.Field);
|
root = FormatValue(o, info.Field);
|
||||||
else
|
else
|
||||||
nodes.Add(new MiniYamlNode(info.YamlName, FormatValue(o, info.Field)));
|
nodes.Add(new MiniYamlNode(info.YamlName, FormatValue(o, info.Field)));
|
||||||
@@ -99,6 +110,25 @@ namespace OpenRA
|
|||||||
return ((System.Collections.IEnumerable)v).Cast<object>().JoinWith(", ");
|
return ((System.Collections.IEnumerable)v).Cast<object>().JoinWith(", ");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This is only for documentation generation
|
||||||
|
if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Dictionary<,>))
|
||||||
|
{
|
||||||
|
var result = "";
|
||||||
|
var dict = (System.Collections.IDictionary)v;
|
||||||
|
foreach (var kvp in dict)
|
||||||
|
{
|
||||||
|
var key = ((System.Collections.DictionaryEntry)kvp).Key;
|
||||||
|
var value = ((System.Collections.DictionaryEntry)kvp).Value;
|
||||||
|
|
||||||
|
var formattedKey = FormatValue(key, key.GetType());
|
||||||
|
var formattedValue = FormatValue(value, value.GetType());
|
||||||
|
|
||||||
|
result += "{0}: {1}{2}".F(formattedKey, formattedValue, Environment.NewLine);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(OpenRA.Primitives.Cache<,>))
|
if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(OpenRA.Primitives.Cache<,>))
|
||||||
return ""; // TODO
|
return ""; // TODO
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user