Skip to content

Commit

Permalink
chore: reformat some files
Browse files Browse the repository at this point in the history
  • Loading branch information
Js41637 committed Nov 19, 2024
1 parent b8f5129 commit e796e71
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 99 deletions.
48 changes: 24 additions & 24 deletions TACTLib/Core/Product/Tank/AssetPackageManifest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,45 +20,45 @@ public struct APMHeader {
public int m_entryCount;
public uint m_checksum; // ?
}

[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct Entry {
public uint m_index;
public ulong m_hashA;
public ulong m_hashB;
}

[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct PackageEntry {
public ulong m_packageGUID; // 077 file
public ulong m_keyID;
public uint m_unknown1;
public uint m_unknown2;
}

[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct PackageHeader {
public long m_offsetRecords; // 0
public long m_offsetRecords; // 0
public long m_offset8; // 8
public long m_offset16; // 16
public long m_offset24; // 24
public long m_offsetBundles; // 32
public long m_offsetBundles; // 32
public long m_offset40; // 40
public uint m_recordCount; // 48

public uint m_recordCount; // 48
public uint m_count52; // 52
public uint m_count56; // 56
public uint m_count60; // 60
public uint m_count64; // 64
public uint m_count56; // 56
public uint m_count60; // 60
public uint m_count64; // 64
public uint m_count68; // 68
public uint m_bundleCount; // 72
public uint m_bundleCount; // 72
}

[StructLayout(LayoutKind.Explicit, Pack = 1, Size = 12)] // size = 12
[StructLayout(LayoutKind.Explicit, Pack = 1, Size = 12)] // size = 12
public struct PackageRecord {
[FieldOffset(0)] public ulong m_GUID;
[FieldOffset(8)] public uint m_flagsReal;

[FieldOffset(8)] public short m_unknown1;
[FieldOffset(10)] public byte m_unknown2;
[FieldOffset(11)] public RecordFlags m_flags;
Expand All @@ -77,25 +77,25 @@ public enum RecordFlags : byte {
public PackageHeader[] m_packages;
public PackageRecord[][] m_packageRecords;
public ulong[][] m_packageBundles;

public AssetPackageManifest(ClientHandler client, ProductHandler_Tank tankHandler, Stream stream, string name) {
m_name = name;
var cmf = tankHandler.m_rootContentManifest;

using (BinaryReader reader = new BinaryReader(stream)) {
m_header = reader.Read<APMHeader>();

if(m_header.m_buildVersion >= 12923648 || m_header.m_buildVersion < 52320) {
if (m_header.m_buildVersion >= 12923648 || m_header.m_buildVersion < 52320) {
throw new NotSupportedException("Overwatch 1.29 or earlier is not supported");
}

m_entries = reader.ReadArray<Entry>(m_header.m_entryCount);
m_packageEntries = reader.ReadArray<PackageEntry>(m_header.m_packageCount);

if (!VerifyEntries(cmf)) {
Logger.Debug("APM", "Entry hash invalid. IV may be wrong");
}

m_packages = new PackageHeader[m_header.m_packageCount];
m_packageRecords = new PackageRecord[m_header.m_packageCount][];
m_packageBundles = new ulong[m_header.m_packageCount][];
Expand All @@ -108,15 +108,15 @@ public AssetPackageManifest(ClientHandler client, ProductHandler_Tank tankHandle
c++;
if (c % 1000 == 0) {
if (!Console.IsOutputRedirected) {
Console.Out.Write($"Loading packages: {Math.Floor(c / (float)m_header.m_packageCount * 10000) / 100:F0}% ({c}/{m_header.m_packageCount})\r");
Console.Out.Write($"Loading packages: {Math.Floor(c / (float) m_header.m_packageCount * 10000) / 100:F0}% ({c}/{m_header.m_packageCount})\r");
}
}

LoadPackage(i, tankHandler);
});

if (!Console.IsOutputRedirected) {
Console.Write(new string(' ', Console.WindowWidth-1)+"\r");
Console.Write(new string(' ', Console.WindowWidth - 1) + "\r");
}
}
}
Expand All @@ -132,10 +132,10 @@ private void LoadPackage(int i, ProductHandler_Tank tankHandler) {
m_packageRecords[i] = Array.Empty<PackageRecord>();
return;
}

if (package.m_bundleCount > 0) {
packageStream.Position = package.m_offsetBundles;
m_packageBundles[i] = packageReader.ReadArray<ulong>((int)package.m_bundleCount);
m_packageBundles[i] = packageReader.ReadArray<ulong>((int) package.m_bundleCount);
}

packageStream.Position = package.m_offsetRecords;
Expand Down
19 changes: 10 additions & 9 deletions TACTLib/Core/Product/Tank/Bundle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,43 @@ public struct HeaderData {
public int EntryCount;
public int Flags;
public byte OffsetSize;

public HeaderData148 To148() => new HeaderData148 {
EntryCount = EntryCount,
OffsetSize = OffsetSize
};
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct HeaderData148 {
public int EntryCount;
public byte OffsetSize;
}

[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 9)]
public struct Entry1 {
public ulong GUID;
public byte Offset; // 1
public byte Offset; // 1
}

[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 10)]
public struct Entry2 {
public ulong GUID;
public ushort Offset; // 2
public ushort Offset; // 2
}

[StructLayout(LayoutKind.Sequential, Pack = 1, Size = 12)]
public struct Entry4 {
public ulong GUID;
public uint Offset; // 4
public uint Offset; // 4
}

public Bundle(Stream? stream, bool is148) {
if (stream == null) {
Entries = Array.Empty<Entry4>();
return;
}

using (BinaryReader reader = new BinaryReader(stream)) {
if (is148) {
Header = reader.Read<HeaderData148>();
Expand All @@ -75,6 +76,6 @@ public Bundle(Stream? stream, bool is148) {
// this should never happen
}
}
}
}
}
}
}
43 changes: 20 additions & 23 deletions TACTLib/Core/Product/Tank/ContentManifestFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ public struct HashData : IComparable<HashData> { // version 25?
public byte Unknown;
public CKey ContentKey;

public int CompareTo(HashData other)
{
public int CompareTo(HashData other) {
return GUID.CompareTo(other.GUID);
}
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct HashData24 { // version 24?
public ulong GUID;
Expand Down Expand Up @@ -60,7 +59,7 @@ public CMFHeader Upgrade() {
}

[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct CMFHeader { // 1.48+, version 26
public struct CMFHeader { // 1.48+, version 26
public uint m_buildVersion;
public uint m_unk04;
public uint m_unk08;
Expand All @@ -73,32 +72,29 @@ public struct CMFHeader { // 1.48+, version 26
public int m_entryPatchRecordCount;
public int m_entryCount;
public uint m_magic;

public uint GetNonEncryptedMagic()
{
return (uint)(0x00666D63u | (GetVersion() << 24));

public uint GetNonEncryptedMagic() {
return (uint) (0x00666D63u | (GetVersion() << 24));
}

public byte GetVersion()
{
return IsEncrypted() ? (byte)(m_magic & 0x000000FF) : (byte)((m_magic & 0xFF000000) >> 24);

public byte GetVersion() {
return IsEncrypted() ? (byte) (m_magic & 0x000000FF) : (byte) ((m_magic & 0xFF000000) >> 24);
}

public bool IsEncrypted()
{

public bool IsEncrypted() {
return (m_magic >> 8) == ENCRYPTED_MAGIC;
}
}

public string m_name;
public CMFHeader m_header;

public AssetPackageManifest.Entry[] m_entries = null!;
public HashData[] m_hashList = null!;

// ReSharper disable once InconsistentNaming
public const int ENCRYPTED_MAGIC = 0x636D66; // todo: use the thingy again?

public ContentManifestFile(ClientHandler client, Stream stream, string name) {
m_name = name;
using (BinaryReader reader = new BinaryReader(stream)) {
Expand All @@ -107,8 +103,8 @@ public ContentManifestFile(ClientHandler client, Stream stream, string name) {
stream.Position = 0;
m_header = reader.Read<CMFHeader25>().Upgrade();
}
if(m_header.m_buildVersion >= 12923648 || m_header.m_buildVersion < 52320) {

if (m_header.m_buildVersion >= 12923648 || m_header.m_buildVersion < 52320) {
throw new NotSupportedException("Overwatch 1.29 or earlier is not supported");
}

Expand All @@ -135,16 +131,16 @@ private void ParseEntries(BinaryReader reader) {
}

public bool TryGet(ulong guid, out HashData hashData) {
var speculativeEntry = new HashData
{
var speculativeEntry = new HashData {
GUID = guid
};

var index = Array.BinarySearch(m_hashList, speculativeEntry);
if (index < 0 || index >= m_hashList.Length)
{
if (index < 0 || index >= m_hashList.Length) {
hashData = default;
return false;
}

hashData = m_hashList[index];
return true;
}
Expand All @@ -157,6 +153,7 @@ public HashData GetHashData(ulong guid) {
if (TryGet(guid, out var data)) {
return data;
}

throw new FileNotFoundException($"{guid:X16}");
}

Expand Down
4 changes: 2 additions & 2 deletions TACTLib/Core/Product/Tank/IManifestCrypto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ public interface IManifestCrypto<in T> {
}

public interface ICMFEncryptionProc : IManifestCrypto<ContentManifestFile.CMFHeader> { }

public interface ITRGEncryptionProc : IManifestCrypto<ResourceGraph.TRGHeader> { }
}
}
22 changes: 13 additions & 9 deletions TACTLib/Core/Product/Tank/ManifestCryptoHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@
namespace TACTLib.Core.Product.Tank {
public static class ManifestCryptoHandler {
#region Helpers

// ReSharper disable once InconsistentNaming
public const uint SHA1_DIGESTSIZE = 20;

public static uint Constrain(long value) {
return (uint)(value % uint.MaxValue);
return (uint) (value % uint.MaxValue);
}

public static int SignedMod(long p1, long p2)
{
var a = (int)p1;
var b = (int)p2;
public static int SignedMod(long p1, long p2) {
var a = (int) p1;
var b = (int) p2;
return (a % b) < 0 ? (a % b + b) : (a % b);
}

#endregion

public static bool AttemptFallbackManifests = false;
Expand All @@ -39,9 +40,11 @@ public static void GenerateKeyIV<T>(string name, string manifestType, T header,
if (!s_headerTypeToProviderType.TryGetValue(typeof(T), out var providerType)) {
throw new InvalidDataException($"[Manifest]: Unable to get crypto provider for {typeof(T)}");
}

if (!Providers.TryGetValue(product, out var cryptoTypeMap)) {
throw new InvalidDataException($"[Manifest]: {product} does not have any crypto providers?");
}

if (!cryptoTypeMap.TryGetValue(providerType, out var providerVersions)) {
throw new InvalidDataException($"[Manifest]: {product} does not have any {providerType} providers?");
}
Expand Down Expand Up @@ -106,6 +109,7 @@ public static Dictionary<Type, Dictionary<uint, object>> GetManifestProvidersFor
providerCryptoTypeMap = new Dictionary<Type, Dictionary<uint, object>>();
Providers[product] = providerCryptoTypeMap;
}

return providerCryptoTypeMap;
}

Expand All @@ -116,14 +120,14 @@ public static Dictionary<uint, object> GetManifestProvidersForProductAndInterfac
typeVersionMap = new Dictionary<uint, object>();
providerCryptoTypeMap[t] = typeVersionMap;
}

return typeVersionMap;
}

public static void AddProvider(TACTProduct product, Type @interface, object provider, uint buildVersion) {
if (!s_headerTypeToProviderType.ContainsKey(@interface)) {
var thisInterface = @interface.GetInterfaces().First(x =>
x.IsGenericType &&
x.GetGenericTypeDefinition() == typeof(IManifestCrypto<>));
var thisInterface = @interface.GetInterfaces().First(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IManifestCrypto<>));

s_headerTypeToProviderType[thisInterface.GetGenericArguments()[0]] = @interface;
}

Expand All @@ -139,7 +143,7 @@ public static void AddProvidersFromAssembly<T>(Assembly asm) {
var metadata = tt.GetCustomAttribute<ManifestCryptoAttribute>();
if (metadata == null) continue;

var provider = (T)Activator.CreateInstance(tt)!;
var provider = (T) Activator.CreateInstance(tt)!;
if (metadata.AutoDetectVersion) {
AddProvider(metadata.Product, typeof(T), provider, uint.Parse(tt.Name.Split('_')[1]));
}
Expand Down
Loading

0 comments on commit e796e71

Please sign in to comment.