From 70490be1b0b766d9540afadff2bf61ecb0718358 Mon Sep 17 00:00:00 2001 From: zt147369 Date: Wed, 20 Dec 2023 17:36:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0resfile=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zt147369 --- include/resource_append.h | 4 ++-- include/resource_data.h | 8 ++++++++ include/resource_pack.h | 5 +++-- src/cmd_parser.cpp | 2 +- src/json_compiler.cpp | 2 +- src/resource_append.cpp | 27 ++++++++++++++----------- src/resource_directory.cpp | 2 +- src/resource_pack.cpp | 40 +++++++++++++++++++++++++------------- 8 files changed, 58 insertions(+), 32 deletions(-) diff --git a/include/resource_append.h b/include/resource_append.h index 5549941..840f4e2 100644 --- a/include/resource_append.h +++ b/include/resource_append.h @@ -52,8 +52,8 @@ private: bool WriteFileInner(std::ostringstream &outStream, const std::string &outputPath) const; bool WriteResourceItem(const ResourceItem &resourceItem, std::ostringstream &out); bool LoadResourceItem(const std::string &filePath); - bool ScanRawFiles(const std::string &path, const std::string &outputPath); - bool WriteRawFile(const std::string &filePath, const std::string &outputPath); + bool ScanRawFilesOrResFiles(const std::string &path, const std::string &outputPath, const std::string &limit); + bool WriteRawFilesOrResFiles(const std::string &filePath, const std::string &outputPath, const std::string &limit); bool Push(const std::shared_ptr &resourceItem); void AddRef(const std::shared_ptr &resourceItem); bool LoadResourceItemFromMem(const char buffer[], int32_t length); diff --git a/include/resource_data.h b/include/resource_data.h index 63f9df6..1f58628 100644 --- a/include/resource_data.h +++ b/include/resource_data.h @@ -29,6 +29,7 @@ const static std::string RESOURCES_DIR = "resources"; const static std::string CONFIG_JSON = "config.json"; const static std::string MODULE_JSON = "module.json"; const static std::string RAW_FILE_DIR = "rawfile"; +const static std::string RES_FILE_DIR = "resfile"; const static std::string ID_DEFINED_FILE = "id_defined.json"; const static std::string RESOURCE_INDEX_FILE = "resources.index"; const static std::string JSON_EXTENSION = ".json"; @@ -75,6 +76,7 @@ enum class ResType { PROF = 20, PATTERN = 22, SYMBOL = 23, + RES = 24, INVALID_RES_TYPE = -1, }; @@ -182,6 +184,11 @@ struct IdData { uint32_t dataOffset; }; +const std::map g_copyFileMap = { + { RAW_FILE_DIR, ResType::RAW }, + { RES_FILE_DIR, ResType::RES }, +}; + const std::map g_fileClusterMap = { { "element", ResType::ELEMENT }, { "media", ResType::MEDIA }, @@ -220,6 +227,7 @@ const std::map g_resTypeMap = { { static_cast(ResType::PROF), ResType::PROF}, { static_cast(ResType::PATTERN), ResType::PATTERN}, { static_cast(ResType::SYMBOL), ResType::SYMBOL}, + { static_cast(ResType::RES), ResType::RES}, { static_cast(ResType::INVALID_RES_TYPE), ResType::INVALID_RES_TYPE}, }; diff --git a/include/resource_pack.h b/include/resource_pack.h index eb10054..a291dbe 100644 --- a/include/resource_pack.h +++ b/include/resource_pack.h @@ -42,8 +42,9 @@ private: uint32_t GenerateTextHeader(const std::string &headerPath) const; uint32_t GenerateCplusHeader(const std::string &headerPath) const; uint32_t GenerateJsHeader(const std::string &headerPath) const; - uint32_t CopyRawFile(const std::vector &inputs) const; - uint32_t CopyRawFileImpl(const std::string &src, const std::string &dst) const; + uint32_t CopyRawFileOrResFile(const std::string &filePath, const std::string &fileType) const; + uint32_t CopyRawFileOrResFile(const std::vector &inputs) const; + uint32_t CopyRawFileOrResFileImpl(const std::string &src, const std::string &dst) const; uint32_t GenerateConfigJson(); uint32_t ScanResources(const std::vector &inputs, const std::string &output); uint32_t PackNormal(); diff --git a/src/cmd_parser.cpp b/src/cmd_parser.cpp index 9fe4d6a..ad1d9bb 100644 --- a/src/cmd_parser.cpp +++ b/src/cmd_parser.cpp @@ -280,7 +280,7 @@ uint32_t PackageParser::AddAppend(const string& argValue) { string appendPath = ResourceUtil::RealPath(argValue); if (appendPath.empty()) { - cout << "Warning: invaild compress '" << argValue << "'" << endl; + cout << "Warning: invalid compress '" << argValue << "'" << endl; appendPath = argValue; } auto ret = find_if(append_.begin(), append_.end(), [appendPath](auto iter) {return appendPath == iter;}); diff --git a/src/json_compiler.cpp b/src/json_compiler.cpp index 24691c7..7d3f876 100644 --- a/src/json_compiler.cpp +++ b/src/json_compiler.cpp @@ -311,7 +311,7 @@ bool JsonCompiler::CheckJsonStringValue(const Json::Value &valueNode, const Reso string value = valueNode.asString(); ResType type = resourceItem.GetResType(); if (type == ResType::COLOR && !CheckColorValue(value.c_str())) { - string error = "invaild color value '" + value + \ + string error = "invalid color value '" + value + \ "', only support refer '$color:xxx' or '#rgb','#argb','#rrggbb','#aarrggbb'."; cerr << "Error: " << error << NEW_LINE_PATH << resourceItem.GetFilePath() << endl; return false; diff --git a/src/resource_append.cpp b/src/resource_append.cpp index f3765d2..d18297f 100644 --- a/src/resource_append.cpp +++ b/src/resource_append.cpp @@ -163,8 +163,8 @@ bool ResourceAppend::ScanSubLimitkeyResources(const FileEntry entry, const strin continue; } - if (limitKey == RAW_FILE_DIR) { - if (!ScanRawFiles(child->GetFilePath().GetPath(), outputPath)) { + if (limitKey == RAW_FILE_DIR || limitKey == RES_FILE_DIR) { + if (!ScanRawFilesOrResFiles(child->GetFilePath().GetPath(), outputPath, limitKey)) { return false; } continue; @@ -292,7 +292,11 @@ bool ResourceAppend::ScanFile(const FileInfo &fileInfo, const string &outputPath bool ResourceAppend::ScanSingleFile(const string &filePath, const string &outputPath) { if (filePath.find(RAW_FILE_DIR) != string::npos) { - return WriteRawFile(filePath, outputPath); + return WriteRawFilesOrResFiles(filePath, outputPath, RAW_FILE_DIR); + } + + if (filePath.find(RES_FILE_DIR) != string::npos) { + return WriteRawFilesOrResFiles(filePath, outputPath, RES_FILE_DIR); } FileEntry::FilePath path(filePath); @@ -400,7 +404,7 @@ bool ResourceAppend::LoadResourceItem(const string &filePath) #endif } -bool ResourceAppend::ScanRawFiles(const string &path, const string &outputPath) +bool ResourceAppend::ScanRawFilesOrResFiles(const string &path, const string &outputPath, const string &limit) { FileEntry entry(path); if (!entry.Init()) { @@ -415,9 +419,9 @@ bool ResourceAppend::ScanRawFiles(const string &path, const string &outputPath) bool ret = false; if (child->IsFile()) { - ret = WriteRawFile(child->GetFilePath().GetPath(), outputPath); + ret = WriteRawFilesOrResFiles(child->GetFilePath().GetPath(), outputPath, limit); } else { - ret = ScanRawFiles(child->GetFilePath().GetPath(), outputPath); + ret = ScanRawFilesOrResFiles(child->GetFilePath().GetPath(), outputPath, limit); } if (!ret) { @@ -427,18 +431,19 @@ bool ResourceAppend::ScanRawFiles(const string &path, const string &outputPath) return true; } -bool ResourceAppend::WriteRawFile(const string &filePath, const string &outputPath) +bool ResourceAppend::WriteRawFilesOrResFiles(const string &filePath, const string &outputPath, const string &limit) { - string::size_type pos = filePath.find(RAW_FILE_DIR); + string::size_type pos = filePath.find(limit); if (pos == string::npos) { - cerr << "Error: invaild raw file." << NEW_LINE_PATH << filePath << endl; + cerr << "Error: invalid file path." << NEW_LINE_PATH << filePath << endl; return false; } string sub = filePath.substr(pos); sub = FileEntry::FilePath(RESOURCES_DIR).Append(sub).GetPath(); vector keyParams; - ResourceItem resourceItem("", keyParams, ResType::RAW); + auto iter = g_copyFileMap.find(limit); + ResourceItem resourceItem("", keyParams, iter->second); resourceItem.SetData(sub); resourceItem.SetFilePath(filePath); resourceItem.SetLimitKey(""); @@ -538,7 +543,7 @@ bool ResourceAppend::LoadResourceItemFromMem(const char buffer[], int32_t length } // data string data = ParseString(buffer, length, offset); - if (resType == ResType::RAW) { + if (resType == ResType::RAW || resType == ResType::RES) { FileEntry::FilePath outPath(packageParser_.GetOutput()); if (ResourceUtil::FileExist(outPath.Append(data).GetPath())) { continue; diff --git a/src/resource_directory.cpp b/src/resource_directory.cpp index ff99414..979866d 100644 --- a/src/resource_directory.cpp +++ b/src/resource_directory.cpp @@ -41,7 +41,7 @@ bool ResourceDirectory::ScanResources(const string &resourcesDir, function &inputs) const +uint32_t ResourcePack::CopyRawFileOrResFile(const string &filePath, const string &fileType) const +{ + if (!ResourceUtil::FileExist(filePath)) { + return RESTOOL_SUCCESS; + } + + if (!FileEntry::IsDirectory(filePath)) { + cerr << "Error: '" << filePath << "' not directory." << endl; + return RESTOOL_ERROR; + } + + string dst = FileEntry::FilePath(packageParser_.GetOutput()) + .Append(RESOURCES_DIR).Append(fileType).GetPath(); + if (CopyRawFileOrResFileImpl(filePath, dst) != RESTOOL_SUCCESS) { + return RESTOOL_ERROR; + } + return RESTOOL_SUCCESS; +} + +uint32_t ResourcePack::CopyRawFileOrResFile(const vector &inputs) const { for (const auto &input : inputs) { string rawfilePath = FileEntry::FilePath(input).Append(RAW_FILE_DIR).GetPath(); - if (!ResourceUtil::FileExist(rawfilePath)) { - continue; - } - - if (!FileEntry::IsDirectory(rawfilePath)) { - cerr << "Error: '" << rawfilePath << "' not directory." << endl; + if (CopyRawFileOrResFile(rawfilePath, RAW_FILE_DIR) == RESTOOL_ERROR) { return RESTOOL_ERROR; } - - string dst = FileEntry::FilePath(packageParser_.GetOutput()) - .Append(RESOURCES_DIR).Append(RAW_FILE_DIR).GetPath(); - if (CopyRawFileImpl(rawfilePath, dst) != RESTOOL_SUCCESS) { + string resfilePath = FileEntry::FilePath(input).Append(RES_FILE_DIR).GetPath(); + if (CopyRawFileOrResFile(resfilePath, RES_FILE_DIR) == RESTOOL_ERROR) { return RESTOOL_ERROR; } } return RESTOOL_SUCCESS; } -uint32_t ResourcePack::CopyRawFileImpl(const string &src, const string &dst) const +uint32_t ResourcePack::CopyRawFileOrResFileImpl(const string &src, const string &dst) const { if (!ResourceUtil::CreateDirs(dst)) { return RESTOOL_ERROR; @@ -278,7 +290,7 @@ uint32_t ResourcePack::CopyRawFileImpl(const string &src, const string &dst) con string subPath = FileEntry::FilePath(dst).Append(filename).GetPath(); if (!entry->IsFile()) { - if (CopyRawFileImpl(entry->GetFilePath().GetPath(), subPath) != RESTOOL_SUCCESS) { + if (CopyRawFileOrResFileImpl(entry->GetFilePath().GetPath(), subPath) != RESTOOL_SUCCESS) { return RESTOOL_ERROR; } } else { @@ -338,7 +350,7 @@ uint32_t ResourcePack::PackNormal() return RESTOOL_ERROR; } - if (CopyRawFile(resourceMerge.GetInputs()) != RESTOOL_SUCCESS) { + if (CopyRawFileOrResFile(resourceMerge.GetInputs()) != RESTOOL_SUCCESS) { return RESTOOL_ERROR; }