From 358a464ba17ae7518212d2cd9bdc0cf668b6de6d Mon Sep 17 00:00:00 2001 From: liduo Date: Fri, 22 Nov 2024 18:25:45 +0800 Subject: [PATCH 1/2] optimize load library error message Signed-off-by: liduo --- src/compression_parser.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/compression_parser.cpp b/src/compression_parser.cpp index 5b33012..4feb177 100644 --- a/src/compression_parser.cpp +++ b/src/compression_parser.cpp @@ -306,8 +306,14 @@ bool CompressionParser::LoadImageTranscoder() if (!handle_) { handle_ = LoadLibrary(TEXT(extensionPath_.c_str())); if (!handle_) { + DWORD err = GetLastError(); + LPVOID lpMsgBuf; + FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, + NULL, err, MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL); + const char *msg = static_cast(lpMsgBuf); cerr << "Error: open '" << extensionPath_.c_str() << "' fail." << endl; - cerr << "Error: LoadLibrary failed with error: " << GetLastError() << endl; + cerr << "Error: LoadLibrary failed with error: " << err << ", " << msg << endl; + LocalFree(lpMsgBuf); return false; } } @@ -320,8 +326,9 @@ bool CompressionParser::LoadImageTranscoder() } handle_ = dlopen(realPath.c_str(), RTLD_LAZY); if (!handle_) { + char *err = dlerror(); cerr << "Error: open '" << realPath.c_str() << "' fail." << endl; - cerr << "Error: dlopen failed with error: " << dlerror() << endl; + cerr << "Error: dlopen failed with error: " << err << endl; return false; } } -- Gitee From d85a4165246a8b42656c131ccb9b275646c57f51 Mon Sep 17 00:00:00 2001 From: sunjie Date: Fri, 6 Dec 2024 15:33:32 +0800 Subject: [PATCH 2/2] add restool dump command Signed-off-by: sunjie Change-Id: I32459795e00969e3623798178e9a623c287d80bf --- BUILD.gn | 6 +- bundle.json | 3 +- include/binary_file_packer.h | 2 +- include/command_parser/cmd_parser.h | 111 +++++ .../dump_parser.h} | 20 +- .../package_parser.h} | 79 +--- include/id_defined_parser.h | 2 +- include/resource_append.h | 2 +- include/resource_data.h | 13 + include/resource_dump.h | 59 +++ include/resource_item.h | 3 + include/resource_pack.h | 6 +- include/resource_table.h | 20 +- include/resource_util.h | 14 +- .../dump_parser.cpp} | 48 ++- .../package_parser.cpp} | 14 +- src/id_worker.cpp | 2 +- src/resource_dump.cpp | 392 ++++++++++++++++++ src/resource_item.cpp | 34 ++ src/resource_merge.cpp | 2 +- src/resource_pack.cpp | 19 +- src/resource_table.cpp | 35 +- src/resource_util.cpp | 17 + src/restool.cpp | 41 +- src/select_compile_parse.cpp | 2 +- 25 files changed, 797 insertions(+), 149 deletions(-) create mode 100644 include/command_parser/cmd_parser.h rename include/{task_handle.h => command_parser/dump_parser.h} (60%) rename include/{cmd_parser.h => command_parser/package_parser.h} (58%) create mode 100644 include/resource_dump.h rename src/{task_handle.cpp => command_parser/dump_parser.cpp} (34%) rename src/{cmd_parser.cpp => command_parser/package_parser.cpp} (98%) create mode 100644 src/resource_dump.cpp diff --git a/BUILD.gn b/BUILD.gn index 532a37f..281b84e 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -19,7 +19,8 @@ ohos_executable("restool") { sources = [ "src/append_compiler.cpp", "src/binary_file_packer.cpp", - "src/cmd_parser.cpp", + "src/command_parser/dump_parser.cpp", + "src/command_parser/package_parser.cpp", "src/compression_parser.cpp", "src/config_parser.cpp", "src/factory_resource_compiler.cpp", @@ -37,6 +38,7 @@ ohos_executable("restool") { "src/resource_append.cpp", "src/resource_check.cpp", "src/resource_directory.cpp", + "src/resource_dump.cpp", "src/resource_item.cpp", "src/resource_merge.cpp", "src/resource_module.cpp", @@ -45,7 +47,6 @@ ohos_executable("restool") { "src/resource_util.cpp", "src/restool.cpp", "src/select_compile_parse.cpp", - "src/task_handle.cpp", "src/thread_pool.cpp", "src/translatable_parser.cpp", ] @@ -59,6 +60,7 @@ ohos_executable("restool") { "//third_party/bounds_checking_function:libsec_static", "//third_party/cJSON:cjson_static", "//third_party/libpng:libpng_static", + "//third_party/zlib:libz", ] cflags = [ "-std=c++17" ] diff --git a/bundle.json b/bundle.json index 01e4a5a..e04267d 100644 --- a/bundle.json +++ b/bundle.json @@ -22,7 +22,8 @@ "third_party": [ "bounds_checking_function", "cJSON", - "libpng" + "libpng", + "zlib" ] }, "build": { diff --git a/include/binary_file_packer.h b/include/binary_file_packer.h index b01f9ee..bdafa2d 100644 --- a/include/binary_file_packer.h +++ b/include/binary_file_packer.h @@ -16,7 +16,7 @@ #ifndef OHOS_RESTOOL_BINARY_FILE_PACKER_H #define OHOS_RESTOOL_BINARY_FILE_PACKER_H -#include "cmd_parser.h" +#include "command_parser/package_parser.h" #include "resource_util.h" #include "thread_pool.h" diff --git a/include/command_parser/cmd_parser.h b/include/command_parser/cmd_parser.h new file mode 100644 index 0000000..de09440 --- /dev/null +++ b/include/command_parser/cmd_parser.h @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_RESTOOL_CMD_PARSER_H +#define OHOS_RESTOOL_CMD_PARSER_H + +#include +#include +#include +#include +#include +#include "singleton.h" +#include "restool_errors.h" + +namespace OHOS { +namespace Global { +namespace Restool { + +class ITask { +public: + virtual uint32_t Run() = 0; +}; + +class ICmdParser { +public: + virtual uint32_t Parse(int argc, char *argv[]) = 0; + virtual std::shared_ptr GetTask() = 0; +}; + +template +class CmdParser : public Singleton> { + static_assert(std::is_base_of_v, "Template T must inherit ICmdParser."); +public: + T &GetCmdParser(); + uint32_t Parse(int argc, char *argv[]); + uint32_t ExecCommand(); + static void ShowUseage(); + +private: + T cmdParser_; +}; + +template +void CmdParser::ShowUseage() +{ + std::cout << "This is an OHOS Packaging Tool.\n"; + std::cout << "Usage:\n"; + std::cout << TOOL_NAME << " [arguments] Package the OHOS resources.\n"; + std::cout << "[arguments]:\n"; + std::cout << " -i/--inputPath input resource path, can add more.\n"; + std::cout << " -p/--packageName package name.\n"; + std::cout << " -o/--outputPath output path.\n"; + std::cout << " -r/--resHeader resource header file path(like ./ResourceTable.js, ./ResrouceTable.h).\n"; + std::cout << " -f/--forceWrite if output path exists,force delete it.\n"; + std::cout << " -v/--version print tool version.\n"; + std::cout << " -m/--modules module name, can add more, split by ','(like entry1,entry2,...).\n"; + std::cout << " -j/--json config.json path.\n"; + std::cout << " -e/--startId start id mask, e.g 0x01000000,"; + std::cout << " in [0x01000000, 0x06FFFFFF),[0x08000000, 0xFFFFFFFF)\n"; + std::cout << " -x/--append resources folder path\n"; + std::cout << " -z/--combine flag for incremental compilation\n"; + std::cout << " -h/--help Displays this help menu\n"; + std::cout << " -l/--fileList input json file of the option set, e.g resConfig.json."; + std::cout << " For details, see the developer documentation.\n"; + std::cout << " --ids save id_defined.json direcory\n"; + std::cout << " --defined-ids input id_defined.json path\n"; + std::cout << " --dependEntry Build result directory of the specified entry module when the feature"; + std::cout << " module resources are independently built in the FA model.\n"; + std::cout << " --icon-check Enable the PNG image verification function for icons and startwindows.\n"; + std::cout << " --target-config When used with '-i', selective compilation is supported.\n"; + std::cout << " --compressed-config opt-compression.json path.\n"; + std::cout << " dump [config] Dump rsource in Hap to json. If with 'config', will only dump limit path.\n"; +} + +template +T &CmdParser::GetCmdParser() +{ + return cmdParser_; +} + +template +uint32_t CmdParser::Parse(int argc, char *argv[]) +{ + if (cmdParser_.Parse(argc, argv) != RESTOOL_SUCCESS) { + return RESTOOL_ERROR; + } + return RESTOOL_SUCCESS; +} + +template +uint32_t CmdParser::ExecCommand() +{ + auto task = cmdParser_.GetTask(); + return task->Run(); +}; +} +} +} +#endif diff --git a/include/task_handle.h b/include/command_parser/dump_parser.h similarity index 60% rename from include/task_handle.h rename to include/command_parser/dump_parser.h index 72daadf..3c99c28 100644 --- a/include/task_handle.h +++ b/include/command_parser/dump_parser.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -13,19 +13,25 @@ * limitations under the License. */ -#ifndef OHOS_RESTOOL_TASK_HANDLE_H -#define OHOS_RESTOOL_TASK_HANDLE_H +#ifndef OHOS_RESTOOL_DUMP_PARSER_H +#define OHOS_RESTOOL_DUMP_PARSER_H #include "cmd_parser.h" namespace OHOS { namespace Global { namespace Restool { -class TaskHandle { +class DumpParser : public ICmdParser { public: - TaskHandle() {}; - ~TaskHandle() {}; - uint32_t HandlePackage(const PackageParser &packageParser); + virtual ~DumpParser() = default; + uint32_t Parse(int argc, char *argv[]) override; + std::shared_ptr GetTask() override; + const std::string &GetDumpPath() const; + bool IsDumpConfig() const; + +private: + std::string dumpPath_; + bool IsDumpConfig_; }; } } diff --git a/include/cmd_parser.h b/include/command_parser/package_parser.h similarity index 58% rename from include/cmd_parser.h rename to include/command_parser/package_parser.h index 7f3772c..c89794d 100644 --- a/include/cmd_parser.h +++ b/include/command_parser/package_parser.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -12,31 +12,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + +#ifndef OHOS_RESTOOL_PACKAGE_PARSER_PARSER_H +#define OHOS_RESTOOL_PACKAGE_PARSER_PARSER_H -#ifndef OHOS_RESTOOL_CMD_PARSER_H -#define OHOS_RESTOOL_CMD_PARSER_H - +#include "cmd_parser.h" #include -#include #include +#include #include -#include "singleton.h" -#include "resource_data.h" -#include "restool_errors.h" namespace OHOS { namespace Global { namespace Restool { -class ICmdParser { -public: - virtual uint32_t Parse(int argc, char *argv[]) = 0; -}; class PackageParser : public ICmdParser { public: - PackageParser() {}; + PackageParser() = default; virtual ~PackageParser() = default; uint32_t Parse(int argc, char *argv[]) override; + std::shared_ptr GetTask() override; const std::vector &GetInputs() const; const std::string &GetPackageName() const; const std::string &GetOutput() const; @@ -114,63 +109,7 @@ private: std::string compressionPath_; }; -template -class CmdParser : public Singleton> { -public: - T &GetCmdParser(); - uint32_t Parse(int argc, char *argv[]); - static void ShowUseage(); - -private: - T cmdParser_; -}; - -template -void CmdParser::ShowUseage() -{ - std::cout << "This is an OHOS Packaging Tool.\n"; - std::cout << "Usage:\n"; - std::cout << TOOL_NAME << " [arguments] Package the OHOS resources.\n"; - std::cout << "[arguments]:\n"; - std::cout << " -i/--inputPath input resource path, can add more.\n"; - std::cout << " -p/--packageName package name.\n"; - std::cout << " -o/--outputPath output path.\n"; - std::cout << " -r/--resHeader resource header file path(like ./ResourceTable.js, ./ResrouceTable.h).\n"; - std::cout << " -f/--forceWrite if output path exists,force delete it.\n"; - std::cout << " -v/--version print tool version.\n"; - std::cout << " -m/--modules module name, can add more, split by ','(like entry1,entry2,...).\n"; - std::cout << " -j/--json config.json path.\n"; - std::cout << " -e/--startId start id mask, e.g 0x01000000,"; - std::cout << " in [0x01000000, 0x06FFFFFF),[0x08000000, 0xFFFFFFFF)\n"; - std::cout << " -x/--append resources folder path\n"; - std::cout << " -z/--combine flag for incremental compilation\n"; - std::cout << " -h/--help Displays this help menu\n"; - std::cout << " -l/--fileList input json file of the option set, e.g resConfig.json."; - std::cout << " For details, see the developer documentation.\n"; - std::cout << " --ids save id_defined.json direcory\n"; - std::cout << " --defined-ids input id_defined.json path\n"; - std::cout << " --dependEntry Build result directory of the specified entry module when the feature"; - std::cout << " module resources are independently built in the FA model.\n"; - std::cout << " --icon-check Enable the PNG image verification function for icons and startwindows.\n"; - std::cout << " --target-config When used with '-i', selective compilation is supported.\n"; - std::cout << " --compressed-config opt-compression.json path.\n"; -} - -template -T &CmdParser::GetCmdParser() -{ - return cmdParser_; -} - -template -uint32_t CmdParser::Parse(int argc, char *argv[]) -{ - if (cmdParser_.Parse(argc, argv) != RESTOOL_SUCCESS) { - return RESTOOL_ERROR; - } - return RESTOOL_SUCCESS; -} } } } -#endif +#endif \ No newline at end of file diff --git a/include/id_defined_parser.h b/include/id_defined_parser.h index 0fa40d4..4201cb0 100755 --- a/include/id_defined_parser.h +++ b/include/id_defined_parser.h @@ -18,7 +18,7 @@ #include #include -#include "cmd_parser.h" +#include "command_parser/package_parser.h" namespace OHOS { namespace Global { diff --git a/include/resource_append.h b/include/resource_append.h index 2261e5f..ffb2c85 100644 --- a/include/resource_append.h +++ b/include/resource_append.h @@ -17,7 +17,7 @@ #define OHOS_RESTOOL_RESOURCE_APPEND_H #include -#include "cmd_parser.h" +#include "command_parser/package_parser.h" #include "factory_resource_compiler.h" #include "file_entry.h" diff --git a/include/resource_data.h b/include/resource_data.h index c7c34c6..f6e6265 100644 --- a/include/resource_data.h +++ b/include/resource_data.h @@ -245,6 +245,19 @@ const std::map g_contentClusterMap = { { "symbol", ResType::SYMBOL } }; +const std::map g_keyTypeToStrMap = { + {KeyType::MCC, "mcc"}, + {KeyType::MNC, "mnc"}, + {KeyType::LANGUAGE, "language"}, + {KeyType::SCRIPT, "script"}, + {KeyType::REGION, "region"}, + {KeyType::ORIENTATION, "orientation"}, + {KeyType::RESOLUTION, "resolution"}, + {KeyType::DEVICETYPE, "deviceType"}, + {KeyType::NIGHTMODE, "nightMode"}, + {KeyType::INPUTDEVICE, "inputDevice"}, +}; + const std::map g_resTypeMap = { { static_cast(ResType::ELEMENT), ResType::ELEMENT}, { static_cast(ResType::RAW), ResType::RAW}, diff --git a/include/resource_dump.h b/include/resource_dump.h new file mode 100644 index 0000000..39b138f --- /dev/null +++ b/include/resource_dump.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_RESTOOL_RESOURCE_DUMP_H +#define OHOS_RESTOOL_RESOURCE_DUMP_H + +#include +#include +#include +#include "unzip.h" +#include "cJSON.h" +#include "command_parser/dump_parser.h" +#include "resource_data.h" +#include "resource_item.h" + + +namespace OHOS { +namespace Global { +namespace Restool { +class ResourceDump : public ITask { +public: + explicit ResourceDump(const DumpParser &parser); + virtual ~ResourceDump() = default; + uint32_t Run(); + +private: + void ReadHapInfo(const std::unique_ptr &buffer, size_t len); + uint32_t LoadHap(); + uint32_t ReadFileFromZip(unzFile &zip, const char *fileName, std::unique_ptr &buffer, size_t &len); + uint32_t DumpAll(std::string &out) const; + uint32_t DumpConfig(std::string &out) const; + uint32_t AddValueToJson(const ResourceItem &item, cJSON *json) const; + uint32_t AddPairVauleToJson(const ResourceItem &item, cJSON *json) const; + uint32_t AddKeyParamsToJson(const std::vector &keyParams, cJSON *json) const; + uint32_t AddResourceToJson(int64_t id, const std::vector &items, cJSON *json) const; + uint32_t AddItemCommonPropToJson(int32_t resId, const ResourceItem &item, cJSON* json) const; + const std::string dumpPath_; + const bool IsDumpConfig_; + std::string bundleName_; + std::string moduleName_; + std::map> resInfos_; +}; +} // namespace Restool +} // namespace Global +} // namespace OHOS + +#endif \ No newline at end of file diff --git a/include/resource_item.h b/include/resource_item.h index e6699e9..762a352 100644 --- a/include/resource_item.h +++ b/include/resource_item.h @@ -42,6 +42,9 @@ public: const std::vector &GetKeyParam() const; const std::string &GetFilePath() const; const std::string &GetLimitKey() const; + const std::vector SplitValue() const; + bool IsArry() const; + bool IsPair() const; ResourceItem &operator=(const ResourceItem &other); private: diff --git a/include/resource_pack.h b/include/resource_pack.h index 9f566fe..36f36b4 100644 --- a/include/resource_pack.h +++ b/include/resource_pack.h @@ -16,7 +16,7 @@ #ifndef OHOS_RESTOOL_RESOURCE_PACK_H #define OHOS_RESTOOL_RESOURCE_PACK_H -#include "cmd_parser.h" +#include "command_parser/package_parser.h" #include "config_parser.h" #include "resource_append.h" #include "resource_data.h" @@ -27,11 +27,11 @@ namespace OHOS { namespace Global { namespace Restool { -class ResourcePack { +class ResourcePack : public ITask { public: explicit ResourcePack(const PackageParser &packageParser); virtual ~ResourcePack() = default; - uint32_t Package(); + uint32_t Run() override; private: uint32_t Init(); diff --git a/include/resource_table.h b/include/resource_table.h index 3aa5520..ce26ce1 100644 --- a/include/resource_table.h +++ b/include/resource_table.h @@ -17,8 +17,11 @@ #define OHOS_RESTOOL_RESOURCE_TABLE_H #include +#include +#include #include #include +#include #include "resource_item.h" #include "restool_errors.h" @@ -32,6 +35,7 @@ public: uint32_t CreateResourceTable(); uint32_t CreateResourceTable(const std::map>> &items); uint32_t LoadResTable(const std::string path, std::map> &resInfos); + static uint32_t LoadResTable(std::basic_istream &in, std::map> &resInfos); private: struct TableData { uint32_t id; @@ -74,16 +78,16 @@ private: void SaveLimitKeyConfigs(const std::map &limitKeyConfigs, std::ostringstream &out) const; void SaveIdSets(const std::map &idSets, std::ostringstream &out) const; - bool ReadFileHeader(std::ifstream &in, IndexHeader &indexHeader, uint64_t &pos, uint64_t length) const; - bool ReadLimitKeys(std::ifstream &in, std::map> &limitKeys, - uint32_t count, uint64_t &pos, uint64_t length) const; - bool ReadIdTables(std::ifstream &in, std::map> &datas, - uint32_t count, uint64_t &pos, uint64_t length) const; - bool ReadDataRecordPrepare(std::ifstream &in, RecordItem &record, uint64_t &pos, uint64_t length) const; - bool ReadDataRecordStart(std::ifstream &in, RecordItem &record, + static bool ReadFileHeader(std::basic_istream &in, IndexHeader &indexHeader, uint64_t &pos, uint64_t length); + static bool ReadLimitKeys(std::basic_istream &in, std::map> &limitKeys, + uint32_t count, uint64_t &pos, uint64_t length); + static bool ReadIdTables(std::basic_istream &in, std::map> &datas, + uint32_t count, uint64_t &pos, uint64_t length); + static bool ReadDataRecordPrepare(std::basic_istream &in, RecordItem &record, uint64_t &pos, uint64_t length); + static bool ReadDataRecordStart(std::basic_istream &in, RecordItem &record, const std::map> &limitKeys, const std::map> &datas, - std::map> &resInfos) const; + std::map> &resInfos); std::string indexFilePath_; std::string idDefinedPath_; }; diff --git a/include/resource_util.h b/include/resource_util.h index 245abd2..0431c41 100644 --- a/include/resource_util.h +++ b/include/resource_util.h @@ -247,6 +247,19 @@ public: */ static void PrintWarningMsg(std::vector> &noBaseResource); + /** + * @brief covert KeyParam to str by keytype + * @param keyParam: KeyParam + * @return limit value string + */ + static std::string GetKeyParamValue(const KeyParam &KeyParam); + + /** + * @brief keyType to string + * @param type: KeyType + * @return limit type string + */ + static std::string KeyTypeToStr(KeyType type); private: enum class IgnoreType { IGNORE_FILE, @@ -257,7 +270,6 @@ private: static std::string GetLocaleLimitkey(const KeyParam &KeyParam); static std::string GetDeviceTypeLimitkey(const KeyParam &KeyParam); static std::string GetResolutionLimitkey(const KeyParam &KeyParam); - static std::string GetKeyParamValue(const KeyParam &KeyParam); }; } } diff --git a/src/task_handle.cpp b/src/command_parser/dump_parser.cpp similarity index 34% rename from src/task_handle.cpp rename to src/command_parser/dump_parser.cpp index 9067917..23de3e8 100644 --- a/src/task_handle.cpp +++ b/src/command_parser/dump_parser.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -13,16 +13,52 @@ * limitations under the License. */ -#include "task_handle.h" -#include "resource_pack.h" +#include "command_parser/dump_parser.h" +#include +#include +#include "resource_dump.h" +#include "resource_util.h" namespace OHOS { namespace Global { namespace Restool { -uint32_t TaskHandle::HandlePackage(const PackageParser &packageParser) +using namespace std; +uint32_t DumpParser::Parse(int argc, char *argv[]) { - ResourcePack pack(packageParser); - return pack.Package(); + // argv[0] is restool path, argv[1] is 'dump' subcommand. + int currentIndex = 2; + if (currentIndex >= argc) { + cerr << "Error: missing input Hap path." << endl; + return RESTOOL_ERROR; + } + if (strcasecmp(argv[currentIndex], "config") == 0) { + IsDumpConfig_ = true; + currentIndex++; + } + if (currentIndex >= argc) { + cerr << "Error: missing input Hap path." << endl; + return RESTOOL_ERROR; + } + dumpPath_ = ResourceUtil::RealPath(argv[currentIndex]); + if (dumpPath_.empty()) { + cerr << "Error: invalid input path: '" << argv[currentIndex] << "'" << endl; + return RESTOOL_ERROR; + } + return RESTOOL_SUCCESS; +} + +bool DumpParser::IsDumpConfig() const +{ + return IsDumpConfig_; +} + +const std::string& DumpParser::GetDumpPath() const +{ + return dumpPath_; +} +std::shared_ptr DumpParser::GetTask() +{ + return make_shared(*this); } } } diff --git a/src/cmd_parser.cpp b/src/command_parser/package_parser.cpp similarity index 98% rename from src/cmd_parser.cpp rename to src/command_parser/package_parser.cpp index 8c09d3a..56f5023 100644 --- a/src/cmd_parser.cpp +++ b/src/command_parser/package_parser.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021-2024 Huawei Device Co., Ltd. + * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -13,9 +13,10 @@ * limitations under the License. */ -#include "cmd_parser.h" +#include "command_parser/package_parser.h" #include #include "resconfig_parser.h" +#include "resource_pack.h" #include "resource_util.h" #include "select_compile_parse.h" @@ -453,7 +454,7 @@ void PackageParser::InitCommand() handles_.emplace(Option::FORCEWRITE, [this](const string &) -> uint32_t { return ForceWrite(); }); handles_.emplace(Option::VERSION, [this](const string &) -> uint32_t { return PrintVersion(); }); handles_.emplace(Option::MODULES, bind(&PackageParser::AddMoudleNames, this, _1)); - handles_.emplace(Option::JSON, bind(&PackageParser::AddConfig, this, _1)); + handles_.emplace(Option::JSON, bind(&PackageParser::AddConfig, this, _1)); handles_.emplace(Option::STARTID, bind(&PackageParser::AddStartId, this, _1)); handles_.emplace(Option::APPEND, bind(&PackageParser::AddAppend, this, _1)); handles_.emplace(Option::COMBINE, [this](const string &) -> uint32_t { return SetCombine(); }); @@ -527,7 +528,7 @@ uint32_t PackageParser::CheckError(int argc, char *argv[], int c, int optIndex) uint32_t PackageParser::ParseCommand(int argc, char *argv[]) { restoolPath_ = string(argv[0]); - while (true) { + while (optind <= argc) { int optIndex = -1; int c = getopt_long(argc, argv, CMD_PARAMS.c_str(), CMD_OPTS, &optIndex); if (CheckError(argc, argv, c, optIndex) != RESTOOL_SUCCESS) { @@ -568,6 +569,11 @@ bool PackageParser::IsLongOpt(int argc, char *argv[]) const } return false; } + +std::shared_ptr PackageParser::GetTask() +{ + return make_shared(*this); +} } } } diff --git a/src/id_worker.cpp b/src/id_worker.cpp index af0e76e..9e85310 100644 --- a/src/id_worker.cpp +++ b/src/id_worker.cpp @@ -16,7 +16,7 @@ #include "id_worker.h" #include #include -#include "cmd_parser.h" +#include "command_parser/package_parser.h" namespace OHOS { namespace Global { diff --git a/src/resource_dump.cpp b/src/resource_dump.cpp new file mode 100644 index 0000000..a4cabee --- /dev/null +++ b/src/resource_dump.cpp @@ -0,0 +1,392 @@ +/* + * Copyright (c) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "resource_dump.h" + +#include +#include +#include +#include +#include +#include "cJSON.h" +#include "resource_item.h" +#include "resource_table.h" +#include "resource_util.h" +#include "restool_errors.h" + + +namespace OHOS { +namespace Global { +namespace Restool { + +constexpr int PAIR_SIZE = 2; + +ResourceDump::ResourceDump(const DumpParser &packageParser) + :dumpPath_(packageParser.GetDumpPath()), IsDumpConfig_(packageParser.IsDumpConfig()){}; + +uint32_t ResourceDump::Run() +{ + if (LoadHap() != RESTOOL_SUCCESS) { + return RESTOOL_ERROR; + } + std::string jsonStr; + uint32_t ret; + if (IsDumpConfig_) { + ret = DumpConfig(jsonStr); + } else { + ret = DumpAll(jsonStr); + } + if (ret != RESTOOL_SUCCESS) { + return RESTOOL_ERROR; + } + std::cout << jsonStr << std::endl; + return RESTOOL_SUCCESS; +} + +uint32_t ResourceDump::LoadHap() +{ + unzFile zipFile = unzOpen64(dumpPath_.c_str()); + if (!zipFile) { + std::cerr << "Error: Open file is failed. filename: " << dumpPath_ << std::endl; + return RESTOOL_ERROR; + } + std::unique_ptr buffer; + size_t len; + if (ReadFileFromZip(zipFile, "module.json", buffer, len) == RESTOOL_SUCCESS) { + ReadHapInfo(buffer, len); + } + if (ReadFileFromZip(zipFile, "resources.index", buffer, len) != RESTOOL_SUCCESS) { + unzClose(zipFile); + return RESTOOL_ERROR; + } + unzClose(zipFile); + std::stringstream stream; + stream.write(buffer.get(), len); + if (ResourceTable::LoadResTable(stream, resInfos_) != RESTOOL_SUCCESS) { + return RESTOOL_ERROR; + } + return RESTOOL_SUCCESS; +} + +void ResourceDump::ReadHapInfo(const std::unique_ptr &buffer, size_t len) +{ + cJSON *module = cJSON_Parse(buffer.get()); + if (!module) { + return; + } + cJSON *appConfig = cJSON_GetObjectItemCaseSensitive(module, "app"); + cJSON *moduleConfig = cJSON_GetObjectItemCaseSensitive(module, "module"); + if (appConfig) { + cJSON *bundleName = cJSON_GetObjectItemCaseSensitive(appConfig, "bundleName"); + if (cJSON_IsString(bundleName) && bundleName->valuestring) { + bundleName_ = bundleName->valuestring; + } + } + if (moduleConfig) { + cJSON *moduleName = cJSON_GetObjectItemCaseSensitive(moduleConfig, "name"); + if (cJSON_IsString(moduleName) && moduleName->valuestring) { + moduleName_ = moduleName->valuestring; + } + } + cJSON_Delete(module); +} + +uint32_t ResourceDump::ReadFileFromZip( + unzFile &zipFile, const char *fileName, std::unique_ptr &buffer, size_t &len) +{ + unz_file_info fileInfo; + if (unzLocateFile2(zipFile, fileName, 1) != UNZ_OK) { + std::cerr << "Error: Not found filename: " << fileName << " in " << dumpPath_ << std::endl; + return RESTOOL_ERROR; + } + char filenameInZip[256]; + int err = unzGetCurrentFileInfo(zipFile, &fileInfo, filenameInZip, sizeof(filenameInZip), nullptr, 0, nullptr, 0); + if (err != UNZ_OK) { + std::cerr << "Error: Get file info error. filename: " << fileName << " errorCode: " << err << std::endl; + return RESTOOL_ERROR; + } + + len = fileInfo.compressed_size; + buffer = std::make_unique(len); + if (!buffer) { + std::cerr << "Error: Allocating memory failed for buffer in ReadFileFromZip."<< std::endl; + return RESTOOL_ERROR; + } + + err = unzOpenCurrentFilePassword(zipFile, nullptr); + if (err != UNZ_OK) { + std::cerr << "Error in unzOpenCurrentFilePassword. ErrorCode: " << err <> root(cJSON_CreateObject(), + [](cJSON* node) { cJSON_Delete(node); }); + if (!root) { + std::cerr << "Error: failed to create cJSON object for root object." << std::endl; + return RESTOOL_ERROR; + } + if (!bundleName_.empty() && !moduleName_.empty()) { + cJSON *bundleName = cJSON_CreateString(bundleName_.c_str()); + cJSON *moduleName = cJSON_CreateString(moduleName_.c_str()); + if (bundleName) { + cJSON_AddItemToObject(root.get(), "bundleName", bundleName); + } + if (moduleName) { + cJSON_AddItemToObject(root.get(), "moduleName", moduleName); + } + } + cJSON *resource = cJSON_CreateArray(); + if (!resource) { + std::cerr << "Error: failed to create cJSON object for resource array." << std::endl; + return RESTOOL_ERROR; + } + cJSON_AddItemToObject(root.get(), "resource", resource); + for (auto it = resInfos_.begin(); it != resInfos_.end(); it++) { + if (AddResourceToJson(it->first, it->second, resource)) { + return RESTOOL_ERROR; + } + } + char *rawStr = cJSON_Print(root.get()); + if (!rawStr) { + std::cerr << "Error: covert json object to str failed" << std::endl; + return RESTOOL_ERROR; + } + out = rawStr; + cJSON_free(rawStr); + rawStr = nullptr; + return RESTOOL_SUCCESS; +} + +uint32_t ResourceDump::DumpConfig(std::string &out) const +{ + std::unique_ptr> root(cJSON_CreateObject(), + [](cJSON* node) { cJSON_Delete(node); }); + if (!root) { + std::cerr << "Error: failed to create cJSON object for root object." << std::endl; + return RESTOOL_ERROR; + } + cJSON *configs = cJSON_CreateArray(); + if (!configs) { + std::cerr << "Error: failed to create cJSON object for config array." << std::endl; + return RESTOOL_ERROR; + } + cJSON_AddItemToObject(root.get(), "config", configs); + + std::set configSet; + for (auto it = resInfos_.cbegin(); it != resInfos_.cend(); it++) { + for (const auto &item : it->second) { + const std::string &limitKey = item.GetLimitKey(); + if (configSet.count(limitKey) != 0) { + continue; + } + configSet.emplace(limitKey); + cJSON *config = cJSON_CreateString(limitKey.c_str()); + if (!config) { + std::cerr << "Error: failed to create cJSON object for limitkey." << std::endl; + return RESTOOL_ERROR; + } + cJSON_AddItemToArray(configs, config); + } + } + char *rawStr = cJSON_Print(root.get()); + if (!rawStr) { + std::cerr << "Error: covert config json object to str failed" << std::endl; + return RESTOOL_ERROR; + } + out = rawStr; + cJSON_free(rawStr); + rawStr = nullptr; + return RESTOOL_SUCCESS; +} + +uint32_t ResourceDump::AddKeyParamsToJson(const std::vector &keyParams, cJSON *json) const +{ + if (!json) { + std::cerr << "Error: Add keyParam to null json object." << std::endl; + return RESTOOL_ERROR; + } + for (const auto &keyParam : keyParams) { + std::string valueStr = ResourceUtil::GetKeyParamValue(keyParam); + cJSON *value = cJSON_CreateString(valueStr.c_str()); + if (!value) { + std::cerr << "Error: failed to create cJSON object for keyparam." << std::endl; + return RESTOOL_ERROR; + } + cJSON_AddItemToObject(json, ResourceUtil::KeyTypeToStr(keyParam.keyType).c_str(), value); + } + return RESTOOL_SUCCESS; +} + +uint32_t ResourceDump::AddItemCommonPropToJson(int32_t resId, const ResourceItem &item, cJSON* json) const +{ + if (!json) { + std::cerr << "Error: add item common property to null json object." << std::endl; + return RESTOOL_ERROR; + } + cJSON *id = cJSON_CreateNumber(resId); + if (!id) { + std::cerr << "Error: failed to create cJSON object for resource id." << std::endl; + return RESTOOL_ERROR; + } + cJSON_AddItemToObject(json, "id", id); + + cJSON *name = cJSON_CreateString(item.GetName().c_str()); + if (!name) { + std::cerr << "Error: failed to create cJSON object for resource name." << std::endl; + return RESTOOL_ERROR; + } + cJSON_AddItemToObject(json, "name", name); + + cJSON *type = cJSON_CreateString((ResourceUtil::ResTypeToString(item.GetResType())).c_str()); + if (!type) { + std::cerr << "Error: failed to create cJSON object for resource type." << std::endl; + return RESTOOL_ERROR; + } + cJSON_AddItemToObject(json, "type", type); + return RESTOOL_SUCCESS; +} + +uint32_t ResourceDump::AddResourceToJson(int64_t resId, const std::vector &items, cJSON *json) const +{ + if (items.empty()) { + std::cerr << "Error: reourceItem is empty." << std::endl; + return RESTOOL_ERROR; + } + if (!json) { + std::cerr << "Error: add Resource to null json object." << std::endl; + return RESTOOL_ERROR; + } + cJSON *resource = cJSON_CreateObject(); + if (!resource) { + std::cerr << "Error: failed to create cJSON object for resource item." << std::endl; + return RESTOOL_ERROR; + } + cJSON_AddItemToArray(json, resource); + if (AddItemCommonPropToJson(resId, items[0], resource) != RESTOOL_SUCCESS) { + return RESTOOL_ERROR; + }; + cJSON *entryCount = cJSON_CreateNumber(items.size()); + if (!entryCount) { + std::cerr << "Error: failed to create cJSON object for resource value count." << std::endl; + return RESTOOL_ERROR; + } + cJSON_AddItemToObject(resource, "entryCount", entryCount); + + cJSON *entryValues = cJSON_CreateArray(); + if (!resource) { + std::cerr << "Error: failed to create cJSON object for resource value array." << std::endl; + return RESTOOL_ERROR; + } + cJSON_AddItemToObject(resource, "entryValues", entryValues); + + for (const ResourceItem &item : items) { + cJSON *value = cJSON_CreateObject(); + if (!value) { + std::cerr << "Error: failed to create cJSON object for value item." << std::endl; + return RESTOOL_ERROR; + } + cJSON_AddItemToArray(entryValues, value); + if (AddValueToJson(item, value) != RESTOOL_SUCCESS) { + return RESTOOL_ERROR; + } + if (AddKeyParamsToJson(item.GetKeyParam(), value) != RESTOOL_SUCCESS) { + return RESTOOL_ERROR; + } + } + return RESTOOL_SUCCESS; +} + +uint32_t ResourceDump::AddPairVauleToJson(const ResourceItem &item, cJSON *json) const +{ + if (!json) { + std::cerr << "Error: add pair vaule to null json object." << std::endl; + return RESTOOL_ERROR; + } + cJSON *value = cJSON_CreateObject(); + if (!value) { + std::cerr << "Error: failed to create cJSON object for value object." << std::endl; + return RESTOOL_ERROR; + } + cJSON_AddItemToObject(json, "value", value); + const std::vector rawValues = item.SplitValue(); + uint32_t index = 1; + if (rawValues.size() % PAIR_SIZE != 0) { + cJSON *parent = cJSON_CreateString(rawValues[0].c_str()); + if (!parent) { + std::cerr << "Error: failed to create cJSON object for parent." << std::endl; + return RESTOOL_ERROR; + } + cJSON_AddItemToObject(json, "parent", parent); + index++; + } + for (; index < rawValues.size(); index += PAIR_SIZE) { + cJSON *item = cJSON_CreateString(rawValues[index].c_str()); + if (!item) { + std::cerr << "Error: failed to create cJSON object for value item." << std::endl; + return RESTOOL_ERROR; + } + cJSON_AddItemToObject(value, rawValues[index -1].c_str(), item); + } + return RESTOOL_SUCCESS; +} + +uint32_t ResourceDump::AddValueToJson(const ResourceItem &item, cJSON *json) const +{ + if (!json) { + std::cerr << "Error: add value to null json object." << std::endl; + return RESTOOL_ERROR; + } + if (item.IsArry()) { + cJSON *values = cJSON_CreateArray(); + if (!values) { + std::cerr << "Error: failed to create cJSON object for value array." << std::endl; + return RESTOOL_ERROR; + } + cJSON_AddItemToObject(json, "value", values); + const std::vector rawValues = item.SplitValue(); + for (const std::string &value : rawValues) { + cJSON *valueItem = cJSON_CreateString(value.c_str()); + if (!valueItem) { + std::cerr << "Error: failed to create cJSON object for value item." << std::endl; + return RESTOOL_ERROR; + } + cJSON_AddItemToArray(values, valueItem); + } + return RESTOOL_SUCCESS; + } + if (item.IsPair()) { + return AddPairVauleToJson(item, json); + } + cJSON *value = cJSON_CreateString(reinterpret_cast(item.GetData())); + if (!value) { + std::cerr << "Error: failed to create cJSON object for value." << std::endl; + return RESTOOL_ERROR; + } + cJSON_AddItemToObject(json, "value", value); + return RESTOOL_SUCCESS; +} + +} +} +} \ No newline at end of file diff --git a/src/resource_item.cpp b/src/resource_item.cpp index e45c1cd..190bdaf 100644 --- a/src/resource_item.cpp +++ b/src/resource_item.cpp @@ -14,7 +14,11 @@ */ #include "resource_item.h" +#include #include +#include +#include +#include "resource_data.h" #include "securec.h" namespace OHOS { @@ -120,6 +124,36 @@ const string &ResourceItem::GetLimitKey() const return limitKey_; } +bool ResourceItem::IsArry() const +{ + return type_ == ResType::STRARRAY || type_ == ResType::INTARRAY; +} + +bool ResourceItem::IsPair() const +{ + return type_ == ResType::THEME || type_ == ResType::PLURAL || type_ == ResType::PATTERN; +} + +const std::vector ResourceItem::SplitValue() const +{ + std::vector ret; + if (!(IsArry() || IsPair())) { + return ret; + } + char *buffer = reinterpret_cast(data_); + uint32_t index = 0; + while (index < dataLen_) { + uint16_t strLen = *reinterpret_cast(buffer + index); + index += sizeof(uint16_t); + if (index + strLen >= dataLen_) { + return ret; + } + ret.push_back(string(buffer+index, strLen)); + index = index + strLen + 1; + } + return ret; +} + ResourceItem &ResourceItem::operator=(const ResourceItem &other) { if (this == &other) { diff --git a/src/resource_merge.cpp b/src/resource_merge.cpp index 884e9fb..213330e 100644 --- a/src/resource_merge.cpp +++ b/src/resource_merge.cpp @@ -14,7 +14,7 @@ */ #include "resource_merge.h" -#include "cmd_parser.h" +#include "command_parser/package_parser.h" #include "file_entry.h" namespace OHOS { diff --git a/src/resource_pack.cpp b/src/resource_pack.cpp index 7a45297..0ea5866 100644 --- a/src/resource_pack.cpp +++ b/src/resource_pack.cpp @@ -33,16 +33,23 @@ ResourcePack::ResourcePack(const PackageParser &packageParser):packageParser_(pa { } -uint32_t ResourcePack::Package() +uint32_t ResourcePack::Run() { + uint32_t ret = RESTOOL_SUCCESS; if (!packageParser_.GetAppend().empty()) { - return PackAppend(); + ret = PackAppend(); + } else if (packageParser_.GetCombine()) { + ret = PackCombine(); + } else { + ret = PackNormal(); } - - if (packageParser_.GetCombine()) { - return PackCombine(); + if (ret == RESTOOL_SUCCESS) { + cout << "Info: restool resources compile success." << endl; + if (CompressionParser::GetCompressionParser()->GetMediaSwitch()) { + cout << CompressionParser::GetCompressionParser()->PrintTransMessage() << endl; + } } - return PackNormal(); + return ret; } uint32_t ResourcePack::InitCompression() diff --git a/src/resource_table.cpp b/src/resource_table.cpp index 856c5e5..b8c929e 100644 --- a/src/resource_table.cpp +++ b/src/resource_table.cpp @@ -15,7 +15,7 @@ #include "resource_table.h" #include -#include "cmd_parser.h" +#include "command_parser/package_parser.h" #include "file_entry.h" #include "file_manager.h" #include "resource_util.h" @@ -106,11 +106,21 @@ uint32_t ResourceTable::LoadResTable(const string path, map &in, map> &resInfos) +{ + if (!in) { + cerr << "Error: istream state is bad. stateCode: " << in.rdstate() << endl; + return RESTOOL_ERROR; + } in.seekg(0, ios::end); int64_t length = in.tellg(); if (length <= 0) { - in.close(); return RESTOOL_ERROR; } in.seekg(0, ios::beg); @@ -118,19 +128,16 @@ uint32_t ResourceTable::LoadResTable(const string path, map(length))) { - in.close(); return RESTOOL_ERROR; } map> limitKeys; if (!ReadLimitKeys(in, limitKeys, indexHeader.limitKeyConfigSize, pos, static_cast(length))) { - in.close(); return RESTOOL_ERROR; } map> datas; if (!ReadIdTables(in, datas, indexHeader.limitKeyConfigSize, pos, static_cast(length))) { - in.close(); return RESTOOL_ERROR; } @@ -138,11 +145,9 @@ uint32_t ResourceTable::LoadResTable(const string path, map(length)) || !ReadDataRecordStart(in, record, limitKeys, datas, resInfos)) { - in.close(); return RESTOOL_ERROR; } } - in.close(); return RESTOOL_SUCCESS; } @@ -347,7 +352,7 @@ void ResourceTable::SaveIdSets(const map &idSets, ostringstream & } } -bool ResourceTable::ReadFileHeader(ifstream &in, IndexHeader &indexHeader, uint64_t &pos, uint64_t length) const +bool ResourceTable::ReadFileHeader(basic_istream &in, IndexHeader &indexHeader, uint64_t &pos, uint64_t length) { pos += sizeof(indexHeader); if (pos > length) { @@ -360,8 +365,8 @@ bool ResourceTable::ReadFileHeader(ifstream &in, IndexHeader &indexHeader, uint6 return true; } -bool ResourceTable::ReadLimitKeys(ifstream &in, map> &limitKeys, - uint32_t count, uint64_t &pos, uint64_t length) const +bool ResourceTable::ReadLimitKeys(basic_istream &in, map> &limitKeys, + uint32_t count, uint64_t &pos, uint64_t length) { for (uint32_t i = 0; i< count; i++) { pos = pos + TAG_LEN + INT_TO_BYTES + INT_TO_BYTES; @@ -396,8 +401,8 @@ bool ResourceTable::ReadLimitKeys(ifstream &in, map> & return true; } -bool ResourceTable::ReadIdTables(std::ifstream &in, std::map> &datas, - uint32_t count, uint64_t &pos, uint64_t length) const +bool ResourceTable::ReadIdTables(basic_istream &in, std::map> &datas, + uint32_t count, uint64_t &pos, uint64_t length) { for (uint32_t i = 0; i< count; i++) { pos = pos + TAG_LEN + INT_TO_BYTES; @@ -430,7 +435,7 @@ bool ResourceTable::ReadIdTables(std::ifstream &in, std::map &in, RecordItem &record, uint64_t &pos, uint64_t length) { pos = pos + INT_TO_BYTES; if (pos > length) { @@ -448,10 +453,10 @@ bool ResourceTable::ReadDataRecordPrepare(ifstream &in, RecordItem &record, uint return true; } -bool ResourceTable::ReadDataRecordStart(std::ifstream &in, RecordItem &record, +bool ResourceTable::ReadDataRecordStart(basic_istream &in, RecordItem &record, const std::map> &limitKeys, const std::map> &datas, - std::map> &resInfos) const + std::map> &resInfos) { int64_t offset = in.tellg(); offset = offset - INT_TO_BYTES - INT_TO_BYTES - INT_TO_BYTES; diff --git a/src/resource_util.cpp b/src/resource_util.cpp index 1a59c9e..43c05cd 100644 --- a/src/resource_util.cpp +++ b/src/resource_util.cpp @@ -23,6 +23,7 @@ #include #include #include "file_entry.h" +#include "resource_data.h" namespace OHOS { namespace Global { @@ -328,6 +329,10 @@ string ResourceUtil::GetKeyParamValue(const KeyParam &KeyParam) case KeyType::REGION: val = GetLocaleLimitkey(KeyParam); break; + case KeyType::INPUTDEVICE: + val = KeyParam.value == static_cast(InputDevice::INPUTDEVICE_NOT_SET) ? + "not set" : "pointDevice"; + break; default: val = to_string(KeyParam.value); break; @@ -471,6 +476,18 @@ void ResourceUtil::PrintWarningMsg(vector> &noBaseResource cerr << " of '" << item.second << "' does not have a base resource." << endl; } } + +string ResourceUtil::KeyTypeToStr(KeyType type) +{ + string ret("unknown type: "); + auto it = g_keyTypeToStrMap.find(type); + if (it != g_keyTypeToStrMap.end()) { + ret = it->second; + } else { + ret += to_string(static_cast(type)); + } + return ret; +} } } } diff --git a/src/restool.cpp b/src/restool.cpp index e26fdbb..7d058d6 100644 --- a/src/restool.cpp +++ b/src/restool.cpp @@ -13,19 +13,15 @@ * limitations under the License. */ -#include "cmd_parser.h" -#include "task_handle.h" -#include "compression_parser.h" +#include "command_parser/dump_parser.h" +#include "command_parser/package_parser.h" +#include +#include using namespace std; using namespace OHOS::Global::Restool; -namespace { -uint32_t ProccssHap(PackageParser &packageParser) -{ - TaskHandle taskHandle; - return taskHandle.HandlePackage(packageParser); -} -} + +constexpr int PARAM_NUM = 2; int main(int argc, char *argv[]) { @@ -33,19 +29,24 @@ int main(int argc, char *argv[]) cerr << "Error: argv null" << endl; return RESTOOL_ERROR; } - auto &parser = CmdParser::GetInstance(); - if (parser.Parse(argc, argv) != RESTOOL_SUCCESS) { - parser.ShowUseage(); + if (argc < PARAM_NUM) { + cerr << "Error: At least 1 parameters are required, but no parameter is passed in." << endl; return RESTOOL_ERROR; } - auto &packageParser = parser.GetCmdParser(); - if (ProccssHap(packageParser) != RESTOOL_SUCCESS) { - return RESTOOL_ERROR; + if (strcasecmp(argv[1], "dump") == 0) { + auto &dumpParser = CmdParser::GetInstance(); + if (dumpParser.Parse(argc, argv) != RESTOOL_SUCCESS) { + dumpParser.ShowUseage(); + return RESTOOL_ERROR; + } + return dumpParser.ExecCommand(); } - cout << "Info: restool resources compile success." << endl; - if (CompressionParser::GetCompressionParser()->GetMediaSwitch()) { - cout << CompressionParser::GetCompressionParser()->PrintTransMessage() << endl; + + auto &packParser = CmdParser::GetInstance(); + if (packParser.Parse(argc, argv) != RESTOOL_SUCCESS) { + packParser.ShowUseage(); + return RESTOOL_ERROR; } - return RESTOOL_SUCCESS; + return packParser.ExecCommand(); } diff --git a/src/select_compile_parse.cpp b/src/select_compile_parse.cpp index 6f5e81d..c23a1a1 100644 --- a/src/select_compile_parse.cpp +++ b/src/select_compile_parse.cpp @@ -16,7 +16,7 @@ #include "select_compile_parse.h" #include #include "key_parser.h" -#include "cmd_parser.h" +#include "command_parser/package_parser.h" #include "resource_util.h" namespace OHOS { -- Gitee