using System; using System.IO; using System.Runtime.InteropServices; using AOT; using Epic.OnlineServices; 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); #endif [UnmanagedFunctionPointer(Config.LibraryCallingConvention)] public delegate IntPtr AllocateMemoryFunc(IntPtr size, IntPtr alignment); [MonoPInvokeCallback(typeof(AllocateMemoryFunc))] public static IntPtr AllocateMemory(IntPtr size, IntPtr alignment) { return Marshal.AllocHGlobal(size); //return HeapAlloc(GetProcessHeap(), 0, size); } [UnmanagedFunctionPointer(Config.LibraryCallingConvention)] public delegate IntPtr ReallocateMemoryFunc(IntPtr pointer, IntPtr size, IntPtr alignment); [MonoPInvokeCallback(typeof(ReallocateMemoryFunc))] public static IntPtr ReallocateMemory(IntPtr pointer, IntPtr size, IntPtr 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(size); return Marshal.ReAllocHGlobal(pointer, size); // if (pointer == IntPtr.Zero) // return HeapAlloc(GetProcessHeap(), 0, size); // // return HeapReAlloc(GetProcessHeap(), 0, pointer, size); } [UnmanagedFunctionPointer(Config.LibraryCallingConvention)] public delegate void ReleaseMemoryFunc(IntPtr pointer); [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); }