Skip to content

Commit

Permalink
Fix issues with Archive refactor (#424)
Browse files Browse the repository at this point in the history
* get in game

* clang format

* windows build

* pr comments

* return false

* uncomment

* fix fonts

* pr comment

* init dadon't

* raw
  • Loading branch information
briaguya-ai authored Feb 7, 2024
1 parent bce05ad commit 023e2cc
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 44 deletions.
3 changes: 3 additions & 0 deletions include/libultraship/classes.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
#ifndef _LIBULTRASHIP_CLASSES_H
#define _LIBULTRASHIP_CLASSES_H

#include "resource/archive/ArchiveManager.h"
#include "resource/archive/Archive.h"
#include "resource/archive/OtrArchive.h"
#include "resource/archive/O2rArchive.h"
#include "resource/ResourceManager.h"
#include "Context.h"
#include "window/Window.h"
Expand Down
6 changes: 4 additions & 2 deletions src/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,11 @@ void Context::InitResourceManager(const std::vector<std::string>& otrFiles,
paths.push_back(mMainPath);
paths.push_back(mPatchesPath);

mResourceManager = std::make_shared<ResourceManager>(paths, validHashes, reservedThreadCount);
mResourceManager = std::make_shared<ResourceManager>();
GetResourceManager()->Init(paths, validHashes, reservedThreadCount);
} else {
mResourceManager = std::make_shared<ResourceManager>(otrFiles, validHashes, reservedThreadCount);
mResourceManager = std::make_shared<ResourceManager>();
GetResourceManager()->Init(otrFiles, validHashes, reservedThreadCount);
}

if (!GetResourceManager()->DidLoadSuccessfully()) {
Expand Down
4 changes: 2 additions & 2 deletions src/public/bridge/resourcebridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ uint64_t ResourceGetCrcByName(const char* name) {
}

const char* ResourceGetNameByCrc(uint64_t crc) {
const std::string& hashStr =
const std::string* hashStr =
LUS::Context::GetInstance()->GetResourceManager()->GetArchiveManager()->HashToString(crc);
return hashStr == "" ? hashStr.c_str() : nullptr;
return hashStr != nullptr ? hashStr->c_str() : nullptr;
}

size_t ResourceGetSizeByName(const char* name) {
Expand Down
10 changes: 7 additions & 3 deletions src/resource/ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ extern bool SFileCheckWildCard(const char* szString, const char* szWildCard);

namespace LUS {

ResourceManager::ResourceManager(const std::vector<std::string>& otrFiles,
const std::unordered_set<uint32_t>& validHashes, int32_t reservedThreadCount) {
ResourceManager::ResourceManager() {
}

void ResourceManager::Init(const std::vector<std::string>& otrFiles, const std::unordered_set<uint32_t>& validHashes,
int32_t reservedThreadCount) {
mResourceLoader = std::make_shared<ResourceLoader>();
mArchiveManager = std::make_shared<ArchiveManager>(otrFiles, validHashes);
mArchiveManager = std::make_shared<ArchiveManager>();
GetArchiveManager()->Init(otrFiles, validHashes);
#if defined(__SWITCH__) || defined(__WIIU__)
size_t threadCount = 1;
#else
Expand Down
5 changes: 3 additions & 2 deletions src/resource/ResourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ class ResourceManager {
typedef enum class ResourceLoadError { None, NotCached, NotFound } ResourceLoadError;

public:
ResourceManager(const std::vector<std::string>& otrFiles, const std::unordered_set<uint32_t>& validHashes,
int32_t reservedThreadCount = 1);
ResourceManager();
void Init(const std::vector<std::string>& otrFiles, const std::unordered_set<uint32_t>& validHashes,
int32_t reservedThreadCount = 1);
~ResourceManager();

bool DidLoadSuccessfully();
Expand Down
14 changes: 11 additions & 3 deletions src/resource/archive/Archive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
extern bool SFileCheckWildCard(const char* szString, const char* szWildCard);

namespace LUS {
Archive::Archive(const std::string& path) : mGameVersion(UNKNOWN_GAME_VERSION), mPath(path), mIsLoaded(false) {
Archive::Archive(const std::string& path)
: mHasGameVersion(false), mGameVersion(UNKNOWN_GAME_VERSION), mPath(path), mIsLoaded(false) {
mHashes = std::make_shared<std::unordered_map<uint64_t, std::string>>();
}

Expand All @@ -29,6 +30,7 @@ void Archive::Load() {
auto t = LoadFileRaw("version");
bool isGameVersionValid = false;
if (t != nullptr && t->IsLoaded) {
mHasGameVersion = true;
auto stream = std::make_shared<MemoryStream>(t->Buffer->data(), t->Buffer->size());
auto reader = std::make_shared<BinaryReader>(stream);
LUS::Endianness endianness = (Endianness)reader->ReadUByte();
Expand All @@ -42,7 +44,7 @@ void Archive::Load() {
}
}

SetLoaded(opened && isGameVersionValid);
SetLoaded(opened && (!mHasGameVersion || isGameVersionValid));

if (!IsLoaded()) {
Unload();
Expand Down Expand Up @@ -76,6 +78,10 @@ bool Archive::HasFile(uint64_t hash) {
return mHashes->count(hash) > 0;
}

bool Archive::HasGameVersion() {
return mHasGameVersion;
}

uint32_t Archive::GetGameVersion() {
return mGameVersion;
}
Expand Down Expand Up @@ -156,7 +162,9 @@ std::shared_ptr<File> Archive::LoadFile(const std::string& filePath) {
}

std::shared_ptr<File> Archive::LoadFile(uint64_t hash) {
return LoadFile(Context::GetInstance()->GetResourceManager()->GetArchiveManager()->HashToString(hash));
const std::string& filePath =
*Context::GetInstance()->GetResourceManager()->GetArchiveManager()->HashToString(hash);
return LoadFile(filePath);
}

std::shared_ptr<ResourceInitData> Archive::CreateDefaultResourceInitData() {
Expand Down
11 changes: 7 additions & 4 deletions src/resource/archive/Archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,23 @@ class Archive {
std::shared_ptr<std::unordered_map<uint64_t, std::string>> ListFiles(const std::string& filter);
bool HasFile(const std::string& filePath);
bool HasFile(uint64_t hash);
bool HasGameVersion();
uint32_t GetGameVersion();
const std::string& GetPath();
bool IsLoaded();

virtual bool LoadRaw() = 0;
virtual bool UnloadRaw() = 0;
virtual std::shared_ptr<File> LoadFileRaw(const std::string& filePath) = 0;
virtual std::shared_ptr<File> LoadFileRaw(uint64_t hash) = 0;

protected:
static std::shared_ptr<ResourceInitData> ReadResourceInitDataBinary(const std::string& filePath,
std::shared_ptr<BinaryReader> headerReader);
static std::shared_ptr<ResourceInitData> ReadResourceInitDataXml(const std::string& filePath,
std::shared_ptr<tinyxml2::XMLDocument> document);
virtual std::shared_ptr<File> LoadFileRaw(const std::string& filePath) = 0;
virtual std::shared_ptr<File> LoadFileRaw(uint64_t hash) = 0;
virtual std::shared_ptr<ResourceInitData> LoadFileMeta(const std::string& filePath) = 0;
virtual std::shared_ptr<ResourceInitData> LoadFileMeta(uint64_t hash) = 0;
virtual bool LoadRaw() = 0;
virtual bool UnloadRaw() = 0;

void SetLoaded(bool isLoaded);
void SetGameVersion(uint32_t gameVersion);
Expand All @@ -53,6 +55,7 @@ class Archive {
static std::shared_ptr<ResourceInitData> CreateDefaultResourceInitData();

bool mIsLoaded;
bool mHasGameVersion;
uint32_t mGameVersion;
std::string mPath;
std::shared_ptr<std::unordered_map<uint64_t, std::string>> mHashes;
Expand Down
52 changes: 40 additions & 12 deletions src/resource/archive/ArchiveManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
extern bool SFileCheckWildCard(const char* szString, const char* szWildCard);

namespace LUS {
ArchiveManager::ArchiveManager() : ArchiveManager(std::vector<std::string>()) {
ArchiveManager::ArchiveManager() {
}

ArchiveManager::ArchiveManager(const std::vector<std::string>& archivePaths) : ArchiveManager(archivePaths, {}) {
void ArchiveManager::Init(const std::vector<std::string>& archivePaths) {
Init(archivePaths, {});
}

ArchiveManager::ArchiveManager(const std::vector<std::string>& archivePaths,
const std::unordered_set<uint32_t>& validGameVersions)
: mValidGameVersions(validGameVersions) {
void ArchiveManager::Init(const std::vector<std::string>& archivePaths,
const std::unordered_set<uint32_t>& validGameVersions) {
mValidGameVersions = validGameVersions;
auto archives = GetArchiveListInPaths(archivePaths);
for (const auto archive : archives) {
AddArchive(archive);
Expand All @@ -39,6 +40,10 @@ bool ArchiveManager::IsArchiveLoaded() {
}

std::shared_ptr<File> ArchiveManager::LoadFile(const std::string& filePath) {
if (filePath == "") {
return nullptr;
}

const auto archive = mFileToArchive[CRC64(filePath.c_str())];
if (archive == nullptr) {
return nullptr;
Expand All @@ -56,6 +61,28 @@ std::shared_ptr<File> ArchiveManager::LoadFile(uint64_t hash) {
return archive->LoadFile(hash);
}

std::shared_ptr<File> ArchiveManager::LoadFileRaw(const std::string& filePath) {
if (filePath == "") {
return nullptr;
}

const auto archive = mFileToArchive[CRC64(filePath.c_str())];
if (archive == nullptr) {
return nullptr;
}

return archive->LoadFileRaw(filePath);
}

std::shared_ptr<File> ArchiveManager::LoadFileRaw(uint64_t hash) {
const auto archive = mFileToArchive[hash];
if (archive == nullptr) {
return nullptr;
}

return archive->LoadFileRaw(hash);
}

bool ArchiveManager::HasFile(const std::string& filePath) {
return HasFile(CRC64(filePath.c_str()));
}
Expand Down Expand Up @@ -105,9 +132,9 @@ void ArchiveManager::SetArchives(const std::vector<std::shared_ptr<Archive>>& ar
}
}

const std::string& ArchiveManager::HashToString(uint64_t hash) {
const std::string* ArchiveManager::HashToString(uint64_t hash) const {
auto it = mHashes.find(hash);
return it != mHashes.end() ? it->second : "";
return it != mHashes.end() ? &it->second : nullptr;
}

std::vector<std::string> ArchiveManager::GetArchiveListInPaths(const std::vector<std::string>& archivePaths) {
Expand Down Expand Up @@ -154,6 +181,7 @@ std::shared_ptr<Archive> ArchiveManager::AddArchive(const std::string& archivePa
archive = std::make_shared<O2rArchive>(archivePath);
}

archive->Load();
return AddArchive(archive);
}

Expand All @@ -172,12 +200,12 @@ std::shared_ptr<Archive> ArchiveManager::AddArchive(std::shared_ptr<Archive> arc
SPDLOG_INFO("Adding Archive {} to Archive Manager", archive->GetPath());

mArchives.push_back(archive);
mGameVersions.push_back(archive->GetGameVersion());
if (archive->HasGameVersion()) {
mGameVersions.push_back(archive->GetGameVersion());
}
const auto fileList = archive->ListFiles();
for (size_t i = 0; i < fileList->size(); i++) {
const auto file = fileList->operator[](i);
const auto hash = CRC64(file.c_str());
mHashes[hash] = file;
for (auto& [hash, filename] : *fileList.get()) {
mHashes[hash] = filename;
mFileToArchive[hash] = archive;
}
return archive;
Expand Down
8 changes: 5 additions & 3 deletions src/resource/archive/ArchiveManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,23 @@ class Archive;
class ArchiveManager {
public:
ArchiveManager();
ArchiveManager(const std::vector<std::string>& archivePaths);
ArchiveManager(const std::vector<std::string>& archivePaths, const std::unordered_set<uint32_t>& validGameVersions);
void Init(const std::vector<std::string>& archivePaths);
void Init(const std::vector<std::string>& archivePaths, const std::unordered_set<uint32_t>& validGameVersions);
~ArchiveManager();

bool IsArchiveLoaded();
std::shared_ptr<File> LoadFile(const std::string& filePath);
std::shared_ptr<File> LoadFile(uint64_t hash);
std::shared_ptr<File> LoadFileRaw(const std::string& filePath);
std::shared_ptr<File> LoadFileRaw(uint64_t hash);
bool HasFile(const std::string& filePath);
bool HasFile(uint64_t hash);
std::shared_ptr<std::vector<std::string>> ListFiles(const std::string& filter);
std::shared_ptr<std::vector<std::string>> ListFiles();
std::vector<uint32_t> GetGameVersions();
std::vector<std::shared_ptr<Archive>> GetArchives();
void SetArchives(const std::vector<std::shared_ptr<Archive>>& archives);
const std::string& HashToString(uint64_t hash);
const std::string* HashToString(uint64_t hash) const;
bool IsGameVersionValid(uint32_t gameVersion);

protected:
Expand Down
8 changes: 4 additions & 4 deletions src/resource/archive/O2rArchive.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ class O2rArchive : virtual public Archive {
O2rArchive(const std::string& archivePath);
~O2rArchive();

protected:
bool LoadRaw();
bool UnloadRaw();
std::shared_ptr<File> LoadFileRaw(const std::string& filePath);
std::shared_ptr<File> LoadFileRaw(uint64_t hash);

protected:
std::shared_ptr<ResourceInitData> LoadFileMeta(const std::string& filePath);
std::shared_ptr<ResourceInitData> LoadFileMeta(uint64_t hash);

bool LoadRaw();
bool UnloadRaw();

private:
};
} // namespace LUS
9 changes: 5 additions & 4 deletions src/resource/archive/OtrArchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ std::shared_ptr<File> OtrArchive::LoadFileRaw(const std::string& filePath) {
}

auto fileToLoad = std::make_shared<File>();
fileToLoad->InitData->Path = filePath;
DWORD fileSize = SFileGetFileSize(fileHandle, 0);
DWORD readBytes;
fileToLoad->Buffer->resize(fileSize);
fileToLoad->Buffer = std::make_shared<std::vector<char>>(fileSize);
bool readFileSuccess = SFileReadFile(fileHandle, fileToLoad->Buffer->data(), fileSize, &readBytes, NULL);

if (!readFileSuccess) {
Expand All @@ -52,7 +51,9 @@ std::shared_ptr<File> OtrArchive::LoadFileRaw(const std::string& filePath) {
}

std::shared_ptr<File> OtrArchive::LoadFileRaw(uint64_t hash) {
return LoadFileRaw(Context::GetInstance()->GetResourceManager()->GetArchiveManager()->HashToString(hash));
const std::string& filePath =
*Context::GetInstance()->GetResourceManager()->GetArchiveManager()->HashToString(hash);
return LoadFileRaw(filePath);
}

std::shared_ptr<ResourceInitData> OtrArchive::LoadFileMeta(const std::string& filePath) {
Expand All @@ -67,10 +68,10 @@ bool OtrArchive::LoadRaw() {
const bool opened = SFileOpenArchive(GetPath().c_str(), 0, MPQ_OPEN_READ_ONLY, &mHandle);
if (opened) {
SPDLOG_INFO("Opened mpq file \"{}\"", GetPath());
SFileCloseArchive(mHandle);
} else {
SPDLOG_ERROR("Failed to load mpq file \"{}\"", GetPath());
mHandle = nullptr;
return false;
}

// Generate the file list by reading the list file.
Expand Down
8 changes: 4 additions & 4 deletions src/resource/archive/OtrArchive.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ class OtrArchive : virtual public Archive {
OtrArchive(const std::string& archivePath);
~OtrArchive();

protected:
bool LoadRaw();
bool UnloadRaw();
std::shared_ptr<File> LoadFileRaw(const std::string& filePath);
std::shared_ptr<File> LoadFileRaw(uint64_t hash);

protected:
std::shared_ptr<ResourceInitData> LoadFileMeta(const std::string& filePath);
std::shared_ptr<ResourceInitData> LoadFileMeta(uint64_t hash);

bool LoadRaw();
bool UnloadRaw();

private:
HANDLE mHandle;
};
Expand Down
2 changes: 1 addition & 1 deletion src/window/gui/GameOverlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ GameOverlay::~GameOverlay() {

void GameOverlay::LoadFont(const std::string& name, const std::string& path, float fontSize) {
ImGuiIO& io = ImGui::GetIO();
std::shared_ptr<File> font = Context::GetInstance()->GetResourceManager()->GetArchiveManager()->LoadFile(path);
std::shared_ptr<File> font = Context::GetInstance()->GetResourceManager()->GetArchiveManager()->LoadFileRaw(path);
if (font->IsLoaded) {
// TODO: Nothing is ever unloading the font or this fontData array.
char* fontData = new char[font->Buffer->size()];
Expand Down

0 comments on commit 023e2cc

Please sign in to comment.