internal static void serializeToJson() { try { string path = Path.Combine(modDir, settings.saveFolder, "WIIC_systemControl.json"); using (StreamWriter writer = new StreamWriter(path, false)) { GalaxyData data = new GalaxyData(systemControl, flareups); writer.Write(JsonConvert.SerializeObject(data, Formatting.Indented)); writer.Flush(); } modLog.Info?.Write($"Wrote {path} with {systemControl.Count} control tags"); } catch (Exception e) { modLog.Error?.Write(e); } }
internal static void readFromJson(string filename, bool deleteAfterImport) { try { string path = Path.Combine(modDir, filename); if (!File.Exists(path)) { modLog.Info?.Write($"No {path} found, doing nothing"); return; } using (StreamReader reader = new StreamReader(path)) { modLog.Info?.Write($"Reading GalaxyData from {path}"); GalaxyData data; string jdata = reader.ReadToEnd(); // Current serialization format data = JsonConvert.DeserializeObject <GalaxyData>(jdata); if (data.systemControl == null) { modLog.Info?.Write($"Failed to read GalaxyData, attempting to interpret as old format"); // Older serialization format, for backwards compatibility Dictionary <string, string> control = JsonConvert.DeserializeObject <Dictionary <string, string> >(jdata); data = new GalaxyData(control, new Dictionary <string, Flareup>()); } data.apply(); if (deleteAfterImport) { modLog.Info?.Write($"Deleting {path}"); File.Delete(path); } } } catch (Exception e) { modLog.Error?.Write(e); } }