forked from atenfyr/UAssetAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cs
More file actions
96 lines (85 loc) · 2.8 KB
/
Copy pathUtils.cs
File metadata and controls
96 lines (85 loc) · 2.8 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
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
namespace UAssetAPI
{
public static class Utils
{
public static UString ReadUStringWithEncoding(this BinaryReader reader)
{
int length = reader.ReadInt32();
switch (length)
{
case 0:
return null;
default:
if (length < 0)
{
byte[] data = reader.ReadBytes(-length * 2);
return new UString(Encoding.Unicode.GetString(data, 0, data.Length - 2), Encoding.Unicode);
}
else
{
byte[] data = reader.ReadBytes(length);
return new UString(Encoding.ASCII.GetString(data, 0, data.Length - 1), Encoding.ASCII);
}
}
}
public static string ReadUString(this BinaryReader reader)
{
return ReadUStringWithEncoding(reader)?.Value;
}
public static string ReadUStringWithGUID(this BinaryReader reader, out uint guid)
{
string str = reader.ReadUString();
if (!string.IsNullOrEmpty(str))
{
guid = reader.ReadUInt32();
}
else
{
guid = 0;
}
return str;
}
public static void WriteUString(this BinaryWriter writer, string str, Encoding encoding = null)
{
if (encoding == null) encoding = Encoding.ASCII;
switch (str)
{
case null:
writer.Write((int)0);
break;
default:
int realLen = str.Length + 1;
if (encoding.Equals(Encoding.Unicode)) realLen = -realLen;
writer.Write(realLen);
writer.Write(encoding.GetBytes(str));
for (int k = 0; k < encoding.GetByteCount(new char[] { 'a' }); k++)
{
writer.Write((byte)0);
}
break;
}
}
public static T Clamp<T>(T val, T min, T max) where T : IComparable<T>
{
if (val.CompareTo(min) < 0) return min;
else if (val.CompareTo(max) > 0) return max;
else return val;
}
public static void WriteUString(this BinaryWriter writer, UString str)
{
WriteUString(writer, str?.Value, str?.Encoding);
}
public static int GetLinkIndex(int index)
{
return -(index + 1);
}
public static int GetNormalIndex(int index)
{
return -(index + 1);
}
}
}