From 43942ba6d9e52b53802861a34dc82422814cd702 Mon Sep 17 00:00:00 2001 From: fangyunzhong Date: Thu, 28 Mar 2024 22:48:36 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=8C=87=E5=AE=9A=E7=B3=BB?= =?UTF-8?q?=E7=BB=9Fid=5Fdefined.json=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fangyunzhong --- include/cmd_parser.h | 3 +++ include/id_defined_parser.h | 6 ++++-- include/resource_data.h | 3 ++- src/cmd_parser.cpp | 29 ++++++++++++++++++++++++++ src/id_defined_parser.cpp | 41 +++++++++++++++++++++++++------------ src/resconfig_parser.cpp | 2 ++ 6 files changed, 68 insertions(+), 16 deletions(-) diff --git a/include/cmd_parser.h b/include/cmd_parser.h index 3c45bfe..b6b458e 100644 --- a/include/cmd_parser.h +++ b/include/cmd_parser.h @@ -55,6 +55,7 @@ public: bool GetIconCheck() const; const TargetConfig &GetTargetConfigValues() const; bool IsTargetConfig() const; + const std::vector &GetSysIdDefinedPaths() const; private: void InitCommand(); @@ -78,6 +79,7 @@ private: uint32_t ShowHelp() const; uint32_t SetIdDefinedOutput(const std::string& argValue); uint32_t SetIdDefinedInputPath(const std::string& argValue); + uint32_t AddSysIdDefined(const std::string& argValue); bool IsAscii(const std::string& argValue) const; bool IsLongOpt(char *argv[]) const; uint32_t IconCheck(); @@ -105,6 +107,7 @@ private: bool isIconCheck_ = false; TargetConfig targetConfig_; bool isTtargetConfig_; + std::vector sysIdDefinedPaths_; }; template diff --git a/include/id_defined_parser.h b/include/id_defined_parser.h index 6c407f7..0fa40d4 100755 --- a/include/id_defined_parser.h +++ b/include/id_defined_parser.h @@ -40,13 +40,15 @@ private: uint32_t Init(const std::string &filePath, bool isSystem); using ParseFunction = std::function; void InitParser(); - uint32_t IdDefinedToResourceIds(const cJSON *record, bool isSystem, const int64_t strtSysId = 0); + uint32_t IdDefinedToResourceIds(const std::string &filePath, const cJSON *record, + bool isSystem, const int64_t strtSysId = 0); bool ParseType(const cJSON *type, ResourceId &resourceId); bool ParseName(const cJSON *name, ResourceId &resourceId); bool ParseOrder(const cJSON *order, ResourceId &resourceId); bool ParseId(const cJSON *id, ResourceId &resourceId); - bool PushResourceId(const ResourceId &resourceId, bool isSystem); + bool PushResourceId(const std::string &filePath, const ResourceId &resourceId, bool isSystem); int64_t GetStartId() const; + std::map>> checkDefinedIds_; std::map, ResourceId> sysDefinedIds_; std::map, ResourceId> appDefinedIds_; std::map idDefineds_; diff --git a/include/resource_data.h b/include/resource_data.h index 3a3cbc3..14ec2d3 100644 --- a/include/resource_data.h +++ b/include/resource_data.h @@ -39,7 +39,7 @@ const static std::string NEW_LINE_PATH = "\nat "; const static std::string LONG_PATH_HEAD = "\\\\?\\"; const static int32_t VERSION_MAX_LEN = 128; const static int32_t INT_TO_BYTES = sizeof(uint32_t); -static const int8_t RESTOOL_VERSION[VERSION_MAX_LEN] = { "Restool 5.006" }; +static const int8_t RESTOOL_VERSION[VERSION_MAX_LEN] = { "Restool 5.007" }; const static int32_t TAG_LEN = 4; enum class KeyType { @@ -126,6 +126,7 @@ enum Option { DEPENDENTRY = 3, ICON_CHECK = 4, TARGET_CONFIG = 5, + DEFINED_SYSIDS = 6, STARTID = 'e', FORCEWRITE = 'f', HELP = 'h', diff --git a/src/cmd_parser.cpp b/src/cmd_parser.cpp index 60297e2..44294d2 100644 --- a/src/cmd_parser.cpp +++ b/src/cmd_parser.cpp @@ -42,6 +42,7 @@ const struct option PackageParser::CMD_OPTS[] = { { "defined-ids", required_argument, nullptr, Option::DEFINED_IDS}, { "icon-check", no_argument, nullptr, Option::ICON_CHECK}, { "target-config", required_argument, nullptr, Option::TARGET_CONFIG}, + { "defined-sysids", required_argument, nullptr, Option::DEFINED_SYSIDS}, { 0, 0, 0, 0}, }; @@ -110,6 +111,11 @@ const string &PackageParser::GetDependEntry() const return dependEntry_; } +const vector &PackageParser::GetSysIdDefinedPaths() const +{ + return sysIdDefinedPaths_; +} + uint32_t PackageParser::AddInput(const string& argValue) { string inputPath = ResourceUtil::RealPath(argValue); @@ -131,6 +137,28 @@ uint32_t PackageParser::AddInput(const string& argValue) return RESTOOL_SUCCESS; } +uint32_t PackageParser::AddSysIdDefined(const std::string& argValue) +{ + string sysIdDefinedPath = ResourceUtil::RealPath(argValue); + if (sysIdDefinedPath.empty()) { + cerr << "Error: invalid system id_defined.json path: '" << argValue << "'" << endl; + return RESTOOL_ERROR; + } + + auto ret = find_if(sysIdDefinedPaths_.begin(), sysIdDefinedPaths_.end(), + [sysIdDefinedPath](auto iter) {return sysIdDefinedPath == iter;}); + if (ret != sysIdDefinedPaths_.end()) { + cerr << "Error: repeat system id_defined.json path: '" << argValue << "'" << endl; + return RESTOOL_ERROR; + } + + if (!IsAscii(sysIdDefinedPath)) { + return RESTOOL_ERROR; + } + sysIdDefinedPaths_.push_back(sysIdDefinedPath); + return RESTOOL_SUCCESS; +} + uint32_t PackageParser::AddPackageName(const string& argValue) { if (!packageName_.empty()) { @@ -419,6 +447,7 @@ void PackageParser::InitCommand() handles_.emplace(Option::DEFINED_IDS, bind(&PackageParser::SetIdDefinedInputPath, this, _1)); handles_.emplace(Option::ICON_CHECK, [this](const string &) -> uint32_t { return IconCheck(); }); handles_.emplace(Option::TARGET_CONFIG, bind(&PackageParser::ParseTargetConfig, this, _1)); + handles_.emplace(Option::DEFINED_SYSIDS, bind(&PackageParser::AddSysIdDefined, this, _1)); } uint32_t PackageParser::HandleProcess(int c, const string& argValue) diff --git a/src/id_defined_parser.cpp b/src/id_defined_parser.cpp index 8e67955..b3db750 100755 --- a/src/id_defined_parser.cpp +++ b/src/id_defined_parser.cpp @@ -56,6 +56,7 @@ uint32_t IdDefinedParser::Init() return RESTOOL_ERROR; } } + //SystemResource.hap only defined by base/element/id_defined.json if (isSys) { return RESTOOL_SUCCESS; } @@ -66,6 +67,11 @@ uint32_t IdDefinedParser::Init() return RESTOOL_ERROR; } } + for (const auto &sysIdDefinedPath : packageParser_.GetSysIdDefinedPaths()) { + if (Init(sysIdDefinedPath, true) != RESTOOL_SUCCESS) { + return RESTOOL_ERROR; + } + } string sysIdDefinedPath = FileEntry::FilePath(packageParser_.GetRestoolPath()) .GetParent().Append(ID_DEFINED_FILE).GetPath(); if (Init(sysIdDefinedPath, true) != RESTOOL_SUCCESS) { @@ -109,7 +115,7 @@ uint32_t IdDefinedParser::Init(const string &filePath, bool isSystem) } } - if (IdDefinedToResourceIds(recordNode, isSystem, startSysId) != RESTOOL_SUCCESS) { + if (IdDefinedToResourceIds(filePath, recordNode, isSystem, startSysId) != RESTOOL_SUCCESS) { return RESTOOL_ERROR; } return RESTOOL_SUCCESS; @@ -124,7 +130,8 @@ void IdDefinedParser::InitParser() handles_.emplace("order", bind(&IdDefinedParser::ParseOrder, this, _1, _2)); } -uint32_t IdDefinedParser::IdDefinedToResourceIds(const cJSON *record, bool isSystem, const int64_t startSysId) +uint32_t IdDefinedParser::IdDefinedToResourceIds(const std::string &filePath, const cJSON *record, + bool isSystem, const int64_t startSysId) { int64_t index = -1; for (cJSON *item = record->child; item; item = item->next) { @@ -143,14 +150,14 @@ uint32_t IdDefinedParser::IdDefinedToResourceIds(const cJSON *record, bool isSys return RESTOOL_ERROR; } } - if (!PushResourceId(resourceId, isSystem)) { + if (!PushResourceId(filePath, resourceId, isSystem)) { return RESTOOL_ERROR; } } return RESTOOL_SUCCESS; } -bool IdDefinedParser::PushResourceId(const ResourceId &resourceId, bool isSystem) +bool IdDefinedParser::PushResourceId(const std::string &filePath, const ResourceId &resourceId, bool isSystem) { ResType resType = ResourceUtil::GetResTypeFromString(resourceId.type); auto ret = idDefineds_.emplace(resourceId.id, resourceId); @@ -158,18 +165,26 @@ bool IdDefinedParser::PushResourceId(const ResourceId &resourceId, bool isSystem cerr << "Error: '" << ret.first->second.name << "' and '" << resourceId.name << "' defind the same ID." << endl; return false; } - if (isSystem) { - auto ret1 = sysDefinedIds_.emplace(make_pair(resType, resourceId.name), resourceId); - if (!ret1.second) { - cerr << "Error: the same type of '" << resourceId.name << "' exists in the id_defined.json. " << endl; + auto checkRet = checkDefinedIds_.find(filePath); + if (checkRet != checkDefinedIds_.end()) { + bool found = any_of(checkRet->second.begin(), checkRet->second.end(), + [resType, resourceId](const auto &iterItem) { + return (resType == iterItem.first) && (resourceId.name == iterItem.second); + }); + if (found) { + cerr << "Error: the same resource of '" << resourceId.name << "' exists in the " << filePath << endl; return false; } + checkRet->second.push_back(make_pair(resType, resourceId.name)); } else { - auto ret2 = appDefinedIds_.emplace(make_pair(resType, resourceId.name), resourceId); - if (!ret2.second) { - cerr << "Error: the same type of '" << resourceId.name << "' exists in the id_defined.json. " << endl; - return false; - } + std::vector> vects; + vects.push_back(make_pair(resType, resourceId.name)); + checkDefinedIds_.emplace(filePath, vects); + } + if (isSystem) { + sysDefinedIds_.emplace(make_pair(resType, resourceId.name), resourceId); + } else { + appDefinedIds_.emplace(make_pair(resType, resourceId.name), resourceId); } return true; } diff --git a/src/resconfig_parser.cpp b/src/resconfig_parser.cpp index 31c52e8..815b7ec 100644 --- a/src/resconfig_parser.cpp +++ b/src/resconfig_parser.cpp @@ -88,6 +88,8 @@ void ResConfigParser::InitFileListCommand(HandleBack callback) Option::MODULES, callback)); fileListHandles_.emplace("iconCheck", bind(&ResConfigParser::GetBool, this, _1, Option::ICON_CHECK, callback)); + fileListHandles_.emplace("definedSysIds", bind(&ResConfigParser::GetString, this, _1, + Option::DEFINED_SYSIDS, callback)); } uint32_t ResConfigParser::GetString(const cJSON *node, int c, HandleBack callback)