资源ID固定

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