From 3b576be36454736248749f81020b432f467f6d88 Mon Sep 17 00:00:00 2001
From: "Dr. Victor Vasconcelos de Souza"
 <32229843+drvictorvs@users.noreply.github.com>
Date: Wed, 31 Jan 2024 10:23:52 -0300
Subject: [PATCH 1/2] Update AreaEditor8a.cs to save AreaSettings

---
 pkNX.WinForms/Subforms/AreaEditor8a.cs | 33 ++++++++++++++++++++++++--
 1 file changed, 31 insertions(+), 2 deletions(-)

diff --git a/pkNX.WinForms/Subforms/AreaEditor8a.cs b/pkNX.WinForms/Subforms/AreaEditor8a.cs
index b120d488..3ac04b22 100644
--- a/pkNX.WinForms/Subforms/AreaEditor8a.cs
+++ b/pkNX.WinForms/Subforms/AreaEditor8a.cs
@@ -163,9 +163,38 @@ private void B_Save_Click(object sender, EventArgs e)
 
     private void AreaEditor8a_FormClosing(object sender, FormClosingEventArgs e)
     {
-        if (Save)
+        if (Save){
             SaveArea();
-        else
+            SaveSettings();
+        }
+        else {
             Resident.CancelEdits();
+        }
+    }
+
+    private void SaveSettings()
+    {
+        TryWrite("bin/field/resident/AreaSettings.bin", Settings);
+    }
+
+    private static byte[] Write<T>(T obj) where T : class, IFlatBufferSerializable<T>
+    {
+        var pool = ArrayPool<byte>.Shared;
+        var serializer = obj.Serializer;
+        var data = pool.Rent(serializer.GetMaxSize(obj));
+        var len = serializer.Write(data, obj);
+        var result = data.AsSpan(0, len).ToArray();
+        pool.Return(data);
+        return result;
+    }
+
+    private void TryWrite<T>(string path, T obj) where T : class, IFlatBufferSerializable<T>
+    {
+        var index = Resident.GetIndexFull(path);
+        if (index == -1)
+            return;
+
+        byte[] result = Write(obj);
+        Resident[index] = result;
     }
 }

From 0b3de10461651d47c147a918e0f5b1c3c5e4e992 Mon Sep 17 00:00:00 2001
From: "Dr. Victor Vasconcelos de Souza"
 <32229843+drvictorvs@users.noreply.github.com>
Date: Wed, 31 Jan 2024 10:48:14 -0300
Subject: [PATCH 2/2] Fix AreaEditor8a.cs

---
 pkNX.WinForms/Subforms/AreaEditor8a.cs | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/pkNX.WinForms/Subforms/AreaEditor8a.cs b/pkNX.WinForms/Subforms/AreaEditor8a.cs
index 3ac04b22..fa6c1bb9 100644
--- a/pkNX.WinForms/Subforms/AreaEditor8a.cs
+++ b/pkNX.WinForms/Subforms/AreaEditor8a.cs
@@ -11,6 +11,8 @@
 using pkNX.Structures.FlatBuffers.Arceus;
 using static pkNX.Structures.Species;
 using Util = pkNX.Randomization.Util;
+using FlatSharp;
+using System.Buffers;
 
 namespace pkNX.WinForms.Subforms;