using System; using System.IO; using System.Runtime.InteropServices; using AOT; using Epic.OnlineServices; using Epic.OnlineServices.Platform; public static class EOSNativeHelper { #if UNITY_STANDALONE_WIN [DllImport("kernel32", EntryPoint = "LoadLibrary", SetLastError = true, CharSet = CharSet.Unicode)] private static extern IntPtr LoadLibraryNative(string lpFileName); [DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool SetDllDirectory(string lpPathName); public static IntPtr LoadLibrary(string lpFileName) { string lpPathName = Path.GetDirectoryName(lpFileName); lpFileName = Path.GetFileName(lpFileName); SetDllDirectory(lpPathName.Replace('/', '\\')); return LoadLibraryNative(lpFileName); } [DllImport("kernel32", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] public static extern bool FreeLibrary(IntPtr hModule); [DllImport("kernel32")] public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName); #else [MonoPInvokeCallback(typeof(AllocateMemoryFunc))] public static IntPtr AllocateMemory(UIntPtr size, UIntPtr alignment) { return Marshal.AllocHGlobal((IntPtr)size.ToUInt64()); //return HeapAlloc(GetProcessHeap(), 0, size); } [MonoPInvokeCallback(typeof(ReallocateMemoryFunc))] public static IntPtr ReallocateMemory(IntPtr pointer, UIntPtr size, UIntPtr alignment) { // EOS will sometimes request a reallocation for a null pointer, so we need to specifically handle that if (pointer == IntPtr.Zero) return Marshal.AllocHGlobal((IntPtr)size.ToUInt64()); return Marshal.ReAllocHGlobal(pointer, (IntPtr)size.ToUInt64()); // if (pointer == IntPtr.Zero) // return HeapAlloc(GetProcessHeap(), 0, size); // // return HeapReAlloc(GetProcessHeap(), 0, pointer, size); } [MonoPInvokeCallback(typeof(ReleaseMemoryFunc))] public static void ReleaseMemory(IntPtr pointer) { Marshal.FreeHGlobal(pointer); // HeapFree(GetProcessHeap(), 0, pointer); } [DllImport("kernel32", SetLastError = true)] private static extern IntPtr GetProcessHeap(); [DllImport("kernel32")] private static extern IntPtr HeapAlloc(IntPtr hHeap, int dwFlags, IntPtr dwBytes); [DllImport("kernel32")] private static extern IntPtr HeapReAlloc(IntPtr hHeap, int dwFlags, IntPtr lpMem, IntPtr dwBytes); [DllImport("kernel32")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool HeapFree(IntPtr hHeap, int dwFlags, IntPtr lpMem); #endif #if UNITY_PS4 || UNITY_PS5 [DllImport("PS5Utils.prx")] public static extern void GetMemoryFunctions(out IntPtr allocFunc, out IntPtr reallocFunc, out IntPtr releaseFunc); #endif }