From 6eb915c129fc90c6f4c82ae097dd6ffad5239efc Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 25 Jan 2021 14:28:30 +0800 Subject: +scripts --- Client/Assets/Scripts/NativePluginHelper.cs | 127 ++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 Client/Assets/Scripts/NativePluginHelper.cs (limited to 'Client/Assets/Scripts/NativePluginHelper.cs') diff --git a/Client/Assets/Scripts/NativePluginHelper.cs b/Client/Assets/Scripts/NativePluginHelper.cs new file mode 100644 index 00000000..e0fbb5f6 --- /dev/null +++ b/Client/Assets/Scripts/NativePluginHelper.cs @@ -0,0 +1,127 @@ +using System; +using System.Collections; +using System.Runtime.InteropServices; +using UnityEngine; +#if UNITY_EDITOR +using UnityEditor; +#endif +public class NativePluginHelper: XUtliPoolLib.INativePlugin +{ + public bool enable = false; + // Unity editor doesn't unload dlls after 'preview' +#if UNITY_EDITOR + static class NativeMethods + { + [DllImport("kernel32.dll")] + public static extern IntPtr LoadLibrary(string dllToLoad); + + [DllImport("kernel32.dll")] + public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName); + + [DllImport("kernel32.dll")] + public static extern bool FreeLibrary(IntPtr hModule); + } + + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + public delegate void InitEngine(); + + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + public delegate void UpdateEngine(); + + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + public delegate void ReadTable(short tableID, short tableType, byte[] buffer, int length); + // Make sure that delegate instances are identical to the actual method being called + InitEngine _InitEngine; + UpdateEngine _UpdateEngine; + ReadTable _ReadTable; + IntPtr? plugin_dll = null; + + public void InitializeHooks() + { + if(enable) + { + plugin_dll = NativeMethods.LoadLibrary(@"Assets/Plugins/x64/NativeClient.dll"); + if (plugin_dll != IntPtr.Zero) + { + IntPtr pAddressOfFunctionToCall = NativeMethods.GetProcAddress(plugin_dll.Value, "InitEngine"); + if (pAddressOfFunctionToCall != IntPtr.Zero) + _InitEngine = (InitEngine)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(InitEngine)); + + pAddressOfFunctionToCall = NativeMethods.GetProcAddress(plugin_dll.Value, "UpdateEngine"); + if (pAddressOfFunctionToCall != IntPtr.Zero) + _UpdateEngine = (UpdateEngine)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(UpdateEngine)); + + pAddressOfFunctionToCall = NativeMethods.GetProcAddress(plugin_dll.Value, "ReadTable"); + if (pAddressOfFunctionToCall != IntPtr.Zero) + _ReadTable = (ReadTable)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(ReadTable)); + } + } + } + public void CloseHooks() + { + if (plugin_dll == null) + { + return; + } + bool result = NativeMethods.FreeLibrary(plugin_dll.Value); + + plugin_dll = null; + } + public void UnloadNativePlugin() + { + } +#elif UNITY_ANDROID + //[DllImport("NativeClient")] + //public delegate void InitEngine(); + + //[DllImport("NativeClient")] + //public delegate void UpdateEngine(); + + public void InitializeHooks(){} + public void CloseHooks(){} +#else + //[DllImport("unityplugin.dll")] + //public delegate void InitEngine(); + + //[DllImport("unityplugin.dll")] + //public delegate void UpdateEngine(); + + public void InitializeHooks(){} + public void CloseHooks(){} +#endif + + public NativePluginHelper() + { + CloseHooks(); + } + + public void Init() + { +#if UNITY_EDITOR + if (_InitEngine != null) + { + _InitEngine(); + } +#endif + } + + public void Update() + { +#if UNITY_EDITOR + if (_UpdateEngine != null) + { + _UpdateEngine(); + } +#endif + } + + public void InputData(short tableID, short tableType, byte[] buffer, int length) + { +#if UNITY_EDITOR + if (_ReadTable != null) + { + _ReadTable(tableID, tableType, buffer, length); + } +#endif + } +} \ No newline at end of file -- cgit v1.1-26-g67d0