forked from Unity-Technologies/UnityDataTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUnityFileSystem.cs
More file actions
93 lines (71 loc) · 2.86 KB
/
UnityFileSystem.cs
File metadata and controls
93 lines (71 loc) · 2.86 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
using System;
using System.IO;
namespace UnityDataTools.FileSystem;
// This is the main entry point. Provides methods to mount archives and open files.
public static class UnityFileSystem
{
public static void Init()
{
// Initialize the native library.
var r = DllWrapper.Init();
if (r != ReturnCode.Success && r != ReturnCode.AlreadyInitialized)
{
HandleErrors(r);
}
}
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 SerializedFile OpenSerializedFile(string path)
{
var r = DllWrapper.OpenSerializedFile(path, out var handle);
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.");
}
}
}