summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/NativePluginHelper.cs
blob: e0fbb5f6a35abf93d1dad21bae2b8deeb06034c3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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
    }
}