• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 };
ResourceModule(const string & modulePath,const string & moduleOutput,const string & moduleName)31 ResourceModule::ResourceModule(const string &modulePath, const string &moduleOutput, const string &moduleName)
32     : modulePath_(modulePath), moduleOutput_(moduleOutput), moduleName_(moduleName)
33 {
34 }
35 
ScanResource()36 uint32_t ResourceModule::ScanResource()
37 {
38     if (!ResourceUtil::FileExist(modulePath_)) {
39         return RESTOOL_SUCCESS;
40     }
41 
42     ResourceDirectory directory;
43     if (!directory.ScanResources(modulePath_, [this](const DirectoryInfo &info) -> bool {
44             scanDirs_[info.dirType].push_back(info);
45             return true;
46         })) {
47         return RESTOOL_ERROR;
48     }
49 
50     for (const auto &type : SCAN_SEQ) {
51         const auto &item = scanDirs_.find(type);
52         if (item == scanDirs_.end() || item->second.empty()) {
53             continue;
54         }
55 
56         unique_ptr<IResourceCompiler> resourceCompiler =
57             FactoryResourceCompiler::CreateCompiler(type, moduleOutput_);
58         resourceCompiler->SetModuleName(moduleName_);
59         if (resourceCompiler->Compile(item->second) != RESTOOL_SUCCESS) {
60             return RESTOOL_ERROR;
61         }
62         Push(resourceCompiler->GetResult());
63     }
64     return RESTOOL_SUCCESS;
65 }
66 
GetOwner() const67 const map<int64_t, vector<ResourceItem>> &ResourceModule::GetOwner() const
68 {
69     return owner_;
70 }
71 
GetScanDirectorys() const72 const map<ResType, vector<DirectoryInfo>> &ResourceModule::GetScanDirectorys() const
73 {
74     return scanDirs_;
75 }
76 
MergeResourceItem(map<int64_t,vector<ResourceItem>> & alls,const map<int64_t,vector<ResourceItem>> & other,bool tipError)77 uint32_t ResourceModule::MergeResourceItem(map<int64_t, vector<ResourceItem>> &alls,
78     const map<int64_t, vector<ResourceItem>> &other, bool tipError)
79 {
80     for (const auto &iter : other) {
81         auto result = alls.emplace(iter.first, iter.second);
82         if (result.second) {
83             continue;
84         }
85 
86         for (const auto &resourceItem : iter.second) {
87             auto ret = find_if(result.first->second.begin(), result.first->second.end(), [&resourceItem](auto &iter) {
88                 return resourceItem.GetLimitKey() == iter.GetLimitKey();
89             });
90             if (ret == result.first->second.end()) {
91                 result.first->second.push_back(resourceItem);
92                 continue;
93             }
94             if (tipError) {
95                 cerr << "Error: '"<< resourceItem.GetName() <<"' conflict, first declared.";
96                 cerr << NEW_LINE_PATH << ret->GetFilePath() << endl;
97                 cerr << "but declared again." <<NEW_LINE_PATH << resourceItem.GetFilePath() << endl;
98                 return RESTOOL_ERROR;
99             }
100             cout << "Warning: '"<< resourceItem.GetName() <<"' conflict, first declared.";
101             cout << NEW_LINE_PATH << ret->GetFilePath() << endl;
102             cout << "but declared again." << NEW_LINE_PATH << resourceItem.GetFilePath() << endl;
103         }
104     }
105     return RESTOOL_SUCCESS;
106 }
107 // below private
Push(const map<int64_t,std::vector<ResourceItem>> & other)108 void ResourceModule::Push(const map<int64_t, std::vector<ResourceItem>> &other)
109 {
110     for (const auto &iter : other) {
111         owner_.emplace(iter.first, iter.second);
112     }
113 }
114 }
115 }
116 }
117