diff options
| author | KiritoDv <kiritodev01@gmail.com> | 2024-03-29 03:02:56 -0600 |
|---|---|---|
| committer | Lywx <kiritodev01@gmail.com> | 2024-03-29 03:03:55 -0600 |
| commit | 216a56a9b7b07dcfd26a15a843e92d39ce5319f6 (patch) | |
| tree | a45f0cd9103c839386ce1c1f6d55802bd4c781d2 /src/Companion.cpp | |
| parent | 1f2627a9a691e7cbbd4677d08774540214e63982 (diff) | |
Fixed parse from cache
Diffstat (limited to 'src/Companion.cpp')
| -rw-r--r-- | src/Companion.cpp | 73 |
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); |
