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 if (tipError) {
99 cerr << "Error: '"<< resourceItem.GetName() <<"' conflict, first declared.";
100 cerr << NEW_LINE_PATH << ret->GetFilePath() << endl;
101 cerr << "but declared again." <<NEW_LINE_PATH << resourceItem.GetFilePath() << endl;
102 return RESTOOL_ERROR;
103 }
104 cout << "Warning: '"<< resourceItem.GetName() <<"' conflict, first declared.";
105 cout << NEW_LINE_PATH << ret->GetFilePath() << endl;
106 cout << "but declared again." << NEW_LINE_PATH << resourceItem.GetFilePath() << endl;
107 }
108 }
109 return RESTOOL_SUCCESS;
110 }
111 // below private
Push(const map<int32_t,std::vector<ResourceItem>> & other)112 void ResourceModule::Push(const map<int32_t, std::vector<ResourceItem>> &other)
113 {
114 if (previewMode_) {
115 return;
116 }
117
118 for (const auto &iter : other) {
119 owner_.emplace(iter.first, iter.second);
120 }
121 }
122 }
123 }
124 }
125