示例#1
0
        ////////////////

        /// <summary>
        /// Saves a custom mod data JSON file in binary form.
        /// </summary>
        /// <param name="mod"></param>
        /// <param name="fileNameWithExt"></param>
        /// <param name="overrides">Replaces any existing files.</param>
        /// <param name="data"></param>
        public static void SaveAsBinary(
            Mod mod,
            string fileNameWithExt,
            bool overrides,
            byte[] data)
        {
            if (data == null)
            {
                string fullPath = ModCustomDataFileLibraries.GetFullPath(mod, fileNameWithExt);
                LogLibraries.Warn("Failed to save binary file " + fullPath + " - Data is null.");
                return;
            }

            try {
                ModCustomDataFileLibraries.PrepareDir(mod);

                string fullPath = ModCustomDataFileLibraries.GetFullPath(mod, fileNameWithExt);

                FileLibraries.SaveBinaryFile(data, fullPath, false, !overrides);
            } catch (IOException e) {
                string fullDir = ModCustomDataFileLibraries.GetFullDirectoryPath(mod);
                LogLibraries.Warn("Failed to save binary file " + fileNameWithExt + " at " + fullDir + " - " + e.ToString());
                throw new IOException("Failed to save binary file " + fileNameWithExt + " at " + fullDir, e);
            }
        }
示例#2
0
        ////////////////

        /// <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
        {
            if (data == null)
            {
                string fullPath = ModCustomDataFileLibraries.GetFullPath(mod, fileNameWithExt);
                LogLibraries.Warn("Failed to save binary file " + fullPath + " - Data is null.");
                return;
            }

            try {
                ModCustomDataFileLibraries.PrepareDir(mod);

                string fullPath = ModCustomDataFileLibraries.GetFullPath(mod, fileNameWithExt);

                string dataJson  = JsonConvert.SerializeObject(data, jsonSettings);
                byte[] dataBytes = System.Text.Encoding.UTF8.GetBytes(dataJson);

                FileLibraries.SaveBinaryFile(dataBytes, fullPath, false, !overrides);
            } catch (IOException e) {
                string fullDir = ModCustomDataFileLibraries.GetFullDirectoryPath(mod);
                LogLibraries.Warn("Failed to save binary file " + fileNameWithExt + " at " + fullDir + " - " + e.ToString());
                throw new IOException("Failed to save binary file " + fileNameWithExt + " at " + fullDir, e);
            }
        }
示例#3
0
        /// <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
        {
            string relDir = ModCustomDataFileLibraries.GetRelativeDirectoryPath(mod);

            if (data == null)
            {
                LogLibraries.Warn("Failed to save json file " + fileNameNoExt + " at " + relDir + " - Data is null.");
                return(false);
            }

            try {
                ModCustomDataFileLibraries.PrepareDir(mod);

                string fullPath = ModCustomDataFileLibraries.GetFullPath(mod, fileNameNoExt + ".json");
                string dataJson = JsonConvert.SerializeObject(data, jsonSettings);

                return(FileLibraries.SaveTextFile(dataJson, fullPath, false, !overrides));
            } catch (IOException e) {
                LogLibraries.Warn("Failed to save json file " + fileNameNoExt + " at " + relDir + " - " + e.ToString());
                throw new IOException("Failed to save json file " + fileNameNoExt + " at " + relDir, e);
            }
        }
        ////////////////

        /// <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
        {
            try {
                ModCustomDataFileLibraries.PrepareDir(mod);

                string fullPath  = ModCustomDataFileLibraries.GetFullPath(mod, fileNameWithExt);
                byte[] dataBytes = FileLibraries.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
                {
                    LogLibraries.Alert("No json file " + fileNameWithExt + ".");
                    return(null);
                }
            } catch (IOException e) {
                string fullDir = ModCustomDataFileLibraries.GetFullDirectoryPath(mod);
                LogLibraries.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 ModLibsException("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
        {
            try {
                ModCustomDataFileLibraries.PrepareDir(mod);

                string fullPath = ModCustomDataFileLibraries.GetFullPath(mod, fileNameNoExt + ".json");
                string dataStr  = FileLibraries.LoadTextFile(fullPath, false);

                if (dataStr != null)
                {
                    return(JsonConvert.DeserializeObject <T>(dataStr, jsonSettings));
                }
                else
                {
                    LogLibraries.Alert("No json file " + fileNameNoExt + ".");
                    return(null);
                }
            } catch (IOException e) {
                string fullDir = ModCustomDataFileLibraries.GetFullDirectoryPath(mod);
                LogLibraries.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 ModLibsException("From " + fileNameNoExt + " (" + typeof(T).Name + ")", e);
            }
        }
示例#6
0
        ////////////////

        /// <summary>
        /// Creates a mod's data directory, if needed.
        /// </summary>
        /// <param name="mod"></param>
        public static void PrepareDir(Mod mod)
        {
            string fullDir = ModCustomDataFileLibraries.GetFullDirectoryPath(mod);

            try {
                Directory.CreateDirectory(Main.SavePath);
                Directory.CreateDirectory(Main.SavePath + Path.DirectorySeparatorChar + ModCustomDataFileLibraries.BaseFolder);
                Directory.CreateDirectory(fullDir);
            } catch (IOException e) {
                LogLibraries.Warn("Failed to prepare directory: " + fullDir + " - " + e.ToString());
                throw new IOException("Failed to prepare directory: " + fullDir, e);
            }
        }
示例#7
0
 /// <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(ModCustomDataFileLibraries.SaveAsJson <T>(mod, fileNameNoExt, new JsonSerializerSettings(), overrides, data));
 }
示例#8
0
 /// <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
 {
     ModCustomDataFileLibraries.SaveAsBinaryJson <T>(mod, fileNameWithExt, new JsonSerializerSettings(), overrides, data);
 }
示例#9
0
 /// <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(ModCustomDataFileLibraries.GetFullDirectoryPath(mod) + Path.DirectorySeparatorChar + fileNameHasExt);
 }
示例#10
0
 /// <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 + ModCustomDataFileLibraries.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(ModCustomDataFileLibraries.LoadJson <T>(mod, fileNameNoExt, new JsonSerializerSettings()));
 }