From fda69261ca44bcbf16d8d562df591c513372b850 Mon Sep 17 00:00:00 2001 From: Nico de Poel Date: Wed, 26 Jan 2022 18:14:03 +0100 Subject: [PATCH] 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. --- Assets/Plugins/PS5.meta | 8 --- Assets/Plugins/PS5/PS5Utils.prx | Bin 77008 -> 0 bytes Assets/Plugins/PS5/PS5Utils.prx.meta | 32 --------- Assets/Scripts/EOSNativeHelper.cpp | 52 ++++++++++++++ Assets/Scripts/EOSNativeHelper.cpp.meta | 87 ++++++++++++++++++++++++ Assets/Scripts/EOSNativeHelper.cs | 10 ++- Assets/Scripts/EpicVoiceChatTest.cs | 10 +-- 7 files changed, 146 insertions(+), 53 deletions(-) delete mode 100644 Assets/Plugins/PS5.meta delete mode 100644 Assets/Plugins/PS5/PS5Utils.prx delete mode 100644 Assets/Plugins/PS5/PS5Utils.prx.meta create mode 100644 Assets/Scripts/EOSNativeHelper.cpp create mode 100644 Assets/Scripts/EOSNativeHelper.cpp.meta diff --git a/Assets/Plugins/PS5.meta b/Assets/Plugins/PS5.meta deleted file mode 100644 index 5991894..0000000 --- a/Assets/Plugins/PS5.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 0677463dddd99144083c9a34be65a720 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/Plugins/PS5/PS5Utils.prx b/Assets/Plugins/PS5/PS5Utils.prx deleted file mode 100644 index 831cccab3b4e47b4ce396f284bfd5e24b380c762..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 77008 zcmeHQe{dVsoqsD?j%+8f9fx2@AW9t5&=$pZ661sfti*C;a1tf90|};zEU#rHvZSn5 zsgn{A4fLX#OcU-VN3ZPxJzD6s2kso#$w2|r(9j=rE@h@ir@eM)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Xy2U-4+<6_)s&&**%k^LM)(jcjA3YPHaHjq9-$G%EW*&V zYKx@;f2+3G_+J(5o@JSZ0fU_^h?A_W3U0A#-6f6yFeN?Rl4QVGHEgE{mw8l)7XKIq zj%_A!KI{-z#K!yM{}jR+P1%9nSR%8$Q7)NT0j9ffwn32H(9LHcIHc?@n0{a{)ZwsZ z&yu{s$GPxVDzMyv^{;vlme**zsH^pc@*p<9;I~_}sqbkDU(+s7YCR;hne~g(7eXaJ z9@o)W&b4#FmcV^5zo_<=4WEGXXKI{Vql9)Aep zk$cs@X95%`9^NWr>U*=W&OU$c0SCP0w&Ka-1r6xr~D=a zLs8$;-DM(PeNRUo@hz6hU&a3|*f3t@ufFHkxJ3LY_^_yPu*ZK6d@k5jy!xJgOvXQm z!BA8@_Ju{se}X!G(^v7Hv*-o>%0>Sxglg?ooHBe3DtPTu^I!eHLjf6&&lgybKP)PL zmCy5F6FqUawL5{Y-JlFx7Ew`g%6twg#o{l@_^i!oQ`vt(JRZNjj8+Mx8YktqAOFw6 zVQ&+$CuBT6m*y8c_E+Gv+YtXB^Mv(jo71MUcT2?Qss++%3$UpS=cnC<{oh|BtS3;( zD|YPVGQKGfgViOq0{Q(C$FJI1ULyXNbpr8qTe~Vw$q&hR8;pHvp+H<~bEr6FzM(|? zD>D8o3Mi#Lep`w7yZ9F}#@hc#{2m!^=kGZg|Dvr!r7F1(DmIFFkH>Ieu;9rF1@E(S sVN>Uyn%DRyT%Eg!Q{~uIBK}_4jI!HQhVPk*#m`+TkQ;3$j6ME;0kr?)LI3~& diff --git a/Assets/Plugins/PS5/PS5Utils.prx.meta b/Assets/Plugins/PS5/PS5Utils.prx.meta deleted file mode 100644 index b640be5..0000000 --- a/Assets/Plugins/PS5/PS5Utils.prx.meta +++ /dev/null @@ -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: diff --git a/Assets/Scripts/EOSNativeHelper.cpp b/Assets/Scripts/EOSNativeHelper.cpp new file mode 100644 index 0000000..911d14b --- /dev/null +++ b/Assets/Scripts/EOSNativeHelper.cpp @@ -0,0 +1,52 @@ +#include + +#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 +#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; +} diff --git a/Assets/Scripts/EOSNativeHelper.cpp.meta b/Assets/Scripts/EOSNativeHelper.cpp.meta new file mode 100644 index 0000000..5c7c0b9 --- /dev/null +++ b/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: diff --git a/Assets/Scripts/EOSNativeHelper.cs b/Assets/Scripts/EOSNativeHelper.cs index 7f8265d..48edc7d 100644 --- a/Assets/Scripts/EOSNativeHelper.cs +++ b/Assets/Scripts/EOSNativeHelper.cs @@ -30,7 +30,10 @@ public static class EOSNativeHelper [DllImport("kernel32")] 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))] 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); #endif - -#if UNITY_PS4 || UNITY_PS5 - [DllImport("PS5Utils.prx")] - public static extern void GetMemoryFunctions(out IntPtr allocFunc, out IntPtr reallocFunc, out IntPtr releaseFunc); -#endif } diff --git a/Assets/Scripts/EpicVoiceChatTest.cs b/Assets/Scripts/EpicVoiceChatTest.cs index 138c522..7210146 100644 --- a/Assets/Scripts/EpicVoiceChatTest.cs +++ b/Assets/Scripts/EpicVoiceChatTest.cs @@ -122,7 +122,7 @@ public class EpicVoiceChatTest : MonoBehaviour yield return new WaitForSeconds(5f); #endif -#if UNITY_PS4 || UNITY_PS5 +#if !UNITY_STANDALONE EOSNativeHelper.GetMemoryFunctions(out var allocFunc, out var reallocFunc, out var releaseFunc); #endif @@ -130,12 +130,8 @@ public class EpicVoiceChatTest : MonoBehaviour { ProductName = "WW1Test", 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, ReallocateMemoryFunction = reallocFunc, ReleaseMemoryFunction = releaseFunc,