存档模块迭代

This commit is contained in:
ALIENJACK\alien 2025-01-20 12:03:49 +08:00
parent 67617057ed
commit 4af2168748
2 changed files with 101 additions and 7 deletions

View File

@ -1,9 +1,12 @@
using AxibugProtobuf; using AxibugEmuOnline.Client.ClientCore;
using AxibugProtobuf;
using System; using System;
using System.IO;
using System.Runtime.InteropServices;
namespace AxibugEmuOnline.Client namespace AxibugEmuOnline.Client
{ {
/// <summary> 存档文件类 </summary> /// <summary> 存档文件管理类 </summary>
public class SaveFile public class SaveFile
{ {
/// <summary> 指示该存档是否是自动存档 </summary> /// <summary> 指示该存档是否是自动存档 </summary>
@ -14,17 +17,102 @@ namespace AxibugEmuOnline.Client
public int RomID { get; private set; } public int RomID { get; private set; }
/// <summary> 指示该存档所属模拟器平台 </summary> /// <summary> 指示该存档所属模拟器平台 </summary>
public RomPlatformType EmuPlatform { get; private set; } public RomPlatformType EmuPlatform { get; private set; }
/// <summary> 指示该存档是否为空 </summary>
public bool IsEmpty { get; }
/// <summary> 存档文件路径 </summary>
public string FilePath
{
get
{
var path = App.PersistentDataPath(EmuPlatform);
path = $"{path}/Slot/{EmuPlatform}/{RomID}";
Directory.CreateDirectory(path);
var filePath = $"{path}/slot{SlotIndex}.SlotSav";
return filePath;
}
}
public SaveFile(int romID, RomPlatformType platform, int slotIndex) public SaveFile(int romID, RomPlatformType platform, int slotIndex)
{ {
RomID = romID; RomID = romID;
EmuPlatform = platform; EmuPlatform = platform;
SlotIndex = slotIndex; SlotIndex = slotIndex;
IsEmpty = File.Exists(FilePath);
} }
internal void Save(byte[] savData, byte[] screenShotData) public unsafe void GetSavData(out byte[] savData, out byte[] screenShotData)
{ {
throw new NotImplementedException(); savData = null;
screenShotData = null;
if (!File.Exists(FilePath)) return;
var raw = File.ReadAllBytes(FilePath);
int headerSize = Marshal.SizeOf(typeof(Header));
if (raw.Length < headerSize)
{
App.log.Warning("无效存档");
return;
}
var header = new Header();
IntPtr ptr = Marshal.AllocHGlobal(headerSize);
Marshal.StructureToPtr(header, ptr, false);
Marshal.Copy(raw, 0, ptr, headerSize);
Marshal.FreeHGlobal(ptr);
savData = new byte[header.DataLength];
Array.Copy(raw, headerSize, savData, 0, savData.Length);
screenShotData = new byte[header.ScreenShotLength];
Array.Copy(raw, headerSize + savData.Length, screenShotData, 0, screenShotData.Length);
return;
}
public unsafe void Save(byte[] savData, byte[] screenShotData)
{
var filePath = FilePath;
var header = new Header { EmuPlatform = (byte)EmuPlatform, SlotIndex = (byte)SlotIndex, RomID = RomID, DataLength = savData.Length, ScreenShotLength = screenShotData.Length };
int headerSize = Marshal.SizeOf(typeof(Header));
IntPtr ptr = Marshal.AllocHGlobal(headerSize);
var totalSize = headerSize + savData.Length + screenShotData.Length;
byte[] raw = new byte[totalSize];
try
{
Marshal.StructureToPtr(header, ptr, false);
Marshal.Copy(ptr, raw, 0, headerSize);
}
finally
{
Marshal.FreeHGlobal(ptr);
}
Array.Copy(savData, 0, raw, headerSize, savData.Length);
Array.Copy(screenShotData, 0, raw, headerSize + savData.Length, screenShotData.Length);
File.WriteAllBytes(filePath, raw);
}
[StructLayout(LayoutKind.Explicit, Size = 14)]
struct Header
{
[FieldOffset(0)]
public byte EmuPlatform;
[FieldOffset(1)]
public byte SlotIndex;
[FieldOffset(2)]
public int RomID;
[FieldOffset(6)]
public int DataLength;
[FieldOffset(10)]
public int ScreenShotLength;
} }
} }
} }

View File

@ -13,10 +13,16 @@ namespace AxibugEmuOnline.Client
SavCloudApi m_cloudApi = new SavCloudApi(); SavCloudApi m_cloudApi = new SavCloudApi();
Dictionary<int, SaveFile[]> m_saveFileDict = new Dictionary<int, SaveFile[]>(); Dictionary<int, SaveFile[]> m_saveFileDict = new Dictionary<int, SaveFile[]>();
public void Save(int romID, RomPlatformType platform, int slotIndex, byte[] savData, byte[] screenShotData)
public List<SaveFile> GetSlotSaves(int romID, RomPlatformType platform)
{ {
var fileIns = GetSaveFile(romID, platform, slotIndex); List<SaveFile> result = new List<SaveFile>();
fileIns.Save(savData, screenShotData); for (int i = 0; i < MAX_SLOT_COUNT; i++)
{
result.Add(GetSaveFile(romID, platform, i));
}
return result;
} }
SaveFile GetSaveFile(int romID, RomPlatformType platform, int slotIndex) SaveFile GetSaveFile(int romID, RomPlatformType platform, int slotIndex)