Files
CalamityModPublic/Systems/GlobalSaveDataSystem.cs
T
fryzahh e3044a8d72 Implemented GlobalSaveDataSystem
- Added a new system which allows the mod to globally save data across all worlds and players via a text file which stores and reads string keys relating to a piece of content which should be permanently unlocked in the mod.
- Some minor code cleanup as well
2025-05-24 18:30:39 -04:00

82 lines
3.6 KiB
C#

using System.Collections.Generic;
using System.IO;
using System.Linq;
using Terraria;
using Terraria.ModLoader;
namespace CalamityMod.Systems
{
public class GlobalSaveDataSystem : ModSystem
{
internal static List<string> GlobalSaveDataKeys = [];
/// <summary>
/// The directory in which all files related to globall saved data are stored.
/// </summary>
internal static string GlobalSaveDataDirectoryPath => Main.SavePath + "\\CalamityModGlobalSaveData\\";
/// <summary>
/// The path to the text file which contains all globally saved data keys.
/// </summary>
internal static string GlobalSaveDataKeysPath => Main.SavePath + "\\CalamityModGlobalSaveData\\GlobalSaveDataKeys.txt";
/// <summary>
/// The path to the README file explaining the purpose of the directory.
/// </summary>
internal static string READMEPath => Main.SavePath + "\\CalamityModGlobalSaveData\\README.txt";
public override void OnModLoad()
{
// Ensure that the save data keys text file exists. If not, create a new one.
if (!File.Exists(GlobalSaveDataKeysPath))
{
// Create the directory if necessary.
if (!Directory.Exists(GlobalSaveDataDirectoryPath))
Directory.CreateDirectory(GlobalSaveDataDirectoryPath);
// Create both the Global Save Data text file, as well as a README file explaining the directory's purpose.
if (!File.Exists(READMEPath))
{
using StreamWriter readme = File.CreateText(READMEPath);
readme.WriteLine("Hi! You're probably wondering what this directory is and why it exists if you've found it.");
readme.WriteLine("This is how the Calamity Mod currently manages data that is saved globally across all worlds. " +
"You will find all keys managing globally saved data in the other text file within this folder. You are free to remove " +
"any of those keys if you wish to reapply the lock on whatever in-game content they unlock. If you have no interest in doing so " +
"then it is recommended you simply leave the file as is.");
}
File.CreateText(GlobalSaveDataKeysPath);
}
// If it does exist, read all keys from the text file and save them into the list.
else
{
GlobalSaveDataKeys = File.ReadAllLines(GlobalSaveDataKeysPath).ToList();
}
}
/// <summary>
/// Checks whether or not a key has been saved into the GlobalSaveDataKeys text file.
/// </summary>
/// <returns>True if the key has been saved, false otherwise.</returns>
public static bool IsKeyAlreadySaved(string key) => GlobalSaveDataKeys.Contains(key);
/// <summary>
/// Saves a specified key into the GlobalSaveDataKeys text file.
/// </summary>
public static void SaveKey(string key)
{
// If the key already exists, log it as a warning and don't run any code.
if (GlobalSaveDataKeys.Contains(key))
{
CalamityMod.Instance.Logger.Error($"WARNING! A global save data key \"{key}\" which has already been registered is attempting to be registered again. " +
"Please report this to the Calamity Mod Team if you see this!");
return;
}
File.AppendAllLines(GlobalSaveDataKeysPath, [key]);
GlobalSaveDataKeys.Add(key);
}
}
}