| 1 |
#include "ScriptingSystem.h" |
| 2 |
|
| 3 |
#include "itextstream.h" |
| 4 |
#include "iundo.h" |
| 5 |
|
| 6 |
#include "interfaces/MathInterface.h" |
| 7 |
#include "interfaces/RegistryInterface.h" |
| 8 |
#include "interfaces/RadiantInterface.h" |
| 9 |
#include "interfaces/SceneGraphInterface.h" |
| 10 |
#include "interfaces/EClassInterface.h" |
| 11 |
#include "interfaces/SelectionInterface.h" |
| 12 |
#include "interfaces/BrushInterface.h" |
| 13 |
#include "interfaces/PatchInterface.h" |
| 14 |
#include "interfaces/EntityInterface.h" |
| 15 |
#include "interfaces/MapInterface.h" |
| 16 |
#include "interfaces/CommandSystemInterface.h" |
| 17 |
#include "interfaces/GameInterface.h" |
| 18 |
#include "interfaces/FileSystemInterface.h" |
| 19 |
#include "interfaces/GridInterface.h" |
| 20 |
#include "interfaces/ShaderSystemInterface.h" |
| 21 |
#include "interfaces/ModelInterface.h" |
| 22 |
#include "interfaces/SkinInterface.h" |
| 23 |
#include "interfaces/SoundInterface.h" |
| 24 |
#include "interfaces/DialogInterface.h" |
| 25 |
#include "interfaces/SelectionSetInterface.h" |
| 26 |
#include "interfaces/SelectionGroupInterface.h" |
| 27 |
#include "interfaces/CameraInterface.h" |
| 28 |
#include "interfaces/LayerInterface.h" |
| 29 |
#include "interfaces/DeclarationManagerInterface.h" |
| 30 |
#include "interfaces/FxManagerInterface.h" |
| 31 |
|
| 32 |
#include "PythonModule.h" |
| 33 |
|
| 34 |
#include "SceneNodeBuffer.h" |
| 35 |
|
| 36 |
#include "os/fs.h" |
| 37 |
#include "os/path.h" |
| 38 |
#include <functional> |
| 39 |
#include "string/case_conv.h" |
| 40 |
|
| 41 |
namespace script |
| 42 |
{ |
| 43 |
|
| 44 |
namespace |
| 45 |
{ |
| 46 |
constexpr const char* const EXAMPLE_SCRIPT_NAME = "Example"; |
| 47 |
constexpr const char* const INIT_SCRIPT_FILENAME = "init.py"; |
| 48 |
constexpr const char* const PYTHON_FILE_EXTENSION = "py"; |
| 49 |
constexpr const char* const SCRIPT_PATH = "scripts/"; // relative to the runtime data folder |
| 50 |
constexpr const char* const COMMAND_PATH = "commands/"; // relative to SCRIPT_PATH |
| 51 |
} |
| 52 |
|
| 53 |
ScriptingSystem::ScriptingSystem() : |
| 54 |
_initialised(false) |
| 55 |
{} |
| 56 |
|
| 57 |
void ScriptingSystem::addInterface(const std::string& name, const IScriptInterfacePtr& iface) |
| 58 |
{ |
| 59 |
_pythonModule->addInterface(NamedInterface(name, iface)); |
| 60 |
} |
| 61 |
|
| 62 |
void ScriptingSystem::executeScriptFile(const std::string& filename) |
| 63 |
{ |
| 64 |
executeScriptFile(filename, false); |
| 65 |
} |
| 66 |
|
| 67 |
void ScriptingSystem::executeScriptFile(const std::string& filename, bool setExecuteCommandAttr) |
| 68 |
{ |
| 69 |
_pythonModule->executeScriptFile(_scriptPath, filename, setExecuteCommandAttr); |
| 70 |
} |
| 71 |
|
| 72 |
ExecutionResultPtr ScriptingSystem::executeString(const std::string& scriptString) |
| 73 |
{ |
| 74 |
return _pythonModule->executeString(scriptString); |
| 75 |
} |
| 76 |
|
| 77 |
void ScriptingSystem::foreachScriptCommand(const std::function<void(const IScriptCommand&)>& functor) |
| 78 |
{ |
| 79 |
for (const auto& pair : _commands) |
| 80 |
{ |
| 81 |
if (pair.first == EXAMPLE_SCRIPT_NAME) continue; // skip the example script |
| 82 |
|
| 83 |
functor(*pair.second); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
sigc::signal<void>& ScriptingSystem::signal_onScriptsReloaded() |
| 88 |
{ |
| 89 |
return _sigScriptsReloaded; |
| 90 |
} |
| 91 |
|
| 92 |
void ScriptingSystem::initialise() |
| 93 |
{ |
| 94 |
// Fire up the interpreter |
| 95 |
_pythonModule->initialise(); |
| 96 |
|
| 97 |
_initialised = true; |
| 98 |
|
| 99 |
// Start the init script |
| 100 |
executeScriptFile(INIT_SCRIPT_FILENAME); |
| 101 |
|
| 102 |
// Search script folder for commands |
| 103 |
reloadScripts(); |
| 104 |
} |
| 105 |
|
| 106 |
void ScriptingSystem::runScriptFile(const cmd::ArgumentList& args) |
| 107 |
{ |
| 108 |
if (args.empty()) return; |
| 109 |
|
| 110 |
executeScriptFile(args[0].getString()); |
| 111 |
} |
| 112 |
|
| 113 |
void ScriptingSystem::runScriptCommand(const cmd::ArgumentList& args) |
| 114 |
{ |
| 115 |
if (args.empty()) return; |
| 116 |
|
| 117 |
executeCommand(args[0].getString()); |
| 118 |
} |
| 119 |
|
| 120 |
void ScriptingSystem::reloadScriptsCmd(const cmd::ArgumentList& args) |
| 121 |
{ |
| 122 |
reloadScripts(); |
| 123 |
} |
| 124 |
|
| 125 |
void ScriptingSystem::executeCommand(const std::string& name) |
| 126 |
{ |
| 127 |
// Sanity check |
| 128 |
if (!_initialised) |
| 129 |
{ |
| 130 |
rError() << "Cannot execute script command " << name |
| 131 |
<< ", ScriptingSystem not initialised yet." << std::endl; |
| 132 |
return; |
| 133 |
} |
| 134 |
|
| 135 |
// Lookup the name |
| 136 |
auto found = _commands.find(name); |
| 137 |
|
| 138 |
if (found == _commands.end()) |
| 139 |
{ |
| 140 |
rError() << "Couldn't find command " << name << std::endl; |
| 141 |
return; |
| 142 |
} |
| 143 |
|
| 144 |
UndoableCommand cmd("runScriptCommand " + name); |
| 145 |
|
| 146 |
// Execute the script file behind this command |
| 147 |
executeScriptFile(found->second->getFilename(), true); |
| 148 |
} |
| 149 |
|
| 150 |
void ScriptingSystem::loadCommandScript(const std::string& scriptFilename) |
| 151 |
{ |
| 152 |
auto command = _pythonModule->createScriptCommand(_scriptPath, scriptFilename); |
| 153 |
|
| 154 |
if (!command) |
| 155 |
{ |
| 156 |
// The python module already emitted some errors to the log, just exit |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
// Try to register this named command |
| 161 |
auto result = _commands.emplace(command->getName(), command); |
| 162 |
|
| 163 |
// Result.second is TRUE if the insert succeeded |
| 164 |
if (result.second) |
| 165 |
{ |
| 166 |
rMessage() << "Registered script file " << scriptFilename << " as " << command->getName() << std::endl; |
| 167 |
} |
| 168 |
else |
| 169 |
{ |
| 170 |
rError() << "Error in " << scriptFilename << ": Script command " |
| 171 |
<< command->getName() << " has already been registered in " |
| 172 |
<< _commands[command->getName()]->getFilename() << std::endl; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
void ScriptingSystem::reloadScripts() |
| 177 |
{ |
| 178 |
// Release all previously allocated commands |
| 179 |
_commands.clear(); |
| 180 |
|
| 181 |
// Initialise the search's starting point |
| 182 |
fs::path start = fs::path(_scriptPath) / COMMAND_PATH; |
| 183 |
|
| 184 |
if (!fs::exists(start)) |
| 185 |
{ |
| 186 |
rWarning() << "Couldn't find scripts folder: " << start.string() << std::endl; |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
for (fs::recursive_directory_iterator it(start); |
| 191 |
it != fs::recursive_directory_iterator(); ++it) |
| 192 |
{ |
| 193 |
// Get the candidate |
| 194 |
const fs::path& candidate = *it; |
| 195 |
|
| 196 |
if (fs::is_directory(candidate)) continue; |
| 197 |
|
| 198 |
std::string extension = os::getExtension(candidate.string()); |
| 199 |
string::to_lower(extension); |
| 200 |
|
| 201 |
if (extension != PYTHON_FILE_EXTENSION) continue; |
| 202 |
|
| 203 |
// Script file found, construct a new command |
| 204 |
loadCommandScript(os::getRelativePath(candidate.generic_string(), _scriptPath)); |
| 205 |
} |
| 206 |
|
| 207 |
rMessage() << "ScriptModule: Found " << _commands.size() << " commands." << std::endl; |
| 208 |
|
| 209 |
_sigScriptsReloaded.emit(); |
| 210 |
} |
| 211 |
|
| 212 |
// RegisterableModule implementation |
| 213 |
const std::string& ScriptingSystem::getName() const |
| 214 |
{ |
| 215 |
static std::string _name(MODULE_SCRIPTING_SYSTEM); |
| 216 |
return _name; |
| 217 |
} |
| 218 |
|
| 219 |
const StringSet& ScriptingSystem::getDependencies() const |
| 220 |
{ |
| 221 |
static StringSet _dependencies; |
| 222 |
|
| 223 |
if (_dependencies.empty()) |
| 224 |
{ |
| 225 |
_dependencies.insert(MODULE_COMMANDSYSTEM); |
| 226 |
} |
| 227 |
|
| 228 |
return _dependencies; |
| 229 |
} |
| 230 |
|
| 231 |
void ScriptingSystem::initialiseModule(const IApplicationContext& ctx) |
| 232 |
{ |
| 233 |
// Subscribe to get notified as soon as Radiant is fully initialised |
| 234 |
module::GlobalModuleRegistry().signal_allModulesInitialised() |
| 235 |
.connect(sigc::mem_fun(*this, &ScriptingSystem::initialise)); |
| 236 |
|
| 237 |
// Construct the script path |
| 238 |
_scriptPath = ctx.getRuntimeDataPath() + SCRIPT_PATH; |
| 239 |
|
| 240 |
// Set up the python interpreter |
| 241 |
_pythonModule.reset(new PythonModule); |
| 242 |
|
| 243 |
// Add the built-in interfaces (the order is important, as we don't have dependency-resolution yet) |
| 244 |
addInterface("Math", std::make_shared<MathInterface>()); |
| 245 |
addInterface("GameManager", std::make_shared<GameInterface>()); |
| 246 |
addInterface("CommandSystem", std::make_shared<CommandSystemInterface>()); |
| 247 |
addInterface("SceneGraph", std::make_shared<SceneGraphInterface>()); |
| 248 |
addInterface("GlobalRegistry", std::make_shared<RegistryInterface>()); |
| 249 |
addInterface("GlobalEntityClassManager", std::make_shared<EClassManagerInterface>()); |
| 250 |
addInterface("GlobalSelectionSystem", std::make_shared<SelectionInterface>()); |
| 251 |
addInterface("Brush", std::make_shared<BrushInterface>()); |
| 252 |
addInterface("Patch", std::make_shared<PatchInterface>()); |
| 253 |
addInterface("Entity", std::make_shared<EntityInterface>()); |
| 254 |
addInterface("Radiant", std::make_shared<RadiantInterface>()); |
| 255 |
addInterface("Map", std::make_shared<MapInterface>()); |
| 256 |
addInterface("FileSystem", std::make_shared<FileSystemInterface>()); |
| 257 |
addInterface("Grid", std::make_shared<GridInterface>()); |
| 258 |
addInterface("ShaderSystem", std::make_shared<ShaderSystemInterface>()); |
| 259 |
addInterface("Model", std::make_shared<ModelInterface>()); |
| 260 |
addInterface("ModelSkinCacheInterface", std::make_shared<ModelSkinCacheInterface>()); |
| 261 |
addInterface("SoundManager", std::make_shared<SoundManagerInterface>()); |
| 262 |
addInterface("DialogInterface", std::make_shared<DialogManagerInterface>()); |
| 263 |
addInterface("SelectionSetInterface", std::make_shared<SelectionSetInterface>()); |
| 264 |
addInterface("SelectionGroupInterface", std::make_shared<SelectionGroupInterface>()); |
| 265 |
addInterface("CameraInterface", std::make_shared<CameraInterface>()); |
| 266 |
addInterface("LayerInterface", std::make_shared<LayerInterface>()); |
| 267 |
addInterface("DeclarationManager", std::make_shared<DeclarationManagerInterface>()); |
| 268 |
addInterface("FxManager", std::make_shared<FxManagerInterface>()); |
| 269 |
|
| 270 |
GlobalCommandSystem().addCommand( |
| 271 |
"RunScript", |
| 272 |
std::bind(&ScriptingSystem::runScriptFile, this, std::placeholders::_1), |
| 273 |
{ cmd::ARGTYPE_STRING } |
| 274 |
); |
| 275 |
|
| 276 |
GlobalCommandSystem().addCommand( |
| 277 |
"ReloadScripts", |
| 278 |
std::bind(&ScriptingSystem::reloadScriptsCmd, this, std::placeholders::_1) |
| 279 |
); |
| 280 |
|
| 281 |
GlobalCommandSystem().addCommand( |
| 282 |
"RunScriptCommand", |
| 283 |
std::bind(&ScriptingSystem::runScriptCommand, this, std::placeholders::_1), |
| 284 |
{ cmd::ARGTYPE_STRING } |
| 285 |
); |
| 286 |
|
| 287 |
SceneNodeBuffer::Instance().clear(); |
| 288 |
} |
| 289 |
|
| 290 |
void ScriptingSystem::shutdownModule() |
| 291 |
{ |
| 292 |
rMessage() << getName() << "::shutdownModule called." << std::endl; |
| 293 |
|
| 294 |
_sigScriptsReloaded.clear(); |
| 295 |
|
| 296 |
_initialised = false; |
| 297 |
|
| 298 |
// Clear the buffer so that nodes finally get destructed |
| 299 |
SceneNodeBuffer::Instance().clear(); |
| 300 |
|
| 301 |
_commands.clear(); |
| 302 |
|
| 303 |
_scriptPath.clear(); |
| 304 |
|
| 305 |
_pythonModule.reset(); |
| 306 |
} |
| 307 |
|
| 308 |
} // namespace script |