13920e296Sopenharmony_ci/*
23920e296Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
33920e296Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43920e296Sopenharmony_ci * you may not use this file except in compliance with the License.
53920e296Sopenharmony_ci * You may obtain a copy of the License at
63920e296Sopenharmony_ci *
73920e296Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
83920e296Sopenharmony_ci *
93920e296Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103920e296Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113920e296Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123920e296Sopenharmony_ci * See the License for the specific language governing permissions and
133920e296Sopenharmony_ci * limitations under the License.
143920e296Sopenharmony_ci */
153920e296Sopenharmony_ci
163920e296Sopenharmony_ci#include "resource_module.h"
173920e296Sopenharmony_ci#include <algorithm>
183920e296Sopenharmony_ci#include <iostream>
193920e296Sopenharmony_ci#include "factory_resource_compiler.h"
203920e296Sopenharmony_ci#include "restool_errors.h"
213920e296Sopenharmony_ci
223920e296Sopenharmony_cinamespace OHOS {
233920e296Sopenharmony_cinamespace Global {
243920e296Sopenharmony_cinamespace Restool {
253920e296Sopenharmony_ciusing namespace std;
263920e296Sopenharmony_ciconst vector<ResType> ResourceModule::SCAN_SEQ = {
273920e296Sopenharmony_ci    ResType::ELEMENT,
283920e296Sopenharmony_ci    ResType::MEDIA,
293920e296Sopenharmony_ci    ResType::PROF,
303920e296Sopenharmony_ci};
313920e296Sopenharmony_ciResourceModule::ResourceModule(const string &modulePath, const string &moduleOutput, const string &moduleName)
323920e296Sopenharmony_ci    : modulePath_(modulePath), moduleOutput_(moduleOutput), moduleName_(moduleName)
333920e296Sopenharmony_ci{
343920e296Sopenharmony_ci}
353920e296Sopenharmony_ci
363920e296Sopenharmony_ciuint32_t ResourceModule::ScanResource()
373920e296Sopenharmony_ci{
383920e296Sopenharmony_ci    if (!ResourceUtil::FileExist(modulePath_)) {
393920e296Sopenharmony_ci        return RESTOOL_SUCCESS;
403920e296Sopenharmony_ci    }
413920e296Sopenharmony_ci
423920e296Sopenharmony_ci    ResourceDirectory directory;
433920e296Sopenharmony_ci    if (!directory.ScanResources(modulePath_, [this](const DirectoryInfo &info) -> bool {
443920e296Sopenharmony_ci            scanDirs_[info.dirType].push_back(info);
453920e296Sopenharmony_ci            return true;
463920e296Sopenharmony_ci        })) {
473920e296Sopenharmony_ci        return RESTOOL_ERROR;
483920e296Sopenharmony_ci    }
493920e296Sopenharmony_ci
503920e296Sopenharmony_ci    for (const auto &type : SCAN_SEQ) {
513920e296Sopenharmony_ci        const auto &item = scanDirs_.find(type);
523920e296Sopenharmony_ci        if (item == scanDirs_.end() || item->second.empty()) {
533920e296Sopenharmony_ci            continue;
543920e296Sopenharmony_ci        }
553920e296Sopenharmony_ci
563920e296Sopenharmony_ci        unique_ptr<IResourceCompiler> resourceCompiler =
573920e296Sopenharmony_ci            FactoryResourceCompiler::CreateCompiler(type, moduleOutput_);
583920e296Sopenharmony_ci        resourceCompiler->SetModuleName(moduleName_);
593920e296Sopenharmony_ci        if (resourceCompiler->Compile(item->second) != RESTOOL_SUCCESS) {
603920e296Sopenharmony_ci            return RESTOOL_ERROR;
613920e296Sopenharmony_ci        }
623920e296Sopenharmony_ci        Push(resourceCompiler->GetResult());
633920e296Sopenharmony_ci    }
643920e296Sopenharmony_ci    return RESTOOL_SUCCESS;
653920e296Sopenharmony_ci}
663920e296Sopenharmony_ci
673920e296Sopenharmony_ciconst map<int64_t, vector<ResourceItem>> &ResourceModule::GetOwner() const
683920e296Sopenharmony_ci{
693920e296Sopenharmony_ci    return owner_;
703920e296Sopenharmony_ci}
713920e296Sopenharmony_ci
723920e296Sopenharmony_ciconst map<ResType, vector<DirectoryInfo>> &ResourceModule::GetScanDirectorys() const
733920e296Sopenharmony_ci{
743920e296Sopenharmony_ci    return scanDirs_;
753920e296Sopenharmony_ci}
763920e296Sopenharmony_ci
773920e296Sopenharmony_ciuint32_t ResourceModule::MergeResourceItem(map<int64_t, vector<ResourceItem>> &alls,
783920e296Sopenharmony_ci    const map<int64_t, vector<ResourceItem>> &other, bool tipError)
793920e296Sopenharmony_ci{
803920e296Sopenharmony_ci    for (const auto &iter : other) {
813920e296Sopenharmony_ci        auto result = alls.emplace(iter.first, iter.second);
823920e296Sopenharmony_ci        if (result.second) {
833920e296Sopenharmony_ci            continue;
843920e296Sopenharmony_ci        }
853920e296Sopenharmony_ci
863920e296Sopenharmony_ci        for (const auto &resourceItem : iter.second) {
873920e296Sopenharmony_ci            auto ret = find_if(result.first->second.begin(), result.first->second.end(), [&resourceItem](auto &iter) {
883920e296Sopenharmony_ci                return resourceItem.GetLimitKey() == iter.GetLimitKey();
893920e296Sopenharmony_ci            });
903920e296Sopenharmony_ci            if (ret == result.first->second.end()) {
913920e296Sopenharmony_ci                result.first->second.push_back(resourceItem);
923920e296Sopenharmony_ci                continue;
933920e296Sopenharmony_ci            }
943920e296Sopenharmony_ci            if (tipError) {
953920e296Sopenharmony_ci                cerr << "Error: '"<< resourceItem.GetName() <<"' conflict, first declared.";
963920e296Sopenharmony_ci                cerr << NEW_LINE_PATH << ret->GetFilePath() << endl;
973920e296Sopenharmony_ci                cerr << "but declared again." <<NEW_LINE_PATH << resourceItem.GetFilePath() << endl;
983920e296Sopenharmony_ci                return RESTOOL_ERROR;
993920e296Sopenharmony_ci            }
1003920e296Sopenharmony_ci            cout << "Warning: '"<< resourceItem.GetName() <<"' conflict, first declared.";
1013920e296Sopenharmony_ci            cout << NEW_LINE_PATH << ret->GetFilePath() << endl;
1023920e296Sopenharmony_ci            cout << "but declared again." << NEW_LINE_PATH << resourceItem.GetFilePath() << endl;
1033920e296Sopenharmony_ci        }
1043920e296Sopenharmony_ci    }
1053920e296Sopenharmony_ci    return RESTOOL_SUCCESS;
1063920e296Sopenharmony_ci}
1073920e296Sopenharmony_ci// below private
1083920e296Sopenharmony_civoid ResourceModule::Push(const map<int64_t, std::vector<ResourceItem>> &other)
1093920e296Sopenharmony_ci{
1103920e296Sopenharmony_ci    for (const auto &iter : other) {
1113920e296Sopenharmony_ci        owner_.emplace(iter.first, iter.second);
1123920e296Sopenharmony_ci    }
1133920e296Sopenharmony_ci}
1143920e296Sopenharmony_ci}
1153920e296Sopenharmony_ci}
1163920e296Sopenharmony_ci}
117