From e9172e58f495972e45160e0bd7e550f605823ce5 Mon Sep 17 00:00:00 2001 From: fangyunzhong Date: Sat, 29 Jun 2024 11:11:53 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BD=AC=E7=A0=81=E6=94=AF=E6=8C=81=E5=A2=9E?= =?UTF-8?q?=E9=87=8F=E5=92=8C=E8=BD=AC=E7=A0=81=E4=BF=A1=E6=81=AFdebug?= =?UTF-8?q?=E6=97=B6=E6=89=93=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fangyunzhong --- include/compression_parser.h | 3 +++ include/resource_data.h | 1 + src/compression_parser.cpp | 50 ++++++++++++++++++++++++++++++++---- src/resource_pack.cpp | 1 + src/restool.cpp | 2 +- 5 files changed, 51 insertions(+), 6 deletions(-) diff --git a/include/compression_parser.h b/include/compression_parser.h index 0cf2c03..9c9704a 100644 --- a/include/compression_parser.h +++ b/include/compression_parser.h @@ -67,6 +67,7 @@ public: bool GetMediaSwitch(); std::string PrintTransMessage(); bool GetDefaultCompress(); + void SetOutPath(const std::string &path); private: bool ParseContext(const cJSON *contextNode); bool ParseCompression(const cJSON *compressionNode); @@ -88,6 +89,7 @@ private: std::string GetOptionsString(const std::shared_ptr &compressFilter, int type); bool CheckAndTranscode(const std::string &src, std::string &dst, std::string &output, const std::shared_ptr &compressFilter, const bool extAppend); + bool CopyForTrans(const std::string &src, const std::string &originDst, const std::string &dst); bool IsDefaultCompress(); std::string filePath_; std::string extensionPath_; @@ -95,6 +97,7 @@ private: bool mediaSwitch_; cJSON *root_; bool defaultCompress_; + std::string outPath_; unsigned long long totalTime_ = 0; uint32_t totalCounts_ = 0; unsigned long long compressTime_ = 0; diff --git a/include/resource_data.h b/include/resource_data.h index 45ab1a9..855308e 100644 --- a/include/resource_data.h +++ b/include/resource_data.h @@ -27,6 +27,7 @@ namespace Global { namespace Restool { const static std::string TOOL_NAME = "restool"; const static std::string RESOURCES_DIR = "resources"; +const static std::string CACHES_DIR = ".caches"; const static std::string CONFIG_JSON = "config.json"; const static std::string MODULE_JSON = "module.json"; const static std::string RAW_FILE_DIR = "rawfile"; diff --git a/src/compression_parser.cpp b/src/compression_parser.cpp index 9ddb252..95dfbd2 100644 --- a/src/compression_parser.cpp +++ b/src/compression_parser.cpp @@ -46,12 +46,13 @@ const map ERRORCODEMAP = { }; CompressionParser::CompressionParser() - : filePath_(""), extensionPath_(""), mediaSwitch_(false), root_(nullptr), defaultCompress_(false) + : filePath_(""), extensionPath_(""), mediaSwitch_(false), root_(nullptr), defaultCompress_(false), outPath_("") { } CompressionParser::CompressionParser(const string &filePath) - : filePath_(filePath), extensionPath_(""), mediaSwitch_(false), root_(nullptr), defaultCompress_(false) + : filePath_(filePath), extensionPath_(""), mediaSwitch_(false), root_(nullptr), defaultCompress_(false), + outPath_("") { } @@ -114,6 +115,12 @@ uint32_t CompressionParser::Init() if (!LoadImageTranscoder()) { return RESTOOL_ERROR; } + string caches = outPath_; + caches.append(SEPARATOR_FILE).append(CACHES_DIR); + if (!ResourceUtil::CreateDirs(caches)) { + cerr << "Error: create caches dir failed. dir = " << caches << endl; + return RESTOOL_ERROR; + } return RESTOOL_SUCCESS; } @@ -233,6 +240,11 @@ bool CompressionParser::IsDefaultCompress() (compressFilter->rules.size() == 0) && (compressFilter->expandRules.size() == 0); } +void CompressionParser::SetOutPath(const std::string &path) +{ + outPath_ = path; +} + vector CompressionParser::ParseRules(const cJSON *rulesNode) { vector res; @@ -508,6 +520,27 @@ bool CompressionParser::CheckAndTranscode(const string &src, string &dst, string return false; } +bool CompressionParser::CopyForTrans(const string &src, const string &originDst, const string &dst) +{ + auto srcIndex = src.find_last_of("."); + auto dstIndex = dst.find_last_of("."); + if (srcIndex == string::npos || dstIndex == string::npos) { + cerr << "Error: invalid copy path." << endl; + return false; + } + string srcSuffix = src.substr(srcIndex + 1); + string dstSuffix = dst.substr(dstIndex + 1); + auto ret = false; + if (srcSuffix == dstSuffix) { + ret = ResourceUtil::CopyFileInner(src, dst); + } else { + uint32_t startIndex = outPath_.size() + CACHES_DIR.size() + 1; + string dstPath = outPath_ + SEPARATOR_FILE + RESOURCES_DIR + dst.substr(startIndex); + ret = ResourceUtil::CopyFileInner(dst, dstPath); + } + return ret; +} + bool CompressionParser::CopyAndTranscode(const string &src, string &dst, const bool extAppend) { auto t0 = std::chrono::steady_clock::now(); @@ -522,15 +555,22 @@ bool CompressionParser::CopyAndTranscode(const string &src, string &dst, const b cerr << "Error: invalid output path." << NEW_LINE_PATH << dst << endl; return false; } - string output = dst.substr(0, index); + uint32_t startIndex = outPath_.size() + RESOURCES_DIR.size() + 1; + string endStr = dst.substr(startIndex, index - startIndex); + string output = outPath_ + SEPARATOR_FILE + CACHES_DIR + endStr; + string originDst = dst; + if (!ResourceUtil::CreateDirs(output)) { + cerr << "Error: create output dir failed. dir = " << output << endl; + return false; + } for (const auto &compressFilter : compressFilters_) { if (!CheckAndTranscode(src, dst, output, compressFilter, extAppend)) { continue; } - return true; + break; } auto t2 = std::chrono::steady_clock::now(); - auto ret = ResourceUtil::CopyFileInner(src, dst); + auto ret = CopyForTrans(src, originDst, dst); CollectTime(totalCounts_, totalTime_, t2); return ret; } diff --git a/src/resource_pack.cpp b/src/resource_pack.cpp index 331927a..18db19a 100644 --- a/src/resource_pack.cpp +++ b/src/resource_pack.cpp @@ -48,6 +48,7 @@ uint32_t ResourcePack::InitCompression() { if (!packageParser_.GetCompressionPath().empty()) { auto compressionMgr = CompressionParser::GetCompressionParser(packageParser_.GetCompressionPath()); + compressionMgr->SetOutPath(packageParser_.GetOutput()); if (compressionMgr->Init() != RESTOOL_SUCCESS) { return RESTOOL_ERROR; } diff --git a/src/restool.cpp b/src/restool.cpp index 448427d..e26fdbb 100644 --- a/src/restool.cpp +++ b/src/restool.cpp @@ -45,7 +45,7 @@ int main(int argc, char *argv[]) } cout << "Info: restool resources compile success." << endl; if (CompressionParser::GetCompressionParser()->GetMediaSwitch()) { - cerr << CompressionParser::GetCompressionParser()->PrintTransMessage() << endl; + cout << CompressionParser::GetCompressionParser()->PrintTransMessage() << endl; } return RESTOOL_SUCCESS; }