Skip to content

Commit f5da78d

Browse files
committed
fenris: updates
1 parent d07f396 commit f5da78d

9 files changed

Lines changed: 68 additions & 21 deletions

File tree

TACTLib/Core/Product/Fenris/CoreTOC.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,44 @@ namespace TACTLib.Core.Product.Fenris;
1010

1111
public class CoreTOC {
1212
[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 0xC)]
13-
public struct TOCEntry {
13+
public record struct TOCEntry {
1414
public SnoHandle Sno;
1515
public int NameOffset;
1616
}
1717

1818
public int[] GroupCounts { get; }
1919
public int[] GroupOffsets { get; }
2020
public int[] GroupCounts2 { get; }
21+
public uint[] GroupFormats { get; }
2122
public uint PrimaryId { get; }
22-
public Dictionary<SnoHandle, string> Files { get; } = new();
23+
public Dictionary<SnoHandle, string> Files { get; } = [];
2324

2425
public CoreTOC(Stream? stream, EncryptedSnos encrypted) {
2526
using var _ = new PerfCounter("CoreTOC::cctor`Stream`EncryptedSnos");
2627
if (stream == null) {
2728
throw new ArgumentNullException(nameof(stream));
2829
}
2930

31+
var magic = stream.Read<uint>();
32+
var isNew = magic == 0xBCDE6611;
33+
if (!isNew) {
34+
stream.Position = 0;
35+
}
36+
3037
var count = stream.Read<int>();
3138
GroupCounts = stream.ReadArray<int>(count);
3239
GroupOffsets = stream.ReadArray<int>(count);
3340
GroupCounts2 = stream.ReadArray<int>(count);
41+
GroupFormats = isNew ? stream.ReadArray<uint>(count) : [];
3442

3543
PrimaryId = stream.Read<uint>();
3644

3745
var baseOffset = stream.Position;
3846

39-
Span<byte> stringBuffer = stackalloc byte[0xFF];
47+
Span<byte> stringBuffer = stackalloc byte[0x1FF];
4048
for (var i = 0; i < count; ++i) {
4149
// I'm assuming that GroupCounts2 is the count but with replaced files included.
42-
Debug.Assert(GroupCounts[i] == GroupCounts2[i], "GroupCounts[i] == GroupCounts2[i]");
50+
Debug.Assert(GroupCounts[i] == GroupCounts2[i]);
4351
stream.Position = baseOffset + GroupOffsets[i];
4452

4553
var stringOffset = baseOffset + GroupOffsets[i] + 0xC * GroupCounts[i];

TACTLib/Core/Product/Fenris/EncryptedNameDict.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace TACTLib.Core.Product.Fenris;
88

99
public class EncryptedNameDict {
10-
public Dictionary<SnoHandle, string> Files { get; } = new();
10+
public Dictionary<SnoHandle, string> Files { get; } = [];
1111

1212
public EncryptedNameDict(Stream stream) {
1313
var magic = stream.Read<uint>();

TACTLib/Core/Product/Fenris/EncryptedSnos.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ namespace TACTLib.Core.Product.Fenris;
99

1010
public class EncryptedSnos {
1111
[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 0x10)]
12-
public struct EncryptedSno {
12+
public record struct EncryptedSno {
1313
public SnoHandle Sno;
1414
public ulong KeyId;
1515
}
1616

1717
public EncryptedSno[] Entries { get; }
18-
public Dictionary<SnoHandle, ulong> Lookup = new();
18+
public Dictionary<SnoHandle, ulong> Lookup = [];
1919

2020
public EncryptedSnos(Stream? stream) {
2121
using var _ = new PerfCounter("EncryptedSnos::cctor`Stream");
2222
if (stream == null) {
23-
Entries = Array.Empty<EncryptedSno>();
23+
Entries = [];
2424
return;
2525
}
2626

TACTLib/Core/Product/Fenris/ProductHandler_Fenris.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class ProductHandler_Fenris : IProductHandler {
2121
public ReplacedSnos ReplacedSnos { get; }
2222
public SharedPayloadsMapping SharedPayloads { get; }
2323
public CoreTOC TOC { get; }
24-
public Dictionary<ulong, EncryptedNameDict> EncryptedNameDicts = new();
24+
public Dictionary<ulong, EncryptedNameDict> EncryptedNameDicts = [];
2525

2626
#endregion
2727

@@ -166,6 +166,23 @@ private void LoadLocale() {
166166
return null;
167167
}
168168

169+
public SnoHandle LocateHandle(uint id) {
170+
// i assume if replacedId is zero then it's deleted
171+
if (ReplacedSnos.Lookup.TryGetValue(id, out var replacedId)) {
172+
id = replacedId;
173+
}
174+
175+
if (id is 0 or uint.MaxValue) { // 0 = null
176+
if (LogLevel >= 1) {
177+
Logger.Debug("Fenris", $"{id} is deleted");
178+
}
179+
180+
return SnoHandle.Invalid;
181+
}
182+
183+
return TOC.Files.Keys.FirstOrDefault(file => file.Id == id, SnoHandle.Invalid);
184+
}
185+
169186
// logLevel 2 logs reads, logLevel 1 logs missing.
170187
public Stream? OpenFile(uint id, SnoType type, uint subId = 0, bool waterfall = true) {
171188
// i assume if replacedId is zero then it's deleted

TACTLib/Core/Product/Fenris/ReplacedSnos.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ public struct ReplacedSno {
1616
}
1717

1818
public ReplacedSno[] Entries { get; }
19-
public Dictionary<uint, uint> Lookup = new();
19+
public Dictionary<uint, uint> Lookup = [];
2020

2121
public ReplacedSnos(Stream? stream) {
2222
using var _ = new PerfCounter("ReplacedSnos::cctor`Stream");
2323
if (stream == null) {
24-
Entries = Array.Empty<ReplacedSno>();
24+
Entries = [];
2525
return;
2626
}
2727

TACTLib/Core/Product/Fenris/SharedPayloadsMapping.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
namespace TACTLib.Core.Product.Fenris;
66

77
public class SharedPayloadsMapping {
8-
public Dictionary<uint, uint> SharedPayloads { get; set; } = new();
9-
public Dictionary<SnoChild, SnoChild> SharedChildren { get; set; } = new();
8+
public Dictionary<uint, uint> SharedPayloads { get; set; } = [];
9+
public Dictionary<SnoChild, SnoChild> SharedChildren { get; set; } = [];
1010

1111
public SharedPayloadsMapping(Stream? stream) {
1212
using var _ = new PerfCounter("SharedPayloadsMapping::cctor`Stream");

TACTLib/Core/Product/Fenris/SnoChild.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace TACTLib.Core.Product.Fenris;
44

55
[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 0x8)]
6-
public struct SnoChild {
6+
public record struct SnoChild {
77
public uint SnoId;
88
public uint SubId;
99
}

TACTLib/Core/Product/Fenris/SnoHandle.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace TACTLib.Core.Product.Fenris;
44

55
[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 0x8)]
6-
public struct SnoHandle {
6+
public record struct SnoHandle {
77
public uint Group; // 0xFFFFFFFF if none
88
public uint Id; // 0 if none
9-
}
9+
public static SnoHandle Invalid { get; } = new() { Group = uint.MaxValue };
10+
public bool IsInvalid => Group == uint.MaxValue && Id == 0;
11+
}

TACTLib/Core/Product/Fenris/SnoManifest.cs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,20 @@ public enum SnoManifestRole {
1313
}
1414

1515
public class SnoManifest {
16-
1716
public Locale Locale { get; set; }
1817
public SnoManifestRole Role { get; set; }
1918
public HashSet<uint> SnoIds { get; }
19+
public HashSet<uint> SnoIds2 { get; }
2020
public HashSet<SnoChild> ChildIds { get; }
21+
public HashSet<SnoChild> ChildIds2 { get; }
2122

2223
public SnoManifest(Stream? stream) { // used to assist lookups, i think.
2324
using var _ = new PerfCounter("SnoManifest::cctor`Stream");
2425
if (stream == null) {
25-
SnoIds = new HashSet<uint>();
26-
ChildIds = new HashSet<SnoChild>();
26+
SnoIds = [];
27+
ChildIds = [];
28+
SnoIds2 = [];
29+
ChildIds2 = [];
2730
return;
2831
}
2932

@@ -39,13 +42,30 @@ public SnoManifest(Stream? stream) { // used to assist lookups, i think.
3942
count = stream.Read<int>();
4043
var childIds = stream.ReadArray<SnoChild>(count);
4144
ChildIds = new HashSet<SnoChild>(childIds);
45+
46+
if (stream.Position == stream.Length) {
47+
SnoIds2 = [];
48+
ChildIds2 = [];
49+
return;
50+
}
51+
52+
count = stream.Read<int>();
53+
var childIds2 = stream.ReadArray<SnoChild>(count);
54+
ChildIds2 = new HashSet<SnoChild>(childIds2);
55+
56+
count = stream.Read<int>();
57+
var snoIds2 = stream.ReadArray<uint>(count);
58+
SnoIds2 = new HashSet<uint>(snoIds2);
4259
}
4360

4461
public bool ContainsChild(uint id, uint subId) =>
45-
ChildIds.Contains(new SnoChild() {
62+
ChildIds.Contains(new SnoChild {
63+
SnoId = id,
64+
SubId = subId,
65+
}) || ChildIds2.Contains(new SnoChild {
4666
SnoId = id,
4767
SubId = subId,
4868
});
4969

50-
public bool Contains(uint id) => SnoIds.Contains(id);
70+
public bool Contains(uint id) => SnoIds.Contains(id) || SnoIds2.Contains(id);
5171
}

0 commit comments

Comments
 (0)