using System; using System.IO; using System.Runtime.InteropServices; public static class SystemLibrary { #if UNITY_STANDALONE_WIN || UNITY_GAMECORE [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); #elif UNITY_PS4 || UNITY_PS5 [DllImport("SceKernelShim.prx", EntryPoint = "LoadLibrary")] private static extern Int32 LoadLibraryNative(string filename); public static IntPtr LoadLibrary(string filename) { Int32 moduleHandle = LoadLibraryNative(filename); if (moduleHandle <= 0) { UnityEngine.Debug.LogError($"Error loading PRX module '{filename}', error code = 0x{moduleHandle:X}"); return IntPtr.Zero; } return new IntPtr(moduleHandle); } [DllImport("SceKernelShim.prx", EntryPoint = "FreeLibrary")] private static extern bool FreeLibraryNative(Int32 moduleHandle); public static bool FreeLibrary(IntPtr hModule) { return FreeLibraryNative(hModule.ToInt32()); } [DllImport("SceKernelShim.prx")] public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName); #endif }