Example #1
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);
            }
        }
Example #2
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 + ModCustomDataFileHelpers.GetRelativeDirectoryPath(mod));
 }