Skip to content

Commit

Permalink
Casc Rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
Scobalula committed Sep 30, 2020
1 parent 09d1065 commit dfbe5a0
Show file tree
Hide file tree
Showing 13 changed files with 1,507 additions and 299 deletions.
6 changes: 4 additions & 2 deletions src/WraithX/WraithX/TextReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,17 @@ void TextReader::SetPosition(uint64_t Offset)
}
}

std::string TextReader::ReadLine()
std::string TextReader::ReadLine(bool& Success)
{
Success = true;
// Make sure we have a file
if (FileHandle != nullptr)
{
// Get the next line from the stream
char LineBuffer[0x2000];
// Read
fgets(LineBuffer, 0x2000, FileHandle);
if(fgets(LineBuffer, 0x2000, FileHandle) == NULL)
Success = false;
// Return
return Strings::EndTrim(std::string(LineBuffer));
}
Expand Down
2 changes: 1 addition & 1 deletion src/WraithX/WraithX/TextReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class TextReader
void SetPosition(uint64_t Offset);

// Read the next line from the file
std::string ReadLine();
std::string ReadLine(bool& Success);
// Reads the entire file to the end
std::string ReadToEnd();
// Parse the next line from the file
Expand Down
137 changes: 90 additions & 47 deletions src/WraithXCOD/WraithXCOD.sln

Large diffs are not rendered by default.

90 changes: 30 additions & 60 deletions src/WraithXCOD/WraithXCOD/CASCCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,10 @@
#include "MemoryReader.h"
#include "Siren.h"

#include "CASCFileReader.h"
// We need Casc
#include "Casc.h"

std::map<std::string, CASC_FIND_DATA> GetFiles(HANDLE StorageHandle)
{
// Data
HANDLE FoundHandle;
CASC_FIND_DATA CASCFindData;
std::map<std::string, CASC_FIND_DATA> CASCFiles;
FoundHandle = CascFindFirstFile(StorageHandle, "*", &CASCFindData, NULL);

if (FoundHandle != NULL)
{
do
{
// Check if this is a local file
if (CASCFindData.bFileAvailable == 1)
{
CASCFiles[CASCFindData.szFileName] = CASCFindData;
}
} while (CascFindNextFile(FoundHandle, &CASCFindData));
}

CascFindClose(FoundHandle);
return CASCFiles;

}

CASCCache::CASCCache() : StorageHandle(nullptr)
CASCCache::CASCCache()
{
// Default, attempt to load the siren lib
Siren::Initialize(L"oo2core_6_win64.dll");
Expand All @@ -51,34 +27,39 @@ CASCCache::~CASCCache()
// Clean up if need be
Siren::Shutdown();
// Close Handle
CascCloseStorage(StorageHandle);

}

void CASCCache::LoadPackageCache(const std::string& BasePath)
{
// Call Base function first!
CoDPackageCache::LoadPackageCache(BasePath);

if (!CascOpenStorage(Strings::ToUnicodeString(BasePath).c_str(), NULL, &StorageHandle))
try
{
#if _DEBUG
std::cout << "Failed to open CASC Storage\n";
#endif
return;
}

// Find Files in CASC
auto Files = GetFiles(StorageHandle);
// Open Storage
Container.Open(BasePath);

// Iterate over file paths
for (auto& File : Files)
{
// We only want XPAKs
if (FileSystems::GetExtension(File.first) == ".xpak")
for (auto& File : Container.GetFileEntries())
{
this->LoadPackage(File.first);
// We only want XPAKs
if (FileSystems::GetExtension(File.first) == ".xpak" && File.second.Exists)
{
try
{
this->LoadPackage(File.first);
}
catch (...)
{

}
}
}
}
catch (...)
{

}

// We've finished loading, set status
this->SetLoadedState();
Expand All @@ -97,13 +78,7 @@ bool CASCCache::LoadPackage(const std::string& FilePath)
auto PackageIndex = (uint32_t)PackageFilePaths.size();

// Open CASC File
auto Reader = CASCFileReader(StorageHandle, FilePath);

// Validate
if (!Reader.IsValid())
{
return false;
}
auto Reader = Container.OpenFile(FilePath);

// Read the header
auto Header = Reader.Read<BO3XPakHeader>();
Expand All @@ -112,14 +87,13 @@ bool CASCCache::LoadPackage(const std::string& FilePath)
if (Header.Version == 0xD)
{
Reader.SetPosition(0);
uint64_t Result;
Reader.Read((uint8_t*)&Header, 24, Result);
Reader.Read((uint8_t*)&Header, 0, 24);
Reader.Advance(288);
Reader.Read((uint8_t*)&Header + 24, 96, Result);
Reader.Read((uint8_t*)&Header + 24, 0, 96);
}

// Verify the magic and offset
if (Header.Magic == 0x4950414b && Header.HashOffset < Reader.GetLength())
if (Header.Magic == 0x4950414b && (int64_t)Header.HashOffset < Reader.GetLength())
{
// Jump to hash offset
Reader.SetPosition(Header.HashOffset);
Expand Down Expand Up @@ -232,7 +206,7 @@ std::unique_ptr<uint8_t[]> CASCCache::ExtractPackageObject(uint64_t CacheID, uin
// Get the XPAK name
auto& XPAKFileName = PackageFilePaths[CacheInfo.PackageFileIndex];
// Open CASC File
auto Reader = CASCFileReader(StorageHandle, XPAKFileName);
auto Reader = Container.OpenFile(XPAKFileName);

#if _DEBUG
printf("CASCCache::ExtractPackageObject(): Streaming Object: 0x%llx from CASC File: %s\n", CacheID, XPAKFileName.c_str());
Expand Down Expand Up @@ -413,11 +387,7 @@ std::unique_ptr<uint8_t[]> CASCCache::ExtractPackageObject(const std::string & P
ResultSize = 0;

// Open CASC File
auto Reader = CASCFileReader(StorageHandle, PackageName);

// Check if the file handle is valid
if (Reader.IsValid())
return nullptr;
auto Reader = Container.OpenFile(PackageName);

// Jump to the offset
Reader.SetPosition(AssetOffset);
Expand Down
5 changes: 2 additions & 3 deletions src/WraithXCOD/WraithXCOD/CASCCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
// We need the package cache
#include "CoDAssets.h"
#include "CoDPackageCache.h"
#include "CascLib.h"
#include "Casc.h"
#include <shared_mutex>

// A class that handles reading, caching and extracting CASC Resources
class CASCCache : public CoDPackageCache
{
private:
// Storage Handle
HANDLE StorageHandle;
Casc::Container Container;
// Cache Mutex
std::shared_mutex ReadMutex;
public:
Expand Down
111 changes: 0 additions & 111 deletions src/WraithXCOD/WraithXCOD/CASCFileReader.cpp

This file was deleted.

56 changes: 0 additions & 56 deletions src/WraithXCOD/WraithXCOD/CASCFileReader.h

This file was deleted.

Loading

0 comments on commit dfbe5a0

Please sign in to comment.