1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "resource_module.h"
17 #include<algorithm>
18 #include<iostream>
19 #include "factory_resource_compiler.h"
20 #include "restool_errors.h"
21
22 namespace OHOS {
23 namespace Global {
24 namespace Restool {
25 using namespace std;
26 const vector<ResType> ResourceModule::SCAN_SEQ = {
27 ResType::ELEMENT,
28 ResType::MEDIA,
29 ResType::PROF,
30 ResType::ANIMATION,
31 ResType::GRAPHIC,
32 ResType::LAYOUT,
33 };
ResourceModule(const string & modulePath,const string & moduleOutput,const string & moduleName)34 ResourceModule::ResourceModule(const string &modulePath, const string &moduleOutput, const string &moduleName)
35 : modulePath_(modulePath), moduleOutput_(moduleOutput), moduleName_(moduleName)
36 {
37 }
38
ScanResource()39 uint32_t ResourceModule::ScanResource()
40 {
41 if (!ResourceUtil::FileExist(modulePath_)) {
42 return RESTOOL_SUCCESS;
43 }
44
45 ResourceDirectory directory;
46 if (!directory.ScanResources(modulePath_, [this](const DirectoryInfo &info) -> bool {
47 scanDirs_[info.dirType].push_back(info);
48 return true;
49 })) {
50 return RESTOOL_ERROR;
51 }
52
53 for (const auto &type : SCAN_SEQ) {
54 const auto &item = scanDirs_.find(type);
55 if (item == scanDirs_.end() || item->second.empty()) {
56 continue;
57 }
58
59 unique_ptr<IResourceCompiler> resourceCompiler =
60 FactoryResourceCompiler::CreateCompiler(type, moduleOutput_);
61 resourceCompiler->SetModuleName(moduleName_);
62 resourceCompiler->SetPreviewMode(previewMode_);
63 if (resourceCompiler->Compile(item->second) != RESTOOL_SUCCESS) {
64 return RESTOOL_ERROR;
65 }
66 Push(resourceCompiler->GetResult());
67 }
68 return RESTOOL_SUCCESS;
69 }
70
GetOwner() const71 const map<int32_t, vector<ResourceItem>> &ResourceModule::GetOwner() const
72 {
73 return owner_;
74 }
75
GetScanDirectorys() const76 const map<ResType, vector<DirectoryInfo>> &ResourceModule::GetScanDirectorys() const
77 {
78 return scanDirs_;
79 }
80
MergeResourceItem(map<int32_t,vector<ResourceItem>> & alls,const map<int32_t,vector<ResourceItem>> & other,bool tipError)81 uint32_t ResourceModule::MergeResourceItem(map<int32_t, vector<ResourceItem>> &alls,
82 const map<int32_t, vector<ResourceItem>> &other, bool tipError)
83 {
84 for (const auto &iter : other) {
85 auto result = alls.emplace(iter.first, iter.second);
86 if (result.second) {
87 continue;
88 }
89
90 for (const auto &resourceItem : iter.second) {
91 auto ret = find_if(result.first->second.begin(), result.first->second.end(), [&resourceItem](auto &iter) {
92 return resourceItem.GetLimitKey() == iter.GetLimitKey();
93 });
94 if (ret == result.first->second.end()) {
95 result.first->second.push_back(resourceItem);
96 continue;
97 }
98 string error = resourceItem.GetName() + "' conflict, first declared in " + \
99 ret->GetFilePath() + ", but declared again in " + resourceItem.GetFilePath();
100 if (tipError) {
101 cerr << "Error: " << error << endl;
102 return RESTOOL_ERROR;
103 }
104 cout << "Warning: " << error << endl;
105 }
106 }
107 return RESTOOL_SUCCESS;
108 }
109 // below private
Push(const map<int32_t,std::vector<ResourceItem>> & other)110 void ResourceModule::Push(const map<int32_t, std::vector<ResourceItem>> &other)
111 {
112 if (previewMode_) {
113 return;
114 }
115
116 for (const auto &iter : other) {
117 owner_.emplace(iter.first, iter.second);
118 }
119 }
120 }
121 }
122 }
123