-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProjectMerger.cs
More file actions
521 lines (436 loc) · 23.8 KB
/
ProjectMerger.cs
File metadata and controls
521 lines (436 loc) · 23.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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using Frosty.Controls;
using Frosty.Core;
using Frosty.Core.Controls;
using Frosty.Core.Windows;
using FrostySdk;
using FrostySdk.IO;
using FrostySdk.Managers;
using FrostySdk.Resources;
namespace ProjectMerger
{
public class ProjectMergerMenuExtension : MenuExtension
{
public override string TopLevelMenuName { get; } = "File";
public override string MenuItemName { get; } = "Import from Project";
public override RelayCommand MenuItemClicked => new RelayCommand((o) =>
{
FrostyOpenFileDialog openFileDialog = new FrostyOpenFileDialog("Select a project file", "Project File (*.fbproject)|*.fbproject", "FrostyProject");
if (!openFileDialog.ShowDialog()) return;
FrostyTaskWindow.Show("Importing project...", "", task =>
{
using (NativeReader reader = new NativeReader(new FileStream(openFileDialog.FileName, FileMode.Open, FileAccess.Read)))
{
ulong magic = reader.ReadULong();
if (magic != 0x00005954534F5246)
{
MessageBoxResult result = FrostyMessageBox.Show(
"This project file appears to be a legacy project, and importing it could corrupt the current project file. Are you sure you wish to continue?",
"Project Merger", MessageBoxButton.YesNo);
if (result == MessageBoxResult.No) return;
}
try
{
#region Useless shit
uint version = reader.ReadUInt();
if (version < 9) return;
//Bunch of stuff we don't care about we just want to get to the bundles and assets
reader.ReadNullTerminatedString();
reader.ReadLong();
reader.ReadLong();
reader.ReadUInt();
reader.ReadNullTerminatedString();
reader.ReadNullTerminatedString();
reader.ReadNullTerminatedString();
reader.ReadNullTerminatedString();
reader.ReadNullTerminatedString();
//Read the images the mod has(pointless but idk how to skip these other then just reading them outright)
int size = reader.ReadInt();
if (size > 0)
{
reader.ReadBytes(size);
}
for (int i = 0; i < 4; i++)
{
size = reader.ReadInt();
if (size > 0)
{
reader.ReadBytes(size);
}
}
reader.ReadInt();
#endregion
//FINALLY! We get to the good part; reading the contents of the project
#region Adding stuff(mostly dummies)
//All of this is just copy pasted from FrostyProject.LoadInternal()
#region Added Bundles
// bundles
int numItems = reader.ReadInt();
for (int i = 0; i < numItems; i++)
{
string name = reader.ReadNullTerminatedString();
string sbName = reader.ReadNullTerminatedString();
BundleType type = (BundleType)reader.ReadInt();
App.AssetManager.AddBundle(name, type, App.AssetManager.GetSuperBundleId(sbName));
}
#endregion
//this doesn't ACTUALLY load the Ebx, this just sets up dummy files for us to later write to
#region EBX
numItems = reader.ReadInt();
for (int i = 0; i < numItems; i++)
{
EbxAssetEntry entry = new EbxAssetEntry
{
Name = reader.ReadNullTerminatedString(),
Guid = reader.ReadGuid()
};
App.AssetManager.AddEbx(entry);
entry.IsDirty = true;
}
#endregion
//Same applies here
#region Res & Chunks
// res
numItems = reader.ReadInt();
for (int i = 0; i < numItems; i++)
{
ResAssetEntry entry = new ResAssetEntry
{
Name = reader.ReadNullTerminatedString(),
ResRid = reader.ReadULong(),
ResType = reader.ReadUInt(),
ResMeta = reader.ReadBytes(0x10)
};
App.AssetManager.AddRes(entry);
}
// chunks
numItems = reader.ReadInt();
for (int i = 0; i < numItems; i++)
{
ChunkAssetEntry newEntry = new ChunkAssetEntry
{
Id = reader.ReadGuid(),
H32 = reader.ReadInt()
};
App.AssetManager.AddChunk(newEntry);
}
#endregion
#endregion
#region Writing data(for ebx and stuff)
Dictionary<int, AssetEntry> h32map = new Dictionary<int, AssetEntry>();
bool allowOverwrite = false;
bool userDecision = false;
#region Ebx
numItems = reader.ReadInt();
for (int i = 0; i < numItems; i++)
{
#region Getting original data
string name = reader.ReadNullTerminatedString();
List<AssetEntry> linkedEntries = FrostyProject.LoadLinkedAssets(reader);
List<int> bundles = new List<int>();
if (version >= 13)
{
int length = reader.ReadInt();
for (int j = 0; j < length; j++)
{
string bundleName = reader.ReadNullTerminatedString();
int bid = App.AssetManager.GetBundleId(bundleName);
if (bid != -1)
bundles.Add(bid);
}
}
bool isModified = reader.ReadBoolean();
bool isTransientModified = false;
string userData = "";
byte[] data = null;
bool modifiedResource = false;
if (isModified)
{
isTransientModified = reader.ReadBoolean();
if (version >= 12)
userData = reader.ReadNullTerminatedString();
if (version < 13)
{
int length = reader.ReadInt();
for (int j = 0; j < length; j++)
{
string bundleName = reader.ReadNullTerminatedString();
int bid = App.AssetManager.GetBundleId(bundleName);
if (bid != -1)
bundles.Add(bid);
}
}
if (version >= 13)
modifiedResource = reader.ReadBoolean();
data = reader.ReadBytes(reader.ReadInt());
}
#endregion
#region Writing data to project
EbxAssetEntry entry = App.AssetManager.GetEbxEntry(name);
if (!userDecision && !entry.IsDirty && entry.IsModified)
{
MessageBoxResult result = FrostyMessageBox.Show(
"Would you like me to overwrite modified files in this project with those from the imported one?",
"Project Merger", MessageBoxButton.YesNo);
if (result == MessageBoxResult.Yes)
{
allowOverwrite = true;
}
userDecision = true;
}
if (entry != null && (allowOverwrite || (entry.IsDirty || !entry.IsModified)))
{
entry.LinkedAssets.AddRange(linkedEntries);
entry.AddedBundles.AddRange(bundles);
if (isModified)
{
entry.ModifiedEntry = new ModifiedAssetEntry
{
IsTransientModified = isTransientModified,
UserData = userData
};
if (modifiedResource)
{
// store as modified resource data object
entry.ModifiedEntry.DataObject = ModifiedResource.Read(data);
}
else
{
if (!entry.IsAdded && App.PluginManager.GetCustomHandler(entry.Type) != null)
{
// @todo: throw some kind of error
}
// store as a regular ebx
using (EbxReader ebxReader = EbxReader.CreateProjectReader(new MemoryStream(data)))
{
EbxAsset asset = ebxReader.ReadAsset<EbxAsset>();
entry.ModifiedEntry.DataObject = asset;
if (entry.IsAdded)
entry.Type = asset.RootObject.GetType().Name;
entry.ModifiedEntry.DependentAssets.AddRange(asset.Dependencies);
}
}
entry.OnModified();
entry.IsDirty = true;
}
int hash = Utils.HashString(entry.Name);
if (!h32map.ContainsKey(hash))
h32map.Add(hash, entry);
}
#endregion
}
#endregion
#region Res
// res
numItems = reader.ReadInt();
for (int i = 0; i < numItems; i++)
{
string name = reader.ReadNullTerminatedString();
List<AssetEntry> linkedEntries = FrostyProject.LoadLinkedAssets(reader);
List<int> bundles = new List<int>();
if (version >= 13)
{
int length = reader.ReadInt();
for (int j = 0; j < length; j++)
{
string bundleName = reader.ReadNullTerminatedString();
int bid = App.AssetManager.GetBundleId(bundleName);
if (bid != -1)
bundles.Add(bid);
}
}
bool isModified = reader.ReadBoolean();
Sha1 sha1 = Sha1.Zero;
long originalSize = 0;
byte[] resMeta = null;
byte[] data = null;
string userData = "";
if (isModified)
{
sha1 = reader.ReadSha1();
originalSize = reader.ReadLong();
int length = reader.ReadInt();
if (length > 0)
resMeta = reader.ReadBytes(length);
if (version >= 12)
userData = reader.ReadNullTerminatedString();
if (version < 13)
{
length = reader.ReadInt();
for (int j = 0; j < length; j++)
{
string bundleName = reader.ReadNullTerminatedString();
int bid = App.AssetManager.GetBundleId(bundleName);
if (bid != -1)
bundles.Add(bid);
}
}
data = reader.ReadBytes(reader.ReadInt());
}
ResAssetEntry entry = App.AssetManager.GetResEntry(name);
if (entry != null && (allowOverwrite || (entry.IsDirty || !entry.IsModified)))
{
entry.LinkedAssets.AddRange(linkedEntries);
entry.AddedBundles.AddRange(bundles);
if (isModified)
{
entry.ModifiedEntry = new ModifiedAssetEntry
{
Sha1 = sha1,
OriginalSize = originalSize,
ResMeta = resMeta,
UserData = userData
};
if (sha1 == Sha1.Zero)
{
// store as modified resource data object
entry.ModifiedEntry.DataObject = ModifiedResource.Read(data);
}
else
{
if (!entry.IsAdded && App.PluginManager.GetCustomHandler((ResourceType)entry.ResType) != null)
{
// @todo: throw some kind of error here
}
// store as normal data
entry.ModifiedEntry.Data = data;
}
entry.OnModified();
}
int hash = Utils.HashString(entry.Name);
if (!h32map.ContainsKey(hash))
h32map.Add(hash, entry);
}
}
#endregion
#region Chunk
// chunks
numItems = reader.ReadInt();
for (int i = 0; i < numItems; i++)
{
Guid id = reader.ReadGuid();
List<int> bundles = new List<int>();
if (version >= 13)
{
int length = reader.ReadInt();
for (int j = 0; j < length; j++)
{
string bundleName = reader.ReadNullTerminatedString();
int bid = App.AssetManager.GetBundleId(bundleName);
if (bid != -1)
bundles.Add(bid);
}
}
Sha1 sha1 = Sha1.Zero;
uint logicalOffset = 0;
uint logicalSize = 0;
uint rangeStart = 0;
uint rangeEnd = 0;
int firstMip = -1;
int h32 = 0;
bool addToChunkBundles = false;
string userData = "";
byte[] data = null;
if (version > 13)
{
firstMip = reader.ReadInt();
h32 = reader.ReadInt();
}
bool isModified = true;
if (version >= 13)
isModified = reader.ReadBoolean();
if (isModified)
{
sha1 = reader.ReadSha1();
logicalOffset = reader.ReadUInt();
logicalSize = reader.ReadUInt();
rangeStart = reader.ReadUInt();
rangeEnd = reader.ReadUInt();
if (version < 14)
{
firstMip = reader.ReadInt();
h32 = reader.ReadInt();
}
addToChunkBundles = reader.ReadBoolean();
if (version >= 12)
userData = reader.ReadNullTerminatedString();
if (version < 13)
{
int length = reader.ReadInt();
for (int j = 0; j < length; j++)
{
string bundleName = reader.ReadNullTerminatedString();
int bid = App.AssetManager.GetBundleId(bundleName);
if (bid != -1)
bundles.Add(bid);
}
}
data = reader.ReadBytes(reader.ReadInt());
}
ChunkAssetEntry entry = App.AssetManager.GetChunkEntry(id);
if (entry == null && isModified && (allowOverwrite || (entry.IsDirty || !entry.IsModified)))
{
// hack: since chunks are not modified by FrostEd patches, instead a new one
// is added when something that uses a chunk is modified. If an existing chunk
// from a project is missing, a new one is created, and its linked resource
// is used to fill in the bundles (this may fail if a chunk is not meant to be
// in any bundles)
ChunkAssetEntry newEntry = new ChunkAssetEntry
{
Id = id,
H32 = h32
};
App.AssetManager.AddChunk(newEntry);
if (h32map.ContainsKey(newEntry.H32))
{
foreach (int bundleId in h32map[newEntry.H32].Bundles)
newEntry.AddToBundle(bundleId);
}
entry = newEntry;
}
if (entry != null)
{
entry.AddedBundles.AddRange(bundles);
if (isModified)
{
entry.ModifiedEntry = new ModifiedAssetEntry
{
Sha1 = sha1,
LogicalOffset = logicalOffset,
LogicalSize = logicalSize,
RangeStart = rangeStart,
RangeEnd = rangeEnd,
FirstMip = firstMip,
H32 = h32,
AddToChunkBundle = addToChunkBundles,
UserData = userData,
Data = data
};
entry.OnModified();
}
else
{
entry.H32 = h32;
entry.FirstMip = firstMip;
}
}
}
#endregion
#endregion
}
catch (Exception e)
{
App.Logger.LogError("Project merging has failed! As a result of this, saving may lead to the current project corrupting. Please be careful!");
}
if (reader != null)
{
((IDisposable)reader).Dispose();
}
}
});
});
}
}