-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
148 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System; | ||
using System.Numerics; | ||
using System.Runtime.InteropServices; | ||
|
||
using FFXIVClientStructs.FFXIV.Client.System.Memory; | ||
using FFXIVClientStructs.Havok.Common.Base.Math.Matrix; | ||
using FFXIVClientStructs.Havok.Common.Base.Math.QsTransform; | ||
|
||
namespace CustomizePlus.Armatures.Data; | ||
internal static class InteropAlloc | ||
{ | ||
// Allocations | ||
private static IntPtr MatrixAlloc; | ||
|
||
// Access | ||
internal unsafe static Matrix4x4* Matrix; // Align to 16-byte boundary | ||
internal unsafe static Matrix4x4 GetMatrix(hkQsTransformf* transform) | ||
{ | ||
transform->get4x4ColumnMajor((float*)Matrix); | ||
return *Matrix; | ||
} | ||
internal unsafe static void SetMatrix(hkQsTransformf* transform, Matrix4x4 matrix) | ||
{ | ||
*Matrix = matrix; | ||
transform->set((hkMatrix4f*)Matrix); | ||
} | ||
|
||
// Init & disspose | ||
public unsafe static void Init() | ||
{ | ||
// Allocate space for our matrix to be aligned on a 16-byte boundary. | ||
// This is required due to ffxiv's use of the MOVAPS instruction. | ||
// Thanks to Fayti1703 for helping with debugging and coming up with this fix. | ||
MatrixAlloc = Marshal.AllocHGlobal(sizeof(float) * 16 + 16); | ||
Matrix = (Matrix4x4*)(16 * ((long)(MatrixAlloc + 15) / 16)); | ||
} | ||
public static void Dispose() | ||
{ | ||
Marshal.FreeHGlobal(MatrixAlloc); | ||
} | ||
} | ||
|
||
internal class GameAlloc<T> : IDisposable where T : unmanaged | ||
{ | ||
private bool Disposed; | ||
|
||
internal readonly nint Address; | ||
internal unsafe T* Data => (T*)Address; | ||
|
||
internal unsafe GameAlloc(ulong align = 16) | ||
=> Address = (nint)IMemorySpace.GetDefaultSpace()->Malloc<T>(align); | ||
|
||
public unsafe void Dispose() | ||
{ | ||
if (Disposed) return; | ||
IMemorySpace.Free(Data); // Free our allocated memory. | ||
Disposed = true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters