-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathDllWrapper.cs
More file actions
323 lines (270 loc) · 10.9 KB
/
DllWrapper.cs
File metadata and controls
323 lines (270 loc) · 10.9 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
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace UnityDataTools.FileSystem;
public class UnityArchiveHandle : SafeHandle
{
public UnityArchiveHandle() : base(IntPtr.Zero, true)
{
}
public override bool IsInvalid => handle == IntPtr.Zero;
protected override bool ReleaseHandle()
{
return DllWrapper.UnmountArchive(handle) == ReturnCode.Success;
}
}
public class UnityFileHandle : SafeHandle
{
public UnityFileHandle() : base(IntPtr.Zero, true)
{
}
public override bool IsInvalid => handle == IntPtr.Zero;
protected override bool ReleaseHandle()
{
return DllWrapper.CloseFile(handle) == ReturnCode.Success;
}
}
public class SerializedFileHandle : SafeHandle
{
public SerializedFileHandle() : base(IntPtr.Zero, true)
{
}
public override bool IsInvalid => handle == IntPtr.Zero;
protected override bool ReleaseHandle()
{
return DllWrapper.CloseSerializedFile(handle) == ReturnCode.Success;
}
}
public class TypeTreeHandle : SafeHandle
{
public TypeTreeHandle() : base(IntPtr.Zero, true)
{
}
public override bool IsInvalid => handle == IntPtr.Zero;
protected override bool ReleaseHandle()
{
return true;
}
internal IntPtr Handle => handle;
}
public enum ReturnCode
{
Success,
AlreadyInitialized,
NotInitialized,
FileNotFound,
FileFormatError,
InvalidArgument,
HigherSerializedFileVersion,
DestinationBufferTooSmall,
InvalidObjectId,
UnknownError,
FileError,
ErrorCreatingArchiveFile,
ErrorAddingFileToArchive,
TypeNotFound,
}
[Flags]
public enum ArchiveNodeFlags
{
None = 0,
Directory = 1 << 0,
Deleted = 1 << 1,
SerializedFile = 1 << 2,
}
public enum CompressionType
{
None,
Lzma,
Lz4,
Lz4HC,
};
public enum SeekOrigin
{
Begin,
Current,
End,
}
public enum ExternalReferenceType
{
NonAssetType,
DeprecatedCachedAssetType,
SerializedAssetType,
MetaAssetType,
}
[StructLayout(LayoutKind.Sequential)]
public struct ObjectInfo
{
public readonly long Id;
public readonly long Offset;
public readonly long Size;
public readonly int TypeId;
public ObjectInfo(long id, long offset, long size, int typeId)
{
Id = id;
Offset = offset;
Size = size;
TypeId = typeId;
}
}
[Flags]
public enum TypeTreeFlags
{
None = 0,
IsArray = 1 << 0,
IsManagedReference = 1 << 1,
IsManagedReferenceRegistry = 1 << 2,
IsArrayOfRefs = 1 << 3,
}
[Flags]
public enum TypeTreeMetaFlags
{
None = 0,
AlignBytes = 1 << 14,
AnyChildUsesAlignBytes = 1 << 15,
}
public enum TypeTreeCategory
{
ObjectType = 0,
RefType = 1,
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct TypeTreeInfo
{
public readonly int TypeId;
public readonly int SerializedSize;
public readonly TypeTreeCategory Category;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public readonly uint[] Hash;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public readonly string ClassName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public readonly string NamespaceName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public readonly string AssemblyName;
}
public static class DllWrapper
{
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_Init")]
public static extern ReturnCode Init();
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_Cleanup")]
public static extern ReturnCode Cleanup();
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_MountArchive")]
public static extern ReturnCode MountArchive([MarshalAs(UnmanagedType.LPStr)] string path, [MarshalAs(UnmanagedType.LPStr)] string mountPoint, out UnityArchiveHandle handle);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_UnmountArchive")]
public static extern ReturnCode UnmountArchive(IntPtr handle);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_GetArchiveNodeCount")]
public static extern ReturnCode GetArchiveNodeCount(UnityArchiveHandle handle, out int count);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_GetArchiveNode")]
public static extern ReturnCode GetArchiveNode(UnityArchiveHandle handle, int nodeIndex, StringBuilder path, int pathLen, out long size, out ArchiveNodeFlags flags);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_CreateArchive")]
public static extern ReturnCode CreateArchive([MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr)] string[] sourceFiles,
[MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPStr)] string[] aliases, bool[] isSerializedFile, int count,
[MarshalAs(UnmanagedType.LPStr)] string archiveFile, CompressionType compression, out int crc);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_OpenFile")]
public static extern ReturnCode OpenFile([MarshalAs(UnmanagedType.LPStr)] string path, out UnityFileHandle handle);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl, EntryPoint = "UFS_ReadFile")]
public static extern ReturnCode ReadFile(UnityFileHandle handle, long size,
[MarshalAs(UnmanagedType.LPArray)] byte[] buffer, out long actualSize);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_SeekFile")]
public static extern ReturnCode SeekFile(UnityFileHandle handle, long offset, SeekOrigin origin, out long newPosition);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_GetFileSize")]
public static extern ReturnCode GetFileSize(UnityFileHandle handle, out long size);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_CloseFile")]
public static extern ReturnCode CloseFile(IntPtr handle);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_OpenSerializedFile")]
public static extern ReturnCode OpenSerializedFile([MarshalAs(UnmanagedType.LPStr)] string path, out SerializedFileHandle handle);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_CloseSerializedFile")]
public static extern ReturnCode CloseSerializedFile(IntPtr handle);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_GetExternalReferenceCount")]
public static extern ReturnCode GetExternalReferenceCount(SerializedFileHandle handle, out int count);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_GetExternalReference")]
public static extern ReturnCode GetExternalReference(SerializedFileHandle handle, int index, StringBuilder path, int pathLen, StringBuilder guid, out ExternalReferenceType type);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_GetObjectCount")]
public static extern ReturnCode GetObjectCount(SerializedFileHandle handle, out int count);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_GetObjectInfo")]
public static extern ReturnCode GetObjectInfo(SerializedFileHandle handle, [In, Out] ObjectInfo[] objectData, int len);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_GetTypeTree")]
public static extern ReturnCode GetTypeTree(SerializedFileHandle handle, long objectId, out TypeTreeHandle typeTree);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_GetRefTypeTypeTree")]
public static extern ReturnCode GetRefTypeTypeTree(SerializedFileHandle handle, [MarshalAs(UnmanagedType.LPStr)] string className,
[MarshalAs(UnmanagedType.LPStr)] string namespaceName, [MarshalAs(UnmanagedType.LPStr)] string assemblyName, out TypeTreeHandle typeTree);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_AddTypeTreeSourceFromFile")]
public static extern ReturnCode AddTypeTreeSourceFromFile([MarshalAs(UnmanagedType.LPStr)] string path, out long handle);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_GetTypeTreeNodeInfo")]
public static extern ReturnCode GetTypeTreeNodeInfo(TypeTreeHandle handle, int node, StringBuilder type, int typeLen,
StringBuilder name, int nameLen, out int offset, out int size, [MarshalAs(UnmanagedType.U4)] out TypeTreeFlags flags,
[MarshalAs(UnmanagedType.U4)] out TypeTreeMetaFlags metaFlags, out int firstChildNode,
out int nextNode);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_GetDllVersion")]
public static extern ReturnCode GetDllVersion(out int version);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_GetUnityVersion")]
public static extern ReturnCode GetUnityVersion(StringBuilder version, int versionLen);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_GetSerializedFileVersion")]
public static extern ReturnCode GetSerializedFileVersion(SerializedFileHandle handle, out int version);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_GetTypeTreeCount")]
public static extern ReturnCode GetTypeTreeCount(SerializedFileHandle handle, out int count);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_GetTypeTreeInfo")]
public static extern ReturnCode GetTypeTreeInfo(SerializedFileHandle handle, int index, out TypeTreeInfo info);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_GetTypeTreeByIndex")]
public static extern ReturnCode GetTypeTreeByIndex(SerializedFileHandle handle, int index, out TypeTreeHandle typeTree);
[DllImport("UnityFileSystemApi",
CallingConvention = CallingConvention.Cdecl,
EntryPoint = "UFS_RemoveTypeTreeSource")]
public static extern ReturnCode RemoveTypeTreeSource(long handle);
}