From 7cb1dd18f8b2367992cf13a8fa48c9eefd5d9ec0 Mon Sep 17 00:00:00 2001
From: Star Rauchenberger <fefferburbia@gmail.com>
Date: Sat, 25 May 2024 13:57:22 -0400
Subject: Past connections are now saved

---
 AnodyneArchipelago/AnodyneArchipelago.csproj   |  9 ++++
 AnodyneArchipelago/Menu/ArchipelagoSettings.cs | 74 ++++++++++++++++++++++++++
 AnodyneArchipelago/Menu/MenuState.cs           | 68 ++++++++++++++++++++++-
 3 files changed, 149 insertions(+), 2 deletions(-)
 create mode 100644 AnodyneArchipelago/Menu/ArchipelagoSettings.cs

(limited to 'AnodyneArchipelago')

diff --git a/AnodyneArchipelago/AnodyneArchipelago.csproj b/AnodyneArchipelago/AnodyneArchipelago.csproj
index a5780a0..a750332 100644
--- a/AnodyneArchipelago/AnodyneArchipelago.csproj
+++ b/AnodyneArchipelago/AnodyneArchipelago.csproj
@@ -38,5 +38,14 @@
     <Reference Include="SemanticVersioning">
       <HintPath>..\..\BepInEx\bin\NET.Framework\net462\SemanticVersioning.dll</HintPath>
     </Reference>
+    <Reference Include="System.Memory">
+      <HintPath>..\..\BepInEx\bin\NET.Framework\net462\System.Memory.dll</HintPath>
+    </Reference>
+    <Reference Include="System.Text.Json">
+      <HintPath>..\..\BepInEx\bin\NET.Framework\net462\System.Text.Json.dll</HintPath>
+    </Reference>
+    <Reference Include="System.ValueTuple">
+      <HintPath>..\..\BepInEx\bin\NET.Framework\net462\System.ValueTuple.dll</HintPath>
+    </Reference>
   </ItemGroup>
 </Project>
diff --git a/AnodyneArchipelago/Menu/ArchipelagoSettings.cs b/AnodyneArchipelago/Menu/ArchipelagoSettings.cs
new file mode 100644
index 0000000..eff3ddf
--- /dev/null
+++ b/AnodyneArchipelago/Menu/ArchipelagoSettings.cs
@@ -0,0 +1,74 @@
+using AnodyneSharp.Registry;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Text.Json;
+
+namespace AnodyneArchipelago.Menu
+{
+    internal class ConnectionDetails : IEquatable<ConnectionDetails>
+    {
+        public string ApServer;
+        public string ApSlot;
+        public string ApPassword;
+
+        public override bool Equals(object obj)
+        {
+            return this.Equals(obj as ConnectionDetails);
+        }
+
+        public bool Equals(ConnectionDetails other)
+        {
+            return (ApServer == other.ApServer) && (ApSlot == other.ApSlot) && (ApPassword == other.ApPassword);
+        }
+
+        public override int GetHashCode()
+        {
+            return (ApServer, ApSlot, ApPassword).GetHashCode();
+        }
+    }
+
+    internal class ArchipelagoSettings
+    {
+        public static JsonSerializerOptions serializerOptions = new JsonSerializerOptions()
+        {
+            IncludeFields = true
+        };
+
+        public List<ConnectionDetails> ConnectionDetails = new();
+
+        public static string GetFilePath() => string.Format("{0}Saves/ap_settings.dat", GameConstants.SavePath);
+
+        public static ArchipelagoSettings Load()
+        {
+            try
+            {
+                string s = File.ReadAllText(GetFilePath());
+                return JsonSerializer.Deserialize<ArchipelagoSettings>(s, serializerOptions);
+            } catch (Exception)
+            {
+                return null;
+            }
+        }
+
+        public void Save()
+        {
+            File.WriteAllText(GetFilePath(), JsonSerializer.Serialize(this, serializerOptions));
+        }
+
+        public void AddConnection(ConnectionDetails connectionDetails)
+        {
+            if (ConnectionDetails.Contains(connectionDetails))
+            {
+                ConnectionDetails.Remove(connectionDetails);
+            }
+
+            ConnectionDetails.Insert(0, connectionDetails);
+
+            if (ConnectionDetails.Count > 9)
+            {
+                ConnectionDetails.RemoveAt(ConnectionDetails.Count - 1);
+            }
+        }
+    }
+}
diff --git a/AnodyneArchipelago/Menu/MenuState.cs b/AnodyneArchipelago/Menu/MenuState.cs
index 898eb94..59a4fdf 100644
--- a/AnodyneArchipelago/Menu/MenuState.cs
+++ b/AnodyneArchipelago/Menu/MenuState.cs
@@ -31,6 +31,9 @@ namespace AnodyneArchipelago.Menu
         private string _apSlot = "";
         private string _apPassword = "";
 
+        private ArchipelagoSettings _archipelagoSettings;
+        private int _curPage;
+
         private int _selectorIndex = 0;
 
         private bool _fadingOut = false;
@@ -44,6 +47,12 @@ namespace AnodyneArchipelago.Menu
                 Plugin.ArchipelagoManager = null;
             }
 
+            _archipelagoSettings = ArchipelagoSettings.Load();
+            if (_archipelagoSettings == null)
+            {
+                _archipelagoSettings = new();
+            }
+
             _selector = new();
             _selector.Play("enabledRight");
 
@@ -59,12 +68,19 @@ namespace AnodyneArchipelago.Menu
             _settingsLabel = new(new Vector2(60f, 131f), false, $"Config", new Color(116, 140, 144));
             _quitLabel = new(new Vector2(60f, 147f), false, $"Quit", new Color(116, 140, 144));
 
-            _connectionSwitcher = new(new Vector2(60f, 95f), 32f, 0, true, new string[1] { "1/1" });
+            string[] selectorValues = new string[_archipelagoSettings.ConnectionDetails.Count + 1];
+            for (int i= 0; i < selectorValues.Length; i++)
+            {
+                selectorValues[i] = $"{i+1}/{selectorValues.Length}";
+            }
+
+            _connectionSwitcher = new(new Vector2(60f, 95f), 32f, 0, true, selectorValues);
             _connectionSwitcher.noConfirm = true;
             _connectionSwitcher.noLoop = true;
             _connectionSwitcher.ValueChangedEvent = PageValueChanged;
 
             SetCursorPosition(0);
+            SetPage(_archipelagoSettings.ConnectionDetails.Count == 0 ? 0 : 1);
             UpdateLabels();
         }
 
@@ -249,15 +265,63 @@ namespace AnodyneArchipelago.Menu
                         break;
                 }
             }
+            else if (KeyInput.JustPressedRebindableKey(KeyFunctions.Left))
+            {
+                if (_selectorIndex < 3 && _curPage > 0)
+                {
+                    SoundManager.PlaySoundEffect("menu_move");
+
+                    SetPage(_curPage - 1);
+                }
+            }
+            else if (KeyInput.JustPressedRebindableKey(KeyFunctions.Right))
+            {
+                if (_selectorIndex < 3 && _curPage < _archipelagoSettings.ConnectionDetails.Count)
+                {
+                    SoundManager.PlaySoundEffect("menu_move");
+
+                    SetPage(_curPage + 1);
+                }
+            }
         }
 
-        private void PageValueChanged(string value, int index)
+        private void SetPage(int index)
         {
+            _curPage = index;
+
+            if (index == 0)
+            {
+                _apServer = "";
+                _apSlot = "";
+                _apPassword = "";
+            }
+            else
+            {
+                ConnectionDetails details = _archipelagoSettings.ConnectionDetails[index - 1];
+                _apServer = details.ApServer;
+                _apSlot = details.ApSlot;
+                _apPassword = details.ApPassword;
+            }
 
+            _connectionSwitcher.SetValue(index);
+            UpdateLabels();
+        }
+
+        private void PageValueChanged(string value, int index)
+        {
+            SetPage(index);
         }
 
         private void OnConnected(ArchipelagoManager archipelagoManager)
         {
+            _archipelagoSettings.AddConnection(new()
+            {
+                ApServer = _apServer,
+                ApSlot = _apSlot,
+                ApPassword = _apPassword
+            });
+            _archipelagoSettings.Save();
+
             Plugin.ArchipelagoManager = archipelagoManager;
 
             GlobalState.Save saveFile = GlobalState.Save.GetSave(string.Format("{0}Saves/Save_zzAP{1}_{2}.dat", GameConstants.SavePath, Plugin.ArchipelagoManager.GetSeed(), Plugin.ArchipelagoManager.GetPlayer()));
-- 
cgit 1.4.1