From e3044a8d72aad036c1c1cf7204fb645109bc47a5 Mon Sep 17 00:00:00 2001 From: fryzahh Date: Wed, 21 May 2025 08:55:08 -0400 Subject: [PATCH] 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 --- CalamityMod.cs | 7 +++ MainMenu/CalamityMainMenu_Sunken.cs | 41 +++++++++++++-- Systems/GlobalSaveDataSystem.cs | 81 +++++++++++++++++++++++++++++ 3 files changed, 125 insertions(+), 4 deletions(-) create mode 100644 Systems/GlobalSaveDataSystem.cs diff --git a/CalamityMod.cs b/CalamityMod.cs index 04444e41f5..d117616ed8 100644 --- a/CalamityMod.cs +++ b/CalamityMod.cs @@ -5,6 +5,7 @@ using System.Reflection; using System.Runtime.CompilerServices; using CalamityMod.FluidSimulation; using CalamityMod.Items; +using CalamityMod.MainMenu; using CalamityMod.Particles; using CalamityMod.Projectiles; using Microsoft.Xna.Framework; @@ -50,6 +51,12 @@ namespace CalamityMod Main.QueueMainThreadAction(() => Main.OnPreDraw += PrepareRenderTargets); } } + + public override void PostSetupContent() + { + // Force open certain ModMenus for various reasons and the added flair of "Hey! Something big has happened! Check it out!" + CalamityMainMenu_Sunken.ForceMenuStyle(); + } #endregion #region Unload diff --git a/MainMenu/CalamityMainMenu_Sunken.cs b/MainMenu/CalamityMainMenu_Sunken.cs index c914b20e6d..01fcc3dd0d 100644 --- a/MainMenu/CalamityMainMenu_Sunken.cs +++ b/MainMenu/CalamityMainMenu_Sunken.cs @@ -1,16 +1,16 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Xml; +using System.Reflection; +using CalamityMod.Systems; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using ReLogic.Content; -using ReLogic.Threading; using Terraria; +using Terraria.Audio; using Terraria.Graphics.Shaders; using Terraria.ID; using Terraria.ModLoader; -using Terraria.ModLoader.Default; namespace CalamityMod.MainMenu { @@ -42,20 +42,53 @@ namespace CalamityMod.MainMenu public static List Fishes { get; private set; } = []; + public static Asset LogoWater => ModContent.Request("CalamityMod/MainMenu/LogoSunken_Water"); + public int MaxBoids = 120; public float remixLogoRotation = 0f; + + /// + /// The global save data key which dictates whether or not the player is opening the Sunken Sea Overhaul update for the first time. + /// + public const string FirstTimeOpeningSunkenSeaOverhaulKey = "HasOpenedTheSunkenSeaOverhaulForTheFirstTime"; + public override string DisplayName => "Calamity Style - Sunken"; public override Asset Logo => ModContent.Request("CalamityMod/MainMenu/LogoSunken"); - Asset LogoWater => ModContent.Request("CalamityMod/MainMenu/LogoSunken_Water"); + public override Asset SunTexture => ModContent.Request("CalamityMod/Backgrounds/BlankPixel"); + public override Asset MoonTexture => ModContent.Request("CalamityMod/Backgrounds/BlankPixel"); public override int Music => CalamityMod.Instance.GetMusicFromMusicMod("SunkenSea") ?? MusicID.OceanNight; public override ModSurfaceBackgroundStyle MenuBackgroundStyle => ModContent.GetInstance(); + public static void ForceMenuStyle() + { + // Forcefully open this ModMenu if it's the player's first time opening the Sunken Sea Overhaul update. + if (GlobalSaveDataSystem.IsKeyAlreadySaved(FirstTimeOpeningSunkenSeaOverhaulKey)) + return; + + FieldInfo menusInfo = typeof(MenuLoader).GetField("menus", BindingFlags.Static | BindingFlags.NonPublic); + List modMenus = (List)menusInfo.GetValue(null); + + var sunkenMenu = ModContent.GetInstance(); + if (modMenus.Contains(sunkenMenu)) + { + FieldInfo lastSelectedMenuInfo = typeof(MenuLoader).GetField("LastSelectedModMenu", BindingFlags.Static | BindingFlags.NonPublic); + int sunkenSeaMenuIndex = modMenus.IndexOf(sunkenMenu); + lastSelectedMenuInfo.SetValue(null, modMenus[sunkenSeaMenuIndex].FullName); + } + } + + public override void Update(bool isOnTitleScreen) + { + if (!GlobalSaveDataSystem.IsKeyAlreadySaved(FirstTimeOpeningSunkenSeaOverhaulKey)) + GlobalSaveDataSystem.SaveKey(FirstTimeOpeningSunkenSeaOverhaulKey); + } + public override bool PreDrawLogo(SpriteBatch spriteBatch, ref Vector2 logoDrawCenter, ref float logoRotation, ref float logoScale, ref Color drawColor) { // Draw the main background for the menu. diff --git a/Systems/GlobalSaveDataSystem.cs b/Systems/GlobalSaveDataSystem.cs new file mode 100644 index 0000000000..de52e65491 --- /dev/null +++ b/Systems/GlobalSaveDataSystem.cs @@ -0,0 +1,81 @@ +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 GlobalSaveDataKeys = []; + + /// + /// The directory in which all files related to globall saved data are stored. + /// + internal static string GlobalSaveDataDirectoryPath => Main.SavePath + "\\CalamityModGlobalSaveData\\"; + + + /// + /// The path to the text file which contains all globally saved data keys. + /// + internal static string GlobalSaveDataKeysPath => Main.SavePath + "\\CalamityModGlobalSaveData\\GlobalSaveDataKeys.txt"; + + /// + /// The path to the README file explaining the purpose of the directory. + /// + 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(); + } + } + + /// + /// Checks whether or not a key has been saved into the GlobalSaveDataKeys text file. + /// + /// True if the key has been saved, false otherwise. + public static bool IsKeyAlreadySaved(string key) => GlobalSaveDataKeys.Contains(key); + + /// + /// Saves a specified key into the GlobalSaveDataKeys text file. + /// + 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); + } + } +}