public void SaveConfig() { var rootEl = new XElement("bot"); BotModule.SaveProperties(this, rootEl); _startup.ForEach((startup) => { var startupEl = new XElement("startup", startup); rootEl.Add(startupEl); }); _modules.ForEach((module) => { var moduleEl = new XElement("module", new XAttribute("type", module.GetType().FullName)); module.DoSaveConfig(moduleEl); rootEl.Add(moduleEl); }); var doc = new XDocument(rootEl); try { doc.Save(_configPath, SaveOptions.None); } catch (Exception ex) { throw new BotConfigException(ex); } }
protected override void LoadConfig(XElement moduleEl) { base.LoadConfig(moduleEl); foreach (var feedEl in moduleEl.Elements("feed")) { var feed = new RssFeed(); BotModule.LoadProperties(feed, feedEl); if (_feeds.ContainsKey(feed.Name)) { throw new BotConfigException(string.Format( "An RSS feed with the name {0} already exists.", feed.Name)); } try { feed.CatchUp(); _feeds.Add(feed.Name, feed); } catch (Exception ex) { throw new BotConfigException(string.Format( "An RSS feed with the name {0} could not be loaded: {1}", feed.Name, ex.Message)); } } }
public static void SaveProperties(object obj, XElement configEl) { foreach (var property in obj.GetType().GetProperties( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { var attr = property.GetCustomAttributes( typeof(ModulePropertyAttribute), true).OfType <ModulePropertyAttribute>().FirstOrDefault(); if (attr != null) { try { object val = property.GetValue(obj, null); if (val != null) { var converter = BotModule.GetTypeConverter(property); configEl.Add(new XAttribute(attr.ConfigName, converter.ConvertToString(val))); } } catch (Exception ex) { throw new BotConfigException(string.Format( "Could not save configuration property {0}", attr.ConfigName), ex); } } } }
public static void LoadProperties(object obj, XElement configEl) { foreach (var property in obj.GetType().GetProperties( BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { var attr = property.GetCustomAttributes( typeof(ModulePropertyAttribute), true).OfType <ModulePropertyAttribute>().FirstOrDefault(); if (attr != null) { var configAt = configEl.Attribute(attr.ConfigName); if (configAt != null) { try { var converter = BotModule.GetTypeConverter(property); property.SetValue(obj, converter.ConvertFromString(configAt.Value), null); } catch (Exception ex) { throw new BotConfigException(string.Format( "Could not load configuration property {0}", attr.ConfigName), ex); } } } } }
public void LoadConfig() { if (string.IsNullOrEmpty(_configPath)) { throw new BotConfigException("No configuration path was provided."); } XDocument doc; try { doc = XDocument.Load(_configPath); } catch (Exception ex) { throw new BotConfigException(ex); } var root = doc.Root; if (root.Name != "bot") { throw new BotConfigException("Missing 'bot' element."); } BotModule.LoadProperties(this, root); foreach (var startupEl in root.Elements("startup")) { _startup.Add(startupEl.Value); } foreach (var moduleEl in root.Elements("module")) { this.LoadModule(moduleEl); } this.Irc = new IrcSession(); BotModule.LoadProperties(this.Irc, root); this.Security = this.GetModule(typeof(Security)) as Security; if (this.Security == null) { throw new InvalidOperationException("There must be a configured Security module."); } this.Aliases = this.GetModule(typeof(Aliases)) as Aliases; if (this.Aliases == null) { throw new InvalidOperationException("There must be a configured Aliases module."); } this.Channels = this.GetModule(typeof(Channels)) as Channels; if (this.Channels == null) { throw new InvalidOperationException("There must be a configured Channels module."); } }
protected override void SaveConfig(System.Xml.Linq.XElement moduleEl) { base.SaveConfig(moduleEl); foreach (var user in _users) { var userEl = new XElement("user"); BotModule.SaveProperties(user, userEl); moduleEl.Add(userEl); } }
protected override void SaveConfig(XElement moduleEl) { base.SaveConfig(moduleEl); foreach (var feed in _feeds.Values) { var feedEl = new XElement("feed"); BotModule.SaveProperties(feed, feedEl); moduleEl.Add(feedEl); } }
protected override void LoadConfig(System.Xml.Linq.XElement moduleEl) { base.LoadConfig(moduleEl); foreach (var userEl in moduleEl.Elements("user")) { var user = new User(); BotModule.LoadProperties(user, userEl); if (_userIndex.ContainsKey(user.Name)) { throw new BotConfigException(string.Format( "There are multiple users with the name {0}.", user.Name)); } _users.Add(user); _userIndex.Add(user.Name, user); } }
protected virtual void SaveConfig(XElement moduleEl) { BotModule.SaveProperties(this, moduleEl); }
protected virtual void LoadConfig(XElement moduleEl) { BotModule.LoadProperties(this, moduleEl); }