private static void writeInstalledModToJson(ModFile[] files) { var modexport = new InstalledModsFormat(); modexport.InstalledMods = files; //drops file into installedmodsformat File.WriteAllText(Directory.GetCurrentDirectory() + @"\installedmods.json", JsonConvert.SerializeObject(modexport)); //serialize and write to file }
/// <summary> /// Gets the currently installed mods from the JSON files /// </summary> /// <returns>String array with the installed mods</returns> public static ModFile[] GetInstalledMods() { if (!File.Exists(Directory.GetCurrentDirectory() + @"\installedmods.json")) { return(new ModFile[0]); } Console.WriteLine("Reading from " + Directory.GetCurrentDirectory() + @"\installedmods.json"); InstalledModsFormat input = null; DeprecatedInstalledModsFormat depinput = null; try { input = JsonConvert.DeserializeObject <InstalledModsFormat>( File.ReadAllText(Directory.GetCurrentDirectory() + "/installedmods.json")); } catch { //this here tries to convert the json file into a new file. If it doesn't work, it just deletes and starts over. try { depinput = JsonConvert.DeserializeObject <DeprecatedInstalledModsFormat>( File.ReadAllText(Directory.GetCurrentDirectory() + "/installedmods.json")); File.Delete(Directory.GetCurrentDirectory() + @"\installedmods.json"); Console.WriteLine("installedmods.json is in a deprecated format, converting!"); for (var i = 0; i < depinput.InstalledMods.Length; i++) { AddInstalledMod(depinput.InstalledMods[i]); } } catch { Console.WriteLine("installedmods.json isn't formatted correctly, deleting!"); File.Delete(Directory.GetCurrentDirectory() + @"\installedmods.json"); } } if (input == null) { return(new ModFile[0]); } return(input.InstalledMods); }