Skip to content

Commit 023e2cc

Browse files
authored
Fix issues with Archive refactor (Kenix3#424)
* get in game * clang format * windows build * pr comments * return false * uncomment * fix fonts * pr comment * init dadon't * raw
1 parent bce05ad commit 023e2cc

13 files changed

+96
-44
lines changed

include/libultraship/classes.h

+3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
#ifndef _LIBULTRASHIP_CLASSES_H
55
#define _LIBULTRASHIP_CLASSES_H
66

7+
#include "resource/archive/ArchiveManager.h"
78
#include "resource/archive/Archive.h"
9+
#include "resource/archive/OtrArchive.h"
10+
#include "resource/archive/O2rArchive.h"
811
#include "resource/ResourceManager.h"
912
#include "Context.h"
1013
#include "window/Window.h"

src/Context.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,11 @@ void Context::InitResourceManager(const std::vector<std::string>& otrFiles,
198198
paths.push_back(mMainPath);
199199
paths.push_back(mPatchesPath);
200200

201-
mResourceManager = std::make_shared<ResourceManager>(paths, validHashes, reservedThreadCount);
201+
mResourceManager = std::make_shared<ResourceManager>();
202+
GetResourceManager()->Init(paths, validHashes, reservedThreadCount);
202203
} else {
203-
mResourceManager = std::make_shared<ResourceManager>(otrFiles, validHashes, reservedThreadCount);
204+
mResourceManager = std::make_shared<ResourceManager>();
205+
GetResourceManager()->Init(otrFiles, validHashes, reservedThreadCount);
204206
}
205207

206208
if (!GetResourceManager()->DidLoadSuccessfully()) {

src/public/bridge/resourcebridge.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ uint64_t ResourceGetCrcByName(const char* name) {
2626
}
2727

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

3434
size_t ResourceGetSizeByName(const char* name) {

src/resource/ResourceManager.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ extern bool SFileCheckWildCard(const char* szString, const char* szWildCard);
1515

1616
namespace LUS {
1717

18-
ResourceManager::ResourceManager(const std::vector<std::string>& otrFiles,
19-
const std::unordered_set<uint32_t>& validHashes, int32_t reservedThreadCount) {
18+
ResourceManager::ResourceManager() {
19+
}
20+
21+
void ResourceManager::Init(const std::vector<std::string>& otrFiles, const std::unordered_set<uint32_t>& validHashes,
22+
int32_t reservedThreadCount) {
2023
mResourceLoader = std::make_shared<ResourceLoader>();
21-
mArchiveManager = std::make_shared<ArchiveManager>(otrFiles, validHashes);
24+
mArchiveManager = std::make_shared<ArchiveManager>();
25+
GetArchiveManager()->Init(otrFiles, validHashes);
2226
#if defined(__SWITCH__) || defined(__WIIU__)
2327
size_t threadCount = 1;
2428
#else

src/resource/ResourceManager.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ class ResourceManager {
2121
typedef enum class ResourceLoadError { None, NotCached, NotFound } ResourceLoadError;
2222

2323
public:
24-
ResourceManager(const std::vector<std::string>& otrFiles, const std::unordered_set<uint32_t>& validHashes,
25-
int32_t reservedThreadCount = 1);
24+
ResourceManager();
25+
void Init(const std::vector<std::string>& otrFiles, const std::unordered_set<uint32_t>& validHashes,
26+
int32_t reservedThreadCount = 1);
2627
~ResourceManager();
2728

2829
bool DidLoadSuccessfully();

src/resource/archive/Archive.cpp

+11-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
extern bool SFileCheckWildCard(const char* szString, const char* szWildCard);
1616

1717
namespace LUS {
18-
Archive::Archive(const std::string& path) : mGameVersion(UNKNOWN_GAME_VERSION), mPath(path), mIsLoaded(false) {
18+
Archive::Archive(const std::string& path)
19+
: mHasGameVersion(false), mGameVersion(UNKNOWN_GAME_VERSION), mPath(path), mIsLoaded(false) {
1920
mHashes = std::make_shared<std::unordered_map<uint64_t, std::string>>();
2021
}
2122

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

45-
SetLoaded(opened && isGameVersionValid);
47+
SetLoaded(opened && (!mHasGameVersion || isGameVersionValid));
4648

4749
if (!IsLoaded()) {
4850
Unload();
@@ -76,6 +78,10 @@ bool Archive::HasFile(uint64_t hash) {
7678
return mHashes->count(hash) > 0;
7779
}
7880

81+
bool Archive::HasGameVersion() {
82+
return mHasGameVersion;
83+
}
84+
7985
uint32_t Archive::GetGameVersion() {
8086
return mGameVersion;
8187
}
@@ -156,7 +162,9 @@ std::shared_ptr<File> Archive::LoadFile(const std::string& filePath) {
156162
}
157163

158164
std::shared_ptr<File> Archive::LoadFile(uint64_t hash) {
159-
return LoadFile(Context::GetInstance()->GetResourceManager()->GetArchiveManager()->HashToString(hash));
165+
const std::string& filePath =
166+
*Context::GetInstance()->GetResourceManager()->GetArchiveManager()->HashToString(hash);
167+
return LoadFile(filePath);
160168
}
161169

162170
std::shared_ptr<ResourceInitData> Archive::CreateDefaultResourceInitData() {

src/resource/archive/Archive.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,23 @@ class Archive {
2929
std::shared_ptr<std::unordered_map<uint64_t, std::string>> ListFiles(const std::string& filter);
3030
bool HasFile(const std::string& filePath);
3131
bool HasFile(uint64_t hash);
32+
bool HasGameVersion();
3233
uint32_t GetGameVersion();
3334
const std::string& GetPath();
3435
bool IsLoaded();
3536

37+
virtual bool LoadRaw() = 0;
38+
virtual bool UnloadRaw() = 0;
39+
virtual std::shared_ptr<File> LoadFileRaw(const std::string& filePath) = 0;
40+
virtual std::shared_ptr<File> LoadFileRaw(uint64_t hash) = 0;
41+
3642
protected:
3743
static std::shared_ptr<ResourceInitData> ReadResourceInitDataBinary(const std::string& filePath,
3844
std::shared_ptr<BinaryReader> headerReader);
3945
static std::shared_ptr<ResourceInitData> ReadResourceInitDataXml(const std::string& filePath,
4046
std::shared_ptr<tinyxml2::XMLDocument> document);
41-
virtual std::shared_ptr<File> LoadFileRaw(const std::string& filePath) = 0;
42-
virtual std::shared_ptr<File> LoadFileRaw(uint64_t hash) = 0;
4347
virtual std::shared_ptr<ResourceInitData> LoadFileMeta(const std::string& filePath) = 0;
4448
virtual std::shared_ptr<ResourceInitData> LoadFileMeta(uint64_t hash) = 0;
45-
virtual bool LoadRaw() = 0;
46-
virtual bool UnloadRaw() = 0;
4749

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

5557
bool mIsLoaded;
58+
bool mHasGameVersion;
5659
uint32_t mGameVersion;
5760
std::string mPath;
5861
std::shared_ptr<std::unordered_map<uint64_t, std::string>> mHashes;

src/resource/archive/ArchiveManager.cpp

+40-12
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
extern bool SFileCheckWildCard(const char* szString, const char* szWildCard);
1616

1717
namespace LUS {
18-
ArchiveManager::ArchiveManager() : ArchiveManager(std::vector<std::string>()) {
18+
ArchiveManager::ArchiveManager() {
1919
}
2020

21-
ArchiveManager::ArchiveManager(const std::vector<std::string>& archivePaths) : ArchiveManager(archivePaths, {}) {
21+
void ArchiveManager::Init(const std::vector<std::string>& archivePaths) {
22+
Init(archivePaths, {});
2223
}
2324

24-
ArchiveManager::ArchiveManager(const std::vector<std::string>& archivePaths,
25-
const std::unordered_set<uint32_t>& validGameVersions)
26-
: mValidGameVersions(validGameVersions) {
25+
void ArchiveManager::Init(const std::vector<std::string>& archivePaths,
26+
const std::unordered_set<uint32_t>& validGameVersions) {
27+
mValidGameVersions = validGameVersions;
2728
auto archives = GetArchiveListInPaths(archivePaths);
2829
for (const auto archive : archives) {
2930
AddArchive(archive);
@@ -39,6 +40,10 @@ bool ArchiveManager::IsArchiveLoaded() {
3940
}
4041

4142
std::shared_ptr<File> ArchiveManager::LoadFile(const std::string& filePath) {
43+
if (filePath == "") {
44+
return nullptr;
45+
}
46+
4247
const auto archive = mFileToArchive[CRC64(filePath.c_str())];
4348
if (archive == nullptr) {
4449
return nullptr;
@@ -56,6 +61,28 @@ std::shared_ptr<File> ArchiveManager::LoadFile(uint64_t hash) {
5661
return archive->LoadFile(hash);
5762
}
5863

64+
std::shared_ptr<File> ArchiveManager::LoadFileRaw(const std::string& filePath) {
65+
if (filePath == "") {
66+
return nullptr;
67+
}
68+
69+
const auto archive = mFileToArchive[CRC64(filePath.c_str())];
70+
if (archive == nullptr) {
71+
return nullptr;
72+
}
73+
74+
return archive->LoadFileRaw(filePath);
75+
}
76+
77+
std::shared_ptr<File> ArchiveManager::LoadFileRaw(uint64_t hash) {
78+
const auto archive = mFileToArchive[hash];
79+
if (archive == nullptr) {
80+
return nullptr;
81+
}
82+
83+
return archive->LoadFileRaw(hash);
84+
}
85+
5986
bool ArchiveManager::HasFile(const std::string& filePath) {
6087
return HasFile(CRC64(filePath.c_str()));
6188
}
@@ -105,9 +132,9 @@ void ArchiveManager::SetArchives(const std::vector<std::shared_ptr<Archive>>& ar
105132
}
106133
}
107134

108-
const std::string& ArchiveManager::HashToString(uint64_t hash) {
135+
const std::string* ArchiveManager::HashToString(uint64_t hash) const {
109136
auto it = mHashes.find(hash);
110-
return it != mHashes.end() ? it->second : "";
137+
return it != mHashes.end() ? &it->second : nullptr;
111138
}
112139

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

184+
archive->Load();
157185
return AddArchive(archive);
158186
}
159187

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

174202
mArchives.push_back(archive);
175-
mGameVersions.push_back(archive->GetGameVersion());
203+
if (archive->HasGameVersion()) {
204+
mGameVersions.push_back(archive->GetGameVersion());
205+
}
176206
const auto fileList = archive->ListFiles();
177-
for (size_t i = 0; i < fileList->size(); i++) {
178-
const auto file = fileList->operator[](i);
179-
const auto hash = CRC64(file.c_str());
180-
mHashes[hash] = file;
207+
for (auto& [hash, filename] : *fileList.get()) {
208+
mHashes[hash] = filename;
181209
mFileToArchive[hash] = archive;
182210
}
183211
return archive;

src/resource/archive/ArchiveManager.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,23 @@ class Archive;
1414
class ArchiveManager {
1515
public:
1616
ArchiveManager();
17-
ArchiveManager(const std::vector<std::string>& archivePaths);
18-
ArchiveManager(const std::vector<std::string>& archivePaths, const std::unordered_set<uint32_t>& validGameVersions);
17+
void Init(const std::vector<std::string>& archivePaths);
18+
void Init(const std::vector<std::string>& archivePaths, const std::unordered_set<uint32_t>& validGameVersions);
1919
~ArchiveManager();
2020

2121
bool IsArchiveLoaded();
2222
std::shared_ptr<File> LoadFile(const std::string& filePath);
2323
std::shared_ptr<File> LoadFile(uint64_t hash);
24+
std::shared_ptr<File> LoadFileRaw(const std::string& filePath);
25+
std::shared_ptr<File> LoadFileRaw(uint64_t hash);
2426
bool HasFile(const std::string& filePath);
2527
bool HasFile(uint64_t hash);
2628
std::shared_ptr<std::vector<std::string>> ListFiles(const std::string& filter);
2729
std::shared_ptr<std::vector<std::string>> ListFiles();
2830
std::vector<uint32_t> GetGameVersions();
2931
std::vector<std::shared_ptr<Archive>> GetArchives();
3032
void SetArchives(const std::vector<std::shared_ptr<Archive>>& archives);
31-
const std::string& HashToString(uint64_t hash);
33+
const std::string* HashToString(uint64_t hash) const;
3234
bool IsGameVersionValid(uint32_t gameVersion);
3335

3436
protected:

src/resource/archive/O2rArchive.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ class O2rArchive : virtual public Archive {
1818
O2rArchive(const std::string& archivePath);
1919
~O2rArchive();
2020

21-
protected:
21+
bool LoadRaw();
22+
bool UnloadRaw();
2223
std::shared_ptr<File> LoadFileRaw(const std::string& filePath);
2324
std::shared_ptr<File> LoadFileRaw(uint64_t hash);
25+
26+
protected:
2427
std::shared_ptr<ResourceInitData> LoadFileMeta(const std::string& filePath);
2528
std::shared_ptr<ResourceInitData> LoadFileMeta(uint64_t hash);
2629

27-
bool LoadRaw();
28-
bool UnloadRaw();
29-
3030
private:
3131
};
3232
} // namespace LUS

src/resource/archive/OtrArchive.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ std::shared_ptr<File> OtrArchive::LoadFileRaw(const std::string& filePath) {
2424
}
2525

2626
auto fileToLoad = std::make_shared<File>();
27-
fileToLoad->InitData->Path = filePath;
2827
DWORD fileSize = SFileGetFileSize(fileHandle, 0);
2928
DWORD readBytes;
30-
fileToLoad->Buffer->resize(fileSize);
29+
fileToLoad->Buffer = std::make_shared<std::vector<char>>(fileSize);
3130
bool readFileSuccess = SFileReadFile(fileHandle, fileToLoad->Buffer->data(), fileSize, &readBytes, NULL);
3231

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

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

5859
std::shared_ptr<ResourceInitData> OtrArchive::LoadFileMeta(const std::string& filePath) {
@@ -67,10 +68,10 @@ bool OtrArchive::LoadRaw() {
6768
const bool opened = SFileOpenArchive(GetPath().c_str(), 0, MPQ_OPEN_READ_ONLY, &mHandle);
6869
if (opened) {
6970
SPDLOG_INFO("Opened mpq file \"{}\"", GetPath());
70-
SFileCloseArchive(mHandle);
7171
} else {
7272
SPDLOG_ERROR("Failed to load mpq file \"{}\"", GetPath());
7373
mHandle = nullptr;
74+
return false;
7475
}
7576

7677
// Generate the file list by reading the list file.

src/resource/archive/OtrArchive.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ class OtrArchive : virtual public Archive {
2424
OtrArchive(const std::string& archivePath);
2525
~OtrArchive();
2626

27-
protected:
27+
bool LoadRaw();
28+
bool UnloadRaw();
2829
std::shared_ptr<File> LoadFileRaw(const std::string& filePath);
2930
std::shared_ptr<File> LoadFileRaw(uint64_t hash);
31+
32+
protected:
3033
std::shared_ptr<ResourceInitData> LoadFileMeta(const std::string& filePath);
3134
std::shared_ptr<ResourceInitData> LoadFileMeta(uint64_t hash);
3235

33-
bool LoadRaw();
34-
bool UnloadRaw();
35-
3636
private:
3737
HANDLE mHandle;
3838
};

src/window/gui/GameOverlay.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ GameOverlay::~GameOverlay() {
1717

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

0 commit comments

Comments
 (0)