Example #1
0
 public static void SaveDataObject(object obj, string filePath)
 {
     try
     {
         XDocument xDocument = new XDocument();
         XElement  content   = DirectXmlSaver.XElementFromObject(obj, obj.GetType());
         xDocument.Add(content);
         xDocument.Save(filePath);
     }
     catch (Exception ex)
     {
         Log.Error(string.Concat(new object[]
         {
             "Exception saving data object ",
             obj.ToStringSafe <object>(),
             ": ",
             ex
         }), false);
         GenUI.ErrorDialog("ProblemSavingFile".Translate(new object[]
         {
             filePath,
             ex.ToString()
         }));
     }
 }
Example #2
0
 public static void Save()
 {
     try
     {
         Dictionary <string, KeyBindingData> dictionary = new Dictionary <string, KeyBindingData>();
         foreach (KeyValuePair <KeyBindingDef, KeyBindingData> current in KeyPrefs.data.keyPrefs)
         {
             dictionary[current.Key.defName] = current.Value;
         }
         foreach (KeyValuePair <string, KeyBindingData> current2 in KeyPrefs.unresolvedBindings)
         {
             try
             {
                 dictionary.Add(current2.Key, current2.Value);
             }
             catch (ArgumentException)
             {
             }
         }
         XDocument xDocument = new XDocument();
         XElement  content   = DirectXmlSaver.XElementFromObject(dictionary, typeof(KeyPrefsData));
         xDocument.Add(content);
         xDocument.Save(GenFilePaths.KeyPrefsFilePath);
     }
     catch (Exception ex)
     {
         GenUI.ErrorDialog("ProblemSavingFile".Translate(new object[]
         {
             GenFilePaths.KeyPrefsFilePath,
             ex.ToString()
         }));
         Log.Error("Exception saving keyprefs: " + ex, false);
     }
 }
        public void SaveIn(ModContentPack mod)
        {
            string    fullFolderPath = this.GetFullFolderPath(mod);
            string    str            = Path.Combine(fullFolderPath, this.fileName);
            XDocument xDocument      = new XDocument();
            XElement  xElement       = new XElement("DefPackage");

            xDocument.Add(xElement);
            try
            {
                foreach (Def def in this.defs)
                {
                    XElement content = DirectXmlSaver.XElementFromObject(def, def.GetType());
                    xElement.Add(content);
                }
                DirectXmlSaveFormatter.AddWhitespaceFromRoot(xElement);
                SaveOptions options = SaveOptions.DisableFormatting;
                xDocument.Save(str, options);
                Messages.Message("Saved in " + str, MessageTypeDefOf.PositiveEvent);
            }
            catch (Exception ex)
            {
                Messages.Message("Exception saving XML: " + ex.ToString(), MessageTypeDefOf.NegativeEvent);
                throw;
            }
        }
Example #4
0
 public static void SaveFromList(List <string> mods)
 {
     DirectXmlSaver.SaveDataObject(new ModsConfigData
     {
         version         = VersionControl.CurrentVersionStringWithRev,
         activeMods      = mods,
         knownExpansions = data.knownExpansions
     }, GenFilePaths.ModsConfigFilePath);
 }
Example #5
0
        private static XElement XElementFromField(FieldInfo fi, object owningObj)
        {
            if (Attribute.IsDefined(fi, typeof(UnsavedAttribute)))
            {
                return(null);
            }
            object value = fi.GetValue(owningObj);

            return(DirectXmlSaver.XElementFromObject(value, fi.FieldType, fi.Name, fi, false));
        }
Example #6
0
 public static void Save()
 {
     try
     {
         XDocument xDocument = new XDocument();
         XElement  content   = DirectXmlSaver.XElementFromObject(Prefs.data, typeof(PrefsData));
         xDocument.Add(content);
         xDocument.Save(GenFilePaths.PrefsFilePath);
     }
     catch (Exception ex)
     {
         GenUI.ErrorDialog("ProblemSavingFile".Translate(GenFilePaths.PrefsFilePath, ex.ToString()));
         Log.Error("Exception saving prefs: " + ex, false);
     }
 }
Example #7
0
 public static void SaveDataObject(object obj, string filePath)
 {
     try
     {
         XDocument xDocument = new XDocument();
         XElement  content   = DirectXmlSaver.XElementFromObject(obj, obj.GetType());
         xDocument.Add(content);
         xDocument.Save(filePath);
     }
     catch (Exception ex)
     {
         GenUI.ErrorDialog("ProblemSavingFile".Translate(filePath, ex.ToString()));
         Log.Error("Exception saving data object " + obj + ": " + ex);
     }
 }
Example #8
0
 public static void Save()
 {
     DirectXmlSaver.SaveDataObject(ModsConfig.data, GenFilePaths.ModsConfigFilePath);
 }
Example #9
0
        public static XElement XElementFromObject(object obj, Type expectedType, string nodeName, FieldInfo owningField = null, bool saveDefsAsRefs = false)
        {
            DefaultValueAttribute defaultValueAttribute;

            if (owningField != null && owningField.TryGetAttribute(out defaultValueAttribute) && defaultValueAttribute.ObjIsDefault(obj))
            {
                return(null);
            }
            if (obj == null)
            {
                XElement xElement = new XElement(nodeName);
                xElement.SetAttributeValue("IsNull", "True");
                return(xElement);
            }
            Type     type      = obj.GetType();
            XElement xElement2 = new XElement(nodeName);

            if (DirectXmlSaver.IsSimpleTextType(type))
            {
                xElement2.Add(new XText(obj.ToString()));
            }
            else if (saveDefsAsRefs && typeof(Def).IsAssignableFrom(type))
            {
                string defName = ((Def)obj).defName;
                xElement2.Add(new XText(defName));
            }
            else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List <>))
            {
                Type expectedType2 = type.GetGenericArguments()[0];
                int  num           = (int)type.GetProperty("Count").GetValue(obj, null);
                for (int i = 0; i < num; i++)
                {
                    object[] index = new object[]
                    {
                        i
                    };
                    object value   = type.GetProperty("Item").GetValue(obj, index);
                    XNode  content = DirectXmlSaver.XElementFromObject(value, expectedType2, "li", null, true);
                    xElement2.Add(content);
                }
            }
            else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary <, >))
            {
                Type expectedType3 = type.GetGenericArguments()[0];
                Type expectedType4 = type.GetGenericArguments()[1];
                foreach (object current in (obj as IEnumerable))
                {
                    object   value2    = current.GetType().GetProperty("Key").GetValue(current, null);
                    object   value3    = current.GetType().GetProperty("Value").GetValue(current, null);
                    XElement xElement3 = new XElement("li");
                    xElement3.Add(DirectXmlSaver.XElementFromObject(value2, expectedType3, "key", null, true));
                    xElement3.Add(DirectXmlSaver.XElementFromObject(value3, expectedType4, "value", null, true));
                    xElement2.Add(xElement3);
                }
            }
            else
            {
                if (type != expectedType)
                {
                    XAttribute content2 = new XAttribute("Class", GenTypes.GetTypeNameWithoutIgnoredNamespaces(obj.GetType()));
                    xElement2.Add(content2);
                }
                foreach (FieldInfo current2 in from f in type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)
                         orderby f.MetadataToken
                         select f)
                {
                    try
                    {
                        XElement xElement4 = DirectXmlSaver.XElementFromField(current2, obj);
                        if (xElement4 != null)
                        {
                            xElement2.Add(xElement4);
                        }
                    }
                    catch
                    {
                        throw;
                    }
                }
            }
            return(xElement2);
        }
Example #10
0
 public static XElement XElementFromObject(object obj, Type expectedClass)
 {
     return(DirectXmlSaver.XElementFromObject(obj, expectedClass, expectedClass.Name, null, false));
 }
Example #11
0
 public static void Save()
 {
     data.version = VersionControl.CurrentVersionStringWithRev;
     DirectXmlSaver.SaveDataObject(data, GenFilePaths.ModsConfigFilePath);
 }