Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-Studio-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #include "consoleapp.h"
24 :
25 : #include <QCoreApplication>
26 :
27 : #include "modularity/ioc.h"
28 : #include "audioplugins/iregisteraudiopluginsscenario.h"
29 :
30 : #include "muse_framework_config.h"
31 : #include "app_config.h"
32 :
33 : #include "log.h"
34 : #include "settings.h"
35 :
36 : using namespace muse;
37 : using namespace mu::app;
38 : using namespace mu::converter;
39 :
40 0 : static std::optional<ConvertTarget> parseTarget(const QMap<MuseScoreCmdOptions::ParamKey, QVariant>& params)
41 : {
42 0 : auto it = params.find(MuseScoreCmdOptions::ParamKey::ScoreRegion);
43 0 : if (it != params.end()) {
44 0 : return it.value().toString().toStdString();
45 : }
46 :
47 0 : it = params.find(MuseScoreCmdOptions::ParamKey::PageNumber);
48 0 : if (it == params.end()) {
49 0 : return std::nullopt;
50 : }
51 :
52 0 : bool ok = true;
53 0 : page_num_t num = it.value().toULongLong(&ok) - 1;
54 :
55 0 : if (!ok) {
56 0 : LOGE() << "Invalid page, ignoring...";
57 0 : return std::nullopt;
58 : }
59 :
60 0 : return num;
61 : }
62 :
63 0 : MuseScoreConsoleApp::MuseScoreConsoleApp(const std::shared_ptr<MuseScoreCmdOptions>& options)
64 0 : : muse::ConsoleApplication(options)
65 : {
66 0 : }
67 :
68 0 : void MuseScoreConsoleApp::showSplash()
69 : {
70 0 : std::cout << "================================================" << std::endl;
71 0 : std::cout << "The MuseScore console application is starting..." << std::endl;
72 0 : std::cout << "================================================" << std::endl;
73 0 : }
74 :
75 0 : void MuseScoreConsoleApp::applyCommandLineOptions(const std::shared_ptr<muse::CmdOptions>& opt)
76 : {
77 0 : IF_ASSERT_FAILED(opt) {
78 0 : return;
79 : }
80 :
81 0 : muse::ConsoleApplication::applyCommandLineOptions(opt);
82 :
83 0 : if (opt->runMode == IApplication::RunMode::AudioPluginRegistration) {
84 0 : return;
85 : }
86 :
87 0 : const std::shared_ptr<MuseScoreCmdOptions> options = std::dynamic_pointer_cast<MuseScoreCmdOptions>(opt);
88 0 : IF_ASSERT_FAILED(options) {
89 0 : return;
90 : }
91 :
92 0 : uiConfiguration()->setCustomPhysicalDotsPerInch(options->ui.physicalDotsPerInch);
93 :
94 0 : if (options->network.offline) {
95 0 : networkConfiguration()->setOffline(options->network.offline.value());
96 : }
97 :
98 0 : if (options->audio.outputDisabled) {
99 0 : audioConfiguration()->setOutputDisabled(options->audio.outputDisabled.value());
100 : }
101 :
102 0 : notationConfiguration()->setTemplateModeEnabled(options->notation.templateModeEnabled);
103 0 : notationConfiguration()->setTestModeEnabled(options->notation.testModeEnabled);
104 :
105 0 : if (options->runMode == IApplication::RunMode::ConsoleApp) {
106 0 : project::MigrationOptions migration;
107 0 : migration.appVersion = mu::engraving::Constants::MSC_VERSION;
108 :
109 : //! NOTE Don't ask about migration in convert mode
110 0 : migration.isAskAgain = false;
111 :
112 0 : if (options->project.fullMigration) {
113 0 : bool isMigration = options->project.fullMigration.value();
114 0 : migration.isApplyMigration = isMigration;
115 0 : migration.isApplyEdwin = isMigration;
116 0 : migration.isApplyLeland = isMigration;
117 0 : migration.isRemapPercussion = isMigration;
118 : }
119 :
120 : //! NOTE Don't write to settings, just on current session
121 0 : for (project::MigrationType type : project::allMigrationTypes()) {
122 0 : projectConfiguration()->setMigrationOptions(type, migration, false);
123 : }
124 : }
125 :
126 : #ifdef MUE_BUILD_IMPEXP_IMAGESEXPORT_MODULE
127 0 : imagesExportConfiguration()->setTrimMarginPixelSize(options->exportImage.trimMarginPixelSize);
128 0 : imagesExportConfiguration()->setExportPngDpiResolutionOverride(options->exportImage.pngDpiResolution);
129 : #endif
130 :
131 : #ifdef MUE_BUILD_IMPEXP_VIDEOEXPORT_MODULE
132 0 : videoExportConfiguration()->setResolution(options->exportVideo.resolution);
133 0 : videoExportConfiguration()->setFps(options->exportVideo.fps);
134 0 : videoExportConfiguration()->setLeadingSec(options->exportVideo.leadingSec);
135 0 : videoExportConfiguration()->setTrailingSec(options->exportVideo.trailingSec);
136 : #endif
137 :
138 : #ifdef MUE_BUILD_IMPEXP_MIDI_MODULE
139 0 : midiImportExportConfiguration()->setMidiImportOperationsFile(options->importMidi.operationsFile);
140 : #endif
141 : #ifdef MUE_BUILD_IMPEXP_MUSICXML_MODULE
142 0 : musicXmlConfiguration()->setNeedUseDefaultFontOverride(options->importMusicXml.useDefaultFont);
143 0 : musicXmlConfiguration()->setInferTextTypeOverride(options->importMusicXml.inferTextType);
144 : #endif
145 : #ifdef MUE_BUILD_IMPEXP_AUDIOEXPORT_MODULE
146 0 : audioExportConfiguration()->setExportMp3BitrateOverride(options->exportAudio.mp3Bitrate);
147 : #endif
148 : #ifdef MUE_BUILD_IMPEXP_GUITARPRO_MODULE
149 0 : guitarProConfiguration()->setLinkedTabStaffCreated(options->guitarPro.linkedTabStaffCreated);
150 0 : guitarProConfiguration()->setExperimental(options->guitarPro.experimental);
151 : #endif
152 0 : if (options->app.revertToFactorySettings) {
153 0 : settings()->reset(options->app.revertToFactorySettings.value());
154 : }
155 0 : }
156 :
157 0 : void MuseScoreConsoleApp::doStartupScenario(const muse::modularity::ContextPtr& ctxId)
158 : {
159 0 : const std::shared_ptr<MuseScoreCmdOptions> options = std::dynamic_pointer_cast<MuseScoreCmdOptions>(contextData(ctxId).options);
160 0 : IF_ASSERT_FAILED(options) {
161 0 : return;
162 : }
163 :
164 0 : switch (options->runMode) {
165 0 : case IApplication::RunMode::ConsoleApp: {
166 : // ====================================================
167 : // Process Testflow
168 : // ====================================================
169 : #ifdef MUSE_MODULE_TESTFLOW
170 : if (options->testflow.has_value()) {
171 : testflowCmdRunner()->prepare(ctxId, options->testflow.value());
172 : testflowCmdRunner()->run(ctxId);
173 : break;
174 : }
175 : #endif
176 :
177 : // ====================================================
178 : // Process Diagnostic
179 : // ====================================================
180 0 : MuseScoreCmdOptions::Diagnostic diagnostic = options->diagnostic;
181 0 : if (diagnostic.type != DiagnosticType::Undefined) {
182 0 : int code = processDiagnostic(diagnostic, ctxId);
183 0 : qApp->exit(code);
184 : } else {
185 : // ====================================================
186 : // Process Converter
187 : // ====================================================
188 0 : MuseScoreCmdOptions::ConverterTask task = options->converterTask;
189 0 : int code = processConverter(task, ctxId);
190 0 : qApp->exit(code);
191 0 : }
192 0 : } break;
193 0 : case IApplication::RunMode::AudioPluginRegistration: {
194 0 : MuseScoreCmdOptions::AudioPluginRegistration pluginRegistration = options->audioPluginRegistration;
195 :
196 0 : int code = processAudioPluginRegistration(pluginRegistration, ctxId);
197 0 : qApp->exit(code);
198 0 : } break;
199 0 : default: {
200 0 : UNREACHABLE;
201 : }
202 : }
203 0 : }
204 :
205 0 : int MuseScoreConsoleApp::processConverter(const MuseScoreCmdOptions::ConverterTask& task, const muse::modularity::ContextPtr& ctx)
206 : {
207 0 : muse::ContextInject<converter::IConverterController> converter = { ctx };
208 0 : muse::ContextInject<playback::ISoundProfilesRepository> soundProfilesRepository = { ctx };
209 :
210 0 : Ret ret = make_ret(Ret::Code::Ok);
211 0 : String soundProfile = task.params[MuseScoreCmdOptions::ParamKey::SoundProfile].toString();
212 0 : UriQuery extensionUri = UriQuery(task.params[MuseScoreCmdOptions::ParamKey::ExtensionUri].toString().toStdString());
213 :
214 0 : if (!soundProfile.isEmpty() && !soundProfilesRepository()->containsProfile(soundProfile)) {
215 0 : LOGE() << "Unknown sound profile: " << soundProfile;
216 0 : soundProfile.clear();
217 : }
218 :
219 0 : converter::OpenParams openParams;
220 0 : openParams.stylePath = task.params[MuseScoreCmdOptions::ParamKey::StylePath].toString();
221 0 : openParams.forceMode = task.params[MuseScoreCmdOptions::ParamKey::ForceMode].toBool();
222 0 : openParams.unrollRepeats = task.params[MuseScoreCmdOptions::ParamKey::UnrollRepeats].toBool();
223 :
224 0 : switch (task.type) {
225 0 : case ConvertType::Batch:
226 0 : ret = converter()->batchConvert(task.inputFile, openParams, soundProfile, extensionUri);
227 0 : break;
228 0 : case ConvertType::File: {
229 0 : std::string transposeOptionsJson = task.params[MuseScoreCmdOptions::ParamKey::ScoreTransposeOptions].toString().toStdString();
230 0 : std::optional<ConvertTarget> target = parseTarget(task.params);
231 0 : io::path_t tracksDiffPath = task.params[MuseScoreCmdOptions::ParamKey::TracksDiffPath].toString();
232 0 : ret = converter()->fileConvert(task.inputFile, task.outputFile, openParams, soundProfile, tracksDiffPath,
233 0 : extensionUri, transposeOptionsJson, target);
234 0 : } break;
235 0 : case ConvertType::ConvertScoreParts:
236 0 : ret = converter()->convertScoreParts(task.inputFile, task.outputFile, openParams);
237 0 : break;
238 0 : case ConvertType::ExportScoreMedia: {
239 0 : muse::io::path_t highlightConfigPath = task.params[MuseScoreCmdOptions::ParamKey::HighlightConfigPath].toString();
240 0 : ret = converter()->exportScoreMedia(task.inputFile, task.outputFile, openParams, highlightConfigPath);
241 0 : } break;
242 0 : case ConvertType::ExportScoreMeta:
243 0 : ret = converter()->exportScoreMeta(task.inputFile, task.outputFile, openParams);
244 0 : break;
245 0 : case ConvertType::ExportScoreParts:
246 0 : ret = converter()->exportScoreParts(task.inputFile, task.outputFile, openParams);
247 0 : break;
248 0 : case ConvertType::ExportScorePartsPdf:
249 0 : ret = converter()->exportScorePartsPdfs(task.inputFile, task.outputFile, openParams);
250 0 : break;
251 0 : case ConvertType::ExportScoreTranspose: {
252 0 : std::string scoreTranspose = task.params[MuseScoreCmdOptions::ParamKey::ScoreTransposeOptions].toString().toStdString();
253 0 : ret = converter()->exportScoreTranspose(task.inputFile, task.outputFile, scoreTranspose, openParams);
254 0 : } break;
255 0 : case ConvertType::ExportScoreElements: {
256 0 : ret = converter()->exportScoreElements(task.inputFile, task.outputFile, openParams);
257 0 : } break;
258 0 : case ConvertType::ExportScoreVideo: {
259 0 : bool withAudio = !task.params[MuseScoreCmdOptions::ParamKey::NoAudio].toBool();
260 0 : ret = converter()->exportScoreVideo(task.inputFile, task.outputFile, openParams, withAudio);
261 0 : } break;
262 0 : case ConvertType::SourceUpdate: {
263 0 : std::string scoreSource = task.params[MuseScoreCmdOptions::ParamKey::ScoreSource].toString().toStdString();
264 0 : ret = converter()->updateSource(task.inputFile, scoreSource, openParams.forceMode);
265 0 : } break;
266 : }
267 :
268 0 : if (!ret) {
269 0 : LOGE() << "failed convert, error: " << ret.toString();
270 : }
271 :
272 0 : return ret.code();
273 0 : }
274 :
275 0 : int MuseScoreConsoleApp::processDiagnostic(const MuseScoreCmdOptions::Diagnostic& task, const muse::modularity::ContextPtr&)
276 : {
277 0 : if (!diagnosticDrawProvider()) {
278 0 : return make_ret(Ret::Code::NotSupported);
279 : }
280 :
281 0 : Ret ret = make_ret(Ret::Code::Ok);
282 :
283 0 : if (task.input.isEmpty()) {
284 0 : return make_ret(Ret::Code::UnknownError);
285 : }
286 :
287 0 : io::paths_t input;
288 0 : for (const QString& p : task.input) {
289 0 : input.push_back(p);
290 : }
291 :
292 0 : muse::io::path_t output = task.output;
293 :
294 0 : if (output.empty()) {
295 0 : output = "./";
296 : }
297 :
298 0 : switch (task.type) {
299 0 : case DiagnosticType::GenDrawData:
300 0 : ret = diagnosticDrawProvider()->generateDrawData(input.front(), output);
301 0 : break;
302 0 : case DiagnosticType::ComDrawData: {
303 0 : IF_ASSERT_FAILED(input.size() == 2) {
304 0 : return make_ret(Ret::Code::UnknownError);
305 : }
306 0 : ret = diagnosticDrawProvider()->compareDrawData(input.at(0), input.at(1), output);
307 0 : } break;
308 0 : case DiagnosticType::DrawDataToPng:
309 0 : ret = diagnosticDrawProvider()->drawDataToPng(input.front(), output);
310 0 : break;
311 0 : case DiagnosticType::DrawDiffToPng: {
312 0 : muse::io::path_t diffPath = input.at(0);
313 0 : muse::io::path_t refPath;
314 0 : if (input.size() > 1) {
315 0 : refPath = input.at(1);
316 : }
317 0 : ret = diagnosticDrawProvider()->drawDiffToPng(diffPath, refPath, output);
318 0 : } break;
319 0 : default:
320 0 : break;
321 : }
322 :
323 0 : if (!ret) {
324 0 : LOGE() << "diagnostic ret: " << ret.toString();
325 : }
326 :
327 0 : return ret.code();
328 0 : }
329 :
330 0 : int MuseScoreConsoleApp::processAudioPluginRegistration(const MuseScoreCmdOptions::AudioPluginRegistration& task,
331 : const muse::modularity::ContextPtr& ctx)
332 : {
333 0 : Ret ret = make_ret(Ret::Code::Ok);
334 :
335 0 : muse::ContextInject<audioplugins::IRegisterAudioPluginsScenario> registerAudioPluginsScenario = { ctx };
336 :
337 0 : if (!task.outputFile.empty()) {
338 0 : ret = registerAudioPluginsScenario()->validatePlugin(task.pluginPath, task.outputFile);
339 : } else {
340 0 : ret = registerAudioPluginsScenario()->registerPlugin(task.pluginPath);
341 : }
342 :
343 0 : if (!ret) {
344 0 : LOGE() << ret.toString();
345 : }
346 :
347 0 : return ret.code();
348 0 : }
|