summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKiritoDv <kiritodev01@gmail.com>2024-03-29 03:02:56 -0600
committerLywx <kiritodev01@gmail.com>2024-03-29 03:03:55 -0600
commit216a56a9b7b07dcfd26a15a843e92d39ce5319f6 (patch)
treea45f0cd9103c839386ce1c1f6d55802bd4c781d2
parent1f2627a9a691e7cbbd4677d08774540214e63982 (diff)
Fixed parse from cache
-rw-r--r--src/Companion.cpp73
-rw-r--r--src/Companion.h7
-rw-r--r--src/factories/BaseFactory.h3
-rw-r--r--src/factories/DisplayListFactory.h21
4 files changed, 76 insertions, 28 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);
diff --git a/src/Companion.h b/src/Companion.h
index 8fcde4a..07d9f20 100644
--- a/src/Companion.h
+++ b/src/Companion.h
@@ -129,6 +129,7 @@ private:
std::vector<uint8_t> gRomData;
std::filesystem::path gRomPath;
bool gNodeIsCacheable;
+ YAML::Node gCacheNode;
std::shared_ptr<N64::Cartridge> gCartridge;
std::unordered_map<std::string, std::unordered_map<int32_t, std::string>> gEnums;
@@ -145,17 +146,19 @@ private:
std::variant<std::vector<std::string>, std::string> gWriteOrder;
std::unordered_map<std::string, std::shared_ptr<BaseFactory>> gFactories;
std::unordered_map<std::string, std::string> gModdedAssetPaths;
- std::unordered_map<std::string, std::vector<uint8_t>> gCacheData;
+ std::unordered_map<std::string, std::unordered_map<std::string, std::vector<uint8_t>>> gCacheData;
std::unordered_map<std::string, std::map<std::string, std::vector<WriteEntry>>> gWriteMap;
std::unordered_map<std::string, std::map<std::string, std::pair<YAML::Node, bool>>> gAssetDependencies;
std::unordered_map<std::string, std::unordered_map<uint32_t, std::tuple<std::string, YAML::Node>>> gAddrMap;
void ParseEnums(std::string& file);
+ void ParseCache();
void ParseModdingConfig();
void ParseCurrentFileConfig(YAML::Node node);
void RegisterFactory(const std::string& type, const std::shared_ptr<BaseFactory>& factory);
- void StoreCache(const std::vector<uint8_t>& value);
+ void StoreCache(std::string path, const std::vector<uint8_t>& value);
+ std::vector<uint8_t>& GetCache(std::string path);
void ExtractNode(YAML::Node& node, std::string& name, SWrapper* binary);
};
diff --git a/src/factories/BaseFactory.h b/src/factories/BaseFactory.h
index 5dc8b81..5ea4a8d 100644
--- a/src/factories/BaseFactory.h
+++ b/src/factories/BaseFactory.h
@@ -116,6 +116,9 @@ public:
virtual bool IsCacheable() {
return false;
}
+ virtual std::optional<std::shared_ptr<IParsedData>> CreateDataPointer() {
+ return std::nullopt;
+ }
private:
virtual std::unordered_map<ExportType, std::shared_ptr<BaseExporter>> GetExporters() = 0;
}; \ No newline at end of file
diff --git a/src/factories/DisplayListFactory.h b/src/factories/DisplayListFactory.h
index 71a7e42..fb69e01 100644
--- a/src/factories/DisplayListFactory.h
+++ b/src/factories/DisplayListFactory.h
@@ -8,7 +8,18 @@ class DListData : public IParsedData {
public:
std::vector<uint32_t> mGfxs;
- explicit DListData(std::vector<uint32_t> gfxs) : mGfxs(gfxs) {}
+ DListData() {}
+ DListData(std::vector<uint32_t> gfxs) : mGfxs(gfxs) {}
+
+ std::optional<std::vector<uint8_t>> ToCacheBuffer() override {
+ std::vector<uint8_t> buffer;
+ buffer.insert(buffer.end(), reinterpret_cast<uint8_t *>(mGfxs.data()), reinterpret_cast<uint8_t *>(mGfxs.data()) + mGfxs.size() * sizeof(uint32_t));
+ return buffer;
+ }
+
+ void FromCacheBuffer(std::vector<uint8_t>& buffer) override {
+ mGfxs = std::vector(reinterpret_cast<uint32_t *>(buffer.data()), reinterpret_cast<uint32_t *>(buffer.data()) + buffer.size() / sizeof(uint32_t));
+ }
};
class DListHeaderExporter : public BaseExporter {
@@ -35,5 +46,11 @@ public:
}
uint32_t GetAlignment() override {
return 8;
- };
+ }
+ bool IsCacheable() override {
+ return true;
+ }
+ virtual std::optional<std::shared_ptr<IParsedData>> CreateDataPointer() {
+ return std::make_shared<DListData>();
+ }
};