-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTianLi.TruthEye.Impl.cs
226 lines (197 loc) · 8.39 KB
/
TianLi.TruthEye.Impl.cs
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using Sirenix.Utilities;
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using UnityEngine;
/// <summary>
/// TianLi.TruthEye 的 C API
/// </summary>
namespace TianLi
{
public class TruthEyeApis
{
// void (* progress)(int, int)
public delegate void Progress_Type(int current, int total);
public delegate bool ImplAsyncDownload_Type(Progress_Type progress);
public delegate bool ImplLoad_Type(byte[] path = null, bool isReload = false);
public delegate bool ImplLoadVersion_Type(byte[] version);
public delegate bool ImplFree_Type();
public delegate bool CreateWindow_Type();
public delegate bool DestroyWindow_Type();
public delegate bool ShowWindow_Type();
public delegate bool HideWindow_Type();
public delegate bool SetJsonParams_Type(byte[] json_buff, int buff_size);
public IntPtr TianLiTruthEye_Impl_Async_Download_Func = IntPtr.Zero;
public IntPtr TianLiTruthEye_Impl_Load_Func = IntPtr.Zero;
public IntPtr TianLiTruthEye_Impl_Load_Version_Func = IntPtr.Zero;
public IntPtr TianLiTruthEye_Impl_Free_Func = IntPtr.Zero;
public IntPtr TianLiTruthEye_CreateWindow_Func = IntPtr.Zero;
public IntPtr TianLiTruthEye_DestroyWindow_Func = IntPtr.Zero;
public IntPtr TianLiTruthEye_ShowWindow_Func = IntPtr.Zero;
public IntPtr TianLiTruthEye_HideWindow_Func = IntPtr.Zero;
public IntPtr TianLiTruthEye_SetJsonParams_Func = IntPtr.Zero;
public bool IsLoad = false;
private bool getFuncName(string fieldName, ref string funcName)
{
if (fieldName.Contains("_") == false) return false;
if (fieldName.Substring(fieldName.Length - 4, 4) != "Func") return false;
funcName = fieldName.Substring(0, fieldName.Length - 5);
return true;
}
public delegate IntPtr GetAddress(IntPtr lib, String funcName);
public bool LoadApis(IntPtr libPtr, GetAddress getAddr)
{
if (libPtr == IntPtr.Zero) return false;
// 获取apis类的成员变量列表
FieldInfo[] fields = GetType().GetFields();
foreach (FieldInfo field in fields)
{
string name = "";
if (!getFuncName(field.Name, ref name)) continue;
IntPtr funcPtr = getAddr(libPtr, name);
if (funcPtr == IntPtr.Zero) return false;
field.SetValue(this, funcPtr);
}
IsLoad = true;
return true;
}
public bool FreeApis(IntPtr libPtr)
{
if (libPtr != IntPtr.Zero) return false;
// 获取apis类的成员变量列表
FieldInfo[] fields = GetType().GetFields();
foreach (FieldInfo field in fields)
{
string name = "";
if (!getFuncName(field.Name, ref name)) continue;
IntPtr funcPtr = (IntPtr)field.GetValue(this);
field.SetValue(this, IntPtr.Zero);
}
IsLoad = false;
return true;
}
public void ImplAsyncDownload(Progress_Type progress)
{
if (TianLiTruthEye_Impl_Async_Download_Func == IntPtr.Zero) return;
ImplAsyncDownload_Type func = (ImplAsyncDownload_Type)Marshal.GetDelegateForFunctionPointer(TianLiTruthEye_Impl_Async_Download_Func, typeof(ImplAsyncDownload_Type));
func(progress);
}
public bool ImplLoad(byte[] path = null, bool isReload = false)
{
if (TianLiTruthEye_Impl_Load_Func == IntPtr.Zero) return false;
ImplLoad_Type func = (ImplLoad_Type)Marshal.GetDelegateForFunctionPointer(TianLiTruthEye_Impl_Load_Func, typeof(ImplLoad_Type));
return func(path, isReload);
}
public bool ImplLoadVersion(byte[] version)
{
if (TianLiTruthEye_Impl_Load_Version_Func == IntPtr.Zero) return false;
ImplLoadVersion_Type func = (ImplLoadVersion_Type)Marshal.GetDelegateForFunctionPointer(TianLiTruthEye_Impl_Load_Version_Func, typeof(ImplLoadVersion_Type));
return func(version);
}
public bool ImplFree()
{
if (TianLiTruthEye_Impl_Free_Func == IntPtr.Zero) return false;
ImplFree_Type func = (ImplFree_Type)Marshal.GetDelegateForFunctionPointer(TianLiTruthEye_Impl_Free_Func, typeof(ImplFree_Type));
return func();
}
public bool CreateWindow()
{
if (TianLiTruthEye_CreateWindow_Func == IntPtr.Zero) return false;
CreateWindow_Type func = (CreateWindow_Type)Marshal.GetDelegateForFunctionPointer(TianLiTruthEye_CreateWindow_Func, typeof(CreateWindow_Type));
return func();
}
public bool DestroyWindow()
{
if (TianLiTruthEye_DestroyWindow_Func == IntPtr.Zero) return false;
DestroyWindow_Type func = (DestroyWindow_Type)Marshal.GetDelegateForFunctionPointer(TianLiTruthEye_DestroyWindow_Func, typeof(DestroyWindow_Type));
return func();
}
public bool ShowWindow()
{
if (TianLiTruthEye_ShowWindow_Func == IntPtr.Zero) return false;
ShowWindow_Type func = (ShowWindow_Type)Marshal.GetDelegateForFunctionPointer(TianLiTruthEye_ShowWindow_Func, typeof(ShowWindow_Type));
return func();
}
public bool HideWindow()
{
if (TianLiTruthEye_HideWindow_Func == IntPtr.Zero) return false;
HideWindow_Type func = (HideWindow_Type)Marshal.GetDelegateForFunctionPointer(TianLiTruthEye_HideWindow_Func, typeof(HideWindow_Type));
return func();
}
public bool SetJsonParams(byte[] json_buff, int buff_size)
{
if (TianLiTruthEye_SetJsonParams_Func == IntPtr.Zero) return false;
SetJsonParams_Type func = (SetJsonParams_Type)Marshal.GetDelegateForFunctionPointer(TianLiTruthEye_SetJsonParams_Func, typeof(SetJsonParams_Type));
return func(json_buff, buff_size);
}
}
public class TruthEyeLibrary
{
static public string LibPath = "TianLi.TruthEye.Inface.dll";
static public IntPtr LibPtr = IntPtr.Zero;
static public TruthEyeApis Apis = new TruthEyeApis();
public bool IsLoaded()
{
return Apis.IsLoad;
}
public bool Load(string libPath)
{
LibPath = libPath;
if (LibPtr != IntPtr.Zero) return false;
LibPtr = LoadLibraryW(LibPath);
if (LibPtr == IntPtr.Zero) return false;
return Apis.LoadApis(LibPtr, GetProcAddress);
}
public bool Free()
{
if (LibPtr == IntPtr.Zero) return false;
bool ret = FreeLibrary(LibPtr);
if (ret) LibPtr = IntPtr.Zero;
return Apis.FreeApis(LibPtr);
}
public void ImplAsyncDownload(TruthEyeApis.Progress_Type progress)
{
Apis.ImplAsyncDownload(progress);
}
public bool ImplLoad(byte[] path = null, bool isReload = false)
{
return Apis.ImplLoad(path, isReload);
}
public bool ImplLoadVersion(string version)
{
byte[] version_buff = System.Text.Encoding.Default.GetBytes(version);
return Apis.ImplLoadVersion(version_buff);
}
public bool ImplFree()
{
return Apis.ImplFree();
}
public bool CreateWindow()
{
return Apis.CreateWindow();
}
public bool DestroyWindow()
{
return Apis.DestroyWindow();
}
public bool ShowWindow()
{
return Apis.ShowWindow();
}
public bool HideWindow()
{
return Apis.HideWindow();
}
public bool SetJsonParams(string json)
{
byte[] json_buff = System.Text.Encoding.Default.GetBytes(json);
return Apis.SetJsonParams(json_buff, json_buff.Length);
}
[DllImport("kernel32.dll")]
public static extern IntPtr LoadLibraryW([In()][MarshalAs(System.Runtime.InteropServices.UnmanagedType.LPWStr)] string path);
[DllImport("kernel32.dll")]
public static extern bool FreeLibrary(IntPtr lib);
[DllImport("kernel32.dll")]
public static extern IntPtr GetProcAddress(IntPtr lib, String funcName);
}
}