//////////////// public static void SaveAsJson <T>(Mod mod, string fileNameNoExt, JsonSerializerSettings jsonSettings, T data) where T : class { DataFileHelpers.PrepareDir(mod); string relDir = DataFileHelpers.GetRelativeDirectoryPath(mod); try { var jsonFile = new JsonConfig <T>(fileNameNoExt + ".json", relDir, data, jsonSettings); jsonFile.SaveFile(); } catch (IOException e) { LogHelpers.Warn("Failed to save json file " + fileNameNoExt + " at " + relDir + " - " + e.ToString()); throw new IOException("Failed to save json file " + fileNameNoExt + " at " + relDir, e); } }
//////////////// public static T LoadJson <T>(Mod mod, string fileNameNoExt, JsonSerializerSettings jsonSettings, out bool success) where T : class { DataFileHelpers.PrepareDir(mod); string relDir = DataFileHelpers.GetRelativeDirectoryPath(mod); success = false; try { var jsonFile = new JsonConfig <T>(fileNameNoExt + ".json", relDir, jsonSettings); success = jsonFile.LoadFile(); return(jsonFile.Data); } catch (IOException e) { string fullDir = DataFileHelpers.GetFullDirectoryPath(mod); LogHelpers.Warn("Failed to load json file " + fileNameNoExt + " at " + fullDir + " - " + e.ToString()); throw new IOException("Failed to load json file " + fileNameNoExt + " at " + fullDir, e); } catch (Exception e) { throw new HamstarException("From " + fileNameNoExt + " (" + typeof(T).Name + ")", e); } }
public static string GetFullDirectoryPath(Mod mod) { return(Main.SavePath + Path.DirectorySeparatorChar + DataFileHelpers.GetRelativeDirectoryPath(mod)); }