Skip to content

Commit 8989568

Browse files
authored
Asset Pre-counting (#212)
* Add atomic_ref versions of functions to allow asset count passback. Add process variable to gate full processing (for pre-counting assets). * Bump C++ standard back to 20 for atomic_ref.
1 parent 3dde9e6 commit 8989568

File tree

3 files changed

+36
-13
lines changed

3 files changed

+36
-13
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.12)
22
project(torch)
33
include(FetchContent)
44

5-
set(CMAKE_CXX_STANDARD 17)
5+
set(CMAKE_CXX_STANDARD 20)
66
set(CMAKE_CXX_STANDARD_REQUIRED ON)
77
set(CMAKE_CXX_EXTENSIONS OFF)
88
set(CMAKE_C_STANDARD 11)

src/Companion.cpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@ static std::string GetTypeNode(YAML::Node& node) {
130130
Companion* Companion::Instance;
131131

132132
void Companion::Init(const ExportType type) {
133+
size_t assetCount = 0;
134+
Init(type, std::atomic_ref<size_t>(assetCount));
135+
}
136+
137+
void Companion::Init(const ExportType type, std::atomic_ref<size_t> assetCount) {
133138

134139
spdlog::set_level(spdlog::level::debug);
135140
spdlog::set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%l] %v");
@@ -224,7 +229,7 @@ void Companion::Init(const ExportType type) {
224229
this->RegisterFactory("NAUDIO:V1:SEQUENCE", std::make_shared<NSequenceFactory>());
225230
#endif
226231
#ifndef __EMSCRIPTEN__ // We call this manually
227-
this->Process();
232+
this->Process(assetCount);
228233
#endif
229234
}
230235

@@ -356,7 +361,7 @@ void Companion::ParseModdingConfig() {
356361
}
357362

358363

359-
void Companion::ParseCurrentFileConfig(YAML::Node node) {
364+
void Companion::ParseCurrentFileConfig(YAML::Node node, std::atomic_ref<size_t> assetCount) {
360365
if (node["external_files"]) {
361366
auto externalFiles = node["external_files"];
362367
if (externalFiles.IsSequence() && externalFiles.size()) {
@@ -388,8 +393,10 @@ void Companion::ParseCurrentFileConfig(YAML::Node node) {
388393
YAML::Node root = YAML::LoadFile(externalFileName);
389394

390395
if (!Torch::contains(this->gProcessedFiles, this->gCurrentFile)) {
391-
ProcessFile(root);
392-
this->gProcessedFiles.insert(this->gCurrentFile);
396+
ProcessFile(root, assetCount);
397+
if (process) {
398+
this->gProcessedFiles.insert(this->gCurrentFile);
399+
}
393400
}
394401

395402
SPDLOG_INFO("Finishing processing of file: {}", currentFile);
@@ -610,6 +617,12 @@ void Companion::ProcessTables(YAML::Node& rom) {
610617
}
611618

612619
void Companion::ProcessFile(YAML::Node root) {
620+
size_t assetCount = 0;
621+
ProcessFile(root, std::atomic_ref<size_t>(assetCount));
622+
}
623+
624+
void Companion::ProcessFile(YAML::Node root, std::atomic_ref<size_t> assetCount) {
625+
assetCount++;
613626
// Set compressed file offsets and compression type
614627
if (auto segments = root[":config"]["segments"]) {
615628
if (segments.IsSequence() && segments.size() > 0) {
@@ -672,10 +685,10 @@ void Companion::ProcessFile(YAML::Node root) {
672685
GFXDOverride::ClearVtx();
673686

674687
if(root[":config"]) {
675-
this->ParseCurrentFileConfig(root[":config"]);
688+
this->ParseCurrentFileConfig(root[":config"], assetCount);
676689
}
677690

678-
if(!this->NodeHasChanges(this->gCurrentFile) && !this->gNodeForceProcessing) {
691+
if(!process || (!this->NodeHasChanges(this->gCurrentFile) && !this->gNodeForceProcessing)) {
679692
return;
680693
}
681694

@@ -1018,8 +1031,7 @@ void Companion::ProcessFile(YAML::Node root) {
10181031
}
10191032
}
10201033

1021-
void Companion::Process() {
1022-
1034+
void Companion::Process(std::atomic_ref<size_t> assetCount) {
10231035
auto configPath = this->gSourceDirectory / "config.yml";
10241036

10251037
if(!fs::exists(configPath)) {
@@ -1315,8 +1327,10 @@ void Companion::Process() {
13151327
this->gCurrentFile = yamlPath;
13161328

13171329
if (!Torch::contains(this->gProcessedFiles, this->gCurrentFile)) {
1318-
ProcessFile(root);
1319-
this->gProcessedFiles.insert(this->gCurrentFile);
1330+
ProcessFile(root, assetCount);
1331+
if (process) {
1332+
this->gProcessedFiles.insert(this->gCurrentFile);
1333+
}
13201334
}
13211335
}
13221336

@@ -1672,6 +1686,10 @@ std::optional<std::vector<std::tuple<std::string, YAML::Node>>> Companion::GetNo
16721686

16731687
}
16741688

1689+
void Companion::SetProcess(bool shouldProcess) {
1690+
this->process = shouldProcess;
1691+
}
1692+
16751693
void Companion::RegisterCompanionFile(const std::string path, std::vector<char> data) {
16761694
this->gCompanionFiles[path] = data;
16771695
SPDLOG_TRACE("Registered companion file {}", path);

src/Companion.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <optional>
55
#include <filesystem>
66
#include <vector>
7+
#include <atomic>
78
#include <fstream>
89
#include <unordered_map>
910
#include <unordered_set>
@@ -139,10 +140,11 @@ class Companion {
139140
Companion(rom, otr, debug, false, srcDir, destPath) {}
140141

141142
void Init(ExportType type);
143+
void Init(ExportType type, std::atomic_ref<size_t> assetCount);
142144

143145
bool NodeHasChanges(const std::string& string);
144146

145-
void Process();
147+
void Process(std::atomic_ref<size_t> assetCount);
146148

147149
bool IsOTRMode() const { return (this->gConfig.otrMode != ArchiveType::None); }
148150
bool IsDebug() const { return this->gConfig.debug; }
@@ -189,6 +191,7 @@ class Companion {
189191
void SetAdditionalFiles(const std::vector<std::string>& files) { this->gAdditionalFiles = files; }
190192
void SetVersion(const std::string& version) { this->gVersion = version; }
191193

194+
void SetProcess(bool shouldProcess);
192195
TorchConfig& GetConfig() { return this->gConfig; }
193196
BinaryWrapper* GetCurrentWrapper() { return this->gCurrentWrapper; }
194197

@@ -218,6 +221,7 @@ class Companion {
218221
std::string gCurrentVirtualPath;
219222
std::string gFileHeader;
220223
bool gEnablePadGen = false;
224+
bool process = true;
221225
uint32_t gCurrentPad = 0;
222226
uint32_t gCurrentFileOffset;
223227
uint32_t gCurrentSegmentNumber;
@@ -240,10 +244,11 @@ class Companion {
240244
std::unordered_map<std::string, std::unordered_map<uint32_t, std::tuple<std::string, YAML::Node>>> gAddrMap;
241245

242246
void ProcessFile(YAML::Node root);
247+
void ProcessFile(YAML::Node root, std::atomic_ref<size_t> assetCount);
243248
void ParseEnums(std::string& file);
244249
void ParseHash();
245250
void ParseModdingConfig();
246-
void ParseCurrentFileConfig(YAML::Node node);
251+
void ParseCurrentFileConfig(YAML::Node node, std::atomic_ref<size_t> assetCount);
247252
void RegisterFactory(const std::string& type, const std::shared_ptr<BaseFactory>& factory);
248253
void ExtractNode(YAML::Node& node, std::string& name, BinaryWrapper* binary);
249254
void ProcessTables(YAML::Node& rom);

0 commit comments

Comments
 (0)