//////////////// /// <summary> /// Loads a binary custom data JSON file of a given mod. /// </summary> /// <typeparam name="T">Object type to deserialize from JSON into.</typeparam> /// <param name="mod"></param> /// <param name="fileNameWithExt"></param> /// <param name="jsonSettings"></param> /// <returns></returns> public static T LoadBinaryJson <T>(Mod mod, string fileNameWithExt, JsonSerializerSettings jsonSettings) where T : class { ModCustomDataFileHelpers.PrepareDir(mod); try { string fullPath = ModCustomDataFileHelpers.GetFullPath(mod, fileNameWithExt); byte[] dataBytes = FileHelpers.LoadBinaryFile(fullPath, false); if (dataBytes == null) { return(null); } string dataJson = System.Text.Encoding.UTF8.GetString(dataBytes); if (dataBytes != null) { return(JsonConvert.DeserializeObject <T>(dataJson, jsonSettings)); } else { return(null); } } catch (IOException e) { string fullDir = ModCustomDataFileHelpers.GetFullDirectoryPath(mod); LogHelpers.Warn("Failed to load binary file " + fileNameWithExt + " at " + fullDir + " - " + e.ToString()); throw new IOException("Failed to load binary file " + fileNameWithExt + " at " + fullDir, e); } catch (Exception e) { throw new ModHelpersException("From " + fileNameWithExt + " (" + typeof(T).Name + ")", e); } }
/// <summary> /// Loads a custom data JSON file of a given mod. /// </summary> /// <typeparam name="T">Object type to deserialize from JSON into.</typeparam> /// <param name="mod"></param> /// <param name="fileNameNoExt"></param> /// <param name="jsonSettings"></param> /// <returns></returns> public static T LoadJson <T>(Mod mod, string fileNameNoExt, JsonSerializerSettings jsonSettings) where T : class { ModCustomDataFileHelpers.PrepareDir(mod); try { string fullPath = ModCustomDataFileHelpers.GetFullPath(mod, fileNameNoExt + ".json"); string dataStr = FileHelpers.LoadTextFile(fullPath, false); if (dataStr != null) { return(JsonConvert.DeserializeObject <T>(dataStr, jsonSettings)); } else { return(null); } } catch (IOException e) { string fullDir = ModCustomDataFileHelpers.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 ModHelpersException("From " + fileNameNoExt + " (" + typeof(T).Name + ")", e); } }
//////////////// private static void PrepareDir(Mod mod) { string fullDir = ModCustomDataFileHelpers.GetFullDirectoryPath(mod); try { Directory.CreateDirectory(Main.SavePath); Directory.CreateDirectory(Main.SavePath + Path.DirectorySeparatorChar + ModCustomDataFileHelpers.BaseFolder); Directory.CreateDirectory(fullDir); } catch (IOException e) { LogHelpers.Warn("Failed to prepare directory: " + fullDir + " - " + e.ToString()); throw new IOException("Failed to prepare directory: " + fullDir, e); } }
//////////////// /// <summary> /// Saves a custom mod data JSON file in binary form. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="mod"></param> /// <param name="fileNameWithExt"></param> /// <param name="jsonSettings"></param> /// <param name="overrides">Replaces any existing files.</param> /// <param name="data"></param> public static void SaveAsBinaryJson <T>(Mod mod, string fileNameWithExt, JsonSerializerSettings jsonSettings, bool overrides, T data) where T : class { ModCustomDataFileHelpers.PrepareDir(mod); try { string fullPath = ModCustomDataFileHelpers.GetFullPath(mod, fileNameWithExt); if (data == null) { LogHelpers.Warn("Failed to save binary file " + fullPath + " - Data is null."); return; } string dataJson = JsonConvert.SerializeObject(data, jsonSettings); byte[] dataBytes = System.Text.Encoding.UTF8.GetBytes(dataJson); FileHelpers.SaveBinaryFile(dataBytes, fullPath, false, !overrides); } catch (IOException e) { string fullDir = ModCustomDataFileHelpers.GetFullDirectoryPath(mod); LogHelpers.Warn("Failed to save binary file " + fileNameWithExt + " at " + fullDir + " - " + e.ToString()); throw new IOException("Failed to save binary file " + fileNameWithExt + " at " + fullDir, e); } }
/// <summary> /// Saves a custom mod data JSON file. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="mod"></param> /// <param name="fileNameNoExt"></param> /// <param name="jsonSettings"></param> /// <param name="overrides">Replaces any existing files.</param> /// <param name="data"></param> /// <returns></returns> public static bool SaveAsJson <T>(Mod mod, string fileNameNoExt, JsonSerializerSettings jsonSettings, bool overrides, T data) where T : class { ModCustomDataFileHelpers.PrepareDir(mod); string relDir = ModCustomDataFileHelpers.GetRelativeDirectoryPath(mod); if (data == null) { LogHelpers.Warn("Failed to save json file " + fileNameNoExt + " at " + relDir + " - Data is null."); return(false); } try { string fullPath = ModCustomDataFileHelpers.GetFullPath(mod, fileNameNoExt + ".json"); string dataJson = JsonConvert.SerializeObject(data, jsonSettings); return(FileHelpers.SaveTextFile(dataJson, fullPath, false, !overrides)); } 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); } }
/// <summary> /// Saves a custom mod data JSON file. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="mod"></param> /// <param name="fileNameNoExt"></param> /// <param name="overrides">Replaces any existing files.</param> /// <param name="data"></param> /// <returns></returns> public static bool SaveAsJson <T>(Mod mod, string fileNameNoExt, bool overrides, T data) where T : class { return(ModCustomDataFileHelpers.SaveAsJson <T>(mod, fileNameNoExt, new JsonSerializerSettings(), overrides, data)); }
/// <summary> /// Saves a custom mod data JSON file in binary form. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="mod"></param> /// <param name="fileNameWithExt"></param> /// <param name="overrides">Replaces any existing files.</param> /// <param name="data"></param> public static void SaveAsBinaryJson <T>(Mod mod, string fileNameWithExt, bool overrides, T data) where T : class { ModCustomDataFileHelpers.SaveAsBinaryJson <T>(mod, fileNameWithExt, new JsonSerializerSettings(), overrides, data); }
/// <summary> /// Gets the full system directory and path of a given custom data file of a mod. /// </summary> /// <param name="mod"></param> /// <param name="fileNameHasExt"></param> /// <returns></returns> public static string GetFullPath(Mod mod, string fileNameHasExt) { return(ModCustomDataFileHelpers.GetFullDirectoryPath(mod) + Path.DirectorySeparatorChar + fileNameHasExt); }
/// <summary> /// Gets the full system directory of a mod's custom data folder. /// </summary> /// <param name="mod"></param> /// <returns></returns> public static string GetFullDirectoryPath(Mod mod) { return(Main.SavePath + Path.DirectorySeparatorChar + ModCustomDataFileHelpers.GetRelativeDirectoryPath(mod)); }
/// <summary> /// Loads a custom data JSON file of a given mod. /// </summary> /// <typeparam name="T">Object type to deserialize from JSON into.</typeparam> /// <param name="mod"></param> /// <param name="fileNameNoExt"></param> /// <returns></returns> public static T LoadJson <T>(Mod mod, string fileNameNoExt) where T : class { return(ModCustomDataFileHelpers.LoadJson <T>(mod, fileNameNoExt, new JsonSerializerSettings())); }