Skip to content

Commit

Permalink
Do not require _dir in chunked vpks
Browse files Browse the repository at this point in the history
Fixes #18
  • Loading branch information
xPaw committed Mar 31, 2024
1 parent bf8492f commit f426e60
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 22 deletions.
Binary file not shown.
Binary file not shown.
23 changes: 8 additions & 15 deletions ValvePak/ValvePak.Test/PackageTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,21 +240,6 @@ public void ThrowsOnInvalidCRC32()
Assert.DoesNotThrow(() => package.ReadEntry(file, out _, false));
}

[Test]
public void ThrowsOnExternalFilesNotInDirVpk()
{
var path = Path.Combine(TestContext.CurrentContext.TestDirectory, "Files", "broken_dir.vpk");
using var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

using var package = new Package();
package.SetFileName("test.vpk");
package.Read(fs);

var file = package.FindEntry("UpperCaseFolder/UpperCaseFile.txt");
Assert.That(file.CRC32, Is.EqualTo(0x32CFF012));
Assert.Throws<InvalidOperationException>(() => package.ReadEntry(file, out _));
}

[Test]
public void ThrowsOnInvalidEntryTerminator()
{
Expand Down Expand Up @@ -363,6 +348,14 @@ public void ExtractDirVPK()
TestVPKExtraction(path);
}

[Test]
public void ExtractDirVPKWithoutSuffix()
{
var path = Path.Combine(TestContext.CurrentContext.TestDirectory, "Files", "steamdb_test_without_suffix.vpk");

TestVPKExtraction(path);
}

[Test]
public void ExtractIntoUserProvidedByteArray()
{
Expand Down
15 changes: 8 additions & 7 deletions ValvePak/ValvePak/Package.Read.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,7 @@ private Stream GetFileStream(ushort archiveIndex)

if (archiveIndex != 0x7FFF)
{
if (!IsDirVPK)
{
throw new InvalidOperationException("Given VPK filename does not end in '_dir.vpk', but entry is referencing an external archive.");
}

var fileName = $"{FileName}_{archiveIndex:D3}.vpk";
var fileName = GetArchiveIndexFullFilePath(archiveIndex);

stream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
}
Expand Down Expand Up @@ -383,7 +378,7 @@ public Stream GetMemoryMappedStreamIfPossible(PackageEntry entry)
}
else
{
path = $"{FileName}_{entry.ArchiveIndex:D3}.vpk";
path = GetArchiveIndexFullFilePath(entry.ArchiveIndex);
}

stream = MemoryMappedFile.CreateFromFile(path, FileMode.Open, null, 0, MemoryMappedFileAccess.Read);
Expand All @@ -400,6 +395,12 @@ public Stream GetMemoryMappedStreamIfPossible(PackageEntry entry)
return stream.CreateViewStream(offset, entry.Length, MemoryMappedFileAccess.Read);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private string GetArchiveIndexFullFilePath(ushort archiveIndex)
{
return $"{FileName}_{archiveIndex:D3}.vpk";
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private string ReadNullTermUtf8String(MemoryStream ms)
{
Expand Down

0 comments on commit f426e60

Please sign in to comment.