diff --git a/Code/APMenuStuff.cs b/Code/APMenuStuff.cs index 0aadec1..7deea7e 100644 --- a/Code/APMenuStuff.cs +++ b/Code/APMenuStuff.cs @@ -1,4 +1,4 @@ -using System; +using System.IO; using System.Linq; using UnityEngine; using UnityEngine.UI; @@ -102,15 +102,12 @@ public void ShowAPButton(bool isConnectedToAP = false, bool isInFileSelectMenu = if (isInFileSelectMenu) { - if (Plugin.Instance.APFileData == null) - { - APFileData apFileData = ReadAPFileDataFromSaveFile(); - Plugin.Instance.SetAPFileData(apFileData); + APFileData apFileData = ReadAPFileDataFromSaveFile(); + Plugin.Instance.SetAPFileData(apFileData); - // If selected file is not an AP file, don't show button - if (apFileData == null) - return; - } + // If selected file is not an AP file, don't show button + if (apFileData == null) + return; // Switch button icon ToggleAPConnectedIcon(true); @@ -137,6 +134,32 @@ public void ShowMessage(string message) MessageBoxHandler.Instance.ShowMessageBox(messageData); } + public void HideAPButton() + { + // Hide AP button + Animate(apButtonAnim, 0); + } + + public void SaveAPDataToFile(APFileData apFileData) + { + string path = GetAPDataFilePath(false); + + if (string.IsNullOrEmpty(path)) + return; + + ModCore.Utility.WriteJsonToFile(apFileData, path); + } + + public void DuplicateAPDataFile() + { + File.Copy(GetAPDataFilePath(false), GetAPDataFilePath(true), true); + } + + public void DeleteAPDataFile() + { + File.Delete(GetAPDataFilePath(false)); + } + private void ShowAPMenu() { HideAPButton(); @@ -151,12 +174,6 @@ private void ShowAPMenu() Animate(apMenuAnim, 1); } - public void HideAPButton() - { - // Hide AP button - Animate(apButtonAnim, 0); - } - private void HideAPMenu() { // Hide AP menu @@ -164,6 +181,10 @@ private void HideAPMenu() // Set AP data APFileData apFileData = GetAPFileData(); + + if (apFileData != null && ModCore.Plugin.MainSaver != null) + SaveAPDataToFile(apFileData); + Plugin.Instance.SetAPFileData(apFileData); string errorMessage = string.Empty; @@ -236,21 +257,14 @@ private APFileData GetAPFileData() private APFileData ReadAPFileDataFromSaveFile() { - IDataSaver apSaver = ModCore.Plugin.MainSaver.LocalStorage.GetLocalSaver("archipelago"); - string server = apSaver.LoadData("server"); + string fileName = ModCore.Plugin.MainSaver._localStorage.GetSaverPath(); + fileName = fileName.Substring(0, fileName.Length - 4); + string path = BepInEx.Utility.CombinePaths(Application.persistentDataPath, "steam", $"{fileName}_apData.json"); - if (string.IsNullOrEmpty(server)) + if (!ModCore.Utility.TryParseJson(path, out APFileData apFileData)) return null; - return new APFileData() - { - Server = server, - Port = apSaver.LoadInt("port"), - SlotName = apSaver.LoadData("slotName"), - Password = apSaver.LoadData("password"), - Deathlink = Convert.ToBoolean(apSaver.LoadInt("deathlink")), - AutoEquipOutfits = Convert.ToBoolean(apSaver.LoadInt("autoEquipOutfits")), - }; + return apFileData; } private void PrefillAPMenuFields() @@ -317,6 +331,18 @@ private Vector2 GetPositionForAPButton() return mainMenu.menuImpl.currScreen != fileStartScreen ? new Vector2(0, -150) : new Vector2(77, -250); } + private string GetAPDataFilePath(bool duplicating) + { + SaverOwner mainSaver = ModCore.Plugin.MainSaver; + string fileName = !duplicating ? mainSaver._localStorage.GetSaverPath() : mainSaver.GetUniqueLocalSavePath(); + fileName = fileName.Substring(0, fileName.Length - 4); + + if (fileName == "default") + return string.Empty; + + return BepInEx.Utility.CombinePaths(Application.persistentDataPath, "steam", $"{fileName}_apData.json"); + } + public class APSettingsScreen : MenuScreen { public APSettingsScreen(MainMenu owner, string root, GuiBindData data) : base(owner, root, data) diff --git a/Code/ItemHandler.cs b/Code/ItemHandler.cs index 9f7ad4a..f908e9b 100644 --- a/Code/ItemHandler.cs +++ b/Code/ItemHandler.cs @@ -168,7 +168,7 @@ private void Awake() if (!hasInitialized) { // Parse item JSON - string path = BepInEx.Utility.CombinePaths(PluginInfo.PLUGIN_NAME, "Data", "itemData.json"); + string path = BepInEx.Utility.CombinePaths(BepInEx.Paths.PluginPath, PluginInfo.PLUGIN_NAME, "Data", "itemData.json"); if (!ModCore.Utility.TryParseJson(path, out ItemData? data)) { diff --git a/Code/ItemRandomizer.cs b/Code/ItemRandomizer.cs index 3115db5..7bac714 100644 --- a/Code/ItemRandomizer.cs +++ b/Code/ItemRandomizer.cs @@ -38,7 +38,6 @@ public void OnFileStart(bool newFile, APFileData apFileData) Events.OnPlayerSpawn += OnPlayerSpawn; Events.OnSceneLoaded += OnSceneLoaded; - SaveAPDataToFile(apFileData); rollOpensChests = Convert.ToBoolean(APHandler.GetSlotData("roll_opens_chests")); if (newFile) @@ -158,7 +157,7 @@ private void Awake() // Parse data JSON if (locations == null) { - string path = BepInEx.Utility.CombinePaths(PluginInfo.PLUGIN_NAME, "Data", "locationData.json"); + string path = BepInEx.Utility.CombinePaths(BepInEx.Paths.PluginPath, PluginInfo.PLUGIN_NAME, "Data", "locationData.json"); if (!ModCore.Utility.TryParseJson(path, out LocationData? data)) { @@ -210,6 +209,8 @@ private void OnDisable() private void SetupNewFile(APFileData apFileData) { + APMenuStuff.Instance.SaveAPDataToFile(apFileData); + IDataSaver settingsSaver = mainSaver.LocalStorage.GetLocalSaver("settings"); settingsSaver.SaveInt("hideMapHint", 1); settingsSaver.SaveInt("hideCutscenes", 1); @@ -247,19 +248,6 @@ private void SetupNewFile(APFileData apFileData) mainSaver.SaveLocal(); } - private void SaveAPDataToFile(APFileData apFileData) - { - IDataSaver apSaver = mainSaver.LocalStorage.GetLocalSaver("archipelago"); - apSaver.SaveData("server", apFileData.Server); - apSaver.SaveInt("port", apFileData.Port); - apSaver.SaveData("slotName", apFileData.SlotName); - if (!string.IsNullOrEmpty(apFileData.Password)) - apSaver.SaveData("password", apFileData.Password); - apSaver.SaveInt("deathlink", Convert.ToInt32(apFileData.Deathlink)); - apSaver.SaveInt("autoEquipOutfits", Convert.ToInt32(apFileData.AutoEquipOutfits)); - apSaver.SaveInt("stackStatuses", Convert.ToInt32(apFileData.StackStatuses)); - } - private void UnlockDoors() { // Pillow Fort diff --git a/Code/Patches.cs b/Code/Patches.cs index 8a82a86..e91e3a5 100644 --- a/Code/Patches.cs +++ b/Code/Patches.cs @@ -87,6 +87,20 @@ public static bool MainMenu_FileStartScreen_ClickedStart_Patch() return connected; } + [HarmonyPrefix] + [HarmonyPatch(typeof(MainMenu.FileStartScreen), nameof(MainMenu.FileStartScreen.ClickedDuplicate))] + public static void MainMenu_FileStartScreen_ClickedDuplicate_Patch() + { + APMenuStuff.Instance.DuplicateAPDataFile(); + } + + [HarmonyPrefix] + [HarmonyPatch(typeof(MainMenu.DeleteConfirmScreen), nameof(MainMenu.DeleteConfirmScreen.ClickedConfirm))] + public static void MainMenu_DeleteConfirmScreen_ClickedConfirm_Patch() + { + APMenuStuff.Instance.DeleteAPDataFile(); + } + [HarmonyPrefix] [HarmonyPatch(typeof(RollAction), nameof(RollAction.DoUpdate))] public static bool RollAction_DoUpdate_PrePatch(RollAction __instance)