Browse Source

Added fully native memory allocation functions that are passed directly to EOS without any P/Invoke in-between. This makes EOS work on PS4 & PS5 without causing thread semaphore crashes in Unity.

master
Nico de Poel 4 years ago
parent
commit
9e21ad7092
  1. 8
      Assets/Plugins/PS5.meta
  2. BIN
      Assets/Plugins/PS5/PS5Utils.prx
  3. 32
      Assets/Plugins/PS5/PS5Utils.prx.meta
  4. 28
      Assets/Scripts/EOSNativeHelper.cs
  5. 16
      Assets/Scripts/EpicVoiceChatTest.cs

8
Assets/Plugins/PS5.meta

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 0677463dddd99144083c9a34be65a720
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

BIN
Assets/Plugins/PS5/PS5Utils.prx

32
Assets/Plugins/PS5/PS5Utils.prx.meta

@ -0,0 +1,32 @@
fileFormatVersion: 2
guid: 43fd785f2224f2745a11794c21b7e30c
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
DefaultValueInitialized: true
- first:
PS5: PS5
second:
enabled: 1
settings: {}
userData:
assetBundleName:
assetBundleVariant:

28
Assets/Scripts/EOSNativeHelper.cs

@ -3,6 +3,7 @@ using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using AOT; using AOT;
using Epic.OnlineServices; using Epic.OnlineServices;
using Epic.OnlineServices.Platform;
public static class EOSNativeHelper public static class EOSNativeHelper
{ {
@ -28,29 +29,24 @@ public static class EOSNativeHelper
[DllImport("kernel32")] [DllImport("kernel32")]
public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName); public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
#endif
[UnmanagedFunctionPointer(Config.LibraryCallingConvention)]
public delegate IntPtr AllocateMemoryFunc(IntPtr size, IntPtr alignment);
#else
[MonoPInvokeCallback(typeof(AllocateMemoryFunc))] [MonoPInvokeCallback(typeof(AllocateMemoryFunc))]
public static IntPtr AllocateMemory(IntPtr size, IntPtr alignment)
public static IntPtr AllocateMemory(UIntPtr size, UIntPtr alignment)
{ {
return Marshal.AllocHGlobal(size);
return Marshal.AllocHGlobal((IntPtr)size.ToUInt64());
//return HeapAlloc(GetProcessHeap(), 0, size); //return HeapAlloc(GetProcessHeap(), 0, size);
} }
[UnmanagedFunctionPointer(Config.LibraryCallingConvention)]
public delegate IntPtr ReallocateMemoryFunc(IntPtr pointer, IntPtr size, IntPtr alignment);
[MonoPInvokeCallback(typeof(ReallocateMemoryFunc))] [MonoPInvokeCallback(typeof(ReallocateMemoryFunc))]
public static IntPtr ReallocateMemory(IntPtr pointer, IntPtr size, IntPtr alignment)
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 // EOS will sometimes request a reallocation for a null pointer, so we need to specifically handle that
if (pointer == IntPtr.Zero) if (pointer == IntPtr.Zero)
return Marshal.AllocHGlobal(size);
return Marshal.AllocHGlobal((IntPtr)size.ToUInt64());
return Marshal.ReAllocHGlobal(pointer, size);
return Marshal.ReAllocHGlobal(pointer, (IntPtr)size.ToUInt64());
// if (pointer == IntPtr.Zero) // if (pointer == IntPtr.Zero)
// return HeapAlloc(GetProcessHeap(), 0, size); // return HeapAlloc(GetProcessHeap(), 0, size);
@ -58,9 +54,6 @@ public static class EOSNativeHelper
// return HeapReAlloc(GetProcessHeap(), 0, pointer, size); // return HeapReAlloc(GetProcessHeap(), 0, pointer, size);
} }
[UnmanagedFunctionPointer(Config.LibraryCallingConvention)]
public delegate void ReleaseMemoryFunc(IntPtr pointer);
[MonoPInvokeCallback(typeof(ReleaseMemoryFunc))] [MonoPInvokeCallback(typeof(ReleaseMemoryFunc))]
public static void ReleaseMemory(IntPtr pointer) public static void ReleaseMemory(IntPtr pointer)
{ {
@ -80,4 +73,11 @@ public static class EOSNativeHelper
[DllImport("kernel32")] [DllImport("kernel32")]
[return: MarshalAs(UnmanagedType.Bool)] [return: MarshalAs(UnmanagedType.Bool)]
private static extern bool HeapFree(IntPtr hHeap, int dwFlags, IntPtr lpMem); 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
} }

16
Assets/Scripts/EpicVoiceChatTest.cs

@ -122,15 +122,23 @@ public class EpicVoiceChatTest : MonoBehaviour
yield return new WaitForSeconds(5f); yield return new WaitForSeconds(5f);
#endif #endif
#if UNITY_PS4 || UNITY_PS5
EOSNativeHelper.GetMemoryFunctions(out var allocFunc, out var reallocFunc, out var releaseFunc);
#endif
var result = PlatformInterface.Initialize(new InitializeOptions var result = PlatformInterface.Initialize(new InitializeOptions
{ {
ProductName = "WW1Test", ProductName = "WW1Test",
ProductVersion = "1.0.0.0", ProductVersion = "1.0.0.0",
#if UNITY_GAMECORE || UNITY_PS4 || UNITY_PS5
#if UNITY_GAMECORE
// EOS SDK on Game Core will not initialize without these memory management function pointers // EOS SDK on Game Core will not initialize without these memory management function pointers
AllocateMemoryFunction = Marshal.GetFunctionPointerForDelegate((EOSNativeHelper.AllocateMemoryFunc)EOSNativeHelper.AllocateMemory),
ReallocateMemoryFunction = Marshal.GetFunctionPointerForDelegate((EOSNativeHelper.ReallocateMemoryFunc)EOSNativeHelper.ReallocateMemory),
ReleaseMemoryFunction = Marshal.GetFunctionPointerForDelegate((EOSNativeHelper.ReleaseMemoryFunc)EOSNativeHelper.ReleaseMemory),
AllocateMemoryFunction = Marshal.GetFunctionPointerForDelegate((AllocateMemoryFunc)EOSNativeHelper.AllocateMemory),
ReallocateMemoryFunction = Marshal.GetFunctionPointerForDelegate((ReallocateMemoryFunc)EOSNativeHelper.ReallocateMemory),
ReleaseMemoryFunction = Marshal.GetFunctionPointerForDelegate((ReleaseMemoryFunc)EOSNativeHelper.ReleaseMemory),
#elif UNITY_PS4 || UNITY_PS5
AllocateMemoryFunction = allocFunc,
ReallocateMemoryFunction = reallocFunc,
ReleaseMemoryFunction = releaseFunc,
#endif #endif
}); });

Loading…
Cancel
Save