Skip to content

Commit

Permalink
Fix stupid.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ottermandias committed Jan 20, 2025
1 parent 9ca0145 commit 39c73af
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
14 changes: 8 additions & 6 deletions Penumbra/Meta/Files/ImcFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,22 +197,24 @@ public void Replace(ResourceHandle* resource)
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);
}
}
18 changes: 18 additions & 0 deletions Penumbra/Meta/Files/MetaBaseFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,24 @@ public void Release<T>(ref T* pointer, int length) where T : unmanaged
}
}

public sealed unsafe class XivDefaultAllocator : IFileAllocator, IService
{
public T* Allocate<T>(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<T>(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;
Expand Down
26 changes: 14 additions & 12 deletions Penumbra/Meta/MetaFileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 39c73af

Please sign in to comment.