-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProfile.cs
266 lines (228 loc) · 8.75 KB
/
Profile.cs
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
using System;
using System.Buffers.Binary;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
namespace bagpipe {
class ProfileUpdateEventArgs : EventArgs {
public string Path;
public ProfileUpdateEventArgs(string Path) {
this.Path = Path;
}
}
class Profile {
public event EventHandler<ProfileUpdateEventArgs> ProfileLoaded;
public event EventHandler<ProfileUpdateEventArgs> ProfileSaved;
public readonly ObservableCollection<ProfileEntry> Entries = new ObservableCollection<ProfileEntry>();
public bool Load(string path) {
Entries.Clear();
byte[] decompressedData;
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read)) {
fs.SeekSafe(20, SeekOrigin.Begin);
// Don't really know if this is signed or not, but it should never practically matter
int size = BinaryPrimitives.ReadInt32BigEndian(fs.ReadByteArray(4));
using (MemoryStream ms = new MemoryStream()) {
fs.CopyTo(ms);
try {
decompressedData = LZO.Decompress(size, ms.GetBuffer(), 0, (int)ms.Length);
} catch (Win32Exception ex) {
throw new IOException(ex.Message, ex);
}
}
}
bool unknownData = false;
using (MemoryStream ms = new MemoryStream(decompressedData)) {
int entryCount = BinaryPrimitives.ReadInt32BigEndian(ms.ReadByteArray(4));
for (int i = 0; i < entryCount; i++) {
ProfileEntry entry = new ProfileEntry();
entry.Owner = (OnlineProfilePropertyOwner)ms.ReadByteSafe();
if (
entry.Owner != OnlineProfilePropertyOwner.Game
&& entry.Owner != OnlineProfilePropertyOwner.OnlineService
) {
unknownData = true;
}
entry.ID = BinaryPrimitives.ReadInt32BigEndian(ms.ReadByteArray(4));
entry.Type = (SettingsDataType)ms.ReadByteSafe();
switch (entry.Type) {
// This is signed (confirmed via testing badass rank)
case SettingsDataType.Int32: {
entry.Value = BinaryPrimitives.ReadInt32BigEndian(ms.ReadByteArray(4));
break;
}
case SettingsDataType.String: {
int len = BinaryPrimitives.ReadInt32BigEndian(ms.ReadByteArray(4));
entry.Value = Encoding.ASCII.GetString(ms.ReadByteArray(len));
break;
}
case SettingsDataType.Float: {
entry.Value = BitConverter.Int32BitsToSingle(
BinaryPrimitives.ReadInt32BigEndian(ms.ReadByteArray(4))
);
break;
}
case SettingsDataType.Blob: {
int len = BinaryPrimitives.ReadInt32BigEndian(ms.ReadByteArray(4));
entry.Value = ms.ReadByteArray(len);
break;
}
case SettingsDataType.Byte: {
entry.Value = ms.ReadByteSafe();
break;
}
// Haven't encountered these in practice, but can make decent educated guesses
case SettingsDataType.Empty: {
unknownData = true;
break;
}
case SettingsDataType.Int64: {
unknownData = true;
entry.Value = BinaryPrimitives.ReadInt64BigEndian(ms.ReadByteArray(8));
break;
}
case SettingsDataType.Double: {
unknownData = true;
entry.Value = BitConverter.Int64BitsToDouble(
BinaryPrimitives.ReadInt64BigEndian(ms.ReadByteArray(8))
);
break;
}
/*
This one's harder to guess, the only definite hint we have is getters/setters which take two seperate int values
UnrealScript ints are 32bit, so we're probably looking at 8 bytes data
UE4 has a FDateTime struct, which uses int64 ticks - maybe it just splits this value in two out of neccesity?
https://docs.unrealengine.com/4.26/en-US/API/Runtime/Core/Misc/FDateTime/
Very conveniently, this seems to use the exact same format as dotnet's DateTime
In practice, nothing ever sets values of this type, so doesn't really matter if this is wrong
*/
case SettingsDataType.DateTime: {
unknownData = true;
entry.Value = new DateTime(BinaryPrimitives.ReadInt64BigEndian(ms.ReadByteArray(8)));
break;
}
default: {
unknownData = true;
break;
}
}
entry.AdvertisementType = (OnlineDataAdvertisementType)ms.ReadByteSafe();
if (entry.AdvertisementType != OnlineDataAdvertisementType.DontAdvertise) {
unknownData = true;
}
Entries.Add(entry);
}
if (ms.Position != ms.Length) {
bool allowed = false;
// Allow a single trailing 1 (AoDK magic)
if (ms.Length - ms.Position == 1) {
int val = ms.ReadByte();
if (val == 1) {
allowed = true;
// Don't reset position so the zero padding check doesn't include it
} else {
ms.Position--;
}
}
// Allow trailing zero padding
allowed |= ms.ReadByteArray((int)(ms.Length - ms.Position)).All(x => x == 0);
if (!allowed) {
unknownData = true;
}
}
}
ProfileLoaded?.Invoke(this, new ProfileUpdateEventArgs(path));
return unknownData;
}
private byte[] GetDecompressedData() {
using (MemoryStream ms = new MemoryStream()) {
void WriteInt32(int val) {
byte[] buf = new byte[4];
BinaryPrimitives.WriteInt32BigEndian(buf, val);
ms.Write(buf);
}
void WriteInt64(long val) {
byte[] buf = new byte[8];
BinaryPrimitives.WriteInt64BigEndian(buf, val);
ms.Write(buf);
}
WriteInt32(Entries.Count);
foreach (ProfileEntry entry in Entries) {
ms.WriteByte((byte)entry.Owner);
WriteInt32(entry.ID);
ms.WriteByte((byte)entry.Type);
switch (entry.Type) {
case SettingsDataType.Int32: {
WriteInt32((int)entry.Value);
break;
}
case SettingsDataType.String: {
WriteInt32(((string)entry.Value).Length);
ms.Write(Encoding.ASCII.GetBytes((string)entry.Value));
break;
}
case SettingsDataType.Float: {
WriteInt32(BitConverter.SingleToInt32Bits((float)entry.Value));
break;
}
case SettingsDataType.Blob: {
WriteInt32(((byte[])entry.Value).Length);
ms.Write((byte[])entry.Value);
break;
}
case SettingsDataType.Byte: {
ms.WriteByte((byte)entry.Value);
break;
}
// Educated guesses
case SettingsDataType.Empty: {
break;
}
case SettingsDataType.Int64: {
WriteInt64((long)entry.Value);
break;
}
case SettingsDataType.Double: {
WriteInt64(BitConverter.DoubleToInt64Bits((double)entry.Value));
break;
}
case SettingsDataType.DateTime: {
WriteInt64(((DateTime)entry.Value).Ticks);
break;
}
default: {
throw new IOException($"Unable to encode entry of type {entry.Type}!");
}
}
ms.WriteByte((byte)entry.AdvertisementType);
}
ms.WriteByte(1);
return ms.ToArray();
}
}
public bool IsOverSizeLimit() => GetDecompressedData().Length > 9000;
public void Save(string path) {
byte[] decompressed = GetDecompressedData();
byte[] compressed;
try {
compressed = LZO.Compress(decompressed, 0, decompressed.Length);
} catch (Win32Exception ex) {
throw new IOException(ex.Message, ex);
}
using (SHA1Managed sha1 = new SHA1Managed()) {
byte[] decompressedSize = new byte[4];
BinaryPrimitives.WriteInt32BigEndian(decompressedSize, decompressed.Length);
sha1.TransformBlock(decompressedSize, 0, decompressedSize.Length, null, 0);
sha1.TransformFinalBlock(compressed, 0, compressed.Length);
using (FileStream fs = new FileStream(path, FileMode.Create)) {
fs.Write(sha1.Hash);
fs.Write(decompressedSize);
fs.Write(compressed);
}
ProfileSaved?.Invoke(this, new ProfileUpdateEventArgs(path));
}
}
}
}