From 207e2259e723a1efdbf830b7c341abe2df5f912c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Fri, 10 Mar 2023 15:39:45 +0100 Subject: [PATCH] Cleanup texture saving a bit --- GPU/Common/TextureReplacer.cpp | 54 ++++++++++++++++++---------------- GPU/Common/TextureReplacer.h | 2 +- 2 files changed, 30 insertions(+), 26 deletions(-) diff --git a/GPU/Common/TextureReplacer.cpp b/GPU/Common/TextureReplacer.cpp index 3fd199ffc6..430b45f21c 100644 --- a/GPU/Common/TextureReplacer.cpp +++ b/GPU/Common/TextureReplacer.cpp @@ -671,8 +671,11 @@ public: int h = 0; int pitch = 0; // bytes - Path basePath; - std::string hashfile; + Path filename; + Path saveFilename; + bool createSaveDirectory; + Path saveDirectory; + u32 replacedInfoHash = 0; bool skipIfExists = false; @@ -687,9 +690,6 @@ public: } void Run() override { - const Path filename = basePath / hashfile; - const Path saveFilename = basePath / NEW_TEXTURE_DIR / hashfile; - // Should we skip writing if the newly saved data already exists? if (skipIfExists && File::Exists(saveFilename)) { return; @@ -699,19 +699,9 @@ public: if (File::Exists(filename)) return; - // Create subfolder as needed. -#ifdef _WIN32 - size_t slash = hashfile.find_last_of("/\\"); -#else - size_t slash = hashfile.find_last_of("/"); -#endif - if (slash != hashfile.npos) { - // Create any directory structure as needed. - const Path saveDirectory = basePath / NEW_TEXTURE_DIR / hashfile.substr(0, slash); - if (!File::Exists(saveDirectory)) { - File::CreateFullPath(saveDirectory); - File::CreateEmptyFile(saveDirectory / ".nomedia"); - } + if (createSaveDirectory && !File::Exists(saveDirectory)) { + File::CreateFullPath(saveDirectory); + File::CreateEmptyFile(saveDirectory / ".nomedia"); } png_image png{}; @@ -771,8 +761,6 @@ void TextureReplacer::NotifyTextureDecoded(const ReplacedTextureDecodeInfo &repl // Generate a new PNG filename, complete with level. hashfile = HashName(cachekey, replacedInfo.hash, level) + ".png"; - const Path filename = newTextureDir_ / hashfile; - ReplacementCacheKey replacementKey(cachekey, replacedInfo.hash); auto it = savedCache_.find(replacementKey); bool skipIfExists = false; @@ -808,11 +796,27 @@ void TextureReplacer::NotifyTextureDecoded(const ReplacedTextureDecodeInfo &repl pitch = w * 4; SaveTextureTask *task = new SaveTextureTask(std::move(saveBuf)); + + task->filename = basePath_ / hashfile; + task->saveFilename = newTextureDir_ / hashfile; + task->createSaveDirectory = false; + + // Create subfolder as needed. +#ifdef _WIN32 + size_t slash = hashfile.find_last_of("/\\"); +#else + size_t slash = hashfile.find_last_of("/"); +#endif + if (slash != hashfile.npos) { + // Does this ever happen? + // Create any directory structure as needed. + task->saveDirectory = newTextureDir_ / hashfile.substr(0, slash); + task->createSaveDirectory = true; + } + task->w = w; task->h = h; task->pitch = pitch; - task->basePath = basePath_; - task->hashfile = hashfile; task->replacedInfoHash = replacedInfo.hash; task->skipIfExists = skipIfExists; g_threadManager.EnqueueTask(task); // We don't care about waiting for the task. It'll be fine. @@ -923,16 +927,16 @@ bool TextureReplacer::FindFiltering(u64 cachekey, u32 hash, TextureFiltering *fo return false; } -std::string TextureReplacer::LookupHashFile(u64 cachekey, u32 hash, bool *foundReplacement, bool *ignored) { +std::string TextureReplacer::LookupHashFile(u64 cachekey, u32 hash, bool *foundAlias, bool *ignored) { ReplacementCacheKey key(cachekey, hash); auto alias = LookupWildcard(aliases_, key, cachekey, hash, ignoreAddress_); if (alias != aliases_.end()) { // Note: this will be blank if explicitly ignored. - *foundReplacement = true; + *foundAlias = true; *ignored = alias->second.empty(); return alias->second; } - *foundReplacement = false; + *foundAlias = false; *ignored = false; return ""; } diff --git a/GPU/Common/TextureReplacer.h b/GPU/Common/TextureReplacer.h index 08cac604e3..bb3c020a98 100644 --- a/GPU/Common/TextureReplacer.h +++ b/GPU/Common/TextureReplacer.h @@ -137,7 +137,7 @@ protected: void ParseReduceHashRange(const std::string& key, const std::string& value); bool LookupHashRange(u32 addr, int &w, int &h); float LookupReduceHashRange(int& w, int& h); - std::string LookupHashFile(u64 cachekey, u32 hash, bool *foundReplacement, bool *ignored); + std::string LookupHashFile(u64 cachekey, u32 hash, bool *foundAlias, bool *ignored); std::string HashName(u64 cachekey, u32 hash, int level); void PopulateReplacement(ReplacedTexture *result, u64 cachekey, u32 hash, int w, int h); bool PopulateLevel(ReplacedTextureLevel &level, bool ignoreError);