////////////////

        /// <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);
            }
        }
Exemplo n.º 3
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
        {
            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);
            }
        }
Exemplo n.º 4
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
        {
            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);
            }
        }