Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #357] Update AreaEditor8a.cs to save AreaSettings #358

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions pkNX.WinForms/Subforms/AreaEditor8a.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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>(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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this can be FlatBufferConverter.SerializeFrom(obj)

}
}
Loading