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 "generic_compiler.h"
17 #include <iostream>
18 #include "file_entry.h"
19 #include "resource_util.h"
20 #include "restool_errors.h"
21 #include "compression_parser.h"
22
23 namespace OHOS {
24 namespace Global {
25 namespace Restool {
26 using namespace std;
GenericCompiler(ResType type,const string & output)27 GenericCompiler::GenericCompiler(ResType type, const string &output)
28 : IResourceCompiler(type, output)
29 {
30 }
~GenericCompiler()31 GenericCompiler::~GenericCompiler()
32 {
33 }
34
CompileSingleFile(const FileInfo & fileInfo)35 uint32_t GenericCompiler::CompileSingleFile(const FileInfo &fileInfo)
36 {
37 if (IsIgnore(fileInfo)) {
38 return RESTOOL_SUCCESS;
39 }
40
41 string output = "";
42 if (!CopyMediaFile(fileInfo, output)) {
43 return RESTOOL_ERROR;
44 }
45
46 if (!PostMediaFile(fileInfo, output)) {
47 return RESTOOL_ERROR;
48 }
49 return RESTOOL_SUCCESS;
50 }
51
PostMediaFile(const FileInfo & fileInfo,const std::string & output)52 bool GenericCompiler::PostMediaFile(const FileInfo &fileInfo, const std::string &output)
53 {
54 ResourceItem resourceItem(fileInfo.filename, fileInfo.keyParams, type_);
55 resourceItem.SetFilePath(fileInfo.filePath);
56 resourceItem.SetLimitKey(fileInfo.limitKey);
57
58 auto index = output.find_last_of(SEPARATOR_FILE);
59 if (index == string::npos) {
60 cerr << "Error: invalid output path." << NEW_LINE_PATH << output << endl;
61 return false;
62 }
63 string data = output.substr(index + 1);
64 data = moduleName_ + SEPARATOR + RESOURCES_DIR + SEPARATOR + \
65 fileInfo.limitKey + SEPARATOR + fileInfo.fileCluster + SEPARATOR + data;
66 if (!resourceItem.SetData(reinterpret_cast<const int8_t *>(data.c_str()), data.length())) {
67 cerr << "Error: resource item set data fail, data: " << data << NEW_LINE_PATH << fileInfo.filePath << endl;
68 return false;
69 }
70 return MergeResourceItem(resourceItem);
71 }
72
GetOutputFilePath(const FileInfo & fileInfo) const73 string GenericCompiler::GetOutputFilePath(const FileInfo &fileInfo) const
74 {
75 string outputFolder = GetOutputFolder(fileInfo);
76 string outputFilePath = FileEntry::FilePath(outputFolder).Append(fileInfo.filename).GetPath();
77 return outputFilePath;
78 }
79
IsIgnore(const FileInfo & fileInfo)80 bool GenericCompiler::IsIgnore(const FileInfo &fileInfo)
81 {
82 string output = GetOutputFilePath(fileInfo);
83 if (!g_resourceSet.emplace(output).second) {
84 cerr << "Warning: '" << fileInfo.filePath << "' is defined repeatedly." << endl;
85 return true;
86 }
87 return false;
88 }
89
CopyMediaFile(const FileInfo & fileInfo,std::string & output)90 bool GenericCompiler::CopyMediaFile(const FileInfo &fileInfo, std::string &output)
91 {
92 string outputFolder = GetOutputFolder(fileInfo);
93 if (!ResourceUtil::CreateDirs(outputFolder)) {
94 return false;
95 }
96 output = GetOutputFilePath(fileInfo);
97 if (moduleName_ == "har" || type_ != ResType::MEDIA) {
98 return ResourceUtil::CopyFileInner(fileInfo.filePath, output);
99 } else {
100 return CompressionParser::GetCompressionParser()->CopyAndTranscode(fileInfo.filePath, output);
101 }
102 }
103 }
104 }
105 }