Made FieldLoader thread-safe, and cache some expensive reflection calls.
This commit is contained in:
@@ -33,9 +33,17 @@ namespace OpenRA
|
|||||||
throw new NotImplementedException("FieldLoader: Missing field `{0}` on `{1}`".F(s, f.Name));
|
throw new NotImplementedException("FieldLoader: Missing field `{0}` on `{1}`".F(s, f.Name));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static readonly ConcurrentCache<Type, FieldLoadInfo[]> TypeLoadInfo =
|
||||||
|
new ConcurrentCache<Type, FieldLoadInfo[]>(BuildTypeLoadInfo);
|
||||||
|
static readonly ConcurrentCache<MemberInfo, bool> MemberHasTranslateAttribute =
|
||||||
|
new ConcurrentCache<MemberInfo, bool>(member => member.HasAttribute<TranslateAttribute>());
|
||||||
|
|
||||||
|
static readonly object TranslationsLock = new object();
|
||||||
|
static Dictionary<string, string> translations;
|
||||||
|
|
||||||
public static void Load(object self, MiniYaml my)
|
public static void Load(object self, MiniYaml my)
|
||||||
{
|
{
|
||||||
var loadInfo = typeLoadInfo[self.GetType()];
|
var loadInfo = TypeLoadInfo[self.GetType()];
|
||||||
|
|
||||||
Dictionary<string, MiniYaml> md = null;
|
Dictionary<string, MiniYaml> md = null;
|
||||||
|
|
||||||
@@ -82,7 +90,6 @@ namespace OpenRA
|
|||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
static readonly object[] NoIndexes = { };
|
|
||||||
public static void LoadField(object target, string key, string value)
|
public static void LoadField(object target, string key, string value)
|
||||||
{
|
{
|
||||||
const BindingFlags Flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
|
const BindingFlags Flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
|
||||||
@@ -103,7 +110,7 @@ namespace OpenRA
|
|||||||
{
|
{
|
||||||
var sa = prop.GetCustomAttributes<SerializeAttribute>(false).DefaultIfEmpty(SerializeAttribute.Default).First();
|
var sa = prop.GetCustomAttributes<SerializeAttribute>(false).DefaultIfEmpty(SerializeAttribute.Default).First();
|
||||||
if (!sa.FromYamlKey)
|
if (!sa.FromYamlKey)
|
||||||
prop.SetValue(target, GetValue(prop.Name, prop.PropertyType, value, prop), NoIndexes);
|
prop.SetValue(target, GetValue(prop.Name, prop.PropertyType, value, prop), null);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -162,7 +169,7 @@ namespace OpenRA
|
|||||||
}
|
}
|
||||||
else if (fieldType == typeof(string))
|
else if (fieldType == typeof(string))
|
||||||
{
|
{
|
||||||
if (field != null && field.HasAttribute<TranslateAttribute>())
|
if (field != null && MemberHasTranslateAttribute[field])
|
||||||
return Regex.Replace(value, "@[^@]+@", m => Translate(m.Value.Substring(1, m.Value.Length - 2)), RegexOptions.Compiled);
|
return Regex.Replace(value, "@[^@]+@", m => Translate(m.Value.Substring(1, m.Value.Length - 2)), RegexOptions.Compiled);
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
@@ -438,12 +445,10 @@ namespace OpenRA
|
|||||||
|
|
||||||
public static IEnumerable<FieldLoadInfo> GetTypeLoadInfo(Type type, bool includePrivateByDefault = false)
|
public static IEnumerable<FieldLoadInfo> GetTypeLoadInfo(Type type, bool includePrivateByDefault = false)
|
||||||
{
|
{
|
||||||
return typeLoadInfo[type].Where(fli => includePrivateByDefault || fli.Field.IsPublic || (fli.Attribute.Serialize && !fli.Attribute.IsDefault));
|
return TypeLoadInfo[type].Where(fli => includePrivateByDefault || fli.Field.IsPublic || (fli.Attribute.Serialize && !fli.Attribute.IsDefault));
|
||||||
}
|
}
|
||||||
|
|
||||||
static Cache<Type, List<FieldLoadInfo>> typeLoadInfo = new Cache<Type, List<FieldLoadInfo>>(BuildTypeLoadInfo);
|
static FieldLoadInfo[] BuildTypeLoadInfo(Type type)
|
||||||
|
|
||||||
static List<FieldLoadInfo> BuildTypeLoadInfo(Type type)
|
|
||||||
{
|
{
|
||||||
var ret = new List<FieldLoadInfo>();
|
var ret = new List<FieldLoadInfo>();
|
||||||
|
|
||||||
@@ -465,7 +470,7 @@ namespace OpenRA
|
|||||||
ret.Add(fli);
|
ret.Add(fli);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret.ToArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
[AttributeUsage(AttributeTargets.Field)]
|
[AttributeUsage(AttributeTargets.Field)]
|
||||||
@@ -520,17 +525,27 @@ namespace OpenRA
|
|||||||
|
|
||||||
public static string Translate(string key)
|
public static string Translate(string key)
|
||||||
{
|
{
|
||||||
if (Translations == null || string.IsNullOrEmpty(key))
|
if (string.IsNullOrEmpty(key))
|
||||||
return key;
|
return key;
|
||||||
|
|
||||||
string value;
|
lock (TranslationsLock)
|
||||||
if (!Translations.TryGetValue(key, out value))
|
{
|
||||||
return key;
|
if (translations == null)
|
||||||
|
return key;
|
||||||
|
|
||||||
return value;
|
string value;
|
||||||
|
if (!translations.TryGetValue(key, out value))
|
||||||
|
return key;
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Dictionary<string, string> Translations = new Dictionary<string, string>();
|
public static void SetTranslations(IDictionary<string, string> translations)
|
||||||
|
{
|
||||||
|
lock (TranslationsLock)
|
||||||
|
FieldLoader.translations = new Dictionary<string, string>(translations);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
|
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
|
||||||
@@ -546,6 +561,7 @@ namespace OpenRA
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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)]
|
||||||
public sealed class DescAttribute : Attribute
|
public sealed class DescAttribute : Attribute
|
||||||
{
|
{
|
||||||
public readonly string[] Lines;
|
public readonly string[] Lines;
|
||||||
|
|||||||
@@ -116,7 +116,6 @@ namespace OpenRA
|
|||||||
if (!Manifest.Translations.Any())
|
if (!Manifest.Translations.Any())
|
||||||
{
|
{
|
||||||
Languages = new string[0];
|
Languages = new string[0];
|
||||||
FieldLoader.Translations = new Dictionary<string, string>();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,7 +143,7 @@ namespace OpenRA
|
|||||||
translations.Add(tkv.Key, tkv.Value);
|
translations.Add(tkv.Key, tkv.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
FieldLoader.Translations = translations;
|
FieldLoader.SetTranslations(translations);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map PrepareMap(string uid)
|
public Map PrepareMap(string uid)
|
||||||
|
|||||||
Reference in New Issue
Block a user