• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2024 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 <future>
17 #include "resource_overlap.h"
18 #include "overlap_binary_file_packer.h"
19 #include "file_manager.h"
20 #include "resource_table.h"
21 #include "id_worker.h"
22 
23 namespace OHOS {
24 namespace Global {
25 namespace Restool {
26 using namespace std;
27 
ResourceOverlap(const PackageParser & packageParser)28 ResourceOverlap::ResourceOverlap(const PackageParser &packageParser) : ResourcePack(packageParser)
29 {
30 }
31 
Pack()32 uint32_t ResourceOverlap::Pack()
33 {
34     cout << "Info: Pack: overlap pack mode" << endl;
35 
36     if (InitResourcePack() != RESTOOL_SUCCESS) {
37         return RESTOOL_ERROR;
38     }
39 
40     ResourceMerge resourceMerge;
41     if (resourceMerge.Init(packageParser_) != RESTOOL_SUCCESS) {
42         return RESTOOL_ERROR;
43     }
44 
45     OverlapBinaryFilePacker rawFilePacker(packageParser_, moduleName_);
46     future<uint32_t> copyFuture = rawFilePacker.CopyBinaryFileAsync(resourceMerge.GetInputs());
47 
48     if (LoadHapResources() != RESTOOL_SUCCESS) {
49         rawFilePacker.StopCopy();
50         return RESTOOL_ERROR;
51     }
52 
53     if (PackResources(resourceMerge) != RESTOOL_SUCCESS) {
54         rawFilePacker.StopCopy();
55         return RESTOOL_ERROR;
56     }
57 
58     if (copyFuture.get() != RESTOOL_SUCCESS) {
59         return RESTOOL_ERROR;
60     }
61     return RESTOOL_SUCCESS;
62 }
63 
ScanResources(const std::vector<std::string> & inputs,const std::string & output)64 uint32_t ResourceOverlap::ScanResources(const std::vector<std::string> &inputs, const std::string &output)
65 {
66     auto &fileManager = FileManager::GetInstance();
67     fileManager.SetModuleName(moduleName_);
68     vector<string> hapResInput{inputs[0]};
69     if (fileManager.ScanModules(hapResInput, output, configJson_.IsHar()) != RESTOOL_SUCCESS) {
70         return RESTOOL_ERROR;
71     }
72     fileManager.SetScanHap(false);
73 
74     vector<string> resInputs(inputs.begin() + 1, inputs.end());
75     if (fileManager.ScanModules(resInputs, output, configJson_.IsHar()) != RESTOOL_SUCCESS) {
76         return RESTOOL_ERROR;
77     }
78     return RESTOOL_SUCCESS;
79 }
80 
LoadHapResources()81 uint32_t ResourceOverlap::LoadHapResources()
82 {
83     ResourceTable resourceTabel;
84     map<int64_t, vector<ResourceItem>> items;
85     string resourceIndexPath =
86         FileEntry::FilePath(packageParser_.GetInputs()[0]).GetParent().Append(RESOURCE_INDEX_FILE).GetPath();
87     if (resourceTabel.LoadResTable(resourceIndexPath, items) != RESTOOL_SUCCESS) {
88         return RESTOOL_ERROR;
89     }
90 
91     if (IdWorker::GetInstance().LoadIdFromHap(items) != RESTOOL_SUCCESS) {
92         return RESTOOL_ERROR;
93     }
94 
95     FileManager &fileManager = FileManager::GetInstance();
96     fileManager.SetModuleName(moduleName_);
97     if (fileManager.MergeResourceItem(items) != RESTOOL_SUCCESS) {
98         return RESTOOL_ERROR;
99     }
100     fileManager.SetScanHap(true);
101     return RESTOOL_SUCCESS;
102 }
103 }
104 }
105 }