Browse Source

Changed memory allocation routines into a C++ plugin which will includes right into the IL2CPP build. No need for any separate dynamic libraries anymore. Also works for Game Core platforms.

The only platform *not* working at the moment is PS4, because it seems Unity doesn't include the C++ file properly during build.
master
Nico de Poel 4 years ago
parent
commit
fda69261ca
  1. 8
      Assets/Plugins/PS5.meta
  2. BIN
      Assets/Plugins/PS5/PS5Utils.prx
  3. 32
      Assets/Plugins/PS5/PS5Utils.prx.meta
  4. 52
      Assets/Scripts/EOSNativeHelper.cpp
  5. 87
      Assets/Scripts/EOSNativeHelper.cpp.meta
  6. 10
      Assets/Scripts/EOSNativeHelper.cs
  7. 10
      Assets/Scripts/EpicVoiceChatTest.cs

8
Assets/Plugins/PS5.meta

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

BIN
Assets/Plugins/PS5/PS5Utils.prx

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

@ -1,32 +0,0 @@
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:

52
Assets/Scripts/EOSNativeHelper.cpp

@ -0,0 +1,52 @@
#include <stdlib.h>
#if defined(__ORBIS__) || defined(__PROSPERO__)
#define _aligned_malloc(s,a) aligned_alloc(a,s)
#define _aligned_realloc(p,s,a) reallocalign(p,s,a)
#define _aligned_free(p) free(p)
#else
#include <malloc.h>
#endif
#define DLL_EXPORT extern "C" __declspec(dllexport)
static void *AllocateMemory(size_t size, size_t alignment)
{
if (size == 0)
return nullptr;
return _aligned_malloc(size, alignment);
}
static void *ReallocateMemory(void *ptr, size_t size, size_t alignment)
{
if (size == 0)
{
free(ptr);
return nullptr;
}
// EOS will sometimes request a reallocation for a null pointer, so we need to specifically handle that
if (ptr == nullptr)
{
return _aligned_malloc(size, alignment);
}
return _aligned_realloc(ptr, size, alignment);
}
static void ReleaseMemory(void *ptr)
{
return _aligned_free(ptr);
}
typedef void* (*AllocateMemoryFunc)(size_t, size_t);
typedef void* (*ReallocateMemoryFunc)(void*, size_t, size_t);
typedef void (*ReleaseMemoryFunc)(void*);
DLL_EXPORT void EOS_GetMemoryFunctions(AllocateMemoryFunc *allocFunc, ReallocateMemoryFunc *reallocFunc, ReleaseMemoryFunc *releaseFunc)
{
*allocFunc = AllocateMemory;
*reallocFunc = ReallocateMemory;
*releaseFunc = ReleaseMemory;
}

87
Assets/Scripts/EOSNativeHelper.cpp.meta

@ -0,0 +1,87 @@
fileFormatVersion: 2
guid: 9e01755363649f24bbc946e6dd5dd6ef
PluginImporter:
externalObjects: {}
serializedVersion: 2
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
- first:
: Any
second:
enabled: 0
settings:
Exclude Editor: 1
Exclude GameCoreScarlett: 0
Exclude GameCoreXboxOne: 0
Exclude Linux64: 1
Exclude OSXUniversal: 1
Exclude PS4: 0
Exclude PS5: 0
Exclude Win: 1
Exclude Win64: 1
- first:
Any:
second:
enabled: 0
settings: {}
- first:
Editor: Editor
second:
enabled: 0
settings:
CPU: AnyCPU
DefaultValueInitialized: true
OS: AnyOS
- first:
GameCoreScarlett: GameCoreScarlett
second:
enabled: 1
settings: {}
- first:
GameCoreXboxOne: GameCoreXboxOne
second:
enabled: 1
settings: {}
- first:
PS4: PS4
second:
enabled: 1
settings: {}
- first:
PS5: PS5
second:
enabled: 1
settings: {}
- first:
Standalone: Linux64
second:
enabled: 0
settings:
CPU: None
- first:
Standalone: OSXUniversal
second:
enabled: 0
settings:
CPU: None
- first:
Standalone: Win
second:
enabled: 0
settings:
CPU: None
- first:
Standalone: Win64
second:
enabled: 0
settings:
CPU: None
userData:
assetBundleName:
assetBundleVariant:

10
Assets/Scripts/EOSNativeHelper.cs

@ -30,7 +30,10 @@ 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);
#else
#elif !UNITY_STANDALONE
[DllImport("__Internal", EntryPoint = "EOS_GetMemoryFunctions")]
public static extern void GetMemoryFunctions(out IntPtr allocFunc, out IntPtr reallocFunc, out IntPtr releaseFunc);
[MonoPInvokeCallback(typeof(AllocateMemoryFunc))] [MonoPInvokeCallback(typeof(AllocateMemoryFunc))]
public static IntPtr AllocateMemory(UIntPtr size, UIntPtr alignment) public static IntPtr AllocateMemory(UIntPtr size, UIntPtr alignment)
@ -75,9 +78,4 @@ public static class EOSNativeHelper
private static extern bool HeapFree(IntPtr hHeap, int dwFlags, IntPtr lpMem); private static extern bool HeapFree(IntPtr hHeap, int dwFlags, IntPtr lpMem);
#endif #endif
#if UNITY_PS4 || UNITY_PS5
[DllImport("PS5Utils.prx")]
public static extern void GetMemoryFunctions(out IntPtr allocFunc, out IntPtr reallocFunc, out IntPtr releaseFunc);
#endif
} }

10
Assets/Scripts/EpicVoiceChatTest.cs

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

Loading…
Cancel
Save