-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathResourceLib.cs
More file actions
355 lines (282 loc) · 16.7 KB
/
ResourceLib.cs
File metadata and controls
355 lines (282 loc) · 16.7 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
using System.Runtime.InteropServices;
/**
* ResourceLib .NET bindings
*
* Example usage:
*
* if (ResourceLib.IsResourceTypeSupported("TEMP", ResourceLib.Game.Hitman3))
* {
* // Create a new converter to convert game resources to json.
* var s_Converter = new ResourceLib.ResourceConverter("TEMP", ResourceLib.Game.Hitman3);
* var s_Json = s_Converter.FromResourceFileToJsonString("C:\\somewhere\\0000006196A6C097.TEMP");
* Console.WriteLine(s_Json);
*
* // Create a new generator to convert json to game resource data.
* var s_Generator = new ResourceLib.ResourceGenerator("TEMP", ResourceLib.Game.Hitman3);
* var s_ResourceMem = s_Generator.FromJsonStringToResourceMem(s_Json);
* }
*
* This file (ResourceLib.cs) is licensed under the MIT license.
*
* Copyright (c) 2023 Orfeas Zafeiris
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
public static class ResourceLib
{
private static class Native
{
// ResourceLib exports.
// HM3
[DllImport("ResourceLib_HM3.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr HM3_GetConverterForResource(string p_ResourceType);
[DllImport("ResourceLib_HM3.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr HM3_GetGeneratorForResource(string p_ResourceType);
[DllImport("ResourceLib_HM3.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr HM3_GetSupportedResourceTypes();
[DllImport("ResourceLib_HM3.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void HM3_FreeSupportedResourceTypes(IntPtr p_Array);
[DllImport("ResourceLib_HM3.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern bool HM3_IsResourceTypeSupported(string p_ResourceType);
// HM2
[DllImport("ResourceLib_HM2.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr HM2_GetConverterForResource(string p_ResourceType);
[DllImport("ResourceLib_HM2.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr HM2_GetGeneratorForResource(string p_ResourceType);
[DllImport("ResourceLib_HM2.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr HM2_GetSupportedResourceTypes();
[DllImport("ResourceLib_HM2.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void HM2_FreeSupportedResourceTypes(IntPtr p_Array);
[DllImport("ResourceLib_HM2.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern bool HM2_IsResourceTypeSupported(string p_ResourceType);
// HM2016
[DllImport("ResourceLib_HM2016.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr HM2016_GetConverterForResource(string p_ResourceType);
[DllImport("ResourceLib_HM2016.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr HM2016_GetGeneratorForResource(string p_ResourceType);
[DllImport("ResourceLib_HM2016.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr HM2016_GetSupportedResourceTypes();
[DllImport("ResourceLib_HM2016.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void HM2016_FreeSupportedResourceTypes(IntPtr p_Array);
[DllImport("ResourceLib_HM2016.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern bool HM2016_IsResourceTypeSupported(string p_ResourceType);
// ResourceConverter method declarations.
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public delegate bool FromResourceFileToJsonFileDelegate(string p_ResourceFilePath, string p_OutputFilePath);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate bool FromMemoryToJsonFileDelegate(IntPtr p_ResourceData, UIntPtr p_Size, string p_OutputFilePath);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public delegate IntPtr FromResourceFileToJsonStringDelegate(string p_ResourceFilePath);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate IntPtr FromMemoryToJsonStringDelegate(IntPtr p_ResourceData, UIntPtr p_Size);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void FreeJsonStringDelegate(IntPtr p_JsonString);
// ResourceGenerator method declarations.
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public delegate bool FromJsonFileToResourceFileDelegate(string p_JsonFilePath, string p_ResourceFilePath, bool p_GenerateCompatible);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public delegate bool FromJsonStringToResourceFileDelegate(string p_JsonStr, UIntPtr p_JsonStrLength, string p_ResourceFilePath, bool p_GenerateCompatible);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public delegate IntPtr FromJsonFileToResourceMemDelegate(string p_JsonFilePath, bool p_GenerateCompatible);
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public delegate IntPtr FromJsonStringToResourceMemDelegate(string p_JsonStr, UIntPtr p_JsonStrLength, bool p_GenerateCompatible);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void FreeResourceMemDelegate(IntPtr p_ResourceMem);
// Structs.
[StructLayout(LayoutKind.Sequential)]
public struct JsonString
{
public IntPtr JsonData;
public UIntPtr StrSize;
}
[StructLayout(LayoutKind.Sequential)]
public struct ResourceMem
{
public IntPtr ResourceData;
public UIntPtr DataSize;
}
[StructLayout(LayoutKind.Sequential)]
public struct ResourceTypesArray
{
public IntPtr Types;
public UIntPtr TypeCount;
}
[StructLayout(LayoutKind.Sequential)]
public struct ResourceConverter
{
public FromResourceFileToJsonFileDelegate FromResourceFileToJsonFile;
public FromMemoryToJsonFileDelegate FromMemoryToJsonFile;
public FromResourceFileToJsonStringDelegate FromResourceFileToJsonString;
public FromMemoryToJsonStringDelegate FromMemoryToJsonString;
public FreeJsonStringDelegate FreeJsonString;
}
[StructLayout(LayoutKind.Sequential)]
public struct ResourceGenerator
{
public FromJsonFileToResourceFileDelegate FromJsonFileToResourceFile;
public FromJsonStringToResourceFileDelegate FromJsonStringToResourceFile;
public FromJsonFileToResourceMemDelegate FromJsonFileToResourceMem;
public FromJsonStringToResourceMemDelegate FromJsonStringToResourceMem;
public FreeResourceMemDelegate FreeResourceMem;
}
}
public enum Game
{
Hitman2016,
Hitman2,
Hitman3,
}
public class ResourceConverter
{
private readonly Native.ResourceConverter m_NativeConverter;
public ResourceConverter(string p_ResourceType, Game p_Game)
{
var s_ConverterPtr = p_Game switch
{
Game.Hitman2016 => Native.HM2016_GetConverterForResource(p_ResourceType),
Game.Hitman2 => Native.HM2_GetConverterForResource(p_ResourceType),
Game.Hitman3 => Native.HM3_GetConverterForResource(p_ResourceType),
_ => throw new ArgumentOutOfRangeException(nameof(p_Game), p_Game, null)
};
if (s_ConverterPtr == IntPtr.Zero)
throw new Exception($"ResourceLib does not support converting '{p_ResourceType}' resources for {p_Game}.");
m_NativeConverter = Marshal.PtrToStructure<Native.ResourceConverter>(s_ConverterPtr);
}
public bool FromResourceFileToJsonFile(string p_ResourceFilePath, string p_OutputFilePath)
{
return m_NativeConverter.FromResourceFileToJsonFile(p_ResourceFilePath, p_OutputFilePath);
}
public bool FromMemoryToJsonFile(byte[] p_ResourceData, string p_OutputFilePath)
{
var s_ResourceDataPtr = Marshal.AllocHGlobal(p_ResourceData.Length);
Marshal.Copy(p_ResourceData, 0, s_ResourceDataPtr, p_ResourceData.Length);
var s_Result = m_NativeConverter.FromMemoryToJsonFile(s_ResourceDataPtr, (UIntPtr)p_ResourceData.Length, p_OutputFilePath);
Marshal.FreeHGlobal(s_ResourceDataPtr);
return s_Result;
}
public string FromResourceFileToJsonString(string p_ResourceFilePath)
{
var s_NativeJsonStringPtr = m_NativeConverter.FromResourceFileToJsonString(p_ResourceFilePath);
if (s_NativeJsonStringPtr == IntPtr.Zero)
throw new Exception($"Failed to convert '{p_ResourceFilePath}' to JSON.");
var s_NativeJsonString = Marshal.PtrToStructure<Native.JsonString>(s_NativeJsonStringPtr);
var s_JsonString = Marshal.PtrToStringUTF8(s_NativeJsonString.JsonData, (int)s_NativeJsonString.StrSize);
m_NativeConverter.FreeJsonString(s_NativeJsonStringPtr);
return s_JsonString;
}
public string FromMemoryToJsonString(byte[] p_ResourceData)
{
var s_ResourceDataPtr = Marshal.AllocHGlobal(p_ResourceData.Length);
Marshal.Copy(p_ResourceData, 0, s_ResourceDataPtr, p_ResourceData.Length);
var s_NativeJsonStringPtr = m_NativeConverter.FromMemoryToJsonString(s_ResourceDataPtr, (UIntPtr)p_ResourceData.Length);
Marshal.FreeHGlobal(s_ResourceDataPtr);
if (s_NativeJsonStringPtr == IntPtr.Zero)
throw new Exception($"Failed to convert resource data to JSON.");
var s_NativeJsonString = Marshal.PtrToStructure<Native.JsonString>(s_NativeJsonStringPtr);
var s_JsonString = Marshal.PtrToStringUTF8(s_NativeJsonString.JsonData, (int)s_NativeJsonString.StrSize);
m_NativeConverter.FreeJsonString(s_NativeJsonStringPtr);
return s_JsonString;
}
}
public class ResourceGenerator
{
private readonly Native.ResourceGenerator m_NativeGenerator;
public ResourceGenerator(string p_ResourceType, Game p_Game)
{
var s_GeneratorPtr = p_Game switch
{
Game.Hitman2016 => Native.HM2016_GetGeneratorForResource(p_ResourceType),
Game.Hitman2 => Native.HM2_GetGeneratorForResource(p_ResourceType),
Game.Hitman3 => Native.HM3_GetGeneratorForResource(p_ResourceType),
_ => throw new ArgumentOutOfRangeException(nameof(p_Game), p_Game, null)
};
if (s_GeneratorPtr == IntPtr.Zero)
throw new Exception($"ResourceLib does not support generating '{p_ResourceType}' resources for {p_Game}.");
m_NativeGenerator = Marshal.PtrToStructure<Native.ResourceGenerator>(s_GeneratorPtr);
}
public bool FromJsonFileToResourceFile(string p_JsonFilePath, string p_OutputFilePath, bool p_GenerateCompatible = false)
{
return m_NativeGenerator.FromJsonFileToResourceFile(p_JsonFilePath, p_OutputFilePath, p_GenerateCompatible);
}
public bool FromJsonStringToResourceFile(string p_JsonString, string p_OutputFilePath, bool p_GenerateCompatible = false)
{
return m_NativeGenerator.FromJsonStringToResourceFile(p_JsonString, (UIntPtr)p_JsonString.Length, p_OutputFilePath, p_GenerateCompatible);
}
public byte[] FromJsonFileToResourceMem(string p_JsonFilePath, bool p_GenerateCompatible = false)
{
var s_NativeResourceMemPtr = m_NativeGenerator.FromJsonFileToResourceMem(p_JsonFilePath, p_GenerateCompatible);
if (s_NativeResourceMemPtr == IntPtr.Zero)
throw new Exception($"Failed to convert '{p_JsonFilePath}' to resource data.");
var s_NativeResourceMem = Marshal.PtrToStructure<Native.ResourceMem>(s_NativeResourceMemPtr);
var s_ResourceMem = new byte[(int)s_NativeResourceMem.DataSize];
Marshal.Copy(s_NativeResourceMem.ResourceData, s_ResourceMem, 0, (int)s_NativeResourceMem.DataSize);
m_NativeGenerator.FreeResourceMem(s_NativeResourceMemPtr);
return s_ResourceMem;
}
public byte[] FromJsonStringToResourceMem(string p_JsonString, bool p_GenerateCompatible = false)
{
var s_NativeResourceMemPtr = m_NativeGenerator.FromJsonStringToResourceMem(p_JsonString, (UIntPtr)p_JsonString.Length, p_GenerateCompatible);
if (s_NativeResourceMemPtr == IntPtr.Zero)
throw new Exception($"Failed to convert JSON to resource data.");
var s_NativeResourceMem = Marshal.PtrToStructure<Native.ResourceMem>(s_NativeResourceMemPtr);
var s_ResourceMem = new byte[(int)s_NativeResourceMem.DataSize];
Marshal.Copy(s_NativeResourceMem.ResourceData, s_ResourceMem, 0, (int)s_NativeResourceMem.DataSize);
m_NativeGenerator.FreeResourceMem(s_NativeResourceMemPtr);
return s_ResourceMem;
}
}
public static int[] GetSupportedResourceTypes(Game p_Game)
{
var s_SupportedResourcesPtr = p_Game switch
{
Game.Hitman2016 => Native.HM2016_GetSupportedResourceTypes(),
Game.Hitman2 => Native.HM2_GetSupportedResourceTypes(),
Game.Hitman3 => Native.HM3_GetSupportedResourceTypes(),
_ => throw new ArgumentOutOfRangeException(nameof(p_Game), p_Game, null)
};
if (s_SupportedResourcesPtr == IntPtr.Zero)
throw new Exception($"Could not get supported resource types for {p_Game}.");
var s_SupportedResourcesArray = Marshal.PtrToStructure<Native.ResourceTypesArray>(s_SupportedResourcesPtr);
var s_SupportedResources = new int[(int)s_SupportedResourcesArray.TypeCount];
Marshal.Copy(s_SupportedResourcesArray.Types, s_SupportedResources, 0, (int)s_SupportedResourcesArray.TypeCount);
switch (p_Game)
{
case Game.Hitman2016:
Native.HM2016_FreeSupportedResourceTypes(s_SupportedResourcesPtr);
break;
case Game.Hitman2:
Native.HM2_FreeSupportedResourceTypes(s_SupportedResourcesPtr);
break;
case Game.Hitman3:
Native.HM3_FreeSupportedResourceTypes(s_SupportedResourcesPtr);
break;
}
return s_SupportedResources;
}
public static bool IsResourceTypeSupported(string p_ResourceType, Game p_Game)
{
return p_Game switch
{
Game.Hitman2016 => Native.HM2016_IsResourceTypeSupported(p_ResourceType),
Game.Hitman2 => Native.HM2_IsResourceTypeSupported(p_ResourceType),
Game.Hitman3 => Native.HM3_IsResourceTypeSupported(p_ResourceType),
_ => throw new ArgumentOutOfRangeException(nameof(p_Game), p_Game, null)
};
}
}