Debian Codegraph @

plugins/script/interfaces/DeclarationManagerInterface.cpp

raw
1 #include "DeclarationManagerInterface.h"
2
3 namespace script
4 {
5
6 class DeclarationVisitorWrapper :
7 public DeclarationVisitor
8 {
9 public:
10 void visit(const decl::IDeclaration::Ptr& declaration) override
11 {
12 // Wrap this method to python
13 PYBIND11_OVERLOAD_PURE(
14 void, // Return type
15 DeclarationVisitor, // Parent class
16 visit, // Name of function in C++ (must match Python name)
17 ScriptDeclaration(declaration) // Argument(s)
18 );
19 }
20 };
21
22 ScriptDeclaration DeclarationManagerInterface::findDeclaration(decl::Type type, const std::string& name)
23 {
24 return ScriptDeclaration(GlobalDeclarationManager().findDeclaration(type, name));
25 }
26
27 ScriptDeclaration DeclarationManagerInterface::findOrCreateDeclaration(decl::Type type, const std::string& name)
28 {
29 return ScriptDeclaration(GlobalDeclarationManager().findOrCreateDeclaration(type, name));
30 }
31
32 void DeclarationManagerInterface::foreachDeclaration(decl::Type type, DeclarationVisitor& visitor)
33 {
34 GlobalDeclarationManager().foreachDeclaration(type, [&](const decl::IDeclaration::Ptr& decl)
35 {
36 visitor.visit(decl);
37 });
38 }
39
40 bool DeclarationManagerInterface::renameDeclaration(decl::Type type, const std::string& oldName, const std::string& newName)
41 {
42 return GlobalDeclarationManager().renameDeclaration(type, oldName, newName);
43 }
44
45 void DeclarationManagerInterface::removeDeclaration(decl::Type type, const std::string& name)
46 {
47 GlobalDeclarationManager().removeDeclaration(type, name);
48 }
49
50 void DeclarationManagerInterface::reloadDeclarations()
51 {
52 GlobalDeclarationManager().reloadDeclarations();
53 }
54
55 void DeclarationManagerInterface::saveDeclaration(const ScriptDeclaration& decl)
56 {
57 if (!decl.get()) return;
58
59 GlobalDeclarationManager().saveDeclaration(decl.get());
60 }
61
62 void DeclarationManagerInterface::registerInterface(py::module& scope, py::dict& globals)
63 {
64 py::class_<ScriptDeclaration> declaration(scope, "Declaration");
65
66 py::enum_<decl::Type>(declaration, "Type")
67 .value("NullType", decl::Type::None) // None is a reserved word in Python
68 .value("Material", decl::Type::Material)
69 .value("Table", decl::Type::Table)
70 .value("EntityDef", decl::Type::EntityDef)
71 .value("SoundShader", decl::Type::SoundShader)
72 .value("ModelDef", decl::Type::ModelDef)
73 .value("Particle", decl::Type::Particle)
74 .value("Skin", decl::Type::Skin)
75 .value("Fx", decl::Type::Fx)
76 .export_values();
77
78 py::class_<decl::DeclarationBlockSyntax>(scope, "DeclarationBlockSyntax")
79 .def_readwrite("typeName", &decl::DeclarationBlockSyntax::typeName)
80 .def_readwrite("name", &decl::DeclarationBlockSyntax::name)
81 .def_readwrite("contents", &decl::DeclarationBlockSyntax::contents)
82 .def_readwrite("modName", &decl::DeclarationBlockSyntax::modName);
83
84 declaration.def(py::init<const decl::IDeclaration::Ptr&>());
85 declaration.def("isNull", &ScriptDeclaration::isNull);
86 declaration.def("getDeclName", &ScriptDeclaration::getDeclName);
87 declaration.def("getDeclType", &ScriptDeclaration::getDeclType);
88 declaration.def("getBlockSyntax", &ScriptDeclaration::getBlockSyntax);
89 declaration.def("setBlockSyntax", &ScriptDeclaration::setBlockSyntax);
90 declaration.def("getDeclFilePath", &ScriptDeclaration::getDeclFilePath);
91 declaration.def("setDeclFilePath", &ScriptDeclaration::setDeclFilePath);
92
93 py::class_<DeclarationVisitor, DeclarationVisitorWrapper>(scope, "DeclarationVisitor")
94 .def(py::init<>())
95 .def("visit", &DeclarationVisitor::visit);
96
97 // IDeclarationManager interface
98 py::class_<DeclarationManagerInterface>(scope, "DeclarationManager")
99 .def("findDeclaration", &DeclarationManagerInterface::findDeclaration)
100 .def("findOrCreateDeclaration", &DeclarationManagerInterface::findOrCreateDeclaration)
101 .def("foreachDeclaration", &DeclarationManagerInterface::foreachDeclaration)
102 .def("renameDeclaration", &DeclarationManagerInterface::renameDeclaration)
103 .def("removeDeclaration", &DeclarationManagerInterface::removeDeclaration)
104 .def("reloadDeclarations", &DeclarationManagerInterface::reloadDeclarations)
105 .def("saveDeclaration", &DeclarationManagerInterface::saveDeclaration);
106
107 // Now point the Python variable "GlobalDeclarationManager" to this instance
108 globals["GlobalDeclarationManager"] = this;
109 }
110
111 }