Skip to content

Commit

Permalink
#6473: Some ground work to load script algorithms from builtin/*.py. …
Browse files Browse the repository at this point in the history
…Add ScriptingSystemInterface skeleton. Refactoring.
  • Loading branch information
codereader committed Jan 27, 2024
1 parent dacbe45 commit 9ac6de8
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 7 deletions.
11 changes: 11 additions & 0 deletions install/scripts/builtin/brush.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import dr as darkradiant

class ResizeSelectedBrushesToBounds:
def execute(self, min: dr.Vector3, max: dr.Vector3, material: str):
print(min)
print(max)
print(material)

import inspect
for name, obj in inspect.getmembers(sys.modules[__name__], inspect.isclass):
print(name)
1 change: 1 addition & 0 deletions plugins/script/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ add_library(script MODULE
interfaces/PatchInterface.cpp
interfaces/RadiantInterface.cpp
interfaces/SceneGraphInterface.cpp
interfaces/ScriptingSystemInterface.cpp
interfaces/SelectionGroupInterface.cpp
interfaces/SelectionInterface.cpp
interfaces/SelectionSetInterface.cpp
Expand Down
1 change: 1 addition & 0 deletions plugins/script/PythonModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <pybind11/attr.h>
#include <pybind11/eval.h>

#include "PythonConsoleWriter.h"
#include "itextstream.h"
#include "os/file.h"
#include "os/path.h"
Expand Down
4 changes: 1 addition & 3 deletions plugins/script/ScriptModule.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#include "itextstream.h"

#include "ScriptingSystem.h"
#include "debugging/debugging.h"

Expand All @@ -11,5 +9,5 @@ extern "C" void DARKRADIANT_DLLEXPORT RegisterModule(IModuleRegistry& registry)
{
module::performDefaultInitialisation(registry);

registry.registerModule(script::ScriptingSystemPtr(new script::ScriptingSystem));
registry.registerModule(std::make_shared<script::ScriptingSystem>());
}
36 changes: 35 additions & 1 deletion plugins/script/ScriptingSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "interfaces/LayerInterface.h"
#include "interfaces/DeclarationManagerInterface.h"
#include "interfaces/FxManagerInterface.h"
#include "interfaces/ScriptingSystemInterface.h"

#include "PythonModule.h"

Expand All @@ -48,6 +49,7 @@ namespace
constexpr const char* const PYTHON_FILE_EXTENSION = "py";
constexpr const char* const SCRIPT_PATH = "scripts/"; // relative to the runtime data folder
constexpr const char* const COMMAND_PATH = "commands/"; // relative to SCRIPT_PATH
constexpr const char* const BUILTIN_COMMAND_PATH = "builtin/"; // relative to SCRIPT_PATH
}

ScriptingSystem::ScriptingSystem() :
Expand Down Expand Up @@ -209,6 +211,34 @@ void ScriptingSystem::reloadScripts()
_sigScriptsReloaded.emit();
}

void ScriptingSystem::registerBuiltInCommands()
{
fs::path start = fs::path(_scriptPath) / BUILTIN_COMMAND_PATH;

if (!fs::exists(start))
{
rWarning() << "Couldn't find built-in scripts folder: " << start.string() << std::endl;
return;
}

for (fs::recursive_directory_iterator it(start); it != fs::recursive_directory_iterator(); ++it)
{
// Get the candidate
const fs::path& candidate = *it;

if (fs::is_directory(candidate)) continue;

if (string::to_lower_copy(os::getExtension(candidate.string())) != PYTHON_FILE_EXTENSION) continue;

initialiseBuiltInCommandFile(os::getRelativePath(candidate.generic_string(), _scriptPath));
}
}

void ScriptingSystem::initialiseBuiltInCommandFile(const std::string& scriptFilename)
{
//_pythonModule->createScriptCommand(_scriptPath, scriptFilename);
}

// RegisterableModule implementation
const std::string& ScriptingSystem::getName() const
{
Expand Down Expand Up @@ -240,7 +270,8 @@ void ScriptingSystem::initialiseModule(const IApplicationContext& ctx)
// Set up the python interpreter
_pythonModule.reset(new PythonModule);

// Add the built-in interfaces (the order is important, as we don't have dependency-resolution yet)
// Add the built-in interfaces (the order is important, as we don't have dependency resolution yet)
addInterface("ScriptingSystem", std::make_shared<ScriptingSystemInterface>(*this));
addInterface("Math", std::make_shared<MathInterface>());
addInterface("GameManager", std::make_shared<GameInterface>());
addInterface("CommandSystem", std::make_shared<CommandSystemInterface>());
Expand Down Expand Up @@ -284,6 +315,9 @@ void ScriptingSystem::initialiseModule(const IApplicationContext& ctx)
{ cmd::ARGTYPE_STRING }
);

// Initialise the python script files containing built-in commands
registerBuiltInCommands();

SceneNodeBuffer::Instance().clear();
}

Expand Down
7 changes: 4 additions & 3 deletions plugins/script/ScriptingSystem.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#pragma once

#include <map>
#include <vector>
#include <sigc++/signal.h>

#include "iscript.h"
#include "iscriptinterface.h"
#include "PythonConsoleWriter.h"
#include "icommandsystem.h"
#include "PythonModule.h"

Expand Down Expand Up @@ -82,7 +81,9 @@ class ScriptingSystem :
void reloadScripts();

void loadCommandScript(const std::string& scriptFilename);

void registerBuiltInCommands();
void initialiseBuiltInCommandFile(const std::string& scriptFilename);
};
typedef std::shared_ptr<ScriptingSystem> ScriptingSystemPtr;

} // namespace script
22 changes: 22 additions & 0 deletions plugins/script/interfaces/ScriptingSystemInterface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "ScriptingSystemInterface.h"

namespace script
{

void ScriptingSystemInterface::registerBuiltinScriptCommand()
{

}

void ScriptingSystemInterface::registerInterface(py::module& scope, py::dict& globals)
{
// Add the ScriptingSystemInterface module declaration to the given python namespace
py::class_<ScriptingSystemInterface> interface(scope, "ScriptingSystemInterface");

interface.def("registerBuiltinScriptCommand", &ScriptingSystemInterface::registerBuiltinScriptCommand);

// Now point the Python variable "GlobalScriptingSystem" to this instance
globals["GlobalScriptingSystem"] = this;
}

}
30 changes: 30 additions & 0 deletions plugins/script/interfaces/ScriptingSystemInterface.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include "iscriptinterface.h"

namespace script
{

class ScriptingSystem;

/**
* Exposes the GlobalScriptingSystem interface to scripts
*/
class ScriptingSystemInterface :
public IScriptInterface
{
private:
ScriptingSystem& _scriptingSystem;

public:
ScriptingSystemInterface(ScriptingSystem& scriptingSystem) :
_scriptingSystem(scriptingSystem)
{}

void registerBuiltinScriptCommand();

// IScriptInterface implementation
void registerInterface(py::module& scope, py::dict& globals) override;
};

}
2 changes: 2 additions & 0 deletions tools/msvc/script.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@
<ClInclude Include="..\..\plugins\script\interfaces\DeclarationManagerInterface.h" />
<ClInclude Include="..\..\plugins\script\interfaces\FxManagerInterface.h" />
<ClInclude Include="..\..\plugins\script\interfaces\LayerInterface.h" />
<ClInclude Include="..\..\plugins\script\interfaces\ScriptingSystemInterface.h" />
<ClInclude Include="..\..\plugins\script\interfaces\SelectionGroupInterface.h" />
<ClInclude Include="..\..\plugins\script\precompiled.h" />
<ClInclude Include="..\..\plugins\script\PythonConsoleWriter.h" />
Expand Down Expand Up @@ -254,6 +255,7 @@
<ClCompile Include="..\..\plugins\script\interfaces\FxManagerInterface.cpp" />
<ClCompile Include="..\..\plugins\script\interfaces\LayerInterface.cpp" />
<ClCompile Include="..\..\plugins\script\interfaces\SceneGraphInterface.cpp" />
<ClCompile Include="..\..\plugins\script\interfaces\ScriptingSystemInterface.cpp" />
<ClCompile Include="..\..\plugins\script\interfaces\SelectionGroupInterface.cpp" />
<ClCompile Include="..\..\plugins\script\precompiled.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
Expand Down
6 changes: 6 additions & 0 deletions tools/msvc/script.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@
<ClInclude Include="..\..\plugins\script\interfaces\FxManagerInterface.h">
<Filter>src\interfaces</Filter>
</ClInclude>
<ClInclude Include="..\..\plugins\script\interfaces\ScriptingSystemInterface.h">
<Filter>src\interfaces</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\plugins\script\SceneNodeBuffer.cpp">
Expand Down Expand Up @@ -195,5 +198,8 @@
<ClCompile Include="..\..\plugins\script\interfaces\FxManagerInterface.cpp">
<Filter>src\interfaces</Filter>
</ClCompile>
<ClCompile Include="..\..\plugins\script\interfaces\ScriptingSystemInterface.cpp">
<Filter>src\interfaces</Filter>
</ClCompile>
</ItemGroup>
</Project>

0 comments on commit 9ac6de8

Please sign in to comment.