示例#1
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);
            }
        }
示例#2
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);
            }
        }
        ////////////////

        /// <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);
            }
        }
示例#5
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);
            }
        }
示例#6
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);
 }