2015-08-27 23:40:20 +08:00
|
|
|
// Aseprite
|
2017-08-12 04:22:28 +08:00
|
|
|
// Copyright (C) 2001-2017 David Capello
|
2015-08-27 23:40:20 +08:00
|
|
|
//
|
2016-08-27 04:02:58 +08:00
|
|
|
// This program is distributed under the terms of
|
|
|
|
|
// the End-User License Agreement for Aseprite.
|
2015-08-27 23:40:20 +08:00
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2016-11-21 21:25:27 +08:00
|
|
|
#ifndef ENABLE_SCRIPTING
|
|
|
|
|
#error ENABLE_SCRIPTING must be defined
|
|
|
|
|
#endif
|
|
|
|
|
|
2015-08-27 23:40:20 +08:00
|
|
|
#include "app/commands/command.h"
|
|
|
|
|
#include "app/commands/params.h"
|
|
|
|
|
#include "app/console.h"
|
|
|
|
|
#include "app/resource_finder.h"
|
2016-04-07 02:37:13 +08:00
|
|
|
#include "app/script/app_scripting.h"
|
2016-11-02 06:14:05 +08:00
|
|
|
#include "base/fs.h"
|
2016-04-07 02:37:13 +08:00
|
|
|
#include "script/engine_delegate.h"
|
2015-08-27 23:40:20 +08:00
|
|
|
#include "ui/manager.h"
|
|
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
|
|
|
|
|
|
namespace app {
|
|
|
|
|
|
2016-04-07 02:37:13 +08:00
|
|
|
class ConsoleEngineDelegate : public script::EngineDelegate {
|
2015-08-27 23:40:20 +08:00
|
|
|
public:
|
|
|
|
|
void onConsolePrint(const char* text) override {
|
|
|
|
|
m_console.printf("%s\n", text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Console m_console;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class RunScriptCommand : public Command {
|
|
|
|
|
public:
|
|
|
|
|
RunScriptCommand();
|
|
|
|
|
Command* clone() const override { return new RunScriptCommand(*this); }
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void onLoadParams(const Params& params) override;
|
|
|
|
|
void onExecute(Context* context) override;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::string m_filename;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
RunScriptCommand::RunScriptCommand()
|
2017-12-02 02:10:21 +08:00
|
|
|
: Command(CommandId::RunScript(), CmdRecordableFlag)
|
2015-08-27 23:40:20 +08:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RunScriptCommand::onLoadParams(const Params& params)
|
|
|
|
|
{
|
|
|
|
|
m_filename = params.get("filename");
|
|
|
|
|
if (base::get_file_path(m_filename).empty()) {
|
|
|
|
|
ResourceFinder rf;
|
|
|
|
|
rf.includeDataDir(base::join_path("scripts", m_filename).c_str());
|
|
|
|
|
if (rf.findFirst())
|
|
|
|
|
m_filename = rf.filename();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RunScriptCommand::onExecute(Context* context)
|
|
|
|
|
{
|
|
|
|
|
ConsoleEngineDelegate delegate;
|
|
|
|
|
AppScripting engine(&delegate);
|
|
|
|
|
engine.evalFile(m_filename);
|
|
|
|
|
|
|
|
|
|
ui::Manager::getDefault()->invalidate();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Command* CommandFactory::createRunScriptCommand()
|
|
|
|
|
{
|
|
|
|
|
return new RunScriptCommand;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace app
|