aboutsummaryrefslogtreecommitdiff
path: root/libs/plugin_python/src/mobase/mobase.cpp
blob: b1daa2bfef62a8589c58e7dbabfbcfefb20ac9d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#pragma warning(disable : 4100)
#pragma warning(disable : 4996)

#include <format>
#include <tuple>
#include <variant>

#include <pybind11/embed.h>

#include "pybind11_all.h"

#include "wrappers/pyfiletree.h"
#include "wrappers/wrappers.h"

using namespace MOBase;
namespace py = pybind11;

PYBIND11_MODULE(mobase, m)
{
    using namespace mo2::python;

    m.add_object("PyQt6", py::module_::import("PyQt6"));
    m.add_object("PyQt6.QtCore", py::module_::import("PyQt6.QtCore"));
    m.add_object("PyQt6.QtGui", py::module_::import("PyQt6.QtGui"));
    m.add_object("PyQt6.QtWidgets", py::module_::import("PyQt6.QtWidgets"));

    // bindings
    //
    mo2::python::add_basic_bindings(m);
    mo2::python::add_wrapper_bindings(m);

    // game features must be added before plugins
    mo2::python::add_game_feature_bindings(m);
    mo2::python::add_igamefeatures_classes(m);

    mo2::python::add_plugins_bindings(m);

    // widgets
    //
    py::module_ widgets = m.def_submodule("widgets");
    mo2::python::add_widget_bindings(widgets);

    // utils
    //
    py::module_ utils = m.def_submodule("utils");
    mo2::python::add_utils_bindings(utils);

    // functions
    //
    m.def("getFileVersion", wrap_for_filepath(&MOBase::getFileVersion),
          py::arg("filepath"));
    m.def("getProductVersion", wrap_for_filepath(&MOBase::getProductVersion),
          py::arg("executable"));
    m.def("getIconForExecutable", wrap_for_filepath(&MOBase::iconForExecutable),
          py::arg("executable"));

    // typing stuff to be consistent with stubs and allow plugin developers to properly
    // type their code if they want
    {
        m.add_object("TypeVar", py::module_::import("typing").attr("TypeVar"));

        auto s = m.attr("__dict__");

        // expose MoVariant
        //
        // this needs to be defined, otherwise MoVariant is not found when actually
        // running plugins through MO2, making them crash (if plugins use MoVariant in
        // their own code)
        //
        m.add_object(
            "MoVariant",
            py::eval("None | bool | int | str | list[object] | dict[str, object]"));

        // same thing for GameFeatureType
        //
        m.add_object("GameFeatureType", py::eval("TypeVar('GameFeatureType')", s));
    }

    // private stuff for debugging/test
    py::module_ moprivate = m.def_submodule("private");

    // expose a function to create a particular tree, only for debugging
    // purpose, not in mobase.
    mo2::python::add_make_tree_function(moprivate);
    moprivate.def("extract_plugins", &mo2::python::extract_plugins);
}