diff --git a/include/reference_parser.h b/include/reference_parser.h index dbf2b77..bd95f54 100644 --- a/include/reference_parser.h +++ b/include/reference_parser.h @@ -27,18 +27,18 @@ public: ReferenceParser(); virtual ~ReferenceParser(); uint32_t ParseRefInSolidXml(const std::vector &solidXmlFolders) const; - uint32_t ParseRefInElement(std::map> &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> &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 &refs, bool isSystem) const; diff --git a/include/resource_data.h b/include/resource_data.h index 94b007b..5429746 100644 --- a/include/resource_data.h +++ b/include/resource_data.h @@ -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 { diff --git a/src/file_manager.cpp b/src/file_manager.cpp index 2aba0a4..b81224f 100644 --- a/src/file_manager.cpp +++ b/src/file_manager.cpp @@ -74,8 +74,7 @@ uint32_t FileManager::ParseReference(const string &output, const vector { 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; diff --git a/src/i_resource_compiler.cpp b/src/i_resource_compiler.cpp index 044fde9..414d7a3 100644 --- a/src/i_resource_compiler.cpp +++ b/src/i_resource_compiler.cpp @@ -54,7 +54,7 @@ uint32_t IResourceCompiler::Compile(const vector &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; } diff --git a/src/reference_parser.cpp b/src/reference_parser.cpp index 5d06928..ae4bf56 100644 --- a/src/reference_parser.cpp +++ b/src/reference_parser.cpp @@ -87,14 +87,15 @@ uint32_t ReferenceParser::ParseRefInSolidXml(const vector &solidXmlFolde return RESTOOL_SUCCESS; } -uint32_t ReferenceParser::ParseRefInElement(map> &items) const +uint32_t ReferenceParser::ParseRefInResources(map> &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> & 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(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(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(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(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(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 diff --git a/src/resource_append.cpp b/src/resource_append.cpp index 2780308..d99ade6 100644 --- a/src/resource_append.cpp +++ b/src/resource_append.cpp @@ -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(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) string data(reinterpret_cast(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);