/** * Load data for an object's ParserTarget fields and properties from a configuration node * * @param o Object for which to load data. Needs to be instatiated object * @param node Configuration node from which to load data **/ public static void LoadObjectFromConfigurationNode(object o, ConfigNode node, bool getChilds = true) { // Get the object as a parser event subscriber (will be null if 'o' does not conform) IParserEventSubscriber subscriber = o as IParserEventSubscriber; // Generate two lists -> those tagged preapply and those not List <KeyValuePair <bool, MemberInfo> > preapplyMembers = new List <KeyValuePair <bool, MemberInfo> > (); List <KeyValuePair <bool, MemberInfo> > postapplyMembers = new List <KeyValuePair <bool, MemberInfo> > (); // Discover members tagged with parser attributes foreach (MemberInfo member in o.GetType().GetMembers(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)) { // Is this member a parser target? ParserTarget[] attributes = member.GetCustomAttributes(typeof(ParserTarget), true) as ParserTarget[]; if (attributes.Length > 0) { // If this member is a collection bool isCollection = attributes[0].GetType().Equals(typeof(ParserTargetCollection)); KeyValuePair <bool, MemberInfo> entry = new KeyValuePair <bool, MemberInfo>(isCollection, member); // If this member has the preapply attribute, we need to process it if (member.GetCustomAttributes(typeof(PreApply), true).Length > 0) { preapplyMembers.Add(entry); } else { postapplyMembers.Add(entry); } } } // Process the preapply members foreach (KeyValuePair <bool, MemberInfo> member in preapplyMembers) { if (member.Key) { LoadCollectionMemberFromConfigurationNode(member.Value, o, node); } else { LoadObjectMemberFromConfigurationNode(member.Value, o, node); } } // Call Apply if (subscriber != null) { subscriber.Apply(node); } // Process the postapply members foreach (KeyValuePair <bool, MemberInfo> member in postapplyMembers) { if (member.Key) { LoadCollectionMemberFromConfigurationNode(member.Value, o, node); } else { LoadObjectMemberFromConfigurationNode(member.Value, o, node); } } // Call PostApply if (subscriber != null) { subscriber.PostApply(node); } }
/// <summary> /// Load data for an object's ParserTarget fields and properties from a configuration node /// </summary> /// <param name="o">Object for which to load data. Needs to be instantiated object</param> /// <param name="node">Configuration node from which to load data</param> /// <param name="configName">The name of the mod that corresponds to the entry in ParserOptions</param> /// <param name="getChildren">Whether getters on the object should get called</param> public static void LoadObjectFromConfigurationNode(Object o, ConfigNode node, String configName = "Default", Boolean getChildren = true) { // Get the object as a parser event subscriber (will be null if 'o' does not conform) IParserEventSubscriber subscriber = o as IParserEventSubscriber; IParserApplyEventSubscriber applySubscriber = o as IParserApplyEventSubscriber; IParserPostApplyEventSubscriber postApplySubscriber = o as IParserPostApplyEventSubscriber; // Generate two lists -> those tagged preApply and those not List <KeyValuePair <Boolean, MemberInfo> > preApplyMembers = new List <KeyValuePair <Boolean, MemberInfo> >(); List <KeyValuePair <Boolean, MemberInfo> > postApplyMembers = new List <KeyValuePair <Boolean, MemberInfo> >(); // Discover members tagged with parser attributes foreach (MemberInfo member in o.GetType() .GetMembers(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)) { // Is this member a parser target? ParserTarget[] attributes = (ParserTarget[])member.GetCustomAttributes(typeof(ParserTarget), true); if (attributes.Length <= 0) { continue; } // If this member is a collection Boolean isCollection = attributes[0].GetType() == typeof(ParserTargetCollection); // If this member has the preApply attribute, we need to process it if (member.GetCustomAttributes(typeof(PreApply), true).Length > 0) { preApplyMembers.Add(new KeyValuePair <Boolean, MemberInfo>(isCollection, member)); } else { postApplyMembers.Add(new KeyValuePair <Boolean, MemberInfo>(isCollection, member)); } } // Process the preApply members foreach (KeyValuePair <Boolean, MemberInfo> member in preApplyMembers) { if (member.Key) { LoadCollectionMemberFromConfigurationNode(member.Value, o, node, configName, getChildren); } else { LoadObjectMemberFromConfigurationNode(member.Value, o, node, configName, getChildren); } } // Call Apply applySubscriber?.Apply(node); subscriber?.Apply(node); // Process the postApply members foreach (KeyValuePair <Boolean, MemberInfo> member in postApplyMembers) { if (member.Key) { LoadCollectionMemberFromConfigurationNode(member.Value, o, node, configName, getChildren); } else { LoadObjectMemberFromConfigurationNode(member.Value, o, node, configName, getChildren); } } // Call PostApply postApplySubscriber?.PostApply(node); subscriber?.PostApply(node); }