Fixed parse from cache

This commit is contained in:
KiritoDv
2024-03-29 03:03:55 -06:00
committed by Lywx
parent 1f2627a9a6
commit 216a56a9b7
4 changed files with 78 additions and 30 deletions
+51 -26
View File
@@ -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,43 +408,66 @@ void Companion::ParseCurrentFileConfig(YAML::Node node) {
this->gNodeIsCacheable = GetSafeNode<bool>(node, "cacheable", true);
}
void Companion::ParseCache() {
const std::string out = "torch.cache.yml";
if(fs::exists(out)) {
this->gCacheNode = YAML::LoadFile(out);
} else {
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);
const std::string out = "torch.cache.yml";
YAML::Node root;
if(fs::exists(out)) {
root = YAML::LoadFile(out);
} else {
root = YAML::Node();
}
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;
if(hasChanges) {
std::ofstream file("torch.cache.yml", std::ios::binary);
file << this->gCacheNode;
file.close();
}
}
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();
}
void Companion::StoreCache(const std::vector<uint8_t>& value) {
std::ofstream file("cache/" + this->gCurrentCacheHash, 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() {
@@ -587,6 +610,8 @@ void Companion::Process() {
}
}
this->ParseCache();
SPDLOG_INFO("------------------------------------------------");
spdlog::set_pattern(line);
+5 -2
View File
@@ -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);
};
+3
View File
@@ -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;
};
+19 -2
View File
@@ -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>();
}
};