media下的json文件支持解引用

Signed-off-by: fangyunzhong <fangyunzhong2@huawei.com>
This commit is contained in:
fangyunzhong
2023-02-23 07:00:52 +00:00
parent e86f304946
commit 6342c60d8b
6 changed files with 94 additions and 104 deletions
+7 -7
View File
@@ -27,18 +27,18 @@ public:
ReferenceParser();
virtual ~ReferenceParser();
uint32_t ParseRefInSolidXml(const std::vector<std::string> &solidXmlFolders) const;
uint32_t ParseRefInElement(std::map<int32_t, std::vector<ResourceItem>> &items) const;
uint32_t ParseRefInString(std::string &value, bool &update) const;
uint32_t ParseRefInProfile(const std::string &output) const;
uint32_t ParseRefInJson(const std::string &filePath) const;
uint32_t ParseRefInJson(const std::string &from, const std::string &to) const;
uint32_t ParseRefInResources(std::map<int32_t, std::vector<ResourceItem>> &items, const std::string &output);
uint32_t ParseRefInResourceItem(ResourceItem &resourceItem) const;
uint32_t ParseRefInJsonFile(ResourceItem &resourceItem, const std::string &output, const bool isIncrement = false);
uint32_t ParseRefInString(std::string &value, bool &update) const;
private:
bool ParseRefResourceItem(ResourceItem &resourceItem) const;
bool ParseRefJson(const std::string &from, const std::string &to) const;
bool ParseRefResourceItemData(const ResourceItem &resourceItem, std::string &data, bool &update) const;
bool IsStringOfResourceItem(ResType resType) const;
bool IsArrayOfResourceItem(ResType resType) const;
bool IsNotElement(ResType resType) const;
bool IsElementRef(const ResourceItem &resourceItem) const;
bool IsMediaRef(const ResourceItem &resourceItem) const;
bool IsProfileRef(const ResourceItem &resourceItem) const;
bool ParseRefString(std::string &key) const;
bool ParseRefString(std::string &key, bool &update) const;
bool ParseRefImpl(std::string &key, const std::map<std::string, ResType> &refs, bool isSystem) const;
+2 -1
View File
@@ -30,6 +30,7 @@ const static std::string MODULE_JSON = "module.json";
const static std::string RAW_FILE_DIR = "rawfile";
const static std::string ID_DEFINED_FILE = "id_defined.json";
const static std::string RESOURCE_INDEX_FILE = "resources.index";
const static std::string JSON_EXTENSION = ".json";
const static std::string SEPARATOR = "/";
const static std::string WIN_SEPARATOR = "\\";
const static std::string NEW_LINE_PATH = "\r\nat ";
@@ -37,7 +38,7 @@ const static std::string LONG_PATH_HEAD = "\\\\?\\";
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" };
static const int8_t RESTOOL_VERSION[VERSION_MAX_LEN] = { "Restool 4.001" };
const static int32_t TAG_LEN = 4;
enum class KeyType {
+1 -2
View File
@@ -74,8 +74,7 @@ uint32_t FileManager::ParseReference(const string &output, const vector<string>
{
ReferenceParser referenceParser;
if (referenceParser.ParseRefInSolidXml(sxmlFolders) != RESTOOL_SUCCESS ||
referenceParser.ParseRefInElement(items_) != RESTOOL_SUCCESS ||
referenceParser.ParseRefInProfile(output) != RESTOOL_SUCCESS) {
referenceParser.ParseRefInResources(items_, output) != RESTOOL_SUCCESS) {
return RESTOOL_ERROR;
}
return RESTOOL_SUCCESS;
+1 -1
View File
@@ -54,7 +54,7 @@ uint32_t IResourceCompiler::Compile(const vector<DirectoryInfo> &directoryInfos)
}
if (!it->IsFile()) {
cout << "Error: '" << it->GetFilePath().GetPath() << "' not regular." << endl;
cout << "Error: '" << it->GetFilePath().GetPath() << "' must be a file." << endl;
return RESTOOL_ERROR;
}
+76 -80
View File
@@ -87,14 +87,15 @@ uint32_t ReferenceParser::ParseRefInSolidXml(const vector<string> &solidXmlFolde
return RESTOOL_SUCCESS;
}
uint32_t ReferenceParser::ParseRefInElement(map<int32_t, vector<ResourceItem>> &items) const
uint32_t ReferenceParser::ParseRefInResources(map<int32_t, vector<ResourceItem>> &items, const string &output)
{
for (auto &iter : items) {
for (auto &resourceItem : iter.second) {
if (IsNotElement(resourceItem.GetResType())) {
break;
if (IsElementRef(resourceItem) && ParseRefInResourceItem(resourceItem) != RESTOOL_SUCCESS) {
return RESTOOL_ERROR;
}
if (!ParseRefResourceItem(resourceItem)) {
if ((IsMediaRef(resourceItem) || IsProfileRef(resourceItem)) &&
ParseRefInJsonFile(resourceItem, output) != RESTOOL_SUCCESS) {
return RESTOOL_ERROR;
}
}
@@ -102,6 +103,56 @@ uint32_t ReferenceParser::ParseRefInElement(map<int32_t, vector<ResourceItem>> &
return RESTOOL_SUCCESS;
}
uint32_t ReferenceParser::ParseRefInResourceItem(ResourceItem &resourceItem) const
{
ResType resType = resourceItem.GetResType();
string data;
bool update = false;
if (IsStringOfResourceItem(resType)) {
data = string(reinterpret_cast<const char *>(resourceItem.GetData()), resourceItem.GetDataLength());
if (!ParseRefString(data, update)) {
cerr << "Error: " << NEW_LINE_PATH << resourceItem.GetFilePath() << endl;
return RESTOOL_ERROR;
}
if (!update) {
return RESTOOL_SUCCESS;
}
} else if (IsArrayOfResourceItem(resType)) {
if (!ParseRefResourceItemData(resourceItem, data, update)) {
return RESTOOL_ERROR;
}
if (!update) {
return RESTOOL_SUCCESS;
}
}
if (update && !resourceItem.SetData(reinterpret_cast<const int8_t *>(data.c_str()), data.length())) {
cerr << "Error: set data fail. name = '" << resourceItem.GetName() << "' data = '" << data << "'.";
cerr << NEW_LINE_PATH << resourceItem.GetFilePath() << endl;
return RESTOOL_ERROR;
}
return RESTOOL_SUCCESS;
}
uint32_t ReferenceParser::ParseRefInJsonFile(ResourceItem &resourceItem, const string &output, const bool isIncrement)
{
string jsonPath;
if (resourceItem.GetResType() == ResType::MEDIA) {
jsonPath = FileEntry::FilePath(output).Append(RESOURCES_DIR)
.Append(resourceItem.GetLimitKey()).Append("media").Append(resourceItem.GetName()).GetPath();
} else {
jsonPath = FileEntry::FilePath(output).Append(RESOURCES_DIR)
.Append("base").Append("profile").Append(resourceItem.GetName()).GetPath();
}
if (!ParseRefJson(resourceItem.GetFilePath(), jsonPath)) {
return RESTOOL_ERROR;
}
if (isIncrement && ResourceUtil::FileExist(jsonPath)) {
resourceItem.SetData(reinterpret_cast<const int8_t *>(jsonPath.c_str()), jsonPath.length());
}
return RESTOOL_SUCCESS;
}
uint32_t ReferenceParser::ParseRefInString(string &value, bool &update) const
{
if (ParseRefString(value, update)) {
@@ -110,95 +161,27 @@ uint32_t ReferenceParser::ParseRefInString(string &value, bool &update) const
return RESTOOL_ERROR;
}
uint32_t ReferenceParser::ParseRefInProfile(const string &output) const
{
string profileFolder = FileEntry::FilePath(output).Append(RESOURCES_DIR).Append("base").Append("profile").GetPath();
if (!ResourceUtil::FileExist(profileFolder)) {
return RESTOOL_SUCCESS;
}
FileEntry f(profileFolder);
for (const auto &entry : f.GetChilds()) {
if (!entry->IsFile()) {
cerr << "Error: '" << entry->GetFilePath().GetPath() << "' is directory." << endl;
return false;
}
if (entry->GetFilePath().GetExtension() != ".json") {
continue;
}
if (ParseRefInJson(entry->GetFilePath().GetPath()) != RESTOOL_SUCCESS) {
return RESTOOL_ERROR;
}
}
return RESTOOL_SUCCESS;
}
uint32_t ReferenceParser::ParseRefInJson(const string &filePath) const
{
return ParseRefInJson(filePath, filePath);
}
uint32_t ReferenceParser::ParseRefInJson(const string &from, const string &to) const
bool ReferenceParser::ParseRefJson(const string &from, const string &to) const
{
Json::Value root;
if (!ResourceUtil::OpenJsonFile(from, root)) {
return RESTOOL_ERROR;
return false;
}
bool needSave = false;
if (!ParseRefJsonImpl(root, needSave)) {
return RESTOOL_ERROR;
return false;
}
if (!needSave) {
return RESTOOL_SUCCESS;
return true;
}
if (!ResourceUtil::CreateDirs(FileEntry::FilePath(to).GetParent().GetPath())) {
return RESTOOL_ERROR;
return false;
}
if (!ResourceUtil::SaveToJsonFile(to, root)) {
return RESTOOL_ERROR;
}
return RESTOOL_SUCCESS;
}
uint32_t ReferenceParser::ParseRefInResourceItem(ResourceItem &resourceItem) const
{
if (!ParseRefResourceItem(resourceItem)) {
return RESTOOL_ERROR;
}
return RESTOOL_SUCCESS;
}
bool ReferenceParser::ParseRefResourceItem(ResourceItem &resourceItem) const
{
ResType resType = resourceItem.GetResType();
string data;
bool update = false;
if (IsStringOfResourceItem(resType)) {
data = string(reinterpret_cast<const char *>(resourceItem.GetData()), resourceItem.GetDataLength());
if (!ParseRefString(data, update)) {
cerr << "Error: " << NEW_LINE_PATH << resourceItem.GetFilePath() << endl;
return false;
}
if (!update) {
return true;
}
} else if (IsArrayOfResourceItem(resType)) {
if (!ParseRefResourceItemData(resourceItem, data, update)) {
return false;
}
if (!update) {
return true;
}
}
if (update && !resourceItem.SetData(reinterpret_cast<const int8_t *>(data.c_str()), data.length())) {
cerr << "Error: set data fail. name = '" << resourceItem.GetName() << "' data = '" << data << "'.";
cerr << NEW_LINE_PATH << resourceItem.GetFilePath() << endl;
return false;
}
return true;
@@ -260,15 +243,28 @@ bool ReferenceParser::IsArrayOfResourceItem(ResType resType) const
return false;
}
bool ReferenceParser::IsNotElement(ResType resType) const
bool ReferenceParser::IsElementRef(const ResourceItem &resourceItem) const
{
ResType resType = resourceItem.GetResType();
auto result = find_if(g_contentClusterMap.begin(), g_contentClusterMap.end(), [resType](const auto &iter) {
return resType == iter.second;
});
if (result == g_contentClusterMap.end()) {
return true;
return false;
}
return false;
return true;
}
bool ReferenceParser::IsMediaRef(const ResourceItem &resourceItem) const
{
return resourceItem.GetResType() == ResType::MEDIA &&
FileEntry::FilePath(resourceItem.GetFilePath()).GetExtension() == JSON_EXTENSION;
}
bool ReferenceParser::IsProfileRef(const ResourceItem &resourceItem) const
{
return resourceItem.GetResType() == ResType::PROF && resourceItem.GetLimitKey() == "base" &&
FileEntry::FilePath(resourceItem.GetFilePath()).GetExtension() == JSON_EXTENSION;
}
bool ReferenceParser::ParseRefString(string &key) const
+7 -13
View File
@@ -97,20 +97,11 @@ bool ResourceAppend::ParseRef()
{
for (auto &iter : refs_) {
ReferenceParser ref;
if (iter->GetResType() == ResType::PROF) {
string dst = FileEntry::FilePath(packageParser_.GetOutput()).Append(RESOURCES_DIR)
.Append("base").Append("profile").Append(iter->GetName()).GetPath();
if (ref.ParseRefInJson(iter->GetFilePath(), dst) != RESTOOL_SUCCESS) {
if (iter->GetResType() == ResType::PROF || iter->GetResType() == ResType::MEDIA) {
if (ref.ParseRefInJsonFile(*iter, packageParser_.GetOutput(), true) != RESTOOL_SUCCESS) {
return false;
}
if (ResourceUtil::FileExist(dst)) {
iter->SetData(reinterpret_cast<const int8_t *>(dst.c_str()), dst.length());
}
continue;
}
if (ref.ParseRefInResourceItem(*iter) != RESTOOL_SUCCESS) {
} else if (ref.ParseRefInResourceItem(*iter) != RESTOOL_SUCCESS) {
return false;
}
}
@@ -499,12 +490,15 @@ void ResourceAppend::AddRef(const shared_ptr<ResourceItem> &resourceItem)
string data(reinterpret_cast<const char *>(resourceItem->GetData()), resourceItem->GetDataLength());
ResType resType = resourceItem->GetResType();
if (resType == ResType::MEDIA) {
if (FileEntry::FilePath(resourceItem->GetFilePath()).GetExtension() == JSON_EXTENSION) {
refs_.push_back(resourceItem);
}
return;
}
if (resType == ResType::PROF) {
if (resourceItem->GetLimitKey() != "base" ||
FileEntry::FilePath(resourceItem->GetFilePath()).GetExtension() != ".json") {
FileEntry::FilePath(resourceItem->GetFilePath()).GetExtension() != JSON_EXTENSION) {
return;
}
refs_.push_back(resourceItem);