private void LoadFromToml(string path) { LoadDefaultConfiguration(); string doc = System.IO.File.ReadAllText(path); DocumentSyntax docSyn = Toml.Parse(doc); TomlTable table = docSyn.ToModel(); foreach (var prop in this.GetType().GetProperties()) { string _table = prop.Name.Split("_")[0]; string _prop = prop.Name.Split("_")[1]; try { if (prop.GetType() == typeof(bool)) { bool val = (bool)((TomlTable)table[_table])[_prop]; prop.SetValue(this, val); } else { string val = (string)((TomlTable)table[_table])[_prop]; prop.SetValue(this, val); } } catch (Exception e) { // Do nothing. } } }
public static T ToModel <T>(this DocumentSyntax documentSyntax) where T : new() { T instance = new T(); TomlTable model = documentSyntax.ToModel(); foreach (PropertyInfo property in instance.GetType().GetProperties()) { TomlPropertyAttribute?attribute = property.GetCustomAttribute <TomlPropertyAttribute>(); if (attribute is null) { continue; } if (attribute.Header is null) { if (attribute.Required && !model.ContainsKey(property.Name)) { throw new Exception($"Toml file does not have required property '{property.Name}'."); } property.SetValue(instance, Convert.ChangeType(model[property.Name], property.PropertyType)); } else { if ((attribute.Required && !model.ContainsKey(attribute.Header)) || !((TomlTable)model[attribute.Header]).ContainsKey(property.Name)) { throw new Exception($"Toml file does not have required property '{property.Name}'."); } property.SetValue(instance, Convert.ChangeType(((TomlTable)model[attribute.Header])[property.Name], property.PropertyType)); } } return(instance); }
private AppConfig(DocumentSyntax validatedSyntaxTree) { _config = validatedSyntaxTree.ToModel(); }
private static void DowngradeMelonPreferences(string destination, bool legacy_version) { if (!legacy_version || (Program.CurrentInstalledVersion == null) || (Program.CurrentInstalledVersion.CompareTo(new Version("0.3.0")) < 0)) { return; } string userdatapath = Path.Combine(destination, "UserData"); if (!Directory.Exists(userdatapath)) { return; } string oldfilepath = Path.Combine(userdatapath, "MelonPreferences.cfg"); if (!File.Exists(oldfilepath)) { return; } string filestr = File.ReadAllText(oldfilepath); if (string.IsNullOrEmpty(filestr)) { return; } DocumentSyntax docsyn = Toml.Parse(filestr); if (docsyn == null) { return; } TomlTable model = docsyn.ToModel(); if (model.Count <= 0) { return; } string newfilepath = Path.Combine(userdatapath, "modprefs.ini"); if (File.Exists(newfilepath)) { File.Delete(newfilepath); } IniFile iniFile = new IniFile(newfilepath); foreach (KeyValuePair <string, object> keypair in model) { string category_name = keypair.Key; TomlTable tbl = (TomlTable)keypair.Value; if (tbl.Count <= 0) { continue; } foreach (KeyValuePair <string, object> tblkeypair in tbl) { string name = tblkeypair.Key; if (string.IsNullOrEmpty(name)) { continue; } TomlObject obj = TomlObject.ToTomlObject(tblkeypair.Value); if (obj == null) { continue; } switch (obj.Kind) { case ObjectKind.String: iniFile.SetString(category_name, name, ((TomlString)obj).Value); break; case ObjectKind.Boolean: iniFile.SetBool(category_name, name, ((TomlBoolean)obj).Value); break; case ObjectKind.Integer: iniFile.SetInt(category_name, name, (int)((TomlInteger)obj).Value); break; case ObjectKind.Float: iniFile.SetFloat(category_name, name, (float)((TomlFloat)obj).Value); break; default: break; } } } File.Delete(oldfilepath); }
static Config() { FilePath = Path.Combine(Core.BasePath, "Config.cfg"); if (!File.Exists(FilePath)) { Save(); return; } string filestr = File.ReadAllText(FilePath); if (string.IsNullOrEmpty(filestr)) { return; } DocumentSyntax docsyn = Toml.Parse(filestr); if (docsyn == null) { return; } TomlTable model = docsyn.ToModel(); if (model.Count <= 0) { return; } TomlTable tbl = (TomlTable)model["AssemblyGenerator"]; if (tbl == null) { return; } if (tbl.ContainsKey("UnityVersion")) { UnityVersion = (string)tbl["UnityVersion"]; } if (tbl.ContainsKey("Cpp2IL")) { Cpp2ILVersion = (string)tbl["Cpp2IL"]; } if (tbl.ContainsKey("Il2CppAssemblyUnhollower")) { Il2CppAssemblyUnhollowerVersion = (string)tbl["Il2CppAssemblyUnhollower"]; } if (tbl.ContainsKey("GameAssemblyHash")) { GameAssemblyHash = (string)tbl["GameAssemblyHash"]; } if (!tbl.ContainsKey("OldFiles")) { return; } TomlArray oldfilesarr = (TomlArray)tbl["OldFiles"]; if (oldfilesarr.Count <= 0) { return; } for (int i = 0; i < oldfilesarr.Count; i++) { string file = (string)oldfilesarr[i]; if (!string.IsNullOrEmpty(file)) { OldFiles.Add(file); } } }