Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get in game #424

Merged
merged 10 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions src/resource/archive/Archive.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ 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();
Expand All @@ -53,6 +54,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
30 changes: 18 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 Down Expand Up @@ -105,9 +110,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 +159,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 +178,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
6 changes: 3 additions & 3 deletions src/resource/archive/ArchiveManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ 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();
Expand All @@ -28,7 +28,7 @@ class ArchiveManager {
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: 6 additions & 2 deletions src/resource/archive/OtrArchive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ std::shared_ptr<File> OtrArchive::LoadFileRaw(const std::string& filePath) {
}

auto fileToLoad = std::make_shared<File>();
fileToLoad->InitData = std::make_shared<ResourceInitData>();
Kenix3 marked this conversation as resolved.
Show resolved Hide resolved
fileToLoad->InitData->Path = filePath;
DWORD fileSize = SFileGetFileSize(fileHandle, 0);
DWORD readBytes;
fileToLoad->Buffer = std::make_shared<std::vector<char>>();
fileToLoad->Buffer->resize(fileSize);
briaguya-ai marked this conversation as resolved.
Show resolved Hide resolved
bool readFileSuccess = SFileReadFile(fileHandle, fileToLoad->Buffer->data(), fileSize, &readBytes, NULL);

Expand All @@ -52,7 +54,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 +71,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
Loading
Loading