summaryrefslogtreecommitdiff
path: root/src/Companion.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Companion.cpp')
-rw-r--r--src/Companion.cpp73
1 files changed, 49 insertions, 24 deletions
diff --git a/src/Companion.cpp b/src/Companion.cpp
index 97e81f5..e5dcf3d 100644
--- a/src/Companion.cpp
+++ b/src/Companion.cpp
@@ -200,15 +200,15 @@ void Companion::ExtractNode(YAML::Node& node, std::string& name, SWrapper* binar
result = impl->parse_modding(data, node);
} else if(this->gNodeIsCacheable && impl->IsCacheable()) {
- if(this->gCacheData.contains(name)) {
- result = std::make_shared<IParsedData>();
- result->get()->FromCacheBuffer(this->gCacheData[name]);
+ if(this->gCacheData.contains(this->gCurrentCacheHash)) {
+ result = impl->CreateDataPointer();
+ result->get()->FromCacheBuffer(this->GetCache(name));
} else {
result = impl->parse(this->gRomData, node);
if(result.has_value()) {
auto cache = result.value()->ToCacheBuffer();
if(cache.has_value()) {
- this->StoreCache(cache.value());
+ this->StoreCache(name, cache.value());
}
}
}
@@ -408,45 +408,68 @@ void Companion::ParseCurrentFileConfig(YAML::Node node) {
this->gNodeIsCacheable = GetSafeNode<bool>(node, "cacheable", true);
}
-void Companion::PrepareCache(const std::string& path) {
- std::ifstream yaml(path);
- const std::vector<uint8_t> data = std::vector<uint8_t>(std::istreambuf_iterator( yaml ), {});
- this->gCurrentCacheHash = CalculateHash(data);
-
+void Companion::ParseCache() {
const std::string out = "torch.cache.yml";
- YAML::Node root;
if(fs::exists(out)) {
- root = YAML::LoadFile(out);
+ this->gCacheNode = YAML::LoadFile(out);
} else {
- root = YAML::Node();
+ this->gCacheNode = YAML::Node();
}
+}
+
+void Companion::PrepareCache(const std::string& path) {
+ std::ifstream yaml(path);
+ bool hasChanges = false;
+ const std::vector<uint8_t> data = std::vector<uint8_t>(std::istreambuf_iterator( yaml ), {});
+ this->gCurrentCacheHash = CalculateHash(data);
- if(root[this->gCurrentCacheHash]) {
- const auto cache = GetSafeNode<std::string>(root, this->gCurrentCacheHash);
+ if(this->gCacheNode[path]) {
+ const auto cache = GetSafeNode<std::string>(this->gCacheNode, path);
if(cache == this->gCurrentCacheHash) {
- std::ifstream file("cache/" + cache, std::ios::binary);
- this->gCacheData[cache] = std::vector<uint8_t>(std::istreambuf_iterator( file ), {});
- file.close();
+ const auto tdir = "tcache/" + cache;
+ if(!fs::exists(tdir)) {
+ fs::create_directories(tdir);
+ } else {
+ for(auto& entry : fs::directory_iterator("tcache/" + cache)) {
+ std::ifstream file(entry.path(), std::ios::binary);
+ std::vector<uint8_t> buffer = std::vector<uint8_t>(std::istreambuf_iterator(file), {});
+ file.close();
+ std::string cpath = entry.path().filename().string();
+ std::replace(cpath.begin(), cpath.end(), '+', '/');
+ this->gCacheData[cache][cpath] = buffer;
+ }
+ }
} else {
- root[path] = this->gCurrentCacheHash;
+ hasChanges = true;
+ this->gCacheNode[path] = this->gCurrentCacheHash;
fs::remove("cache/" + cache);
}
} else {
- root[path] = this->gCurrentCacheHash;
+ this->gCacheNode[path] = this->gCurrentCacheHash;
}
- std::ofstream file(out, std::ios::binary);
- file << root;
- file.close();
+ if(hasChanges) {
+ std::ofstream file("torch.cache.yml", std::ios::binary);
+ file << this->gCacheNode;
+ file.close();
+ }
}
-void Companion::StoreCache(const std::vector<uint8_t>& value) {
- std::ofstream file("cache/" + this->gCurrentCacheHash, std::ios::binary);
+void Companion::StoreCache(const std::string path, const std::vector<uint8_t>& value) {
+ std::string outpath = path;
+ std::replace(outpath.begin(), outpath.end(), '/', '+');
+ std::ofstream file("tcache/" + this->gCurrentCacheHash + "/" + outpath, std::ios::binary);
file.write(reinterpret_cast<const char*>(value.data()), value.size());
file.close();
}
+std::vector<uint8_t>& Companion::GetCache(const std::string path) {
+ std::string outpath = path;
+ std::replace(outpath.begin(), outpath.end(), '+', '/');
+ return this->gCacheData[this->gCurrentCacheHash][outpath];
+}
+
void Companion::Process() {
if(!fs::exists("config.yml")) {
@@ -587,6 +610,8 @@ void Companion::Process() {
}
}
+ this->ParseCache();
+
SPDLOG_INFO("------------------------------------------------");
spdlog::set_pattern(line);