/// <inheritdoc/> protected override void OnSetup(Type type, ConfigValueAttribute attribute) { ConfigNodeAttribute node = type.GetCustomAttribute <ConfigNodeAttribute>(); Id = node?.Id ?? type.Name; // there should only be 1 root node for one type if (node != null) { IsRootNode = node.IsRoot; ShouldSave = node.ShouldSave; if (node.IsRoot) { AllowMultiple = node.AllowMultiple; } } // if this is a member of some type if (attribute != null) { FARLogger.AssertFormat(!string.IsNullOrEmpty(attribute.Name), "Nested nodes required ConfigValue.Name to be set"); Name = attribute.Name; } // get all public fields var fields = new HashSet <FieldInfo>(type.GetFields(PublicFlags)); // and add all the other fields that declare ConfigValueAttribute fields.UnionWith(type.GetFieldsWithAttribute <ConfigValueAttribute>(flags: AllFlags) .Select(pair => pair.Second)); // only get properties that declare ConfigValueAttribute var properties = new List <PropertyInfo>(type.GetPropertiesWithAttribute <ConfigValueAttribute>(AllFlags) .Select(pair => pair.Second)); FARLogger.TraceFormat("Found {0} fields and {1} properties in {2}", fields.Count.ToString(), properties.Count.ToString(), type); foreach (FieldInfo fi in fields) { SetupType(fi, fi.FieldType); } foreach (PropertyInfo pi in properties) { SetupType(pi, pi.PropertyType); } if (!Children.TryGetValue(type, out List <Type> list)) { return; } foreach (Type subnode in list) { SetupType(null, subnode); } }
public void Initialize(bool force = false) { if (initialized && !force) { return; } try { var allNodes = new List <Pair <ConfigNodeAttribute, Type> >(ReflectionUtils .FindAttribute <ConfigNodeAttribute>(false)); foreach (Pair <ConfigNodeAttribute, Type> pair in allNodes) { if (!pair.First.IsRoot) { continue; } var node = NodeReflection.GetReflection(pair.Second); nodes.Add(node.Id, new ReflectedConfig { Instance = ReflectionUtils.FindInstance(pair.Second), Reflection = node }); } FARLogger.TraceFormat("Config nodes found: {0}", string.Join(", ", nodes.Select(p => p.Key))); } finally { initialized = true; } }
private void SetupType(MemberInfo mi, Type memberType) { // if ignored, nothing to do if (mi?.GetCustomAttribute <ConfigValueIgnoreAttribute>() != null) { return; } // check if the type is a node and contains ConfigValueAttribute ConfigNodeAttribute node = memberType.GetCustomAttribute <ConfigNodeAttribute>(); ConfigValueAttribute value = mi?.GetCustomAttribute <ConfigValueAttribute>(); // try to get the list value type Type listValueType = ReflectionUtils.ListType(ReflectionUtils.ConfigValueType(memberType) ?? memberType); if (listValueType != null) { // is a list var reflection = ListValueReflection.Create(mi, value, listValueType); ListValues.Add(reflection); FARLogger.TraceFormat("Added list value '{1} -> <{0}, {2}>'", reflection.Name ?? "{null}", reflection.NodeId ?? "{null}", reflection.ValueType); } else if (node == null) { // not a node or a list -> simple value ValueReflection reflection = Create(mi, value); Values.Add(reflection); FARLogger.TraceFormat("Added value '{0} -> {1}'", reflection.Name, reflection.ValueType); } else { // ConfigValue name string name = value?.Name; // get clone or create new reflection for the type NodeReflection nodeReflection = GetReflection(memberType, true, name, mi); Nodes.Add(nodeReflection); FARLogger.TraceFormat("Added node '{1} -> <{0}, {2}>'", name ?? "{null}", nodeReflection.Id, nodeReflection.ValueType); } }
private object FindOrMakeInstance() { // handle static classes if (ValueType.IsStatic()) { return(null); } object instance = ReflectionUtils.FindInstance(ValueType); if (instance != null) { return(instance); } FARLogger.TraceFormat("Instance of {0} was not provided and could not find one. Creating one", ValueType); instance = ReflectionUtils.CreateInstance(ValueType); FARLogger.Assert(instance != null, "Could not create instance"); return(instance); }
private int Apply(ref object owner, NodeVisitor visitor) { int count = 0; FARLogger.TraceFormat("Applying visitor to config node {0}[{1}]", Id, Name ?? "{null}"); foreach (ValueReflection value in Values) { FARLogger.TraceFormat("Visiting value {0}[{1}].{2}", Id, Name, value.Name); try { visitor.VisitValue(owner, value); } catch (Exception e) { FARLogger.ExceptionFormat(e, "Exception loading value {0} in {1}", value.Name, value.DeclaringType); count++; } } foreach (ListValueReflection reflection in ListValues) { if (reflection.IsNodeValue) { NodeReflection nodeReflection = GetReflection(reflection.ValueType); FARLogger.TraceFormat("Visiting list nodes {0}[{1}].{2}[{3}]", Id, Name ?? "{null}", reflection.NodeId, reflection.Name ?? "{null}"); try { visitor.VisitNodeList(owner, reflection, nodeReflection); } catch (Exception e) { FARLogger.ExceptionFormat(e, "Exception loading node ({2}) list {0} in {1}", reflection.Name, reflection.DeclaringType, reflection.NodeId); count++; } } else { FARLogger.TraceFormat("Visiting list values {0}[{1}].{2}", Id, Name ?? "{null}", reflection.Name); try { visitor.VisitValueList(owner, reflection); } catch (Exception e) { FARLogger.ExceptionFormat(e, "Exception loading value list {0} in {1}", reflection.Name, reflection.DeclaringType); count++; } } } foreach (NodeReflection node in Nodes) { FARLogger.TraceFormat("Visiting subnode {0}[{1}].{2}[{3}]", Id, Name ?? "{null}", node.Id, node.Name ?? "{null}"); try { visitor.VisitNode(owner, node); } catch (Exception e) { FARLogger.ExceptionFormat(e, "Exception loading node {2}[{0}] in {1}", node.Name, node.DeclaringType, node.Id); count++; } } return(count); }
private void OnLoaderChanged(string name) { FARLogger.TraceFormat("Asset '{0}' loader has changed: '{1}' -> {2}", Key, Loader.Name, name); Loader = AssetLoaders.Get(name); Load(); }
private void OnUrlChanged(string url) { FARLogger.TraceFormat("Asset '{0}' url has changed: '{1}' -> {2}", Key, Url, url); Url = url; Load(); }