Skip to content

Commit da077e8

Browse files
committed
start down the fontfactory path
1 parent aa1091d commit da077e8

12 files changed

+157
-70
lines changed

src/resource/ResourceManager.cpp

+20-20
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ bool ResourceManager::DidLoadSuccessfully() {
4545
return mArchiveManager != nullptr && mArchiveManager->IsArchiveLoaded();
4646
}
4747

48-
std::shared_ptr<File> ResourceManager::LoadFileProcess(const std::string& filePath) {
49-
auto file = mArchiveManager->LoadFile(filePath);
48+
std::shared_ptr<File> ResourceManager::LoadFileProcess(const std::string& filePath, std::shared_ptr<ResourceInitData> initData) {
49+
auto file = mArchiveManager->LoadFile(filePath, initData);
5050
if (file != nullptr) {
5151
SPDLOG_TRACE("Loaded File {} on ResourceManager", file->InitData->Path);
5252
} else {
@@ -55,18 +55,18 @@ std::shared_ptr<File> ResourceManager::LoadFileProcess(const std::string& filePa
5555
return file;
5656
}
5757

58-
std::shared_ptr<IResource> ResourceManager::LoadResourceProcess(const std::string& filePath, bool loadExact) {
58+
std::shared_ptr<IResource> ResourceManager::LoadResourceProcess(const std::string& filePath, bool loadExact, std::shared_ptr<ResourceInitData> initData) {
5959
// Check for and remove the OTR signature
6060
if (OtrSignatureCheck(filePath.c_str())) {
6161
const auto newFilePath = filePath.substr(7);
62-
return LoadResourceProcess(newFilePath);
62+
return LoadResourceProcess(newFilePath, false, initData);
6363
}
6464

6565
// Attempt to load the alternate version of the asset, if we fail then we continue trying to load the standard
6666
// asset.
6767
if (!loadExact && CVarGetInteger("gAltAssets", 0) && !filePath.starts_with(IResource::gAltAssetPrefix)) {
6868
const auto altPath = IResource::gAltAssetPrefix + filePath;
69-
auto altResource = LoadResourceProcess(altPath, loadExact);
69+
auto altResource = LoadResourceProcess(altPath, loadExact, initData);
7070

7171
if (altResource != nullptr) {
7272
return altResource;
@@ -99,7 +99,7 @@ std::shared_ptr<IResource> ResourceManager::LoadResourceProcess(const std::strin
9999
}
100100

101101
// Get the file from the OTR
102-
auto file = LoadFileProcess(filePath);
102+
auto file = LoadFileProcess(filePath, initData);
103103
if (file == nullptr) {
104104
SPDLOG_TRACE("Failed to load resource file at path {}", filePath);
105105
}
@@ -136,20 +136,20 @@ std::shared_ptr<IResource> ResourceManager::LoadResourceProcess(const std::strin
136136
return resource;
137137
}
138138

139-
std::shared_future<std::shared_ptr<File>> ResourceManager::LoadFileAsync(const std::string& filePath, bool priority) {
140-
if (priority) {
141-
return mThreadPool->submit_front(&ResourceManager::LoadFileProcess, this, filePath).share();
142-
} else {
143-
return mThreadPool->submit_back(&ResourceManager::LoadFileProcess, this, filePath).share();
144-
}
145-
}
139+
// std::shared_future<std::shared_ptr<File>> ResourceManager::LoadFileAsync(const std::string& filePath, bool priority) {
140+
// if (priority) {
141+
// return mThreadPool->submit_front(&ResourceManager::LoadFileProcess, this, filePath).share();
142+
// } else {
143+
// return mThreadPool->submit_back(&ResourceManager::LoadFileProcess, this, filePath).share();
144+
// }
145+
// }
146146

147-
std::shared_ptr<File> ResourceManager::LoadFile(const std::string& filePath) {
148-
return LoadFileAsync(filePath, true).get();
149-
}
147+
// std::shared_ptr<File> ResourceManager::LoadFile(const std::string& filePath) {
148+
// return LoadFileAsync(filePath, true).get();
149+
// }
150150

151151
std::shared_future<std::shared_ptr<IResource>> ResourceManager::LoadResourceAsync(const std::string& filePath,
152-
bool loadExact, bool priority) {
152+
bool loadExact, bool priority, std::shared_ptr<ResourceInitData> initData) {
153153
// Check for and remove the OTR signature
154154
if (OtrSignatureCheck(filePath.c_str())) {
155155
auto newFilePath = filePath.substr(7);
@@ -167,13 +167,13 @@ std::shared_future<std::shared_ptr<IResource>> ResourceManager::LoadResourceAsyn
167167
const auto newFilePath = std::string(filePath);
168168

169169
if (priority) {
170-
return mThreadPool->submit_front(&ResourceManager::LoadResourceProcess, this, newFilePath, loadExact);
170+
return mThreadPool->submit_front(&ResourceManager::LoadResourceProcess, this, newFilePath, loadExact, initData);
171171
} else {
172-
return mThreadPool->submit_back(&ResourceManager::LoadResourceProcess, this, newFilePath, loadExact);
172+
return mThreadPool->submit_back(&ResourceManager::LoadResourceProcess, this, newFilePath, loadExact, initData);
173173
}
174174
}
175175

176-
std::shared_ptr<IResource> ResourceManager::LoadResource(const std::string& filePath, bool loadExact) {
176+
std::shared_ptr<IResource> ResourceManager::LoadResource(const std::string& filePath, bool loadExact, std::shared_ptr<ResourceInitData> initData) {
177177
auto resource = LoadResourceAsync(filePath, loadExact, true).get();
178178
if (resource == nullptr) {
179179
SPDLOG_ERROR("Failed to load resource file at path {}", filePath);

src/resource/ResourceManager.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ class ResourceManager {
2929
bool DidLoadSuccessfully();
3030
std::shared_ptr<ArchiveManager> GetArchiveManager();
3131
std::shared_ptr<ResourceLoader> GetResourceLoader();
32-
std::shared_future<std::shared_ptr<File>> LoadFileAsync(const std::string& filePath, bool priority = false);
33-
std::shared_ptr<File> LoadFile(const std::string& filePath);
32+
// std::shared_future<std::shared_ptr<File>> LoadFileAsync(const std::string& filePath, bool priority = false);
33+
// std::shared_ptr<File> LoadFile(const std::string& filePath);
3434
std::shared_ptr<IResource> GetCachedResource(const std::string& filePath, bool loadExact = false);
35-
std::shared_ptr<IResource> LoadResource(const std::string& filePath, bool loadExact = false);
36-
std::shared_ptr<IResource> LoadResourceProcess(const std::string& filePath, bool loadExact = false);
35+
std::shared_ptr<IResource> LoadResource(const std::string& filePath, bool loadExact = false, std::shared_ptr<ResourceInitData> initData = nullptr);
36+
std::shared_ptr<IResource> LoadResourceProcess(const std::string& filePath, bool loadExact = false, std::shared_ptr<ResourceInitData> initData = nullptr);
3737
size_t UnloadResource(const std::string& filePath);
3838
std::shared_future<std::shared_ptr<IResource>> LoadResourceAsync(const std::string& filePath,
39-
bool loadExact = false, bool priority = false);
39+
bool loadExact = false, bool priority = false, std::shared_ptr<ResourceInitData> initData = nullptr);
4040
std::shared_ptr<std::vector<std::shared_ptr<IResource>>> LoadDirectory(const std::string& searchMask);
4141
std::shared_ptr<std::vector<std::shared_future<std::shared_ptr<IResource>>>>
4242
LoadDirectoryAsync(const std::string& searchMask, bool priority = false);
@@ -45,7 +45,7 @@ class ResourceManager {
4545
bool OtrSignatureCheck(const char* fileName);
4646

4747
protected:
48-
std::shared_ptr<File> LoadFileProcess(const std::string& filePath);
48+
std::shared_ptr<File> LoadFileProcess(const std::string& filePath, std::shared_ptr<ResourceInitData> initData = nullptr);
4949
std::shared_ptr<IResource> GetCachedResource(std::variant<ResourceLoadError, std::shared_ptr<IResource>> cacheLine);
5050
std::variant<ResourceLoadError, std::shared_ptr<IResource>> CheckCache(const std::string& filePath,
5151
bool loadExact = false);

src/resource/ResourceType.h

+1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,6 @@ enum class ResourceType {
1414
Array = 0x4F415252, // OARR
1515
Blob = 0x4F424C42, // OBLB
1616
Texture = 0x4F544558, // OTEX
17+
Font = 0x464F4E54 // FONT
1718
};
1819
} // namespace LUS

src/resource/archive/Archive.cpp

+17-11
Original file line numberDiff line numberDiff line change
@@ -203,18 +203,24 @@ std::shared_ptr<tinyxml2::XMLDocument> Archive::CreateXMLReader(std::shared_ptr<
203203
return xmlReader;
204204
}
205205

206-
std::shared_ptr<File> Archive::LoadFile(const std::string& filePath) {
207-
auto metaFilePath = filePath + ".meta";
208-
auto metaFileToLoad = LoadFileRaw(metaFilePath);
209-
206+
std::shared_ptr<File> Archive::LoadFile(const std::string& filePath, std::shared_ptr<ResourceInitData> initData) {
210207
std::shared_ptr<File> fileToLoad = nullptr;
211-
if (metaFileToLoad != nullptr) {
212-
auto initData = ReadResourceInitData(filePath, metaFileToLoad);
213-
fileToLoad = LoadFileRaw(initData->Path);
208+
209+
if (initData != nullptr) {
210+
fileToLoad = LoadFileRaw(filePath);
214211
fileToLoad->InitData = initData;
215212
} else {
216-
fileToLoad = LoadFileRaw(filePath);
217-
fileToLoad->InitData = ReadResourceInitDataLegacy(filePath, fileToLoad);
213+
auto metaFilePath = filePath + ".meta";
214+
auto metaFileToLoad = LoadFileRaw(metaFilePath);
215+
216+
if (metaFileToLoad != nullptr) {
217+
auto initDataFromMetaFile = ReadResourceInitData(filePath, metaFileToLoad);
218+
fileToLoad = LoadFileRaw(initDataFromMetaFile->Path);
219+
fileToLoad->InitData = initDataFromMetaFile;
220+
} else {
221+
fileToLoad = LoadFileRaw(filePath);
222+
fileToLoad->InitData = ReadResourceInitDataLegacy(filePath, fileToLoad);
223+
}
218224
}
219225

220226
if (fileToLoad == nullptr) {
@@ -234,10 +240,10 @@ std::shared_ptr<File> Archive::LoadFile(const std::string& filePath) {
234240
return fileToLoad;
235241
}
236242

237-
std::shared_ptr<File> Archive::LoadFile(uint64_t hash) {
243+
std::shared_ptr<File> Archive::LoadFile(uint64_t hash, std::shared_ptr<ResourceInitData> initData) {
238244
const std::string& filePath =
239245
*Context::GetInstance()->GetResourceManager()->GetArchiveManager()->HashToString(hash);
240-
return LoadFile(filePath);
246+
return LoadFile(filePath, initData);
241247
}
242248

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

src/resource/archive/Archive.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class Archive {
2323
void Load();
2424
void Unload();
2525

26-
virtual std::shared_ptr<File> LoadFile(const std::string& filePath);
27-
virtual std::shared_ptr<File> LoadFile(uint64_t hash);
26+
virtual std::shared_ptr<File> LoadFile(const std::string& filePath, std::shared_ptr<ResourceInitData> initData = nullptr);
27+
virtual std::shared_ptr<File> LoadFile(uint64_t hash, std::shared_ptr<ResourceInitData> initData = nullptr);
2828
std::shared_ptr<std::unordered_map<uint64_t, std::string>> ListFiles();
2929
std::shared_ptr<std::unordered_map<uint64_t, std::string>> ListFiles(const std::string& filter);
3030
bool HasFile(const std::string& filePath);

src/resource/archive/ArchiveManager.cpp

+23-21
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ bool ArchiveManager::IsArchiveLoaded() {
4040
return !mArchives.empty();
4141
}
4242

43-
std::shared_ptr<File> ArchiveManager::LoadFile(const std::string& filePath) {
43+
std::shared_ptr<File> ArchiveManager::LoadFile(const std::string& filePath, std::shared_ptr<ResourceInitData> initData) {
4444
if (filePath == "") {
4545
return nullptr;
4646
}
@@ -50,39 +50,41 @@ std::shared_ptr<File> ArchiveManager::LoadFile(const std::string& filePath) {
5050
return nullptr;
5151
}
5252

53-
return archive->LoadFile(filePath);
53+
return archive->LoadFile(filePath, initData);
5454
}
5555

56-
std::shared_ptr<File> ArchiveManager::LoadFile(uint64_t hash) {
56+
std::shared_ptr<File> ArchiveManager::LoadFile(uint64_t hash, std::shared_ptr<ResourceInitData> initData) {
5757
const auto archive = mFileToArchive[hash];
5858
if (archive == nullptr) {
5959
return nullptr;
6060
}
6161

62-
return archive->LoadFile(hash);
62+
return archive->LoadFile(hash, initData);
6363
}
6464

65-
// std::shared_ptr<File> ArchiveManager::LoadFileRaw(const std::string& filePath) {
66-
// if (filePath == "") {
67-
// return nullptr;
68-
// }
65+
//TODO remove
66+
std::shared_ptr<File> ArchiveManager::LoadFileRaw(const std::string& filePath) {
67+
if (filePath == "") {
68+
return nullptr;
69+
}
6970

70-
// const auto archive = mFileToArchive[CRC64(filePath.c_str())];
71-
// if (archive == nullptr) {
72-
// return nullptr;
73-
// }
71+
const auto archive = mFileToArchive[CRC64(filePath.c_str())];
72+
if (archive == nullptr) {
73+
return nullptr;
74+
}
7475

75-
// return archive->LoadFileRaw(filePath);
76-
// }
76+
return archive->LoadFileRaw(filePath);
77+
}
7778

78-
// std::shared_ptr<File> ArchiveManager::LoadFileRaw(uint64_t hash) {
79-
// const auto archive = mFileToArchive[hash];
80-
// if (archive == nullptr) {
81-
// return nullptr;
82-
// }
79+
std::shared_ptr<File> ArchiveManager::LoadFileRaw(uint64_t hash) {
80+
const auto archive = mFileToArchive[hash];
81+
if (archive == nullptr) {
82+
return nullptr;
83+
}
8384

84-
// return archive->LoadFileRaw(hash);
85-
// }
85+
return archive->LoadFileRaw(hash);
86+
}
87+
//ENDTODO
8688

8789
bool ArchiveManager::HasFile(const std::string& filePath) {
8890
return HasFile(CRC64(filePath.c_str()));

src/resource/archive/ArchiveManager.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <unordered_map>
77
#include <unordered_set>
88
#include <stdint.h>
9+
#include "resource/File.h"
910

1011
namespace LUS {
1112
struct File;
@@ -19,10 +20,12 @@ class ArchiveManager {
1920
~ArchiveManager();
2021

2122
bool IsArchiveLoaded();
22-
std::shared_ptr<File> LoadFile(const std::string& filePath);
23-
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);
23+
std::shared_ptr<File> LoadFile(const std::string& filePath, std::shared_ptr<ResourceInitData> initData = nullptr);
24+
std::shared_ptr<File> LoadFile(uint64_t hash, std::shared_ptr<ResourceInitData> initData = nullptr);
25+
//TODO revome
26+
std::shared_ptr<File> LoadFileRaw(const std::string& filePath);
27+
std::shared_ptr<File> LoadFileRaw(uint64_t hash);
28+
//ENDTODO
2629
bool HasFile(const std::string& filePath);
2730
bool HasFile(uint64_t hash);
2831
std::shared_ptr<std::vector<std::string>> ListFiles(const std::string& filter);

src/resource/factory/FontFactory.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "resource/factory/FontFactory.h"
2+
#include "resource/type/Font.h"
3+
#include "spdlog/spdlog.h"
4+
5+
namespace LUS {
6+
std::shared_ptr<IResource> ResourceFactoryBinaryFontV0::ReadResource(std::shared_ptr<File> file) {
7+
if (!FileHasValidFormatAndReader(file)) {
8+
return nullptr;
9+
}
10+
11+
auto font = std::make_shared<Font>(file->InitData);
12+
auto reader = std::get<std::shared_ptr<BinaryReader>>(file->Reader);
13+
14+
uint32_t dataSize = file->Buffer->size();
15+
16+
font->Data.reserve(dataSize);
17+
18+
for (uint32_t i = 0; i < dataSize; i++) {
19+
font->Data.push_back(reader->ReadChar());
20+
}
21+
22+
return font;
23+
}
24+
} // namespace LUS

src/resource/factory/FontFactory.h

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
#include "resource/Resource.h"
4+
#include "resource/ResourceFactoryBinary.h"
5+
6+
namespace LUS {
7+
class ResourceFactoryBinaryFontV0 : public ResourceFactoryBinary {
8+
public:
9+
std::shared_ptr<IResource> ReadResource(std::shared_ptr<File> file) override;
10+
};
11+
}; // namespace LUS

src/resource/type/Font.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "Font.h"
2+
3+
namespace LUS {
4+
Font::Font() : Resource(std::shared_ptr<ResourceInitData>()) {
5+
}
6+
7+
void* Font::GetPointer() {
8+
return Data.data();
9+
}
10+
11+
size_t Font::GetPointerSize() {
12+
return Data.size() * sizeof(char);
13+
}
14+
} // namespace LUS

src/resource/type/Font.h

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include "resource/Resource.h"
4+
5+
namespace LUS {
6+
class Font : public Resource<void> {
7+
public:
8+
using Resource::Resource;
9+
10+
Font();
11+
12+
void* GetPointer() override;
13+
size_t GetPointerSize() override;
14+
15+
std::vector<char> Data;
16+
};
17+
}; // namespace LUS

src/window/gui/GameOverlay.cpp

+15-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "public/bridge/consolevariablebridge.h"
44
#include "resource/File.h"
5+
#include "resource/type/Font.h"
56
#include "resource/archive/Archive.h"
67
#include "resource/ResourceManager.h"
78
#include "Context.h"
@@ -18,13 +19,21 @@ GameOverlay::~GameOverlay() {
1819
void GameOverlay::LoadFont(const std::string& name, const std::string& path, float fontSize) {
1920
// font resource type in gui
2021
ImGuiIO& io = ImGui::GetIO();
21-
std::shared_ptr<File> font = Context::GetInstance()->GetResourceManager()->GetArchiveManager()->LoadFileRaw(path);
22-
if (font->IsLoaded) {
23-
// TODO: Nothing is ever unloading the font or this fontData array.
24-
char* fontData = new char[font->Buffer->size()];
25-
memcpy(fontData, font->Buffer->data(), font->Buffer->size());
26-
mFonts[name] = io.Fonts->AddFontFromMemoryTTF(fontData, font->Buffer->size(), fontSize);
22+
auto initData = std::make_shared<ResourceInitData>();
23+
initData->Format = RESOURCE_FORMAT_BINARY;
24+
initData->Type = static_cast<uint32_t>(ResourceType::Font);
25+
initData->ResourceVersion = 0;
26+
std::shared_ptr<Font> font = std::static_pointer_cast<Font>(Context::GetInstance()->GetResourceManager()->LoadResource(path, false, initData));
27+
if (font != nullptr) {
28+
mFonts[name] = io.Fonts->AddFontFromMemoryTTF(font->Data.data(), font->Data.size(), fontSize);
2729
}
30+
// std::shared_ptr<File> font = Context::GetInstance()->GetResourceManager()->GetArchiveManager()->LoadFileRaw(path);
31+
// if (font->IsLoaded) {
32+
// // TODO: Nothing is ever unloading the font or this fontData array.
33+
// char* fontData = new char[font->Buffer->size()];
34+
// memcpy(fontData, font->Buffer->data(), font->Buffer->size());
35+
// mFonts[name] = io.Fonts->AddFontFromMemoryTTF(fontData, font->Buffer->size(), fontSize);
36+
// }
2837
}
2938

3039
void GameOverlay::TextDraw(float x, float y, bool shadow, ImVec4 color, const char* fmt, ...) {

0 commit comments

Comments
 (0)