Skip to content

Commit

Permalink
Fix a couple bugs with connection menu
Browse files Browse the repository at this point in the history
- Fixed issue where using some characters (such as spaces) would corrupt the save file and prevent loading a rando file
- Fixed issue where AP menu wouldn't prefill the loaded data every time you entered it
  • Loading branch information
Chris-Is-Awesome committed Jul 24, 2024
1 parent 5591639 commit 9c9e114
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 43 deletions.
80 changes: 53 additions & 27 deletions Code/APMenuStuff.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -151,19 +174,17 @@ private void ShowAPMenu()
Animate(apMenuAnim, 1);
}

public void HideAPButton()
{
// Hide AP button
Animate(apButtonAnim, 0);
}

private void HideAPMenu()
{
// Hide AP menu
Animate(apMenuAnim, 0);

// Set AP data
APFileData apFileData = GetAPFileData();

if (apFileData != null && ModCore.Plugin.MainSaver != null)
SaveAPDataToFile(apFileData);

Plugin.Instance.SetAPFileData(apFileData);

string errorMessage = string.Empty;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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<MainMenu>
{
public APSettingsScreen(MainMenu owner, string root, GuiBindData data) : base(owner, root, data)
Expand Down
2 changes: 1 addition & 1 deletion Code/ItemHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{
Expand Down
18 changes: 3 additions & 15 deletions Code/ItemRandomizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public void OnFileStart(bool newFile, APFileData apFileData)
Events.OnPlayerSpawn += OnPlayerSpawn;
Events.OnSceneLoaded += OnSceneLoaded;

SaveAPDataToFile(apFileData);
rollOpensChests = Convert.ToBoolean(APHandler.GetSlotData<long>("roll_opens_chests"));

if (newFile)
Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions Code/Patches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 9c9e114

Please sign in to comment.