Skip to content

Commit

Permalink
#6023: DeclarationVisitor interface added
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Jul 28, 2022
1 parent c58edd6 commit 81b57a2
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
11 changes: 11 additions & 0 deletions install/scripts/test.py
Expand Up @@ -9,6 +9,17 @@
worldspawn.setKeyValue('test', 'success')
print('Worldspawn edited')

# Test the DeclarationManager interface
class TestDeclarationVisitor(dr.DeclarationVisitor) :
def visit(self, decl):
print(decl)

visitor = TestDeclarationVisitor()

# Visit all skins and modeldefs
GlobalDeclarationManager.foreachDeclaration(Declaration.Type.Skin, visitor)
GlobalDeclarationManager.foreachDeclaration(Declaration.Type.ModelDef, visitor)

# Test the EClassManager interface
eclass = GlobalEntityClassManager.findClass('atdm:func_shooter')
print(eclass.getAttribute('editor_usage').getValue())
Expand Down
34 changes: 34 additions & 0 deletions plugins/script/interfaces/DeclarationManagerInterface.cpp
Expand Up @@ -3,11 +3,35 @@
namespace script
{

class DeclarationVisitorWrapper :
public DeclarationVisitor
{
public:
void visit(const decl::IDeclaration::Ptr& declaration) override
{
// Wrap this method to python
PYBIND11_OVERLOAD_PURE(
void, // Return type
DeclarationVisitor, // Parent class
visit, // Name of function in C++ (must match Python name)
ScriptDeclaration(declaration) // Argument(s)
);
}
};

ScriptDeclaration DeclarationManagerInterface::findDeclaration(decl::Type type, const std::string& name)
{
return ScriptDeclaration({});
}

void DeclarationManagerInterface::foreachDeclaration(decl::Type type, DeclarationVisitor& visitor)
{
GlobalDeclarationManager().foreachDeclaration(type, [&](const decl::IDeclaration::Ptr& decl)
{
visitor.visit(decl);
});
}

void DeclarationManagerInterface::registerInterface(py::module& scope, py::dict& globals)
{
py::class_<ScriptDeclaration> declaration(scope, "Declaration");
Expand All @@ -23,7 +47,17 @@ void DeclarationManagerInterface::registerInterface(py::module& scope, py::dict&
.value("Skin", decl::Type::Skin)
.export_values();

py::class_<DeclarationVisitor, DeclarationVisitorWrapper> visitor(scope, "DeclarationVisitor");
visitor.def(py::init<>());
visitor.def("visit", &DeclarationVisitor::visit);

// IDeclarationManager interface
py::class_<DeclarationManagerInterface> materialManager(scope, "DeclarationManager");

materialManager.def("foreachDeclaration", &DeclarationManagerInterface::foreachDeclaration);

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

}
9 changes: 9 additions & 0 deletions plugins/script/interfaces/DeclarationManagerInterface.h
Expand Up @@ -20,6 +20,13 @@ class ScriptDeclaration
// TODO
};

class DeclarationVisitor
{
public:
virtual ~DeclarationVisitor() {}
virtual void visit(const decl::IDeclaration::Ptr& declaration) = 0;
};

/**
* Exposes the GlobalDeclarationManager interface to scripts
*/
Expand All @@ -29,6 +36,8 @@ class DeclarationManagerInterface :
public:
// Mapped methods
ScriptDeclaration findDeclaration(decl::Type type, const std::string& name);
ScriptDeclaration findOrCreateDeclaration(decl::Type type, const std::string& name);
void foreachDeclaration(decl::Type type, DeclarationVisitor& visitor);

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

0 comments on commit 81b57a2

Please sign in to comment.