public static PersistentXmlSettings LoadDefaultSettings() { var xmlSettings = new PersistentXmlSettings(); var settingsXmls = new List <LoadableXmlAsset>(); foreach (ModContentPack modContentPack in LoadedModManager.RunningMods) { settingsXmls.AddRange(DirectXmlLoader.XmlAssetsInModFolder(modContentPack, "XmlSettings/") .ToList()); } foreach (var asset in settingsXmls) { string modName; XmlModSettings modSettings; modName = asset.mod.Identifier; using (var reader = File.OpenText(asset.FullFilePath)) { modSettings = XmlSettingsSerializer.DeserializeModSettings(reader); } xmlSettings.ModSettings[modName] = modSettings; } return(xmlSettings); }
public static void SaveUserSettings(PersistentXmlSettings settings) { try { using (var fileStream = File.OpenWrite(UserSettingsLocation)) using (var textWriter = new StreamWriter(fileStream)) { settings.Serialize(textWriter); } } catch (Exception ex) { Log.Error("Exception saving prefs: " + ex); } }
public static void Serialize(this PersistentXmlSettings settings, TextWriter textWriter) { try { var doc = new XmlDocument(); var rootElement = doc.CreateElement("RimXmlSettings"); doc.AppendChild(rootElement); var settingsVersionNode = doc.CreateElement("SettingsVersion"); settingsVersionNode.InnerText = settings.XmlSettingsVersion.ToString(); rootElement.AppendChild(settingsVersionNode); var modSettingsNode = doc.CreateElement("ModSettings"); foreach (var modKey in settings.ModSettings.Keys) { var modSettings = settings.ModSettings[modKey]; var modNode = doc.CreateElement("ModSettings"); modNode.SetAttribute("Key", modKey); var versionNode = doc.CreateElement("Version"); versionNode.InnerText = modSettings.ModVersion.ToString(); modNode.AppendChild(versionNode); var propertiesNode = doc.CreateElement("Properties"); foreach (var propertyKey in modSettings.Properties.Keys) { AddPropertyNode(doc, propertiesNode, modSettings.Properties[propertyKey], propertyKey); } modNode.AppendChild(propertiesNode); modSettingsNode.AppendChild(modNode); } rootElement.AppendChild(modSettingsNode); using (var xmlWriter = new XmlTextWriter(textWriter)) { doc.WriteTo(xmlWriter); } } catch (Exception ex) { throw new Exception("Coulnd't serialize XmlSettings", ex); } }
public static PersistentXmlSettings Deserialize(TextReader textReader) { try { PersistentXmlSettings xmlSettings = new PersistentXmlSettings(); var doc = new XmlDocument(); doc.Load(textReader); var rootElement = doc.DocumentElement; if (rootElement.Name != "RimXmlSettings") { throw new Exception("Invalid root element"); } if (!rootElement.HasChildNodes) { throw new Exception("RimXmlSettings has no children"); } var versionNode = rootElement["SettingsVersion"]; xmlSettings.XmlSettingsVersion = new Version(versionNode.InnerText); var modSettingsNode = rootElement["ModSettings"]; foreach (XmlElement modNode in modSettingsNode.ChildNodes) { var modSettings = DeserializeModSettingsCore(modNode); xmlSettings.ModSettings.Add(modNode.GetAttribute("Key"), modSettings); } return(xmlSettings); } catch (Exception ex) { throw new Exception("Coulnd't deserialize XmlSettings", ex); } }