add restool dump command

Signed-off-by: sunjie <sunjie69@huawei.com>
Change-Id: Ifd10f76345a87a40ac73f82486cdce54a4e94a53
This commit is contained in:
sunjie
2024-12-13 17:01:42 +08:00
parent 838d5748c1
commit 261afcfcff
34 changed files with 1003 additions and 267 deletions
+6 -3
View File
@@ -19,11 +19,10 @@ ohos_executable("restool") {
sources = [
"src/append_compiler.cpp",
"src/binary_file_packer.cpp",
"src/cmd_parser.cpp",
"src/cmd/dump_parser.cpp",
"src/cmd/package_parser.cpp",
"src/compression_parser.cpp",
"src/config_parser.cpp",
"src/factory_resource_compiler.cpp",
"src/factory_resource_packer.cpp",
"src/file_entry.cpp",
"src/file_manager.cpp",
"src/generic_compiler.cpp",
@@ -39,12 +38,15 @@ ohos_executable("restool") {
"src/resconfig_parser.cpp",
"src/resource_append.cpp",
"src/resource_check.cpp",
"src/resource_compiler_factory.cpp",
"src/resource_directory.cpp",
"src/resource_dumper.cpp",
"src/resource_item.cpp",
"src/resource_merge.cpp",
"src/resource_module.cpp",
"src/resource_overlap.cpp",
"src/resource_pack.cpp",
"src/resource_packer_factory.cpp",
"src/resource_table.cpp",
"src/resource_util.cpp",
"src/restool.cpp",
@@ -62,6 +64,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" ]
+2 -1
View File
@@ -22,7 +22,8 @@
"third_party": [
"bounds_checking_function",
"cJSON",
"libpng"
"libpng",
"zlib"
]
},
"build": {
+1 -1
View File
@@ -16,7 +16,7 @@
#ifndef OHOS_RESTOOL_BINARY_FILE_PACKER_H
#define OHOS_RESTOOL_BINARY_FILE_PACKER_H
#include "cmd_parser.h"
#include "cmd/package_parser.h"
#include "resource_util.h"
#include "thread_pool.h"
+102
View File
@@ -0,0 +1,102 @@
/*
* Copyright (c) 2024 - 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 <cstdint>
#include <memory>
#include <iostream>
#include <string>
#include <type_traits>
#include "singleton.h"
#include "restool_errors.h"
namespace OHOS {
namespace Global {
namespace Restool {
class ICmdParser {
public:
virtual uint32_t Parse(int argc, char *argv[]) = 0;
virtual uint32_t ExecCommand() = 0;
};
template<class T>
class CmdParser : public Singleton<CmdParser<T>> {
static_assert(std::is_base_of_v<ICmdParser, T>, "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<class T>
void CmdParser<T>::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<class T>
T &CmdParser<T>::GetCmdParser()
{
return cmdParser_;
}
template<class T>
uint32_t CmdParser<T>::Parse(int argc, char *argv[])
{
return cmdParser_.Parse(argc, argv);
}
template<class T>
uint32_t CmdParser<T>::ExecCommand()
{
return cmdParser_.ExecCommand();
};
}
}
}
#endif
+40
View File
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2024 - 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_DUMP_PARSER_H
#define OHOS_RESTOOL_DUMP_PARSER_H
#include "cmd_parser.h"
#include <memory>
#include <string>
namespace OHOS {
namespace Global {
namespace Restool {
class DumpParser : public ICmdParser {
public:
virtual ~DumpParser() = default;
uint32_t Parse(int argc, char *argv[]) override;
uint32_t ExecCommand() override;
const std::string &GetInputPath() const;
private:
std::string inputPath_;
std::string type_;
};
} // namespace Restool
} // namespace Global
} // namespace OHOS
#endif
+112
View File
@@ -0,0 +1,112 @@
/*
* Copyright (c) 2024 - 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_PACKAGE_PARSER_H
#define OHOS_RESTOOL_PACKAGE_PARSER_H
#include <functional>
#include <getopt.h>
#include "cmd_parser.h"
namespace OHOS {
namespace Global {
namespace Restool {
class PackageParser : public ICmdParser {
public:
PackageParser(){};
virtual ~PackageParser() = default;
uint32_t Parse(int argc, char *argv[]) override;
uint32_t ExecCommand() override;
const std::vector<std::string> &GetInputs() const;
const std::string &GetPackageName() const;
const std::string &GetOutput() const;
const std::vector<std::string> &GetResourceHeaders() const;
bool GetForceWrite() const;
const std::vector<std::string> &GetModuleNames() const;
const std::string &GetConfig() const;
const std::string &GetRestoolPath() const;
uint32_t GetStartId() const;
bool IsFileList() const;
const std::vector<std::string> &GetAppend() const;
bool GetCombine() const;
const std::string &GetDependEntry() const;
const std::string &GetIdDefinedOutput() const;
const std::string &GetIdDefinedInputPath() const;
bool GetIconCheck() const;
const TargetConfig &GetTargetConfigValues() const;
bool IsTargetConfig() const;
const std::vector<std::string> &GetSysIdDefinedPaths() const;
const std::string &GetCompressionPath() const;
bool IsOverlap() const;
private:
void InitCommand();
uint32_t ParseCommand(int argc, char *argv[]);
uint32_t CheckError(int argc, char *argv[], int c, int optIndex);
uint32_t AddInput(const std::string &argValue);
uint32_t AddPackageName(const std::string &argValue);
uint32_t AddOutput(const std::string &argValue);
uint32_t AddResourceHeader(const std::string &argValue);
uint32_t ForceWrite();
uint32_t PrintVersion();
uint32_t AddMoudleNames(const std::string &argValue);
uint32_t AddConfig(const std::string &argValue);
uint32_t AddStartId(const std::string &argValue);
void AdaptResourcesDirForInput();
uint32_t CheckParam() const;
uint32_t HandleProcess(int c, const std::string &argValue);
uint32_t ParseFileList(const std::string &fileListPath);
uint32_t AddAppend(const std::string &argValue);
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);
uint32_t AddSysIdDefined(const std::string &argValue);
bool IsAscii(const std::string &argValue) const;
bool IsLongOpt(int argc, char *argv[]) const;
uint32_t IconCheck();
uint32_t ParseTargetConfig(const std::string &argValue);
uint32_t AddCompressionPath(const std::string &argValue);
static const struct option CMD_OPTS[];
static const std::string CMD_PARAMS;
using HandleArgValue = std::function<uint32_t(const std::string &)>;
std::map<int32_t, HandleArgValue> handles_;
std::vector<std::string> inputs_;
std::string packageName_;
std::string output_;
std::vector<std::string> resourceHeaderPaths_;
bool forceWrite_ = false;
std::vector<std::string> moduleNames_;
std::string configPath_;
std::string restoolPath_;
uint32_t startId_ = 0;
bool isFileList_ = false;
std::vector<std::string> append_;
bool combine_ = false;
std::string dependEntry_;
std::string idDefinedOutput_;
std::string idDefinedInputPath_;
bool isIconCheck_ = false;
TargetConfig targetConfig_;
bool isTtargetConfig_;
std::vector<std::string> sysIdDefinedPaths_;
std::string compressionPath_;
};
} // namespace Restool
} // namespace Global
} // namespace OHOS
#endif
-177
View File
@@ -1,177 +0,0 @@
/*
* Copyright (c) 2021 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 <getopt.h>
#include <iostream>
#include <functional>
#include <vector>
#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() {};
virtual ~PackageParser() = default;
uint32_t Parse(int argc, char *argv[]) override;
const std::vector<std::string> &GetInputs() const;
const std::string &GetPackageName() const;
const std::string &GetOutput() const;
const std::vector<std::string> &GetResourceHeaders() const;
bool GetForceWrite() const;
const std::vector<std::string> &GetModuleNames() const;
const std::string &GetConfig() const;
const std::string &GetRestoolPath() const;
uint32_t GetStartId() const;
bool IsFileList() const;
const std::vector<std::string> &GetAppend() const;
bool GetCombine() const;
const std::string &GetDependEntry() const;
const std::string &GetIdDefinedOutput() const;
const std::string &GetIdDefinedInputPath() const;
bool GetIconCheck() const;
const TargetConfig &GetTargetConfigValues() const;
bool IsTargetConfig() const;
const std::vector<std::string> &GetSysIdDefinedPaths() const;
const std::string &GetCompressionPath() const;
bool IsOverlap() const;
private:
void InitCommand();
uint32_t ParseCommand(int argc, char *argv[]);
uint32_t CheckError(int argc, char *argv[], int c, int optIndex);
uint32_t AddInput(const std::string& argValue);
uint32_t AddPackageName(const std::string& argValue);
uint32_t AddOutput(const std::string& argValue);
uint32_t AddResourceHeader(const std::string& argValue);
uint32_t ForceWrite();
uint32_t PrintVersion();
uint32_t AddMoudleNames(const std::string& argValue);
uint32_t AddConfig(const std::string& argValue);
uint32_t AddStartId(const std::string& argValue);
void AdaptResourcesDirForInput();
uint32_t CheckParam() const;
uint32_t HandleProcess(int c, const std::string& argValue);
uint32_t ParseFileList(const std::string& fileListPath);
uint32_t AddAppend(const std::string& argValue);
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);
uint32_t AddSysIdDefined(const std::string& argValue);
bool IsAscii(const std::string& argValue) const;
bool IsLongOpt(int argc, char *argv[]) const;
uint32_t IconCheck();
uint32_t ParseTargetConfig(const std::string& argValue);
uint32_t AddCompressionPath(const std::string& argValue);
static const struct option CMD_OPTS[];
static const std::string CMD_PARAMS;
using HandleArgValue = std::function<uint32_t(const std::string&)>;
std::map<int32_t, HandleArgValue> handles_;
std::vector<std::string> inputs_;
std::string packageName_;
std::string output_;
std::vector<std::string> resourceHeaderPaths_;
bool forceWrite_ = false;
std::vector<std::string> moduleNames_;
std::string configPath_;
std::string restoolPath_;
uint32_t startId_ = 0;
bool isFileList_ = false;
std::vector<std::string> append_;
bool combine_ = false;
std::string dependEntry_;
std::string idDefinedOutput_;
std::string idDefinedInputPath_;
bool isIconCheck_ = false;
TargetConfig targetConfig_;
bool isTtargetConfig_;
std::vector<std::string> sysIdDefinedPaths_;
std::string compressionPath_;
};
template<class T>
class CmdParser : public Singleton<CmdParser<T>> {
public:
T &GetCmdParser();
uint32_t Parse(int argc, char *argv[]);
static void ShowUseage();
private:
T cmdParser_;
};
template<class T>
void CmdParser<T>::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<class T>
T &CmdParser<T>::GetCmdParser()
{
return cmdParser_;
}
template<class T>
uint32_t CmdParser<T>::Parse(int argc, char *argv[])
{
if (cmdParser_.Parse(argc, argv) != RESTOOL_SUCCESS) {
return RESTOOL_ERROR;
}
return RESTOOL_SUCCESS;
}
}
}
}
#endif
+1 -1
View File
@@ -18,7 +18,7 @@
#include <string>
#include <cJSON.h>
#include "cmd_parser.h"
#include "cmd/package_parser.h"
namespace OHOS {
namespace Global {
+2 -2
View File
@@ -17,8 +17,8 @@
#define OHOS_RESTOOL_RESOURCE_APPEND_H
#include <fstream>
#include "cmd_parser.h"
#include "factory_resource_compiler.h"
#include "cmd/package_parser.h"
#include "resource_compiler_factory.h"
#include "file_entry.h"
namespace OHOS {
@@ -22,7 +22,7 @@
namespace OHOS {
namespace Global {
namespace Restool {
class FactoryResourceCompiler {
class ResourceCompilerFactory {
public:
static std::unique_ptr<IResourceCompiler> CreateCompiler(ResType type, const std::string &output, bool isHap);
static std::unique_ptr<IResourceCompiler> CreateCompilerForAppend(ResType type, const std::string &output);
+14 -1
View File
@@ -49,7 +49,7 @@ const static std::string SOLUTIONS_ARROW = "> ";
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.1.0.002" };
static const int8_t RESTOOL_VERSION[VERSION_MAX_LEN] = { "Restool 5.1.0.003" };
const static int32_t TAG_LEN = 4;
static std::set<std::string> g_resourceSet;
static std::set<std::string> g_hapResourceSet;
@@ -251,6 +251,19 @@ const std::map<std::string, ResType> g_contentClusterMap = {
{ "symbol", ResType::SYMBOL }
};
const std::map<KeyType, std::string> g_keyTypeToStrMap = {
{KeyType::MCC, "mcc"},
{KeyType::MNC, "mnc"},
{KeyType::LANGUAGE, "language"},
{KeyType::SCRIPT, "script"},
{KeyType::REGION, "region"},
{KeyType::ORIENTATION, "orientation"},
{KeyType::RESOLUTION, "density"},
{KeyType::DEVICETYPE, "device"},
{KeyType::NIGHTMODE, "colorMode"},
{KeyType::INPUTDEVICE, "inputDevice"},
};
const std::map<int32_t, ResType> g_resTypeMap = {
{ static_cast<int32_t>(ResType::ELEMENT), ResType::ELEMENT},
{ static_cast<int32_t>(ResType::RAW), ResType::RAW},
+81
View File
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2024 - 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 <cstdint>
#include <functional>
#include <map>
#include <memory>
#include <set>
#include <vector>
#include "config_parser.h"
#include "unzip.h"
#include "cJSON.h"
#include "cmd/dump_parser.h"
#include "resource_data.h"
#include "resource_item.h"
namespace OHOS {
namespace Global {
namespace Restool {
class ResourceDumper {
public:
virtual ~ResourceDumper() = default;
virtual uint32_t Dump(const DumpParser &parser);
protected:
virtual uint32_t DumpRes(std::string &out) const = 0;
void ReadHapInfo(const std::unique_ptr<char[]> &buffer, size_t len);
uint32_t LoadHap();
uint32_t ReadFileFromZip(unzFile &zip, const char *fileName, std::unique_ptr<char[]> &buffer, size_t &len);
std::string inputPath_;
std::string bundleName_;
std::string moduleName_;
std::map<int64_t, std::vector<ResourceItem>> resInfos_;
};
class ConfigDumper : public ResourceDumper {
public:
virtual ~ConfigDumper() = default;
uint32_t DumpRes(std::string &out) const override;
};
class CommonDumper : public ResourceDumper {
public:
virtual ~CommonDumper() = default;
uint32_t DumpRes(std::string &out) const override;
private:
uint32_t AddValueToJson(const ResourceItem &item, cJSON *json) const;
uint32_t AddPairVauleToJson(const ResourceItem &item, cJSON *json) const;
uint32_t AddKeyParamsToJson(const std::vector<KeyParam> &keyParams, cJSON *json) const;
uint32_t AddResourceToJson(int64_t id, const std::vector<ResourceItem> &items, cJSON *json) const;
uint32_t AddItemCommonPropToJson(int32_t resId, const ResourceItem &item, cJSON* json) const;
};
class ResourceDumperFactory {
public:
static std::unique_ptr<ResourceDumper> CreateResourceDumper(const std::string &type);
static const std::set<std::string> GetSupportDumpType();
};
} // namespace Restool
} // namespace Global
} // namespace OHOS
#endif
+3
View File
@@ -44,6 +44,9 @@ public:
const std::string &GetFilePath() const;
const std::string &GetLimitKey() const;
bool IsCoverable() const;
const std::vector<std::string> SplitValue() const;
bool IsArray() const;
bool IsPair() const;
ResourceItem &operator=(const ResourceItem &other);
private:
+1 -1
View File
@@ -20,7 +20,7 @@
#include <vector>
#include "config_parser.h"
#include "restool_errors.h"
#include "cmd_parser.h"
#include "cmd/package_parser.h"
namespace OHOS {
namespace Global {
+1 -1
View File
@@ -16,7 +16,7 @@
#ifndef OHOS_RESTOOL_RESOURCE_OVERLAP_H
#define OHOS_RESTOOL_RESOURCE_OVERLAP_H
#include "cmd_parser.h"
#include "cmd/package_parser.h"
#include "resource_pack.h"
namespace OHOS {
+2 -1
View File
@@ -16,7 +16,6 @@
#ifndef OHOS_RESTOOL_RESOURCE_PACK_H
#define OHOS_RESTOOL_RESOURCE_PACK_H
#include "cmd_parser.h"
#include "config_parser.h"
#include "resource_append.h"
#include "resource_data.h"
@@ -65,6 +64,8 @@ private:
void CheckConfigJson();
void CheckConfigJsonForCombine(ResourceAppend &resourceAppend);
bool CopyIcon(std::string &dataPath, const std::string &idName, std::string &fileName) const;
void ShowPackSuccess();
using HeaderCreater = std::function<uint32_t(const std::string&)>;
std::map<std::string, HeaderCreater> headerCreaters_;
PackType packType_ = PackType::NORMAL;
@@ -22,7 +22,7 @@
namespace OHOS {
namespace Global {
namespace Restool {
class FactoryResourcePacker {
class ResourcePackerFactory {
public:
static std::unique_ptr<ResourcePack> CreatePacker(PackType type, const PackageParser &packageParser);
};
+10 -9
View File
@@ -31,7 +31,8 @@ public:
virtual ~ResourceTable();
uint32_t CreateResourceTable();
uint32_t CreateResourceTable(const std::map<int64_t, std::vector<std::shared_ptr<ResourceItem>>> &items);
uint32_t LoadResTable(const std::string path, std::map<int64_t, std::vector<ResourceItem>> &resInfos);
static uint32_t LoadResTable(const std::string path, std::map<int64_t, std::vector<ResourceItem>> &resInfos);
static uint32_t LoadResTable(std::basic_istream<char> &in, std::map<int64_t, std::vector<ResourceItem>> &resInfos);
private:
struct TableData {
uint32_t id;
@@ -74,16 +75,16 @@ private:
void SaveLimitKeyConfigs(const std::map<std::string, LimitKeyConfig> &limitKeyConfigs,
std::ostringstream &out) const;
void SaveIdSets(const std::map<std::string, IdSet> &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<int64_t, std::vector<KeyParam>> &limitKeys,
uint32_t count, uint64_t &pos, uint64_t length) const;
bool ReadIdTables(std::ifstream &in, std::map<int64_t, std::pair<int64_t, int64_t>> &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<char> &in, IndexHeader &indexHeader, uint64_t &pos, uint64_t length);
static bool ReadLimitKeys(std::basic_istream<char> &in, std::map<int64_t, std::vector<KeyParam>> &limitKeys,
uint32_t count, uint64_t &pos, uint64_t length);
static bool ReadIdTables(std::basic_istream<char> &in, std::map<int64_t, std::pair<int64_t, int64_t>> &datas,
uint32_t count, uint64_t &pos, uint64_t length);
static bool ReadDataRecordPrepare(std::basic_istream<char> &in, RecordItem &record, uint64_t &pos, uint64_t length);
static bool ReadDataRecordStart(std::basic_istream<char> &in, RecordItem &record,
const std::map<int64_t, std::vector<KeyParam>> &limitKeys,
const std::map<int64_t, std::pair<int64_t, int64_t>> &datas,
std::map<int64_t, std::vector<ResourceItem>> &resInfos) const;
std::map<int64_t, std::vector<ResourceItem>> &resInfos);
std::string indexFilePath_;
std::string idDefinedPath_;
};
+13 -1
View File
@@ -247,6 +247,19 @@ public:
*/
static void PrintWarningMsg(std::vector<std::pair<ResType, std::string>> &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);
};
}
}
+65
View File
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2024 - 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 "cmd/dump_parser.h"
#include "resource_util.h"
#include "resource_dumper.h"
namespace OHOS {
namespace Global {
namespace Restool {
using namespace std;
uint32_t DumpParser::Parse(int argc, char *argv[])
{
// 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;
}
const set<string> supportedDumpType = ResourceDumperFactory::GetSupportDumpType();
if (supportedDumpType.count(argv[currentIndex]) != 0) {
type_ = argv[currentIndex];
currentIndex++;
}
if (currentIndex >= argc) {
cerr << "Error: missing input Hap path." << endl;
return RESTOOL_ERROR;
}
inputPath_ = ResourceUtil::RealPath(argv[currentIndex]);
if (inputPath_.empty()) {
cerr << "Error: invalid input path: '" << argv[currentIndex] << "'" << endl;
return RESTOOL_ERROR;
}
return RESTOOL_SUCCESS;
}
const string& DumpParser::GetInputPath() const
{
return inputPath_;
}
uint32_t DumpParser::ExecCommand()
{
auto resourceDump = ResourceDumperFactory::CreateResourceDumper(type_);
if (resourceDump) {
return resourceDump->Dump(*this);
}
cerr << "Error: not support dump type '" << type_ << "'" << endl;
return RESTOOL_ERROR;
}
} // namespace Restool
} // namespace Global
} // namespace OHOS
@@ -13,12 +13,13 @@
* limitations under the License.
*/
#include "cmd_parser.h"
#include "cmd/package_parser.h"
#include <algorithm>
#include "resconfig_parser.h"
#include "resource_util.h"
#include "select_compile_parse.h"
#include "file_entry.h"
#include "resource_pack.h"
namespace OHOS {
namespace Global {
@@ -469,7 +470,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(); });
@@ -543,7 +544,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) {
@@ -584,6 +585,11 @@ bool PackageParser::IsLongOpt(int argc, char *argv[]) const
}
return false;
}
uint32_t PackageParser::ExecCommand()
{
return ResourcePack(*this).Package();
}
}
}
}
+1 -1
View File
@@ -17,7 +17,7 @@
#include <algorithm>
#include "compression_parser.h"
#include <iostream>
#include "factory_resource_compiler.h"
#include "resource_compiler_factory.h"
#include "file_entry.h"
#include "key_parser.h"
#include "reference_parser.h"
-1
View File
@@ -16,7 +16,6 @@
#include "id_worker.h"
#include <iostream>
#include <regex>
#include "cmd_parser.h"
namespace OHOS {
namespace Global {
+1 -1
View File
@@ -267,7 +267,7 @@ bool ResourceAppend::ScanFile(const FileInfo &fileInfo, const string &outputPath
}
unique_ptr<IResourceCompiler> resourceCompiler =
FactoryResourceCompiler::CreateCompilerForAppend(fileInfo.dirType, outputPath);
ResourceCompilerFactory::CreateCompilerForAppend(fileInfo.dirType, outputPath);
if (resourceCompiler == nullptr) {
return true;
}
@@ -13,7 +13,7 @@
* limitations under the License.
*/
#include "factory_resource_compiler.h"
#include "resource_compiler_factory.h"
#include "append_compiler.h"
#include "generic_compiler.h"
#include "json_compiler.h"
@@ -23,7 +23,7 @@ namespace OHOS {
namespace Global {
namespace Restool {
using namespace std;
unique_ptr<IResourceCompiler> FactoryResourceCompiler::CreateCompiler(ResType type,
unique_ptr<IResourceCompiler> ResourceCompilerFactory::CreateCompiler(ResType type,
const string &output, bool isOverlap)
{
if (type == ResType::ELEMENT) {
@@ -35,7 +35,7 @@ unique_ptr<IResourceCompiler> FactoryResourceCompiler::CreateCompiler(ResType ty
}
}
unique_ptr<IResourceCompiler> FactoryResourceCompiler::CreateCompilerForAppend(ResType type, const string &output)
unique_ptr<IResourceCompiler> ResourceCompilerFactory::CreateCompilerForAppend(ResType type, const string &output)
{
if (type == ResType::ELEMENT) {
return make_unique<JsonCompiler>(type, output);
+408
View File
@@ -0,0 +1,408 @@
/*
* Copyright (c) 2024 - 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_dumper.h"
#include <cstdint>
#include <iostream>
#include <memory>
#include <ostream>
#include <set>
#include <sstream>
#include <functional>
#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;
static std::map<std::string, std::function<std::unique_ptr<ResourceDumper>()>> dumpMap_ = {
{"config", std::make_unique<ConfigDumper>}
};
uint32_t ResourceDumper::Dump(const DumpParser &packageParser)
{
inputPath_ = packageParser.GetInputPath();
if (LoadHap() != RESTOOL_SUCCESS) {
return RESTOOL_ERROR;
}
std::string jsonStr;
if (DumpRes(jsonStr) != RESTOOL_SUCCESS) {
return RESTOOL_ERROR;
}
std::cout << jsonStr << std::endl;
return RESTOOL_SUCCESS;
}
uint32_t ResourceDumper::LoadHap()
{
unzFile zipFile = unzOpen64(inputPath_.c_str());
if (!zipFile) {
std::cerr << "Error: Open file is failed. filename: " << inputPath_ << std::endl;
return RESTOOL_ERROR;
}
std::unique_ptr<char[]> 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 ResourceDumper::ReadHapInfo(const std::unique_ptr<char[]> &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 ResourceDumper::ReadFileFromZip(
unzFile &zipFile, const char *fileName, std::unique_ptr<char[]> &buffer, size_t &len)
{
unz_file_info fileInfo;
if (unzLocateFile2(zipFile, fileName, 1) != UNZ_OK) {
std::cerr << "Error: Not found filename: " << fileName << " in " << inputPath_ << 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<char[]>(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 <<std::endl;
return RESTOOL_ERROR;
}
err = unzReadCurrentFile(zipFile, buffer.get(), len);
if (err < 0) {
std::cerr << "Error: in unzReadCurrentFile .Errorcode: " << err << std::endl;
return RESTOOL_ERROR;
}
return RESTOOL_SUCCESS;
}
uint32_t CommonDumper::DumpRes(std::string &out) const
{
std::unique_ptr<cJSON, std::function<void(cJSON*)>> 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 CommonDumper::AddKeyParamsToJson(const std::vector<KeyParam> &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 CommonDumper::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 CommonDumper::AddResourceToJson(int64_t resId, const std::vector<ResourceItem> &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 CommonDumper::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<std::string> 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 CommonDumper::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.IsArray()) {
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<std::string> 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);
}
std::string rawValue = std::string(reinterpret_cast<const char*>(item.GetData()), item.GetDataLength());
cJSON *value = cJSON_CreateString(rawValue.c_str());
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;
}
uint32_t ConfigDumper::DumpRes(std::string &out) const
{
std::unique_ptr<cJSON, std::function<void(cJSON*)>> 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<std::string> 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;
}
std::unique_ptr<ResourceDumper> ResourceDumperFactory::CreateResourceDumper(const std::string &type)
{
auto it = dumpMap_.find(type);
if (it != dumpMap_.end()) {
return it->second();
}
return std::make_unique<CommonDumper>();
}
const std::set<std::string> ResourceDumperFactory::GetSupportDumpType()
{
std::set<std::string> dumpType;
for (auto &it : dumpMap_) {
dumpType.insert(it.first);
}
return dumpType;
}
}
}
}
+30
View File
@@ -130,6 +130,36 @@ bool ResourceItem::IsCoverable() const
return coverable_;
}
bool ResourceItem::IsArray() 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<std::string> ResourceItem::SplitValue() const
{
std::vector<std::string> ret;
if (!(IsArray() || IsPair())) {
return ret;
}
char *buffer = reinterpret_cast<char*>(data_);
uint32_t index = 0;
while (index < dataLen_) {
uint16_t strLen = *reinterpret_cast<uint16_t*>(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) {
+2 -2
View File
@@ -16,7 +16,7 @@
#include "resource_module.h"
#include <algorithm>
#include <iostream>
#include "factory_resource_compiler.h"
#include "resource_compiler_factory.h"
#include "restool_errors.h"
namespace OHOS {
@@ -54,7 +54,7 @@ uint32_t ResourceModule::ScanResource(bool isHap)
}
unique_ptr<IResourceCompiler> resourceCompiler =
FactoryResourceCompiler::CreateCompiler(type, moduleOutput_, isHap);
ResourceCompilerFactory::CreateCompiler(type, moduleOutput_, isHap);
resourceCompiler->SetModuleName(moduleName_);
if (resourceCompiler->Compile(item->second) != RESTOOL_SUCCESS) {
return RESTOOL_ERROR;
+25 -15
View File
@@ -15,6 +15,7 @@
#include "resource_pack.h"
#include <algorithm>
#include <cstdint>
#include <iomanip>
#include "file_entry.h"
#include "file_manager.h"
@@ -24,33 +25,34 @@
#include "resource_table.h"
#include "compression_parser.h"
#include "binary_file_packer.h"
#include "factory_resource_packer.h"
#include "resource_packer_factory.h"
namespace OHOS {
namespace Global {
namespace Restool {
using namespace std;
ResourcePack::ResourcePack(const PackageParser &packageParser):packageParser_(packageParser)
{
}
{}
uint32_t ResourcePack::Package()
{
uint32_t errorCode = RESTOOL_SUCCESS;
if (!packageParser_.GetAppend().empty()) {
return PackAppend();
errorCode = PackAppend();
} else if (packageParser_.GetCombine()) {
errorCode = PackCombine();
} else {
if (packageParser_.IsOverlap()) {
packType_ = PackType::OVERLAP;
}
unique_ptr<ResourcePack> resourcePacker =
ResourcePackerFactory::CreatePacker(packType_, packageParser_);
errorCode = resourcePacker->Pack();
}
if (packageParser_.GetCombine()) {
return PackCombine();
if (errorCode == RESTOOL_SUCCESS) {
ShowPackSuccess();
}
if (packageParser_.IsOverlap()) {
packType_ = PackType::OVERLAP;
}
unique_ptr<ResourcePack> resourcePacker =
FactoryResourcePacker::CreatePacker(packType_, packageParser_);
return resourcePacker->Pack();
return errorCode;
}
uint32_t ResourcePack::InitCompression()
@@ -578,6 +580,14 @@ void ResourcePack::CheckConfigJsonForCombine(ResourceAppend &resourceAppend)
ResourceCheck resourceCheck(configJson_.GetCheckNode(), make_shared<ResourceAppend>(resourceAppend));
resourceCheck.CheckConfigJsonForCombine();
}
void ResourcePack::ShowPackSuccess()
{
cout << "Info: restool resources compile success." << endl;
if (CompressionParser::GetCompressionParser()->GetMediaSwitch()) {
cout << CompressionParser::GetCompressionParser()->PrintTransMessage() << endl;
}
}
}
}
}
@@ -13,7 +13,7 @@
* limitations under the License.
*/
#include "factory_resource_packer.h"
#include "resource_packer_factory.h"
#include "resource_overlap.h"
namespace OHOS {
@@ -21,14 +21,14 @@ namespace Global {
namespace Restool {
using namespace std;
unique_ptr<ResourcePack> FactoryResourcePacker::CreatePacker(PackType type, const PackageParser &packageParser)
unique_ptr<ResourcePack> ResourcePackerFactory::CreatePacker(PackType type, const PackageParser &packageParser)
{
if (type == PackType::NORMAL) {
return make_unique<ResourcePack>(packageParser);
} else if (type == PackType::OVERLAP) {
return make_unique<ResourceOverlap>(packageParser);
} else {
cerr << "Error: FactoryResourcePacker: Unknown input PackType." << endl;
cerr << "Error: ResourcePackerFactory: Unknown input PackType." << endl;
return nullptr;
}
}
+25 -16
View File
@@ -15,7 +15,8 @@
#include "resource_table.h"
#include <cJSON.h>
#include "cmd_parser.h"
#include <cstdint>
#include "cmd/package_parser.h"
#include "file_entry.h"
#include "file_manager.h"
#include "resource_util.h"
@@ -115,23 +116,33 @@ uint32_t ResourceTable::LoadResTable(const string path, map<int64_t, vector<Reso
return RESTOOL_ERROR;
}
in.seekg(0, ios::beg);
uint32_t errorCode = LoadResTable(in, resInfos);
in.close();
return errorCode;
}
uint32_t ResourceTable::LoadResTable(basic_istream<char> &in, map<int64_t, vector<ResourceItem>> &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();
in.seekg(0, ios::beg);
uint64_t pos = 0;
IndexHeader indexHeader;
if (!ReadFileHeader(in, indexHeader, pos, static_cast<uint64_t>(length))) {
in.close();
return RESTOOL_ERROR;
}
map<int64_t, vector<KeyParam>> limitKeys;
if (!ReadLimitKeys(in, limitKeys, indexHeader.limitKeyConfigSize, pos, static_cast<uint64_t>(length))) {
in.close();
return RESTOOL_ERROR;
}
map<int64_t, pair<int64_t, int64_t>> datas;
if (!ReadIdTables(in, datas, indexHeader.limitKeyConfigSize, pos, static_cast<uint64_t>(length))) {
in.close();
return RESTOOL_ERROR;
}
@@ -139,11 +150,9 @@ uint32_t ResourceTable::LoadResTable(const string path, map<int64_t, vector<Reso
RecordItem record;
if (!ReadDataRecordPrepare(in, record, pos, static_cast<uint64_t>(length)) ||
!ReadDataRecordStart(in, record, limitKeys, datas, resInfos)) {
in.close();
return RESTOOL_ERROR;
}
}
in.close();
return RESTOOL_SUCCESS;
}
@@ -348,7 +357,7 @@ void ResourceTable::SaveIdSets(const map<string, IdSet> &idSets, ostringstream &
}
}
bool ResourceTable::ReadFileHeader(ifstream &in, IndexHeader &indexHeader, uint64_t &pos, uint64_t length) const
bool ResourceTable::ReadFileHeader(basic_istream<char> &in, IndexHeader &indexHeader, uint64_t &pos, uint64_t length)
{
pos += sizeof(indexHeader);
if (pos > length) {
@@ -361,8 +370,8 @@ bool ResourceTable::ReadFileHeader(ifstream &in, IndexHeader &indexHeader, uint6
return true;
}
bool ResourceTable::ReadLimitKeys(ifstream &in, map<int64_t, vector<KeyParam>> &limitKeys,
uint32_t count, uint64_t &pos, uint64_t length) const
bool ResourceTable::ReadLimitKeys(basic_istream<char> &in, map<int64_t, vector<KeyParam>> &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;
@@ -397,8 +406,8 @@ bool ResourceTable::ReadLimitKeys(ifstream &in, map<int64_t, vector<KeyParam>> &
return true;
}
bool ResourceTable::ReadIdTables(std::ifstream &in, std::map<int64_t, std::pair<int64_t, int64_t>> &datas,
uint32_t count, uint64_t &pos, uint64_t length) const
bool ResourceTable::ReadIdTables(basic_istream<char> &in, std::map<int64_t, std::pair<int64_t, int64_t>> &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;
@@ -431,7 +440,7 @@ bool ResourceTable::ReadIdTables(std::ifstream &in, std::map<int64_t, std::pair<
return true;
}
bool ResourceTable::ReadDataRecordPrepare(ifstream &in, RecordItem &record, uint64_t &pos, uint64_t length) const
bool ResourceTable::ReadDataRecordPrepare(basic_istream<char> &in, RecordItem &record, uint64_t &pos, uint64_t length)
{
pos = pos + INT_TO_BYTES;
if (pos > length) {
@@ -449,10 +458,10 @@ bool ResourceTable::ReadDataRecordPrepare(ifstream &in, RecordItem &record, uint
return true;
}
bool ResourceTable::ReadDataRecordStart(std::ifstream &in, RecordItem &record,
const std::map<int64_t, std::vector<KeyParam>> &limitKeys,
const std::map<int64_t, std::pair<int64_t, int64_t>> &datas,
std::map<int64_t, std::vector<ResourceItem>> &resInfos) const
bool ResourceTable::ReadDataRecordStart(basic_istream<char> &in, RecordItem &record,
const map<int64_t, vector<KeyParam>> &limitKeys,
const map<int64_t, pair<int64_t, int64_t>> &datas,
map<int64_t, vector<ResourceItem>> &resInfos)
{
int64_t offset = in.tellg();
offset = offset - INT_TO_BYTES - INT_TO_BYTES - INT_TO_BYTES;
+16
View File
@@ -328,6 +328,10 @@ string ResourceUtil::GetKeyParamValue(const KeyParam &KeyParam)
case KeyType::REGION:
val = GetLocaleLimitkey(KeyParam);
break;
case KeyType::INPUTDEVICE:
val = KeyParam.value == static_cast<const uint32_t>(InputDevice::INPUTDEVICE_NOT_SET) ?
"not set" : "pointDevice";
break;
default:
val = to_string(KeyParam.value);
break;
@@ -471,6 +475,18 @@ void ResourceUtil::PrintWarningMsg(vector<pair<ResType, string>> &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<uint32_t>(type));
}
return ret;
}
}
}
}
+21 -20
View File
@@ -13,19 +13,16 @@
* limitations under the License.
*/
#include "cmd_parser.h"
#include <cstring>
#include "cmd/package_parser.h"
#include "cmd/dump_parser.h"
#include "resource_pack.h"
#include "compression_parser.h"
using namespace std;
using namespace OHOS::Global::Restool;
namespace {
uint32_t ProccssHap(PackageParser &packageParser)
{
ResourcePack pack(packageParser);
return pack.Package();
}
}
constexpr int PARAM_MIN_NUM = 2;
int main(int argc, char *argv[])
{
@@ -33,19 +30,23 @@ int main(int argc, char *argv[])
cerr << "Error: argv null" << endl;
return RESTOOL_ERROR;
}
auto &parser = CmdParser<PackageParser>::GetInstance();
if (parser.Parse(argc, argv) != RESTOOL_SUCCESS) {
parser.ShowUseage();
if (argc < PARAM_MIN_NUM) {
cerr << "Error: At least 1 parameters are required, but no parameter is passed in." << endl;
return RESTOOL_ERROR;
}
if (strcmp(argv[1], "dump") == 0) {
auto &dumpParser = CmdParser<DumpParser>::GetInstance();
if (dumpParser.Parse(argc, argv) != RESTOOL_SUCCESS) {
dumpParser.ShowUseage();
return RESTOOL_ERROR;
}
return dumpParser.ExecCommand();
}
auto &packParser = CmdParser<PackageParser>::GetInstance();
if (packParser.Parse(argc, argv) != RESTOOL_SUCCESS) {
packParser.ShowUseage();
return RESTOOL_ERROR;
}
auto &packageParser = parser.GetCmdParser();
if (ProccssHap(packageParser) != RESTOOL_SUCCESS) {
return RESTOOL_ERROR;
}
cout << "Info: restool resources compile success." << endl;
if (CompressionParser::GetCompressionParser()->GetMediaSwitch()) {
cout << CompressionParser::GetCompressionParser()->PrintTransMessage() << endl;
}
return RESTOOL_SUCCESS;
return packParser.ExecCommand();
}
+1 -1
View File
@@ -16,7 +16,7 @@
#include "select_compile_parse.h"
#include <algorithm>
#include "key_parser.h"
#include "cmd_parser.h"
#include "cmd/package_parser.h"
#include "resource_util.h"
namespace OHOS {