| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <map> |
| 4 |
#include <vector> |
| 5 |
|
| 6 |
#include "iscript.h" |
| 7 |
#include "iscriptinterface.h" |
| 8 |
#include "PythonConsoleWriter.h" |
| 9 |
#include "icommandsystem.h" |
| 10 |
#include "PythonModule.h" |
| 11 |
|
| 12 |
#include "ScriptCommand.h" |
| 13 |
|
| 14 |
namespace script |
| 15 |
{ |
| 16 |
|
| 17 |
// Forward declaration |
| 18 |
class StartupListener; |
| 19 |
typedef std::shared_ptr<StartupListener> StartupListenerPtr; |
| 20 |
|
| 21 |
class ScriptingSystem : |
| 22 |
public IScriptingSystem |
| 23 |
{ |
| 24 |
private: |
| 25 |
bool _initialised; |
| 26 |
std::unique_ptr<PythonModule> _pythonModule; |
| 27 |
|
| 28 |
// The path where the script files are hosted |
| 29 |
std::string _scriptPath; |
| 30 |
|
| 31 |
// All named script commands (pointing to .py files) |
| 32 |
std::map<std::string, ScriptCommand::Ptr> _commands; |
| 33 |
|
| 34 |
sigc::signal<void> _sigScriptsReloaded; |
| 35 |
|
| 36 |
public: |
| 37 |
ScriptingSystem(); |
| 38 |
|
| 39 |
// Adds a script interface to this system |
| 40 |
void addInterface(const std::string& name, const IScriptInterfacePtr& iface) override; |
| 41 |
|
| 42 |
// (Re)loads all scripts from the scripts/ folder |
| 43 |
void reloadScriptsCmd(const cmd::ArgumentList& args); |
| 44 |
|
| 45 |
/** |
| 46 |
* This actually initialises the Scripting System, adding all |
| 47 |
* registered interfaces to the Python context. After this call |
| 48 |
* the scripting system is ready for use. |
| 49 |
* |
| 50 |
* This method also invokes "scripts/init.py" when done. |
| 51 |
*/ |
| 52 |
void initialise(); |
| 53 |
|
| 54 |
// Runs a specific script file (command target) |
| 55 |
void runScriptFile(const cmd::ArgumentList& args); |
| 56 |
|
| 57 |
// Runs a named script command (command target) |
| 58 |
void runScriptCommand(const cmd::ArgumentList& args); |
| 59 |
|
| 60 |
// Executes a script file |
| 61 |
void executeScriptFile(const std::string& filename) override; |
| 62 |
|
| 63 |
// Execute the given python script string |
| 64 |
ExecutionResultPtr executeString(const std::string& scriptString) override; |
| 65 |
|
| 66 |
void foreachScriptCommand(const std::function<void(const IScriptCommand&)>& functor) override; |
| 67 |
|
| 68 |
sigc::signal<void>& signal_onScriptsReloaded() override; |
| 69 |
|
| 70 |
// Runs the named command (or rather the .py file behind it) |
| 71 |
void executeCommand(const std::string& name); |
| 72 |
|
| 73 |
// RegisterableModule implementation |
| 74 |
const std::string& getName() const override; |
| 75 |
const StringSet& getDependencies() const override; |
| 76 |
void initialiseModule(const IApplicationContext& ctx) override; |
| 77 |
void shutdownModule() override; |
| 78 |
|
| 79 |
private: |
| 80 |
void executeScriptFile(const std::string& filename, bool setExecuteCommandAttr); |
| 81 |
|
| 82 |
void reloadScripts(); |
| 83 |
|
| 84 |
void loadCommandScript(const std::string& scriptFilename); |
| 85 |
}; |
| 86 |
typedef std::shared_ptr<ScriptingSystem> ScriptingSystemPtr; |
| 87 |
|
| 88 |
} // namespace script |