diff --git a/include/cmd_parser.h b/include/cmd_parser.h index ff6dcaf..7f650ff 100644 --- a/include/cmd_parser.h +++ b/include/cmd_parser.h @@ -53,6 +53,8 @@ public: const std::vector &GetAppend() const; bool GetCombine() const; const std::string &GetDependEntry() const; + const std::string &GetIdDefinedOutput() const; + const std::string &GetIdDefinedInputPath() const; private: void InitCommand(); @@ -76,6 +78,8 @@ private: uint32_t SetCombine(); uint32_t AddDependEntry(const std::string& argValue); uint32_t ShowHelp() const; + uint32_t SetIdDefinedOutput(const std::string& argValue); + uint32_t SetIdDefinedInputPath(const std::string& argValue); bool IsAscii(const std::string& argValue) const; static const struct option CMD_OPTS[]; @@ -98,6 +102,8 @@ private: std::vector append_; bool combine_ = false; std::string dependEntry_; + std::string idDefinedOutput_; + std::string idDefinedInputPath_; }; template @@ -130,6 +136,8 @@ void CmdParser::ShowUseage() std::cout << " -x resources folder path\n"; std::cout << " -z flag for incremental compilation\n"; std::cout << " -h Displays this help menu\n"; + std::cout << " --ids save id_defined.json direcory\n"; + std::cout << " --defined-ids input id_defined.json path\n"; } template diff --git a/include/id_worker.h b/include/id_worker.h index 0e90688..d304688 100644 --- a/include/id_worker.h +++ b/include/id_worker.h @@ -53,21 +53,26 @@ private: int32_t GenerateAppId(ResType resType, const std::string &name); int32_t GenerateSysId(ResType resType, const std::string &name); uint32_t InitIdDefined(); - uint32_t InitIdDefined(const std::string &filePath); + uint32_t InitIdDefined(const std::string &filePath, bool isSystem); + uint32_t IdDefinedToResourceIds(const Json::Value &record, bool isSystem, const int32_t strtSysId = 0); using ParseFunction = std::function; void InitParser(); bool ParseType(const Json::Value &type, ResourceId &resourceId); bool ParseName(const Json::Value &name, ResourceId &resourceId); bool ParseOrder(const Json::Value &order, ResourceId &resourceId); - bool PushResourceId(const ResourceId &resourceId); + bool ParseId(const Json::Value &id, ResourceId &resourceId); + bool PushResourceId(const ResourceId &resourceId, bool isSystem); bool IsValidSystemName(const std::string &name) const; int32_t GetStartId(const Json::Value &root) const; int32_t GetMaxId(int32_t startId) const; + int32_t GetCurId(); int32_t appId_; int32_t maxId_; ResourceIdCluster type_; std::map, int32_t> ids_; - std::map, ResourceId> definedIds_; + std::map, ResourceId> sysDefinedIds_; + std::map, ResourceId> appDefinedIds_; + std::map idDefineds_; std::map handles_; std::vector delIds_; std::map, int32_t> cacheIds_; diff --git a/include/resource_append.h b/include/resource_append.h index 453e9ce..b0220b0 100644 --- a/include/resource_append.h +++ b/include/resource_append.h @@ -57,6 +57,7 @@ private: int32_t ParseInt32(const char buffer[], int32_t length, int32_t &offset) const; bool ParseRef(); bool CheckModuleResourceItem(const std::shared_ptr &resourceItem, int32_t id); + bool IsBaseIdDefined(const FileInfo &fileInfo); #ifdef __WIN32 bool LoadResourceItemWin(const std::string &filePath); #endif diff --git a/include/resource_data.h b/include/resource_data.h index 6226c43..85a7d95 100644 --- a/include/resource_data.h +++ b/include/resource_data.h @@ -33,6 +33,7 @@ const static std::string RESOURCE_INDEX_FILE = "resources.index"; const static std::string SEPARATOR = "/"; const static std::string WIN_SEPARATOR = "\\"; const static std::string NEW_LINE_PATH = "\r\nat "; +const static std::string ID_DEFINED_INDENTATION = " "; 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 2.010" }; diff --git a/include/resource_table.h b/include/resource_table.h index 0ae979c..13bbdcd 100644 --- a/include/resource_table.h +++ b/include/resource_table.h @@ -62,6 +62,7 @@ private: int32_t id; }; uint32_t SaveToResouorceIndex(const std::map> &configs) const; + uint32_t CreateIdDefined(const std::map> &allResource) const; bool InitIndexHeader(IndexHeader &indexHeader, uint32_t count) const; bool Prepare(const std::map> &configs, std::map &limitKeyConfigs, @@ -83,6 +84,7 @@ private: const std::map> &datas, std::map> &resInfos) const; std::string indexFilePath_; + std::string idDefinedPath_; }; } } diff --git a/include/resource_util.h b/include/resource_util.h index 649e1d3..d121f99 100644 --- a/include/resource_util.h +++ b/include/resource_util.h @@ -172,6 +172,26 @@ public: * @return limitkey */ static std::string PaserKeyParam(const std::vector &keyParams); + + /** + * @brief Decimal to hexadecimal string + * @param int32_t Decimal + * @return Hexadecimal string + */ + static std::string DecToHexStr(const int32_t id); + + /** + * @brief Check hexadecimal string + * @param string Hexadecimal string + * @return ture Hexadecimal string is legal, other false; + */ + static bool CheckHexStr(const std::string &hex); + + /** + * @brief get g_contentClusterMap key string + * @return All restype string + */ + static std::string GetAllRestypeString(); private: enum class IgnoreType { IGNORE_FILE, diff --git a/src/cmd_parser.cpp b/src/cmd_parser.cpp index 774bb53..8e53378 100644 --- a/src/cmd_parser.cpp +++ b/src/cmd_parser.cpp @@ -40,9 +40,11 @@ const struct option PackageParser::CMD_OPTS[] = { { "combine", required_argument, nullptr, 'z' }, { "dependEntry", required_argument, nullptr, 'd' }, { "help", no_argument, nullptr, 'h'}, + { "ids", required_argument, nullptr, 's'}, + { "defined-ids", required_argument, nullptr, 't'}, }; -const string PackageParser::CMD_PARAMS = "i:p:o:r:m:j:e:c:l:g:x:d:afhvz"; +const string PackageParser::CMD_PARAMS = "i:p:o:r:m:j:e:c:l:g:x:d:s:t:afhvz"; uint32_t PackageParser::Parse(int argc, char *argv[]) { @@ -244,6 +246,12 @@ uint32_t PackageParser::CheckParam() const cerr << "Error: resource header path empty." << endl; return RESTOOL_ERROR; } + + if (startId_ != 0 && !idDefinedInputPath_.empty()) { + cerr << "Error: set -e and --defined-ids cannot be used together." << endl; + return RESTOOL_ERROR; + } + return RESTOOL_SUCCESS; } @@ -323,6 +331,28 @@ uint32_t PackageParser::ShowHelp() const return RESTOOL_SUCCESS; } +uint32_t PackageParser::SetIdDefinedOutput(const string& argValue) +{ + idDefinedOutput_ = argValue; + return RESTOOL_SUCCESS; +} + +const string &PackageParser::GetIdDefinedOutput() const +{ + return idDefinedOutput_; +} + +uint32_t PackageParser::SetIdDefinedInputPath(const string& argValue) +{ + idDefinedInputPath_ = argValue; + return RESTOOL_SUCCESS; +} + +const string &PackageParser::GetIdDefinedInputPath() const +{ + return idDefinedInputPath_; +} + bool PackageParser::IsAscii(const string& argValue) const { #ifdef __WIN32 @@ -359,6 +389,8 @@ void PackageParser::InitCommand() handles_.emplace('z', [this](const string &) -> uint32_t { return SetCombine(); }); handles_.emplace('d', bind(&PackageParser::AddDependEntry, this, _1)); handles_.emplace('h', [this](const string &) -> uint32_t { return ShowHelp(); }); + handles_.emplace('s', bind(&PackageParser::SetIdDefinedOutput, this, _1)); + handles_.emplace('t', bind(&PackageParser::SetIdDefinedInputPath, this, _1)); } uint32_t PackageParser::HandleProcess(int c, const string& argValue) diff --git a/src/id_worker.cpp b/src/id_worker.cpp index d03c8a1..741d076 100644 --- a/src/id_worker.cpp +++ b/src/id_worker.cpp @@ -75,8 +75,8 @@ int32_t IdWorker::GetId(ResType resType, const string &name) const int32_t IdWorker::GetSystemId(ResType resType, const string &name) const { - auto result = definedIds_.find(make_pair(resType, name)); - if (result == definedIds_.end()) { + auto result = sysDefinedIds_.find(make_pair(resType, name)); + if (result == sysDefinedIds_.end()) { return -1; } return result->second.id; @@ -128,6 +128,12 @@ int32_t IdWorker::GenerateAppId(ResType resType, const string &name) return result->second; } + auto defined = appDefinedIds_.find(make_pair(resType, name)); + if (defined != appDefinedIds_.end()) { + ids_.emplace(make_pair(resType, name), defined->second.id); + return defined->second.id; + } + result = cacheIds_.find(make_pair(resType, name)); if (result != cacheIds_.end()) { ids_.emplace(make_pair(resType, name), result->second); @@ -138,18 +144,39 @@ int32_t IdWorker::GenerateAppId(ResType resType, const string &name) cerr << "Error: id count exceed " << appId_ << ">" << maxId_ << endl; return -1; } - int32_t id = 0; + int32_t id = -1; if (!delIds_.empty()) { id = delIds_.front(); delIds_.erase(delIds_.begin()); } else { - id = appId_; - appId_++; + id = GetCurId(); + if (id < 0) { + return -1; + } } ids_.emplace(make_pair(resType, name), id); return id; } +int32_t IdWorker::GetCurId() +{ + if (appDefinedIds_.size() == 0) { + return appId_++; + } + while (appId_ <= maxId_) { + int32_t id = appId_; + auto ret = find_if(appDefinedIds_.begin(), appDefinedIds_.end(), [id](const auto &iter) { + return id == iter.second.id; + }); + if (ret == appDefinedIds_.end()) { + return appId_++; + } + appId_++; + } + cerr << "Error: id count exceed in id_defined." << appId_ << ">" << maxId_ << endl; + return -1; +} + int32_t IdWorker::GenerateSysId(ResType resType, const string &name) { auto result = ids_.find(make_pair(resType, name)); @@ -157,8 +184,8 @@ int32_t IdWorker::GenerateSysId(ResType resType, const string &name) return result->second; } - auto defined = definedIds_.find(make_pair(resType, name)); - if (defined != definedIds_.end()) { + auto defined = sysDefinedIds_.find(make_pair(resType, name)); + if (defined != sysDefinedIds_.end()) { ids_.emplace(make_pair(resType, name), defined->second.id); return defined->second.id; } @@ -170,26 +197,45 @@ uint32_t IdWorker::InitIdDefined() InitParser(); CmdParser &parser = CmdParser::GetInstance(); PackageParser &packageParser = parser.GetCmdParser(); - string idDefinedPath; - if (type_ == ResourceIdCluster::RES_ID_SYS) { - for (const auto &inputPath : packageParser.GetInputs()) { + string idDefinedInput = packageParser.GetIdDefinedInputPath(); + int32_t startId = packageParser.GetStartId(); + bool combine = packageParser.GetCombine(); + bool isSys = type_ == ResourceIdCluster::RES_ID_SYS; + for (const auto &inputPath : packageParser.GetInputs()) { + string idDefinedPath; + if (combine) { + idDefinedPath = FileEntry::FilePath(inputPath).Append(ID_DEFINED_FILE).GetPath(); + } else { idDefinedPath = FileEntry::FilePath(inputPath).Append(RESOURCES_DIR) .Append("base").Append("element").Append(ID_DEFINED_FILE).GetPath(); - if (InitIdDefined(idDefinedPath) != RESTOOL_SUCCESS) { - return RESTOOL_ERROR; - } } + if (ResourceUtil::FileExist(idDefinedPath) && startId > 0) { + cerr << "Error: the set start_id and id_defined.json cannot be used together." << endl; + return RESTOOL_ERROR; + } + if (InitIdDefined(idDefinedPath, isSys) != RESTOOL_SUCCESS) { + return RESTOOL_ERROR; + } + } + if (isSys) { return RESTOOL_SUCCESS; } - - idDefinedPath = FileEntry::FilePath(packageParser.GetRestoolPath()).GetParent().Append(ID_DEFINED_FILE).GetPath(); - if (InitIdDefined(idDefinedPath) != RESTOOL_SUCCESS) { + if (!idDefinedInput.empty()) { + appDefinedIds_.clear(); + string idDefinedPath = FileEntry::FilePath(idDefinedInput).GetPath(); + if (InitIdDefined(idDefinedPath, false) != RESTOOL_SUCCESS) { + return RESTOOL_ERROR; + } + } + string sysIdDefinedPath = FileEntry::FilePath(packageParser.GetRestoolPath()) + .GetParent().Append(ID_DEFINED_FILE).GetPath(); + if (InitIdDefined(sysIdDefinedPath, true) != RESTOOL_SUCCESS) { return RESTOOL_ERROR; } return RESTOOL_SUCCESS; } -uint32_t IdWorker::InitIdDefined(const std::string &filePath) +uint32_t IdWorker::InitIdDefined(const std::string &filePath, bool isSystem) { if (!ResourceUtil::FileExist(filePath)) { return RESTOOL_SUCCESS; @@ -200,11 +246,6 @@ uint32_t IdWorker::InitIdDefined(const std::string &filePath) return RESTOOL_ERROR; } - int32_t startSysId = GetStartId(root); - if (startSysId < 0) { - return RESTOOL_ERROR; - } - auto record = root["record"]; if (record.empty()) { cerr << "Error: id_defined.json record empty." << endl; @@ -214,21 +255,39 @@ uint32_t IdWorker::InitIdDefined(const std::string &filePath) cerr << "Error: id_defined.json record not array." << endl; return RESTOOL_ERROR; } + int32_t startSysId = 0; + if (isSystem) { + startSysId = GetStartId(root); + if (startSysId < 0) { + return RESTOOL_ERROR; + } + } + if (IdDefinedToResourceIds(record, isSystem, startSysId) != RESTOOL_SUCCESS) { + return RESTOOL_ERROR; + } + return RESTOOL_SUCCESS; +} + +uint32_t IdWorker::IdDefinedToResourceIds(const Json::Value &record, bool isSystem, const int32_t startSysId) +{ for (Json::ArrayIndex index = 0; index < record.size(); index++) { auto arrayItem = record[index]; - ResourceId resourceId; - resourceId.seq = index; - resourceId.id = startSysId; if (!arrayItem.isObject()) { return RESTOOL_ERROR; } + ResourceId resourceId; + resourceId.seq = index; + resourceId.id = startSysId; for (const auto &handle : handles_) { + if ((handle.first == "id" && isSystem) || (handle.first == "order" && !isSystem)) { + continue; + } if (!handle.second(arrayItem[handle.first], resourceId)) { return RESTOOL_ERROR; } } - if (!PushResourceId(resourceId)) { + if (!PushResourceId(resourceId, isSystem)) { return RESTOOL_ERROR; } } @@ -240,21 +299,48 @@ void IdWorker::InitParser() using namespace placeholders; handles_.emplace("type", bind(&IdWorker::ParseType, this, _1, _2)); handles_.emplace("name", bind(&IdWorker::ParseName, this, _1, _2)); + handles_.emplace("id", bind(&IdWorker::ParseId, this, _1, _2)); handles_.emplace("order", bind(&IdWorker::ParseOrder, this, _1, _2)); } +bool IdWorker::ParseId(const Json::Value &origId, ResourceId &resourceId) +{ + if (origId.empty()) { + cerr << "Error: id_defined.json seq =" << resourceId.seq << " id empty." << endl; + return false; + } + if (!origId.isString()) { + cerr << "Error: id_defined.json seq =" << resourceId.seq << " id not string." << endl; + return false; + } + string idStr = origId.asString(); + if (!ResourceUtil::CheckHexStr(idStr)) { + cerr << "Error: id_defined.json seq =" << resourceId.seq; + cerr << " id must be a hex string, eg:^0[xX][0-9a-fA-F]{8}" << endl; + return false; + } + int32_t id = strtol(idStr.c_str(), nullptr, 16); + if (id < 0x01000000 || (id >= 0x06FFFFFF && id < 0x08000000) || id >= 0x41FFFFFF) { + cerr << "Error: id_defined.json seq = "<< resourceId.seq; + cerr << " id must in [0x01000000,0x06FFFFFF),[0x08000000,0x41FFFFFF)." << endl; + return false; + } + resourceId.id = id; + return true; +} + bool IdWorker::ParseType(const Json::Value &type, ResourceId &resourceId) { if (type.empty()) { - cerr << "Error: id_defined.json seq=" << resourceId.seq << " type empty." << endl; + cerr << "Error: id_defined.json seq =" << resourceId.seq << " type empty." << endl; return false; } if (!type.isString()) { - cerr << "Error: id_defined.json seq=" << resourceId.seq << " type not string." << endl; + cerr << "Error: id_defined.json seq =" << resourceId.seq << " type not string." << endl; return false; } if (ResourceUtil::GetResTypeFromString(type.asString()) == ResType::INVALID_RES_TYPE) { - cerr << "Error: id_defined.json seq=" << resourceId.seq << " type '"; + cerr << "Error: id_defined.json seq =" << resourceId.seq << " type '"; cerr << type.asString() << "' invalid." << endl; return false; } @@ -265,15 +351,16 @@ bool IdWorker::ParseType(const Json::Value &type, ResourceId &resourceId) bool IdWorker::ParseName(const Json::Value &name, ResourceId &resourceId) { if (name.empty()) { - cerr << "Error: id_defined.json seq=" << resourceId.seq << " name empty." << endl; + cerr << "Error: id_defined.json seq =" << resourceId.seq << " name empty." << endl; return false; } if (!name.isString()) { - cerr << "Error: id_defined.json seq=" << resourceId.seq << " name not string." << endl; + cerr << "Error: id_defined.json seq =" << resourceId.seq << " name not string." << endl; return false; } resourceId.name = name.asString(); - if ((resourceId.id & START_SYS_ID) == START_SYS_ID && !IsValidSystemName(resourceId.name)) { + if (type_ == ResourceIdCluster::RES_ID_SYS && + (resourceId.id & START_SYS_ID) == START_SYS_ID && !IsValidSystemName(resourceId.name)) { cerr << "Error: id_defined.json."<< endl; return false; } @@ -283,15 +370,15 @@ bool IdWorker::ParseName(const Json::Value &name, ResourceId &resourceId) bool IdWorker::ParseOrder(const Json::Value &order, ResourceId &resourceId) { if (order.empty()) { - cerr << "Error: id_defined.json seq=" << resourceId.seq << " order empty." << endl; + cerr << "Error: id_defined.json seq =" << resourceId.seq << " order empty." << endl; return false; } if (!order.isInt()) { - cerr << "Error: id_defined.json seq=" << resourceId.seq << " order not int." << endl; + cerr << "Error: id_defined.json seq =" << resourceId.seq << " order not int." << endl; return false; } if (order.asInt() != resourceId.seq) { - cerr << "Error: id_defined.json seq=" << resourceId.seq << " order value "; + cerr << "Error: id_defined.json seq =" << resourceId.seq << " order value "; cerr << order.asInt() << " vs expect " << resourceId.seq << endl; return false; } @@ -299,14 +386,19 @@ bool IdWorker::ParseOrder(const Json::Value &order, ResourceId &resourceId) return true; } -bool IdWorker::PushResourceId(const ResourceId &resourceId) +bool IdWorker::PushResourceId(const ResourceId &resourceId, bool isSystem) { ResType resType = ResourceUtil::GetResTypeFromString(resourceId.type); - auto result = definedIds_.emplace(make_pair(resType, resourceId.name), resourceId); - if (!result.second) { - cerr << "Error: '" << resourceId.type << "' '" << resourceId.name << "' duplicated." << endl; + auto ret = idDefineds_.emplace(resourceId.id, resourceId); + if (!ret.second) { + cerr << "Error: '" << ret.first->second.name << "' and '" << resourceId.name << "' defind the same ID." << endl; return false; } + 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/reference_parser.cpp b/src/reference_parser.cpp index 29b3168..5d06928 100644 --- a/src/reference_parser.cpp +++ b/src/reference_parser.cpp @@ -307,7 +307,7 @@ bool ReferenceParser::ParseRefImpl(string &key, const map &refs key = to_string(id); if (ref.second != ResType::ID) { - key = key = "$" + ResourceUtil::ResTypeToString(ref.second) + ":" + to_string(id); + key = "$" + ResourceUtil::ResTypeToString(ref.second) + ":" + to_string(id); } return true; } diff --git a/src/resource_append.cpp b/src/resource_append.cpp index 18ad606..2780308 100644 --- a/src/resource_append.cpp +++ b/src/resource_append.cpp @@ -83,7 +83,9 @@ bool ResourceAppend::Combine(const string &folderPath) cerr << "Error:" << child->GetFilePath().GetPath() << " not file" << endl; return false; } - + if (child->GetFilePath().GetFilename() == ID_DEFINED_FILE) { + continue; + } if (!LoadResourceItem(child->GetFilePath().GetPath())) { return false; } @@ -261,6 +263,12 @@ bool ResourceAppend::ScanFiles(const unique_ptr &entry, bool ResourceAppend::ScanFile(const FileInfo &fileInfo, const string &outputPath) { + if (ResourceAppend::IsBaseIdDefined(fileInfo)) { + cout << "Warning: id_defined.json does not compile to generate intermediate files" << endl; + FileEntry::FilePath outPath(outputPath); + return ResourceUtil::CopyFleInner(fileInfo.filePath, outPath.Append(ID_DEFINED_FILE).GetPath()); + } + unique_ptr resourceCompiler = FactoryResourceCompiler::CreateCompilerForAppend(fileInfo.dirType, outputPath); if (resourceCompiler == nullptr) { @@ -648,6 +656,14 @@ bool ResourceAppend::LoadResourceItemWin(const string &filePath) return result; } #endif + +bool ResourceAppend::IsBaseIdDefined(const FileInfo &fileInfo) +{ + FileEntry::FilePath filePath(fileInfo.filePath); + return filePath.GetParent().GetParent().GetFilename() == "base" && + filePath.GetParent().GetFilename() == "element" && + fileInfo.filename == ID_DEFINED_FILE; +} } } } \ No newline at end of file diff --git a/src/resource_table.cpp b/src/resource_table.cpp index cf88890..977fcc0 100644 --- a/src/resource_table.cpp +++ b/src/resource_table.cpp @@ -28,6 +28,9 @@ ResourceTable::ResourceTable() { auto &parser = CmdParser::GetInstance(); auto &packageParser = parser.GetCmdParser(); + if (!packageParser.GetIdDefinedOutput().empty()) { + idDefinedPath_ = FileEntry::FilePath(packageParser.GetIdDefinedOutput()).Append(ID_DEFINED_FILE).GetPath(); + } indexFilePath_ = FileEntry::FilePath(packageParser.GetOutput()).Append(RESOURCE_INDEX_FILE).GetPath(); } @@ -55,13 +58,21 @@ uint32_t ResourceTable::CreateResourceTable() if (SaveToResouorceIndex(configs) != RESTOOL_SUCCESS) { return RESTOOL_ERROR; } + + if (!idDefinedPath_.empty()) { + if (CreateIdDefined(allResource) != RESTOOL_SUCCESS) { + return RESTOOL_ERROR; + } + } return RESTOOL_SUCCESS; } uint32_t ResourceTable::CreateResourceTable(const map>> &items) { map> configs; + map> allResource; for (const auto &item : items) { + vector resourceItems; for (const auto &resourceItemPtr : item.second) { if (resourceItemPtr->GetResType() == ResType::ID) { break; @@ -69,13 +80,21 @@ uint32_t ResourceTable::CreateResourceTable(const mapGetLimitKey()].push_back(tableData); } + allResource.emplace(item.first, resourceItems); } if (SaveToResouorceIndex(configs) != RESTOOL_SUCCESS) { return RESTOOL_ERROR; } + + if (!idDefinedPath_.empty()) { + if (CreateIdDefined(allResource) != RESTOOL_SUCCESS) { + return RESTOOL_ERROR; + } + } return RESTOOL_SUCCESS; } @@ -125,6 +144,41 @@ uint32_t ResourceTable::LoadResTable(const string path, map> &allResource) const +{ + Json::Value root; + for (const auto &pairPtr : allResource) { + Json::Value jsonItem; + ResourceItem item = pairPtr.second.front(); + ResType resType = item.GetResType(); + string type = ResourceUtil::ResTypeToString(resType); + string name = item.GetName(); + int32_t id = pairPtr.first; + if (type.empty()) { + cerr << "Error : name = " << name << " ,ResType must is"; + cerr << ResourceUtil::GetAllRestypeString() << endl; + return RESTOOL_ERROR; + } + jsonItem["type"] = type; + jsonItem["name"] = ResourceUtil::GetIdName(name, resType); + jsonItem["id"] = ResourceUtil::DecToHexStr(id); + root["record"].append(jsonItem); + } + + ofstream out(idDefinedPath_, ofstream::out | ofstream::binary); + if (!out.is_open()) { + cerr << "Error: open failed '" << idDefinedPath_ << "',reason: " << strerror(errno) << endl; + return RESTOOL_ERROR; + } + Json::StreamWriterBuilder jswBuilder; + jswBuilder["indentation"] = ID_DEFINED_INDENTATION; + unique_ptr writer(jswBuilder.newStreamWriter()); + writer->write(root, &out); + out.close(); + return RESTOOL_SUCCESS; +} + // below private uint32_t ResourceTable::SaveToResouorceIndex(const map> &configs) const { @@ -196,7 +250,7 @@ bool ResourceTable::Prepare(const map> &configs, return false; } limitKeyConfig->second.offset = pos; - + IdSet idSet; idSet.idCount = config.second.size(); pos += sizeof(idSet.idTag) + sizeof(idSet.idCount); diff --git a/src/resource_util.cpp b/src/resource_util.cpp index 77f9475..5ac0280 100644 --- a/src/resource_util.cpp +++ b/src/resource_util.cpp @@ -14,11 +14,12 @@ */ #include "resource_util.h" -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include #include "file_entry.h" namespace OHOS { @@ -369,6 +370,32 @@ string ResourceUtil::PaserKeyParam(const vector &keyParams) return result; } +string ResourceUtil::DecToHexStr(const int32_t i) +{ + stringstream ot; + string result; + ot << setiosflags(ios::uppercase) << "0x" << hex << setw(8) << setfill('0') << i;// 0x expadding 8 bit + ot >> result; + return result; +} + +bool ResourceUtil::CheckHexStr(const string &hex) +{ + if (regex_match(hex, regex("^0[xX][0-9a-fA-F]{8}"))) { + return true; + } + return false; +} + +string ResourceUtil::GetAllRestypeString() +{ + string result; + for (auto iter = g_contentClusterMap.begin(); iter != g_contentClusterMap.end(); ++iter) { + result = result + "," + iter->first; + } + return result; +} + } } }