1// Aseprite
2// Copyright (C) 2018-2020 Igara Studio S.A.
3// Copyright (C) 2016-2018 David Capello
4//
5// This program is distributed under the terms of
6// the End-User License Agreement for Aseprite.
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include "app/cli/default_cli_delegate.h"
13
14#include "app/cli/app_options.h"
15#include "app/cli/cli_open_file.h"
16#include "app/commands/commands.h"
17#include "app/commands/params.h"
18#include "app/console.h"
19#include "app/doc.h"
20#include "app/doc_exporter.h"
21#include "app/file/palette_file.h"
22#include "app/ui_context.h"
23#include "base/convert_to.h"
24#include "doc/layer.h"
25#include "doc/palette.h"
26#include "doc/slice.h"
27#include "doc/sprite.h"
28#include "doc/tag.h"
29#include "ver/info.h"
30
31#ifdef ENABLE_SCRIPTING
32 #include "app/app.h"
33 #include "app/script/engine.h"
34#endif
35
36#include <iostream>
37#include <memory>
38
39namespace app {
40
41void DefaultCliDelegate::showHelp(const AppOptions& options)
42{
43 std::cout
44 << get_app_name() << " v" << get_app_version()
45 << " | A pixel art program\n"
46 << get_app_copyright()
47 << "\n\nUsage:\n"
48 << " " << options.exeName() << " [OPTIONS] [FILES]...\n\n"
49 << "Options:\n"
50 << options.programOptions()
51 << "\nFind more information in " << get_app_name()
52 << " web site: " << get_app_url() << "\n\n";
53}
54
55void DefaultCliDelegate::showVersion()
56{
57 std::cout << get_app_name() << ' ' << get_app_version() << '\n';
58}
59
60void DefaultCliDelegate::afterOpenFile(const CliOpenFile& cof)
61{
62 if (!cof.document) // Do nothing
63 return;
64
65 if (cof.listLayers) {
66 for (doc::Layer* layer : cof.document->sprite()->allVisibleLayers())
67 std::cout << layer->name() << "\n";
68 }
69
70 if (cof.listTags) {
71 for (doc::Tag* tag : cof.document->sprite()->tags())
72 std::cout << tag->name() << "\n";
73 }
74
75 if (cof.listSlices) {
76 for (doc::Slice* slice : cof.document->sprite()->slices())
77 std::cout << slice->name() << "\n";
78 }
79}
80
81void DefaultCliDelegate::saveFile(Context* ctx, const CliOpenFile& cof)
82{
83 Command* saveAsCommand = Commands::instance()->byId(CommandId::SaveFileCopyAs());
84 Params params;
85 params.set("filename", cof.filename.c_str());
86 params.set("filename-format", cof.filenameFormat.c_str());
87
88 if (cof.hasTag()) {
89 params.set("frame-tag", cof.tag.c_str());
90 }
91 if (cof.hasFrameRange()) {
92 params.set("from-frame", base::convert_to<std::string>(cof.fromFrame).c_str());
93 params.set("to-frame", base::convert_to<std::string>(cof.toFrame).c_str());
94 }
95 if (cof.hasSlice()) {
96 params.set("slice", cof.slice.c_str());
97 }
98
99 if (cof.ignoreEmpty)
100 params.set("ignoreEmpty", "true");
101
102 ctx->executeCommand(saveAsCommand, params);
103}
104
105void DefaultCliDelegate::loadPalette(Context* ctx,
106 const CliOpenFile& cof,
107 const std::string& filename)
108{
109 std::unique_ptr<doc::Palette> palette(load_palette(filename.c_str()));
110 if (palette) {
111 Command* loadPalCommand = Commands::instance()->byId(CommandId::LoadPalette());
112 Params params;
113 params.set("filename", filename.c_str());
114
115 ctx->executeCommand(loadPalCommand, params);
116 }
117 else {
118 Console().printf("Error loading palette in --palette '%s'\n",
119 filename.c_str());
120 }
121}
122
123void DefaultCliDelegate::exportFiles(Context* ctx, DocExporter& exporter)
124{
125 LOG("APP: Exporting sheet...\n");
126
127 base::task_token token;
128 std::unique_ptr<Doc> spriteSheet(
129 exporter.exportSheet(ctx, token));
130
131 // Sprite sheet isn't used, we just delete it.
132
133 LOG("APP: Export sprite sheet: Done\n");
134}
135
136#ifdef ENABLE_SCRIPTING
137int DefaultCliDelegate::execScript(const std::string& filename,
138 const Params& params)
139{
140 auto engine = App::instance()->scriptEngine();
141 if (!engine->evalFile(filename, params))
142 throw base::Exception("Error executing script %s", filename.c_str());
143 return engine->returnCode();
144}
145#endif // ENABLE_SCRIPTING
146
147} // namespace app
148