Repository navigation
Expand file tree
/
Copy pathUnityFileSystem.cs
More file actions
147 lines (115 loc) · 4.89 KB
/
Copy pathUnityFileSystem.cs
File metadata and controls
147 lines (115 loc) · 4.89 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
using System;
using System.IO;
using System.Text;
namespace UnityDataTools.FileSystem;
// This is the main entry point. Provides methods to mount archives and open files.
public static class UnityFileSystem
{
// Serialized file version 26 (Unity 6.7) needs the shared subtree and registry frame entry
// points, which arrived together in this library version. Older files are read through the
// same library, so there is no reason to keep a fallback for an earlier one.
public const int RequiredDllVersion = 2;
public static void Init()
{
// Initialize the native library.
var r = DllWrapper.Init();
if (r != ReturnCode.Success && r != ReturnCode.AlreadyInitialized)
{
HandleErrors(r);
}
var dllVersion = GetDllVersion();
if (dllVersion < RequiredDllVersion)
{
throw new NotSupportedException(
$"UnityFileSystemApi version {dllVersion} is too old; version {RequiredDllVersion} or newer is required. " +
"Replace the UnityFileSystemApi library shipped beside UnityDataTool with one from Unity 6.7 or newer.");
}
}
public static void Cleanup()
{
// Uninitialize the native library.
var r = DllWrapper.Cleanup();
if (r != ReturnCode.Success && r != ReturnCode.NotInitialized)
{
HandleErrors(r);
}
}
public static UnityArchive MountArchive(string path, string mountPoint)
{
var r = DllWrapper.MountArchive(path, mountPoint, out var handle);
HandleErrors(r, path);
return new UnityArchive() { m_Handle = handle };
}
public static UnityFile OpenFile(string path)
{
var r = DllWrapper.OpenFile(path, out var handle);
UnityFileSystem.HandleErrors(r, path);
return new UnityFile() { m_Handle = handle };
}
public static long AddTypeTreeSourceFromFile(string path)
{
var r = DllWrapper.AddTypeTreeSourceFromFile(path, out var handle);
HandleErrors(r, path);
return handle;
}
public static void RemoveTypeTreeSource(long handle)
{
var r = DllWrapper.RemoveTypeTreeSource(handle);
HandleErrors(r);
}
public static int GetDllVersion()
{
var r = DllWrapper.GetDllVersion(out var version);
HandleErrors(r);
return version;
}
public static string GetUnityVersion()
{
var version = new StringBuilder(256);
var r = DllWrapper.GetUnityVersion(version, version.Capacity);
HandleErrors(r);
return version.ToString();
}
public static SerializedFile OpenSerializedFile(string path)
{
var r = DllWrapper.OpenSerializedFile(path, out var handle);
if (r == ReturnCode.UnknownError)
throw new SerializedFileOpenException(path);
UnityFileSystem.HandleErrors(r, path);
return new SerializedFile() { m_Handle = handle };
}
internal static void HandleErrors(ReturnCode returnCode, string filename = "")
{
switch (returnCode)
{
case ReturnCode.AlreadyInitialized:
throw new InvalidOperationException("UnityFileSystem is already initialized.");
case ReturnCode.NotInitialized:
throw new InvalidOperationException("UnityFileSystem is not initialized.");
case ReturnCode.FileNotFound:
throw new FileNotFoundException("File not found.", filename);
case ReturnCode.FileFormatError:
throw new NotSupportedException($"Invalid file format reading {filename}.");
case ReturnCode.InvalidArgument:
throw new ArgumentException();
case ReturnCode.HigherSerializedFileVersion:
throw new NotSupportedException("SerializedFile version not supported.");
case ReturnCode.DestinationBufferTooSmall:
throw new ArgumentException("Destination buffer too small.");
case ReturnCode.InvalidObjectId:
throw new ArgumentException("Invalid object id.");
case ReturnCode.UnknownError:
throw new Exception("Unknown error.");
case ReturnCode.FileError:
throw new IOException("File operation error.");
case ReturnCode.TypeNotFound:
throw new ArgumentException("Type not found.");
case ReturnCode.HigherTypeTreeVersion:
throw new NotSupportedException($"A TypeTree in {filename} was written by a newer version of Unity.");
// Every node is read through the subtree API, so the native side has no reason to ask
// for it. Reaching this means a walk was added that bypasses TypeTreeNode.
case ReturnCode.RequiresSubtreeApi:
throw new InvalidOperationException("Shared subtree reference read through the non-subtree API.");
}
}
}