Skip to content

Commit b3965d0

Browse files
authored
Merge pull request #480 from AxioDL/name-db-support
Initial resource name database support
2 parents 77c266b + f258b3a commit b3965d0

File tree

6 files changed

+91
-12
lines changed

6 files changed

+91
-12
lines changed

Runtime/CMain.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#include "CResourceNameDatabase.hpp"
2+
13
#include <string>
24
#include <string_view>
35
#include <numeric>
@@ -167,6 +169,7 @@ struct Application {
167169
FileStoreManager& m_fileMgr;
168170
CVarManager& m_cvarManager;
169171
CVarCommons& m_cvarCommons;
172+
CResourceNameDatabase& m_nameDatabase;
170173
ImGuiConsole m_imGuiConsole;
171174

172175
std::string m_deferredProject;
@@ -187,12 +190,14 @@ struct Application {
187190
// is built, i.e during initialization
188191

189192
public:
190-
Application(int argc, char** argv, FileStoreManager& fileMgr, CVarManager& cvarMgr, CVarCommons& cvarCmns)
193+
Application(int argc, char** argv, FileStoreManager& fileMgr, CVarManager& cvarMgr, CVarCommons& cvarCmns,
194+
CResourceNameDatabase& nameDatabase)
191195
: m_argc(argc)
192196
, m_argv(argv)
193197
, m_fileMgr(fileMgr)
194198
, m_cvarManager(cvarMgr)
195199
, m_cvarCommons(cvarCmns)
200+
, m_nameDatabase(nameDatabase)
196201
, m_imGuiConsole(cvarMgr, cvarCmns) {}
197202

198203
void onAppLaunched(const AuroraInfo& info) noexcept {
@@ -284,7 +289,7 @@ struct Application {
284289
const auto targetFrameTime = getTargetFrameTime();
285290
bool skipRetrace = false;
286291
if (g_ResFactory != nullptr) {
287-
// OPTICK_EVENT("Async Load Resources");
292+
// OPTICK_EVENT("Async Load Resources");
288293
const auto idleTime = m_limiter.SleepTime(targetFrameTime);
289294
skipRetrace = g_ResFactory->AsyncIdle(idleTime);
290295
}
@@ -295,12 +300,12 @@ struct Application {
295300
} else {
296301
// No more to load, and we're under frame time
297302
{
298-
//OPTICK_EVENT("Sleep");
303+
// OPTICK_EVENT("Sleep");
299304
m_limiter.Sleep(targetFrameTime);
300305
}
301306
}
302307

303-
//OPTICK_FRAME("MainThread");
308+
// OPTICK_FRAME("MainThread");
304309

305310
// Check if fullscreen has been toggled, if so set the fullscreen cvar accordingly
306311
if (m_fullscreenToggleRequested) {
@@ -364,7 +369,7 @@ struct Application {
364369
}
365370

366371
void onAppDraw() noexcept {
367-
//OPTICK_EVENT("Draw");
372+
// OPTICK_EVENT("Draw");
368373
if (g_Renderer != nullptr) {
369374
g_Renderer->BeginScene();
370375
if (g_mainMP1) {
@@ -376,7 +381,7 @@ struct Application {
376381
}
377382

378383
void onAppPostDraw() noexcept {
379-
//OPTICK_EVENT("PostDraw");
384+
// OPTICK_EVENT("PostDraw");
380385
// if (m_voiceEngine) {
381386
// m_voiceEngine->pumpAndMixVoices();
382387
// }
@@ -545,6 +550,8 @@ int main(int argc, char** argv) {
545550
do {
546551
metaforce::CVarManager cvarMgr{fileMgr};
547552
metaforce::CVarCommons cvarCmns{cvarMgr};
553+
metaforce::CResourceNameDatabase nameDatabase{fileMgr};
554+
548555
cvarMgr.parseCommandLine(args);
549556
if (!restart) {
550557
// TODO add clear loggers func to logvisor so we can recreate loggers on restart
@@ -569,7 +576,7 @@ int main(int argc, char** argv) {
569576
}
570577
}
571578

572-
g_app = std::make_unique<metaforce::Application>(argc, argv, fileMgr, cvarMgr, cvarCmns);
579+
g_app = std::make_unique<metaforce::Application>(argc, argv, fileMgr, cvarMgr, cvarCmns, nameDatabase);
573580
std::string configPath{fileMgr.getStoreRoot()};
574581

575582
const AuroraConfig config{

Runtime/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ set(RUNTIME_SOURCES_B
138138
CTextureCache.hpp CTextureCache.cpp
139139
CMayaSpline.hpp CMayaSpline.cpp
140140
ImGuiPlayerLoadouts.hpp ImGuiPlayerLoadouts.cpp
141+
CResourceNameDatabase.hpp CResourceNameDatabase.cpp
141142
${PLAT_SRCS})
142143

143144
function(add_runtime_common_library name)

Runtime/CResFactory.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#include "Runtime/CResFactory.hpp"
22

3+
#include "CResourceNameDatabase.hpp"
34
#include "Runtime/CSimplePool.hpp"
45
#include "Runtime/CStopwatch.hpp"
56
#include "Runtime/Logging.hpp"
67
#include "Runtime/Formatting.hpp"
78

8-
//#include "optick.h"
9+
// #include "optick.h"
910

1011
namespace metaforce {
1112
void CResFactory::AddToLoadList(SLoadingData&& data) {
@@ -35,13 +36,18 @@ CFactoryFnReturn CResFactory::BuildSync(const SObjectTag& tag, const CVParamTran
3536
}
3637

3738
bool CResFactory::PumpResource(SLoadingData& data) {
38-
//OPTICK_EVENT();
39+
// OPTICK_EVENT();
3940
if (data.x8_dvdReq && data.x8_dvdReq->IsComplete()) {
4041
data.x8_dvdReq.reset();
4142
*data.xc_targetPtr =
4243
x5c_factoryMgr.MakeObjectFromMemory(data.x0_tag, std::move(data.x10_loadBuffer), data.x14_resSize,
4344
data.m_compressed, data.x18_cvXfer, data.m_selfRef);
4445
spdlog::info("async-built {}", data.x0_tag);
46+
if (CResourceNameDatabase::instance()) {
47+
if (const auto* name = CResourceNameDatabase::instance()->assetName(data.x0_tag.id)) {
48+
spdlog::info("rep filepath: {}", *name);
49+
}
50+
}
4551
return true;
4652
}
4753
return false;
@@ -77,7 +83,7 @@ void CResFactory::BuildAsync(const SObjectTag& tag, const CVParamTransfer& xfer,
7783
}
7884

7985
bool CResFactory::AsyncIdle(std::chrono::nanoseconds target) {
80-
//OPTICK_EVENT();
86+
// OPTICK_EVENT();
8187
if (m_loadList.empty()) {
8288
return false;
8389
}
@@ -108,7 +114,7 @@ void CResFactory::CancelBuild(const SObjectTag& tag) {
108114

109115
void CResFactory::LoadPersistentResources(CSimplePool& sp) {
110116
const auto& paks = x4_loader.GetPaks();
111-
for (const auto & pak : paks) {
117+
for (const auto& pak : paks) {
112118
if (!pak->IsWorldPak()) {
113119
for (const CAssetId& id : pak->GetDepList()) {
114120
SObjectTag tag(GetResourceTypeById(id), id);

Runtime/CResourceNameDatabase.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include "Runtime/CResourceNameDatabase.hpp"
2+
3+
#include "Runtime/CBasics.hpp"
4+
#include "Runtime/ConsoleVariables/FileStoreManager.hpp"
5+
#include "Runtime/Streams/CMemoryInStream.hpp"
6+
7+
namespace metaforce {
8+
constexpr std::string_view kDatabaseName = "mp_resource_names_confirmed.bin";
9+
CResourceNameDatabase* CResourceNameDatabase::m_instance = nullptr;
10+
11+
CResourceNameDatabase::CResourceNameDatabase(FileStoreManager& store) {
12+
m_instance = this;
13+
const std::string filename = std::string(store.getStoreRoot()) + "/" + std::string(kDatabaseName);
14+
if (!CBasics::IsFile(filename.c_str())) {
15+
return;
16+
}
17+
18+
CBasics::Sstat st;
19+
if (CBasics::Stat(filename.c_str(), &st) != 0) {
20+
return;
21+
}
22+
23+
std::unique_ptr<u8> const inBuf(new u8[st.st_size]);
24+
auto* file = fopen(filename.c_str(), "rb");
25+
(void)fread(inBuf.get(), 1, st.st_size, file);
26+
(void)fclose(file);
27+
CMemoryInStream mem(inBuf.get(), st.st_size, CMemoryInStream::EOwnerShip::NotOwned);
28+
29+
u32 count = mem.Get<u32>();
30+
while ((count--) != 0u) {
31+
const CAssetId assetId = mem.Get<u32>();
32+
const auto name = mem.Get<std::string>();
33+
m_assetNames[assetId] = name;
34+
}
35+
}
36+
} // namespace metaforce

Runtime/CResourceNameDatabase.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma once
2+
#include "RetroTypes.hpp"
3+
4+
#include <unordered_map>
5+
6+
namespace metaforce {
7+
class FileStoreManager;
8+
class CResourceNameDatabase {
9+
public:
10+
explicit CResourceNameDatabase(FileStoreManager& store);
11+
12+
bool hasAssetName(const CAssetId asset) const { return m_assetNames.contains(asset); }
13+
14+
const std::string* assetName(const CAssetId asset) const {
15+
if (!hasAssetName(asset)) {
16+
return nullptr;
17+
}
18+
19+
return &m_assetNames.at(asset);
20+
}
21+
22+
static CResourceNameDatabase* instance() { return m_instance; }
23+
24+
private:
25+
std::unordered_map<CAssetId, std::string> m_assetNames;
26+
27+
static CResourceNameDatabase* m_instance;
28+
};
29+
} // namespace metaforce

Runtime/ConsoleVariables/CVarManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ std::vector<StoreCVar::CVar> CVarManager::loadCVars(const std::string& filename)
213213
);
214214
if (file != nullptr) {
215215

216-
std::unique_ptr<u8[]> inBuf(new u8[st.st_size]);
216+
std::unique_ptr<u8> inBuf(new u8[st.st_size]);
217217
fread(inBuf.get(), 1, st.st_size, file);
218218
fclose(file);
219219
CMemoryInStream mem(inBuf.get(), st.st_size, CMemoryInStream::EOwnerShip::NotOwned);

0 commit comments

Comments
 (0)