Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions TACTLib/Container/StaticContainerHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Buffers.Binary;
using System.IO;
using System.Linq;
using System.Numerics;
using CommunityToolkit.HighPerformance.Helpers;
using TACTLib.Client;
using TACTLib.Helpers;
Expand All @@ -18,16 +19,17 @@ private struct KeyLayout {
public byte m_chunkBits;
public byte m_archiveBits;
public byte m_offsetBits;
public byte m_offsetMultiplier;
}

public StaticContainerHandler(ClientHandler client) {
m_client = client;
if (client.BasePath == null) throw new Exception("no 'BasePath' specified");
m_containerDirectory = Path.Combine(client.BasePath, GetContainerDirectory(client.Product));

m_keyLayoutIndexBits = byte.Parse(client.ConfigHandler.BuildConfig.Values["key-layout-index-bits"][0]);
m_keyLayouts = new KeyLayout[Math.Max((int)Math.Pow(2, m_keyLayoutIndexBits), 1)];

foreach (var keyLayoutPair in client.ConfigHandler.BuildConfig.Values
.Where(static x => x.Key.StartsWith("key-layout-") && x.Key != "key-layout-index-bits")) {

Expand All @@ -36,7 +38,8 @@ public StaticContainerHandler(ClientHandler client) {
m_keyLayouts[layoutIndex] = new KeyLayout {
m_chunkBits = byte.Parse(keyLayoutPair.Value[0]),
m_archiveBits = byte.Parse(keyLayoutPair.Value[1]),
m_offsetBits = byte.Parse(keyLayoutPair.Value[2])
m_offsetBits = byte.Parse(keyLayoutPair.Value[2]),
m_offsetMultiplier = keyLayoutPair.Value.Count > 3 ? byte.Parse(keyLayoutPair.Value[3]) : (byte)1
};
}
}
Expand All @@ -55,7 +58,7 @@ private void ExtractStorageLocation(FullEKey ekey, out ulong chunk, out ulong ar
var keyLayoutIndexBitOffset = 56-keyLayoutBitCount;
var keyLayoutIndex = BitHelper.ExtractRange(ekeyHiUl, (byte)keyLayoutIndexBitOffset, keyLayoutBitCount);
var keyLayout = m_keyLayouts[keyLayoutIndex];

var chunkBitCount = keyLayout.m_chunkBits;
var chunkBitOffset = keyLayoutIndexBitOffset-chunkBitCount;
chunk = BitHelper.ExtractRange(ekeyHiUl, (byte)chunkBitOffset, chunkBitCount);
Expand All @@ -66,7 +69,7 @@ private void ExtractStorageLocation(FullEKey ekey, out ulong chunk, out ulong ar

var offsetBitCount = keyLayout.m_offsetBits;
var offsetBitOffset = archiveBitOffset-offsetBitCount;
offset = BitHelper.ExtractRange(ekeyHiUl, (byte)offsetBitOffset, offsetBitCount);
offset = BitHelper.ExtractRange(ekeyHiUl, (byte) offsetBitOffset, offsetBitCount) * keyLayout.m_offsetMultiplier;
}

private static string GetFileName(ulong chunk, ulong archive) {
Expand All @@ -79,7 +82,7 @@ private string GetFilePath(ulong chunk, ulong archive) {

public ArraySegment<byte>? OpenEKey(FullEKey ekey, int eSize) {
ExtractStorageLocation(ekey, out var chunk, out var archive, out var offset);

using var stream = File.OpenRead(GetFilePath(chunk, archive));
stream.Position = (long)offset;
var data = GC.AllocateUninitializedArray<byte>(eSize);
Expand Down