-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIntegratorUtils.cs
More file actions
171 lines (150 loc) · 5.67 KB
/
IntegratorUtils.cs
File metadata and controls
171 lines (150 loc) · 5.67 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;
using UAssetAPI;
namespace AstroModIntegrator
{
public class UString
{
public string Value;
public Encoding Encoding;
public UString(string value, Encoding encoding)
{
Value = value;
Encoding = encoding;
}
public UString()
{
}
}
public static class IntegratorUtils
{
public static readonly UE4Version EngineVersion = UE4Version.VER_UE4_23;
public static readonly Version CurrentVersion = new Version(1, 3, 1, 0);
public static readonly string[] IgnoredModIDs = new string[]
{
"AstroModIntegrator"
};
internal static void CopySplitUp(Stream input, Stream output, int start, int leng)
{
input.Seek(start, SeekOrigin.Begin);
output.Seek(0, SeekOrigin.Begin);
byte[] buffer = new byte[32768];
int read;
while (leng > 0 && (read = input.Read(buffer, 0, Math.Min(buffer.Length, leng))) > 0)
{
output.Write(buffer, 0, read);
leng -= read;
}
}
public static byte[] Concatenate(byte[] one, byte[] two)
{
byte[] final = new byte[one.Length + two.Length];
Buffer.BlockCopy(one, 0, final, 0, one.Length);
Buffer.BlockCopy(two, 0, final, one.Length, two.Length);
return final;
}
public static void SplitExportFiles(UAsset y, string desiredPath, Dictionary<string, byte[]> createdPakData)
{
MemoryStream newData = y.WriteData();
long breakingOffPoint = y.Exports[0].SerialOffset;
using (MemoryStream assetFile = new MemoryStream((int)breakingOffPoint))
{
CopySplitUp(newData, assetFile, 0, (int)breakingOffPoint);
createdPakData[Path.ChangeExtension(desiredPath, ".uasset")] = assetFile.ToArray();
}
int lengthOfRest = (int)(newData.Length - breakingOffPoint);
using (MemoryStream exportFile = new MemoryStream(lengthOfRest))
{
CopySplitUp(newData, exportFile, (int)breakingOffPoint, lengthOfRest);
createdPakData[Path.ChangeExtension(desiredPath, ".uexp")] = exportFile.ToArray();
}
}
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 UString ReadUStringWithGUIDAndEncoding(this BinaryReader reader, out uint guid)
{
UString str = reader.ReadUStringWithEncoding();
if (!string.IsNullOrEmpty(str.Value))
{
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:
string nullTerminatedStr = str + "\0";
writer.Write(encoding is UnicodeEncoding ? -nullTerminatedStr.Length : nullTerminatedStr.Length);
writer.Write(encoding.GetBytes(nullTerminatedStr));
break;
}
}
public static void WriteUString(this BinaryWriter writer, UString str)
{
WriteUString(writer, str?.Value, str?.Encoding);
}
public static readonly Regex GameRegex = new Regex(@"^\/Game\/", RegexOptions.Compiled);
public static string ConvertGamePathToAbsolutePath(this string gamePath)
{
if (!GameRegex.IsMatch(gamePath)) return string.Empty;
string newPath = GameRegex.Replace(gamePath, "Astro/Content/", 1);
if (Path.HasExtension(newPath)) return newPath;
return Path.ChangeExtension(newPath, ".uasset");
}
public static string GetEnumMemberAttrValue(this object enumVal)
{
EnumMemberAttribute attr = enumVal.GetType().GetMember(enumVal.ToString())[0].GetCustomAttributes(false).OfType<EnumMemberAttribute>().FirstOrDefault();
return attr?.Value;
}
}
}