Skip to content

Commit

Permalink
Add PakToFolder
Browse files Browse the repository at this point in the history
  • Loading branch information
BigBang1112 committed Dec 30, 2024
1 parent 8dd7044 commit 61ad2aa
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 6 deletions.
7 changes: 7 additions & 0 deletions GBX.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PakToZip", "Tools\PakToZip\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtractPakFileHashes", "Tools\ExtractPakFileHashes\ExtractPakFileHashes.csproj", "{B3C5D493-3172-4780-95EC-CBEE73BE4850}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PakToFolder", "Tools\PakToFolder\PakToFolder.csproj", "{8BCA7A11-5A5E-4B9F-AC1A-384D46C68559}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -424,6 +426,10 @@ Global
{B3C5D493-3172-4780-95EC-CBEE73BE4850}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B3C5D493-3172-4780-95EC-CBEE73BE4850}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B3C5D493-3172-4780-95EC-CBEE73BE4850}.Release|Any CPU.Build.0 = Release|Any CPU
{8BCA7A11-5A5E-4B9F-AC1A-384D46C68559}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8BCA7A11-5A5E-4B9F-AC1A-384D46C68559}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8BCA7A11-5A5E-4B9F-AC1A-384D46C68559}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8BCA7A11-5A5E-4B9F-AC1A-384D46C68559}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -495,6 +501,7 @@ Global
{55BD1330-7431-41A8-90EB-DEB20D9EB981} = {80DCE6B7-4BD9-415C-B053-92B059D7C938}
{47A58E42-EBBC-48D0-B4B1-C25DFD074506} = {F3336145-FDA9-4517-AEDC-7F4C4D526ECB}
{B3C5D493-3172-4780-95EC-CBEE73BE4850} = {F3336145-FDA9-4517-AEDC-7F4C4D526ECB}
{8BCA7A11-5A5E-4B9F-AC1A-384D46C68559} = {F3336145-FDA9-4517-AEDC-7F4C4D526ECB}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8EA2F0DE-BA72-486D-AB3A-9320C0CE5CFD}
Expand Down
16 changes: 16 additions & 0 deletions Tools/PakToFolder/PakToFolder.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net9.0-windows;net9.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishAot>true</PublishAot>
<InvariantGlobalization>true</InvariantGlobalization>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\Src\GBX.NET.PAK\GBX.NET.PAK.csproj" />
</ItemGroup>

</Project>
84 changes: 84 additions & 0 deletions Tools/PakToFolder/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using GBX.NET;
using GBX.NET.Components;
using GBX.NET.Exceptions;
using GBX.NET.PAK;

var pakFileNameOrDirectory = args[0];
var isDirectory = Directory.Exists(pakFileNameOrDirectory);

var directoryPath = isDirectory ? pakFileNameOrDirectory : Path.GetDirectoryName(pakFileNameOrDirectory)!;

var extractFolderPath = args.Length > 1 ? args[1] : "";

var game = PakListGame.TM;

if (args.Length > 2 && args[2].Equals("vsk5", StringComparison.InvariantCultureIgnoreCase))
{
game = PakListGame.Vsk5;
}

Console.WriteLine("Bruteforcing possible file names from hashes...");

var hashes = await Pak.BruteforceFileHashesAsync(directoryPath, game, onlyUsedHashes: false);

var packlistFileName = Path.Combine(directoryPath, "packlist.dat");
var packlist = await PakList.ParseAsync(packlistFileName, game);

var pakFileNames = isDirectory ? Directory.GetFiles(pakFileNameOrDirectory, "*.pak", SearchOption.AllDirectories) : [pakFileNameOrDirectory];

foreach (var pakFileName in pakFileNames)
{
var key = packlist[Path.GetFileNameWithoutExtension(pakFileName).ToLowerInvariant()].Key;

await using var fs = File.OpenRead(pakFileName);
await using var pak = await Pak.ParseAsync(fs, key);

foreach (var file in pak.Files.Values)
{
var fileName = hashes.GetValueOrDefault(file.Name)?.Replace('\\', Path.DirectorySeparatorChar) ?? file.Name;
var fullPath = string.IsNullOrEmpty(extractFolderPath)
? Path.Combine(file.FolderPath, fileName)
: Path.Combine(extractFolderPath, file.FolderPath, fileName);

Console.WriteLine(fullPath);

Directory.CreateDirectory(Path.GetDirectoryName(fullPath)!);

if (File.Exists(fullPath))
{
File.Delete(fullPath);
}

using var stream = File.OpenWrite(fullPath);

try
{
var gbx = await pak.OpenGbxFileAsync(file);

if (gbx.Header is GbxHeaderUnknown)
{
CopyFileToStream(pak, file, stream);
}
else
{
gbx.Save(stream);
}
}
catch (NotAGbxException)
{
CopyFileToStream(pak, file, stream);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}

static void CopyFileToStream(Pak pak, PakFile file, Stream stream)
{
var pakItemFileStream = pak.OpenFile(file, out _);
var data = new byte[file.UncompressedSize];
var count = pakItemFileStream.Read(data);
stream.Write(data, 0, count);
}
12 changes: 6 additions & 6 deletions Tools/PakToZip/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
var hashes = await Pak.BruteforceFileHashesAsync(directoryPath, game, onlyUsedHashes: false);

var packlistFileName = Path.Combine(directoryPath, "packlist.dat");
var packlist = await PakList.ParseAsync(packlistFileName);
var packlist = await PakList.ParseAsync(packlistFileName, game);

var key = packlist[Path.GetFileNameWithoutExtension(pakFileName).ToLowerInvariant()].Key;

using var fs = File.OpenRead(pakFileName);
using var pak = await Pak.ParseAsync(fs, key);
await using var fs = File.OpenRead(pakFileName);
await using var pak = await Pak.ParseAsync(fs, key);

using var zip = ZipFile.Open(Path.ChangeExtension(pakFileName, ".zip"), ZipArchiveMode.Create);

Expand All @@ -35,11 +35,12 @@

Console.WriteLine(fullPath);

var entry = zip.CreateEntry(fullPath);

try
{
var gbx = await pak.OpenGbxFileAsync(file);

var entry = zip.CreateEntry(fullPath);
using var stream = entry.Open();

if (gbx.Header is GbxHeaderUnknown)
Expand All @@ -53,8 +54,7 @@
}
catch (NotAGbxException)
{
var entry = zip.CreateEntry(fullPath);
using var stream = entry.Open();
await using var stream = entry.Open();
CopyFileToStream(pak, file, stream);
}
catch (Exception ex)
Expand Down

0 comments on commit 61ad2aa

Please sign in to comment.