Skip to content

Commit 14e1317

Browse files
committed
Add SplitFileHashesPerPak tool
1 parent d38bc7d commit 14e1317

4 files changed

Lines changed: 130 additions & 0 deletions

File tree

GBX.NET.slnx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122
<Project Path="Tools/PakToZip/PakToZip.csproj" />
123123
<Project Path="Tools/ParseRepeater/ParseRepeater.csproj" />
124124
<Project Path="Tools/PopulateExtensions/PopulateExtensions.csproj" Id="a4d3d9a7-2e22-4cfa-bf7c-6c61cf1236f9" />
125+
<Project Path="Tools/SplitFileHashesPerPak/SplitFileHashesPerPak.csproj" Id="3b8de8d8-dfab-4f67-bf8d-871caf36b57d" />
125126
</Folder>
126127
<Folder Name="/Tools/Old Gbx Explorer/">
127128
<Project Path="Tools/GbxExplorerOld/Client/GbxExplorerOld.Client.csproj" />
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using GBX.NET.PAK;
2+
3+
var hashes = new Dictionary<string, string>();
4+
5+
foreach (var filePath in Directory.GetFiles("../../../../../Resources", "FileHashes_*.txt"))
6+
{
7+
foreach (var line in File.ReadLines(filePath))
8+
{
9+
var parts = line.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
10+
11+
if (parts.Length != 2)
12+
{
13+
continue;
14+
}
15+
16+
var hash = parts[0];
17+
var fileName = parts[1];
18+
19+
hashes[hash] = fileName;
20+
}
21+
}
22+
23+
using (var writer = File.CreateText("../../../../../Resources/FileHashes.txt"))
24+
{
25+
foreach (var (hash, name) in hashes.OrderBy(x => x.Value).ThenBy(x => x.Key))
26+
{
27+
await writer.WriteLineAsync($"{hash} {name}");
28+
}
29+
}
30+
31+
var keyLookup = ParseKeysFromTxt("../../../../../Resources/BaseKeys.txt").ToLookup(x => x.Item1, x => x.Item2);
32+
33+
Directory.CreateDirectory("../../../../../Resources/FileHashes");
34+
35+
var directoryPath = args[0];
36+
37+
foreach (var pakFilePath in Directory.EnumerateFiles(directoryPath, "*.pak").Concat(Directory.EnumerateFiles(directoryPath, "*.Pack.Gbx")))
38+
{
39+
foreach (var key in keyLookup[Path.GetFileNameWithoutExtension(pakFilePath)])
40+
{
41+
try
42+
{
43+
using var pak = await Pak.ParseAsync(pakFilePath, key);
44+
45+
var perPakHashes = new Dictionary<string, string>();
46+
var perPakHashesPath = $"../../../../../Resources/FileHashes/{Path.GetFileNameWithoutExtension(pakFilePath)}.txt";
47+
48+
if (File.Exists(perPakHashesPath))
49+
{
50+
foreach (var line in File.ReadLines(perPakHashesPath))
51+
{
52+
var parts = line.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
53+
54+
if (parts.Length != 2)
55+
{
56+
continue;
57+
}
58+
59+
var hash = parts[0];
60+
var fileName = parts[1];
61+
62+
perPakHashes[hash] = fileName;
63+
}
64+
}
65+
66+
foreach (var (_, file) in pak.Files)
67+
{
68+
if (hashes.TryGetValue(file.Name, out var fileName))
69+
{
70+
perPakHashes[file.Name] = fileName;
71+
}
72+
}
73+
74+
if (perPakHashes.Count > 0)
75+
{
76+
using var writer = File.CreateText(perPakHashesPath);
77+
78+
foreach (var (hash, name) in perPakHashes.OrderBy(x => x.Value).ThenBy(x => x.Key))
79+
{
80+
await writer.WriteLineAsync($"{hash} {name}");
81+
}
82+
}
83+
}
84+
catch
85+
{
86+
87+
}
88+
}
89+
}
90+
91+
static IEnumerable<(string, byte[])> ParseKeysFromTxt(string keysFileName)
92+
{
93+
foreach (var line in File.ReadLines(keysFileName))
94+
{
95+
var parts = line.Split(' ', 2, StringSplitOptions.RemoveEmptyEntries);
96+
97+
if (parts.Length != 2)
98+
{
99+
continue;
100+
}
101+
102+
var pak = parts[0];
103+
var key = Convert.FromHexString(parts[1]);
104+
105+
yield return (pak, key);
106+
}
107+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"profiles": {
3+
"SplitFileHashesPerPak": {
4+
"commandName": "Project",
5+
"commandLineArgs": "\"E:\\Steam\\steamapps\\common\\Trackmania Turbo\\Packs\""
6+
}
7+
}
8+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\..\Src\GBX.NET.PAK\GBX.NET.PAK.csproj" />
12+
</ItemGroup>
13+
14+
</Project>

0 commit comments

Comments
 (0)