From 39c73af2382228a0b5f867baf5be44979136deee Mon Sep 17 00:00:00 2001 From: Ottermandias Date: Mon, 20 Jan 2025 20:33:28 +0100 Subject: [PATCH] Fix stupid. --- Penumbra/Meta/Files/ImcFile.cs | 14 ++++++++------ Penumbra/Meta/Files/MetaBaseFile.cs | 18 ++++++++++++++++++ Penumbra/Meta/MetaFileManager.cs | 26 ++++++++++++++------------ 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/Penumbra/Meta/Files/ImcFile.cs b/Penumbra/Meta/Files/ImcFile.cs index de022f4c..c6e4ec94 100644 --- a/Penumbra/Meta/Files/ImcFile.cs +++ b/Penumbra/Meta/Files/ImcFile.cs @@ -197,22 +197,24 @@ public unsafe class ImcFile : MetaBaseFile if (length >= actualLength) { MemoryUtility.MemCpyUnchecked((byte*)data, Data, actualLength); - MemoryUtility.MemSet((byte*)data + actualLength, 0, length - actualLength); + if (length > actualLength) + MemoryUtility.MemSet((byte*)(data + actualLength), 0, length - actualLength); return; } var paddedLength = actualLength.PadToMultiple(128); - var newData = Manager.XivAllocator.Allocate(paddedLength, 8); + var newData = Manager.XivFileAllocator.Allocate(paddedLength, 8); if (newData == null) { Penumbra.Log.Error($"Could not replace loaded IMC data at 0x{(ulong)resource:X}, allocation failed."); return; } - + MemoryUtility.MemCpyUnchecked(newData, Data, actualLength); - MemoryUtility.MemSet((byte*)data + actualLength, 0, paddedLength - actualLength); - - Manager.XivAllocator.Release((void*)data, length); + if (paddedLength > actualLength) + MemoryUtility.MemSet(newData + actualLength, 0, paddedLength - actualLength); + + Manager.XivFileAllocator.Release((void*)data, length); resource->SetData((nint)newData, paddedLength); } } diff --git a/Penumbra/Meta/Files/MetaBaseFile.cs b/Penumbra/Meta/Files/MetaBaseFile.cs index 0cb34ab3..d04e1bdf 100644 --- a/Penumbra/Meta/Files/MetaBaseFile.cs +++ b/Penumbra/Meta/Files/MetaBaseFile.cs @@ -73,6 +73,24 @@ public sealed unsafe class XivFileAllocator : IFileAllocator, IService } } +public sealed unsafe class XivDefaultAllocator : IFileAllocator, IService +{ + public T* Allocate(int length, int alignment = 1) where T : unmanaged + { + var ret = (T*)IMemorySpace.GetDefaultSpace()->Malloc((ulong)(length * sizeof(T)), (ulong)alignment); + Penumbra.Log.Verbose($"Allocating {length * sizeof(T)} bytes via FFXIV Default Allocator to 0x{(nint)ret:X}."); + return ret; + } + + public void Release(ref T* pointer, int length) where T : unmanaged + { + + IMemorySpace.Free(pointer, (ulong)(length * sizeof(T))); + Penumbra.Log.Verbose($"Freeing {length * sizeof(T)} bytes from 0x{(nint)pointer:X} via FFXIV Default Allocator."); + pointer = null; + } +} + public unsafe class MetaBaseFile(MetaFileManager manager, IFileAllocator alloc, MetaIndex idx) : IDisposable { protected readonly MetaFileManager Manager = manager; diff --git a/Penumbra/Meta/MetaFileManager.cs b/Penumbra/Meta/MetaFileManager.cs index 5250273b..6130a48f 100644 --- a/Penumbra/Meta/MetaFileManager.cs +++ b/Penumbra/Meta/MetaFileManager.cs @@ -28,24 +28,26 @@ public class MetaFileManager : IService internal readonly ImcChecker ImcChecker; internal readonly AtchManager AtchManager; internal readonly IFileAllocator MarshalAllocator = new MarshalAllocator(); - internal readonly IFileAllocator XivAllocator; + internal readonly IFileAllocator XivFileAllocator; + internal readonly IFileAllocator XivDefaultAllocator; public MetaFileManager(CharacterUtility characterUtility, ResidentResourceManager residentResources, IDataManager gameData, ActiveCollectionData activeCollections, Configuration config, ValidityChecker validityChecker, ObjectIdentification identifier, FileCompactor compactor, IGameInteropProvider interop, AtchManager atchManager) { - CharacterUtility = characterUtility; - ResidentResources = residentResources; - GameData = gameData; - ActiveCollections = activeCollections; - Config = config; - ValidityChecker = validityChecker; - Identifier = identifier; - Compactor = compactor; - AtchManager = atchManager; - ImcChecker = new ImcChecker(this); - XivAllocator = new XivFileAllocator(interop); + CharacterUtility = characterUtility; + ResidentResources = residentResources; + GameData = gameData; + ActiveCollections = activeCollections; + Config = config; + ValidityChecker = validityChecker; + Identifier = identifier; + Compactor = compactor; + AtchManager = atchManager; + ImcChecker = new ImcChecker(this); + XivFileAllocator = new XivFileAllocator(interop); + XivDefaultAllocator = new XivDefaultAllocator(); interop.InitializeFromAttributes(this); }