diff --git a/pkNX.WinForms/Subforms/AreaEditor8a.cs b/pkNX.WinForms/Subforms/AreaEditor8a.cs index b120d488..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; @@ -163,9 +165,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 obj) where T : class, IFlatBufferSerializable + { + var pool = ArrayPool.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(string path, T obj) where T : class, IFlatBufferSerializable + { + var index = Resident.GetIndexFull(path); + if (index == -1) + return; + + byte[] result = Write(obj); + Resident[index] = result; } }