Log errors in ReflectedManager

This commit is contained in:
Westin Miller
2017-09-11 05:19:02 -07:00
parent 0073e101dd
commit e57f885d3b

View File

@@ -7,6 +7,7 @@ using System.Linq.Expressions;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using NLog;
using Sandbox.Engine.Multiplayer; using Sandbox.Engine.Multiplayer;
using Torch.API; using Torch.API;
@@ -389,8 +390,9 @@ namespace Torch.Utils
/// </summary> /// </summary>
public class ReflectedManager public class ReflectedManager
{ {
private static readonly Logger _log = LogManager.GetCurrentClassLogger();
private static readonly string[] _namespaceBlacklist = new[] { private static readonly string[] _namespaceBlacklist = new[] {
"System", "VRage", "Sandbox", "SpaceEngineers" "System", "VRage", "Sandbox", "SpaceEngineers", "Microsoft"
}; };
/// <summary> /// <summary>
@@ -426,11 +428,28 @@ namespace Torch.Utils
{ {
if (_processedTypes.Add(t)) if (_processedTypes.Add(t))
{ {
if (string.IsNullOrWhiteSpace(t.Namespace))
return;
foreach (string ns in _namespaceBlacklist) foreach (string ns in _namespaceBlacklist)
if (t.FullName.StartsWith(ns)) if (t.FullName.StartsWith(ns))
return; return;
foreach (FieldInfo field in t.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) foreach (FieldInfo field in t.GetFields(BindingFlags.Static | BindingFlags.Instance |
Process(field); BindingFlags.Public | BindingFlags.NonPublic))
{
try
{
#if DEBUG
if (Process(field))
_log?.Trace($"Field {field.DeclaringType?.FullName}#{field.Name} = {field.GetValue(null) ?? "null"}");
#else
Process(field);
#endif
}
catch (Exception e)
{
_log?.Error(e.InnerException ?? e, $"Unable to fill {field.DeclaringType?.FullName}#{field.Name}. {(e.InnerException ?? e).Message}");
}
}
} }
} }
@@ -484,7 +503,8 @@ namespace Torch.Utils
{ {
if (!field.IsStatic) if (!field.IsStatic)
throw new ArgumentException("Field must be static to be reflected"); throw new ArgumentException("Field must be static to be reflected");
field.SetValue(null, new Func<ReflectedEventReplacer>(() => new ReflectedEventReplacer(reflectedEventReplacer))); field.SetValue(null,
new Func<ReflectedEventReplacer>(() => new ReflectedEventReplacer(reflectedEventReplacer)));
return true; return true;
} }
return false; return false;
@@ -502,14 +522,14 @@ namespace Torch.Utils
case ReflectedFieldInfoAttribute rfia: case ReflectedFieldInfoAttribute rfia:
info = GetFieldPropRecursive(rfia.Type, rfia.Name, info = GetFieldPropRecursive(rfia.Type, rfia.Name,
BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public, BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public,
(a, b, c) => a.GetField(b)); (type, name, bindingFlags) => type.GetField(name, bindingFlags));
if (info == null) if (info == null)
throw new ArgumentException($"Unable to find field {rfia.Type.FullName}#{rfia.Name}"); throw new ArgumentException($"Unable to find field {rfia.Type.FullName}#{rfia.Name}");
break; break;
case ReflectedPropertyInfoAttribute rpia: case ReflectedPropertyInfoAttribute rpia:
info = GetFieldPropRecursive(rpia.Type, rpia.Name, info = GetFieldPropRecursive(rpia.Type, rpia.Name,
BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public, BindingFlags.Instance | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public,
(a, b, c) => a.GetProperty(b)); (type, name, bindingFlags) => type.GetProperty(name, bindingFlags));
if (info == null) if (info == null)
throw new ArgumentException($"Unable to find property {rpia.Type.FullName}#{rpia.Name}"); throw new ArgumentException($"Unable to find property {rpia.Type.FullName}#{rpia.Name}");
break; break;
@@ -674,7 +694,7 @@ namespace Torch.Utils
Expression instanceExpr = null; Expression instanceExpr = null;
if (!isStatic) if (!isStatic)
{ {
instanceExpr = trueType == paramExp[0].Type ? (Expression) paramExp[0] : Expression.Convert(paramExp[0], trueType); instanceExpr = trueType == paramExp[0].Type ? (Expression)paramExp[0] : Expression.Convert(paramExp[0], trueType);
} }
MemberExpression fieldExp = sourceField != null MemberExpression fieldExp = sourceField != null