diff options
| author | SulfurNitride <SulfurNitride@users.noreply.github.com> | 2026-02-11 02:37:39 -0600 |
|---|---|---|
| committer | SulfurNitride <SulfurNitride@users.noreply.github.com> | 2026-02-11 02:37:39 -0600 |
| commit | 7ee008e150bc5bcf76082d726f719ee0fdfda982 (patch) | |
| tree | 27fb39be241fdb5ac2734c574de678977d1856d0 /libs/plugin_python/src | |
Fluorine Manager: full Linux port of Mod Organizer 2
Complete native Linux port with FUSE-based virtual filesystem,
Proton/umu-run integration, and Flatpak packaging.
Key features:
- FUSE VFS replacing Windows USVFS (in-process + standalone helper for Flatpak)
- Proton/GE-Proton/umu-run launcher with env var forwarding
- Flatpak support (sandbox-aware VFS, NXM handler, umu-run)
- Wine prefix management UI
- Case-insensitive path resolution for Linux filesystems
- QSettings-safe INI handling (avoids Bethesda INI corruption)
- Portable instance support with auto-generated launcher scripts
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Diffstat (limited to 'libs/plugin_python/src')
54 files changed, 6885 insertions, 0 deletions
diff --git a/libs/plugin_python/src/CMakeLists.txt b/libs/plugin_python/src/CMakeLists.txt new file mode 100644 index 0000000..533a5ab --- /dev/null +++ b/libs/plugin_python/src/CMakeLists.txt @@ -0,0 +1,8 @@ +cmake_minimum_required(VERSION 3.16) + +# order matters +add_subdirectory(pybind11-qt) +add_subdirectory(pybind11-utils) +add_subdirectory(mobase) +add_subdirectory(runner) +add_subdirectory(proxy) diff --git a/libs/plugin_python/src/mobase/CMakeLists.txt b/libs/plugin_python/src/mobase/CMakeLists.txt new file mode 100644 index 0000000..2fac52f --- /dev/null +++ b/libs/plugin_python/src/mobase/CMakeLists.txt @@ -0,0 +1,40 @@ +cmake_minimum_required(VERSION 3.16) + +find_package(Qt6 COMPONENTS Core) +if(NOT TARGET mo2::uibase) + find_package(mo2-uibase CONFIG REQUIRED) +endif() + +pybind11_add_module(mobase MODULE) +mo2_default_source_group() +mo2_configure_target(mobase + NO_SOURCES + WARNINGS 4 + EXTERNAL_WARNINGS 4 + AUTOMOC ON + TRANSLATIONS OFF +) +mo2_target_sources(mobase + FOLDER src + PRIVATE + deprecation.cpp + deprecation.h + mobase.cpp + pybind11_all.h +) +mo2_target_sources(mobase + FOLDER src/wrappers + PRIVATE + ./wrappers/basic_classes.cpp + ./wrappers/game_features.cpp + ./wrappers/known_folders.h + ./wrappers/pyfiletree.cpp + ./wrappers/pyfiletree.h + ./wrappers/pyplugins.cpp + ./wrappers/pyplugins.h + ./wrappers/utils.cpp + ./wrappers/widgets.cpp + ./wrappers/wrappers.cpp + ./wrappers/wrappers.h +) +target_link_libraries(mobase PRIVATE pybind11::qt pybind11::utils mo2::uibase Qt6::Core) diff --git a/libs/plugin_python/src/mobase/README.md b/libs/plugin_python/src/mobase/README.md new file mode 100644 index 0000000..b4d184e --- /dev/null +++ b/libs/plugin_python/src/mobase/README.md @@ -0,0 +1,110 @@ +# mobase + +`mobase` is a ModOrganizer2 Python API. +It provides access to the part of +[`uibase`](https://github.com/ModOrganizer2/modorganizer-uibase) C++ API from +Python through [`pybind11`](https://github.com/pybind/pybind11). + +## Organization + +**Important:** All (most) files should include `pybind11_all.h` (either directly +or through another header) to get proper `type_caster` available. + +- `mobase.cpp` contains the `PYBIND11_MODULE` definition of `mobase` but is otherwise + the entrypoint for other functions. +- `wrappers.h` contains the declaration of most functions implemented under + `wrappers/`. +- The other files under `wrappers/` contains bindings and trampoline classes (see + below) for `uibase` classes. + - `basic_classes.cpp` contains the bindings for most classes that cannot be extended + in Python (`IOrganizer`, `IModInterface`, etc.) + - `game_features.cpp` contains the bindings and trampoline classes for game features. + - `pyfiletree.h` and `pyfiletree.cpp` contains bindings for the `IFileTree`-related + classes. + - `pyplugins.h` contains the trampoline classes for the `IPluginXXX` classes and + `pyplugins.cpp` the bindings. + - `pyplugins.h` is required since the trampoline classes are tagged with `Q_OBJECT`, + and MOC does not work if the classes are declared in a C++ file. + - `pyplugins.cpp` also contains the `extract_plugins` function in `mobase.private` + that is used to extract plugins from Python object in the runner. + - `widgets.cpp` contains the bindings for the widget classes. + - `wrappers.cpp` contains the trampoline and bindings for non-plugin classes that can + be extended through Python. + +## Updating mobase + +### Classes that cannot be extended through Python + +Updating or adding classes that cannot be extended through Python is quite easily. +One simply needs to declare the appropriate `py::class_` or add new `.def()`. + +See below for things to remember when creating pybind11 bindings. + +### Free functions + +Similar to classes that cannot be extended through Python, see above. + +### Classes that can be extended through Python: Plugins + +To extend plugins, simply update the trampoline classes in `pyplugins.h` and the +bindings in `pyplugins.cpp`. + +**Note:** For new plugins, simply look at the existing one. + +### Classes that can be extended through Python: Game Features + +To extend or expose game features: + +- Create (if there is not already one) a trampoline class for the feature in + `game_features.cpp`. + - Add implementation of missing functions if required. +- Add the bindings in `add_game_feature_bindings` in `game_features.cpp`. +- For new feature, add the feature type to `GameFeaturesHelper::GameFeatures` in + `game_features.cpp`. + +### Classes that can be extended through Python: Others + +Non-plugin classes should be added to the `wrappers.cpp` file and should be exposed +with `std::shared_ptr<>` or `qobject_holder<>` holders. + +- If the classes extends `QObject`, use a `qobject_holder`. +- Otherwise use a `std::shared_ptr<>` holder and add a `MO2_PYBIND11_SHARED_CPP_HOLDER` + declaration in `pybind11_all.h`. + +Trampoline can be defined directly in `wrappers.cpp`, and bindings in the appropriate +function. +See the existing classes for example. + +**Important:** +You need to make sure that `uibase` manipulates such classes through +`std::shared_ptr<>` (unless those inherit `QObject`). +Using `std::unique_ptr<>` is not possible since `std::unique_ptr<>` cannot have custom +runtime-specified deleters. + +## Things to remember + +Here are a few things to remember when creating bindings: + +- If a function has multiple overloads that can conflict in Python, the more complex + one must be defined first as pybind11 will try calling them in order. +- If a C++ function expect a `QString`, `QFileInfo` or `QDir` that represents a file or + a directory, wrapping the function with `wrap_for_filepath` or `wrap_for_directory` is + a good idea. This allows Python to call the function with `pathlib.Path`. +- Most of the C++ function taking a reference to modify in C++ cannot be directly + exposed in Python since Python cannot modify reference to simple type (e.g. + `QString&` or `int&`). + The best way to expose such function is to bind a lambda that returns a variant from + Python, e.g. + +```cpp +// assume the C++ function is QString fn(QString& foo, QString const& bar, int& maz); + +m.def("function", [](QString& foo, QString const& bar, int& maz) { + // call the function + auto ret = function(foo, bar, maz); + + // make a tuple containing the return value (if there is one), and the modified + // values passed by reference + return std::make_tuple(ret, foo, maz); +}); +``` diff --git a/libs/plugin_python/src/mobase/deprecation.cpp b/libs/plugin_python/src/mobase/deprecation.cpp new file mode 100644 index 0000000..958712d --- /dev/null +++ b/libs/plugin_python/src/mobase/deprecation.cpp @@ -0,0 +1,55 @@ +#include "deprecation.h" + +#include <filesystem> +#include <set> + +#include <pybind11/pybind11.h> + +#include <QCoreApplication> + +#include <uibase/log.h> + +namespace py = pybind11; + +namespace mo2::python { + + void show_deprecation_warning(std::string_view name, std::string_view message, + bool show_once) + { + + // Contains the list of filename / line number for which a deprecation + // warning has already been shown. + static std::set<std::pair<std::string, int>> DeprecatedLines; + + // Find the caller: + auto inspect = py::module_::import("inspect"); + auto current_frame = inspect.attr("currentframe")(); + py::sequence callable_frame = inspect.attr("getouterframes")(current_frame, 2); + auto last_frame = callable_frame[py::int_(-1)]; + auto filename = last_frame.attr("filename").cast<std::string>(); + auto function = last_frame.attr("function").cast<std::string>(); + auto lineno = last_frame.attr("lineno").cast<int>(); + + // Only show once if requested: + if (show_once && DeprecatedLines.contains({filename, lineno})) { + return; + } + + // Register the deprecation: + DeprecatedLines.emplace(filename, lineno); + + auto path = relative(std::filesystem::path(filename), + QCoreApplication::applicationDirPath().toStdWString()); + + // Show the message: + if (message.empty()) { + MOBase::log::warn("[deprecated] {} in {} [{}:{}].", name, function, + path.native(), lineno); + } + else { + MOBase::log::warn("[deprecated] {} in {} [{}:{}]: {}", name, function, + path.native(), lineno, message); + } + } + +} // namespace mo2::python diff --git a/libs/plugin_python/src/mobase/deprecation.h b/libs/plugin_python/src/mobase/deprecation.h new file mode 100644 index 0000000..d9a445c --- /dev/null +++ b/libs/plugin_python/src/mobase/deprecation.h @@ -0,0 +1,27 @@ +#ifndef PYTHONRUNNER_UTILS_H +#define PYTHONRUNNER_UTILS_H + +#include <string_view> + +#include <pybind11/pybind11.h> + +namespace mo2::python { + + /** + * @brief Show a deprecation warning. + * + * This methods will print a warning in MO2 log containing the location of + * the call to the deprecated function. If show_once is true, the + * deprecation warning will only be logged the first time the function is + * called at this location. + * + * @param name Name of the deprecated function. + * @param message Deprecation message. + * @param show_once Only show the message once per call location. + */ + void show_deprecation_warning(std::string_view name, std::string_view message = "", + bool show_once = true); + +} // namespace mo2::python + +#endif diff --git a/libs/plugin_python/src/mobase/mobase.cpp b/libs/plugin_python/src/mobase/mobase.cpp new file mode 100644 index 0000000..b1daa2b --- /dev/null +++ b/libs/plugin_python/src/mobase/mobase.cpp @@ -0,0 +1,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); +} diff --git a/libs/plugin_python/src/mobase/pybind11_all.h b/libs/plugin_python/src/mobase/pybind11_all.h new file mode 100644 index 0000000..c92d7ff --- /dev/null +++ b/libs/plugin_python/src/mobase/pybind11_all.h @@ -0,0 +1,146 @@ +#ifndef PYTHON_PYBIND11_ALL_H +#define PYTHON_PYBIND11_ALL_H + +#include <filesystem> + +#include <QDir> + +#include <pybind11/operators.h> +#include <pybind11/pybind11.h> +#include <pybind11/stl.h> +#include <pybind11/stl/filesystem.h> + +#include "pybind11_qt/pybind11_qt.h" + +#include "pybind11_utils/functional.h" +#include "pybind11_utils/generator.h" +#include "pybind11_utils/shared_cpp_owner.h" +#include "pybind11_utils/smart_variant_wrapper.h" + +#include <uibase/game_features/game_feature.h> +#include <uibase/isavegame.h> +#include <uibase/pluginrequirements.h> + +namespace mo2::python { + + namespace detail { + + template <> + struct smart_variant_converter<QString> { + + static QString from(std::filesystem::path const& path) + { + return QString::fromStdString(path.string()); + } + + static QString from(QFileInfo const& fileInfo) + { + return fileInfo.filePath(); + } + + static QString from(QDir const& dir) { return dir.path(); } + }; + + template <> + struct smart_variant_converter<std::filesystem::path> { + + static std::filesystem::path from(QString const& value) + { + return {value.toStdWString()}; + } + + static std::filesystem::path from(QFileInfo const& fileInfo) + { + return fileInfo.filesystemFilePath(); + } + + static std::filesystem::path from(QDir const& dir) + { + return dir.filesystemPath(); + } + }; + + // we do not need specialization for QFileInfo and QDir because both of them can + // be constructed from std::filesystem::path and QString already + + } // namespace detail + + using FileWrapper = smart_variant<QString, std::filesystem::path, QFileInfo>; + using DirectoryWrapper = smart_variant<QString, std::filesystem::path, QDir>; + + // wrap the given function to accept FileWrapper (str | PathLike | QFileInfo) at the + // given argument positions (or any valid positions if Is... is empty) + // + template <std::size_t... Is, class Fn> + auto wrap_for_filepath(Fn&& fn) + { + return mo2::python::wrap_arguments<FileWrapper, Is...>(std::forward<Fn>(fn)); + } + + // wrap the given function to accept DirectoryWrapper (str | PathLike | QDir) + // at the given argument positions (or any valid positions if Is... is empty) + // + template <std::size_t... Is, class Fn> + auto wrap_for_directory(Fn&& fn) + { + return mo2::python::wrap_arguments<DirectoryWrapper, Is...>( + std::forward<Fn>(fn)); + } + + // wrap a function-like object to return a FileWrapper instead of its return type, + // useful to generate proper typing in Python + // + // note that QFileInfo has a __fspath__ in Python, so it is quite easy to convert + // from "FileWrapper", a.k.a., str | os.PathLike | QFileInfo to Path by simply + // calling Path() on the return type if necessary + // + // this should be combined with custom return-value in PYBIND11_OVERRIDE(_PURE), see + // ISaveGame binding for an example + // + template <class Fn> + auto wrap_return_for_filepath(Fn&& fn) + { + return mo2::python::wrap_return<FileWrapper>(std::forward<Fn>(fn)); + } + + // similar to wrap_return_for_filepath, except it returns a DirectoryWrapper instead + // of its return type + // + // this is much less practical than wrap_return_for_filepath since QDir does not + // expose __fspath__, so more complex things need to be done in Python, which is why + // this should be used carefully (e.g., should not be used if the return type is + // already QDir) + // + template <class Fn> + auto wrap_return_for_directory(Fn&& fn) + { + return mo2::python::wrap_return<DirectoryWrapper>(std::forward<Fn>(fn)); + } + + // convert a QList to QStringList - QString must be constructible from QString + // + template <class T> + QStringList toQStringList(QList<T> const& list) + { + static_assert(std::is_constructible_v<QString, T>, + "QString must be constructible from T."); + return {list.begin(), list.end()}; + } + + // convert a QStringList to a QList - T must be constructible from QString + // + template <class T> + QList<T> toQList(QStringList const& list) + { + static_assert(std::is_constructible_v<T, QString>, + "T must be constructible from QString."); + return {list.begin(), list.end()}; + } + +} // namespace mo2::python + +MO2_PYBIND11_SHARED_CPP_HOLDER(MOBase::IPluginRequirement) +MO2_PYBIND11_SHARED_CPP_HOLDER(MOBase::ISaveGame) +MO2_PYBIND11_SHARED_CPP_HOLDER(MOBase::GameFeature) + +#endif diff --git a/libs/plugin_python/src/mobase/wrappers/basic_classes.cpp b/libs/plugin_python/src/mobase/wrappers/basic_classes.cpp new file mode 100644 index 0000000..ea69a46 --- /dev/null +++ b/libs/plugin_python/src/mobase/wrappers/basic_classes.cpp @@ -0,0 +1,952 @@ +#include "wrappers.h" + +#include "../pybind11_all.h" + +#include <format> +#include <memory> + +#include <pybind11_utils/generator.h> +#include <uibase/executableinfo.h> +#include <uibase/filemapping.h> +#include <uibase/game_features/igamefeatures.h> +#include <uibase/guessedvalue.h> +#include <uibase/idownloadmanager.h> +#include <uibase/iexecutable.h> +#include <uibase/iexecutableslist.h> +#include <uibase/iinstallationmanager.h> +#include <uibase/iinstance.h> +#include <uibase/iinstancemanager.h> +#include <uibase/imodinterface.h> +#include <uibase/imodrepositorybridge.h> +#include <uibase/imoinfo.h> +#include <uibase/iplugin.h> +#include <uibase/iplugindiagnose.h> +#include <uibase/iplugingame.h> +#include <uibase/ipluginlist.h> +#include <uibase/pluginsetting.h> +#include <uibase/versioninfo.h> + +#include "../deprecation.h" +#include "pyfiletree.h" + +using namespace MOBase; + +namespace mo2::python { + + namespace py = pybind11; + + using namespace pybind11::literals; + + void add_versioninfo_classes(py::module_ m) + { + // Version + py::class_<Version> pyVersion(m, "Version"); + + py::enum_<Version::ReleaseType>(pyVersion, "ReleaseType") + .value("DEVELOPMENT", Version::Development) + .value("ALPHA", Version::Alpha) + .value("BETA", Version::Beta) + .value("RELEASE_CANDIDATE", Version::ReleaseCandidate) + .export_values(); + + py::enum_<Version::ParseMode>(pyVersion, "ParseMode") + .value("SEMVER", Version::ParseMode::SemVer) + .value("MO2", Version::ParseMode::MO2); + + py::enum_<Version::FormatMode>(pyVersion, "FormatMode", py::arithmetic{}) + .value("FORCE_SUBPATCH", Version::FormatMode::ForceSubPatch) + .value("NO_SEPARATOR", Version::FormatMode::NoSeparator) + .value("SHORT_ALPHA_BETA", Version::FormatMode::ShortAlphaBeta) + .value("NO_METADATA", Version::FormatMode::NoMetadata) + .value("CONDENSED", + static_cast<Version::FormatMode>(Version::FormatCondensed.toInt())) + .export_values() + .def("__xor__", + py::overload_cast<Version::FormatMode, Version::FormatModes>( + &operator^)) + .def("__and__", + py::overload_cast<Version::FormatMode, Version::FormatModes>( + &operator&)) + .def("__or__", py::overload_cast<Version::FormatMode, Version::FormatModes>( + &operator|)) + .def("__rxor__", + py::overload_cast<Version::FormatMode, Version::FormatModes>( + &operator^)) + .def("__rand__", + py::overload_cast<Version::FormatMode, Version::FormatModes>( + &operator&)) + .def("__ror__", + py::overload_cast<Version::FormatMode, Version::FormatModes>( + &operator|)); + + pyVersion + .def_static("parse", &Version::parse, "value"_a, + "mode"_a = Version::ParseMode::SemVer) + .def(py::init<int, int, int, QString>(), "major"_a, "minor"_a, "patch"_a, + "metadata"_a = "") + .def(py::init<int, int, int, int, QString>(), "major"_a, "minor"_a, + "patch"_a, "subpatch"_a, "metadata"_a = "") + .def(py::init<int, int, int, Version::ReleaseType, QString>(), "major"_a, + "minor"_a, "patch"_a, "type"_a, "metadata"_a = "") + .def(py::init<int, int, int, int, Version::ReleaseType, QString>(), + "major"_a, "minor"_a, "patch"_a, "subpatch"_a, "type"_a, + "metadata"_a = "") + .def(py::init<int, int, int, Version::ReleaseType, int, QString>(), + "major"_a, "minor"_a, "patch"_a, "type"_a, "prerelease"_a, + "metadata"_a = "") + .def(py::init<int, int, int, int, Version::ReleaseType, int, QString>(), + "major"_a, "minor"_a, "patch"_a, "subpatch"_a, "type"_a, + "prerelease"_a, "metadata"_a = "") + .def(py::init<int, int, int, int, + std::vector<std::variant<int, Version::ReleaseType>>, + QString>(), + "major"_a, "minor"_a, "patch"_a, "subpatch"_a, "prereleases"_a, + "metadata"_a = "") + .def("isPreRelease", &Version::isPreRelease) + .def_property_readonly("major", &Version::major) + .def_property_readonly("minor", &Version::minor) + .def_property_readonly("patch", &Version::patch) + .def_property_readonly("subpatch", &Version::subpatch) + .def_property_readonly("prereleases", &Version::preReleases) + .def_property_readonly("build_metadata", &Version::buildMetadata) + .def("string", &Version::string, "mode"_a = Version::FormatModes{}) + .def("__str__", + [](Version const& version) { + return version.string(Version::FormatCondensed); + }) + .def(py::self < py::self) + .def(py::self > py::self) + .def(py::self <= py::self) + .def(py::self >= py::self) + .def(py::self != py::self) + .def(py::self == py::self); + + // VersionInfo + py::enum_<MOBase::VersionInfo::ReleaseType>(m, "ReleaseType") + .value("final", MOBase::VersionInfo::RELEASE_FINAL) + .value("candidate", MOBase::VersionInfo::RELEASE_CANDIDATE) + .value("beta", MOBase::VersionInfo::RELEASE_BETA) + .value("alpha", MOBase::VersionInfo::RELEASE_ALPHA) + .value("prealpha", MOBase::VersionInfo::RELEASE_PREALPHA) + + .value("FINAL", MOBase::VersionInfo::RELEASE_FINAL) + .value("CANDIDATE", MOBase::VersionInfo::RELEASE_CANDIDATE) + .value("BETA", MOBase::VersionInfo::RELEASE_BETA) + .value("ALPHA", MOBase::VersionInfo::RELEASE_ALPHA) + .value("PRE_ALPHA", MOBase::VersionInfo::RELEASE_PREALPHA); + + py::enum_<MOBase::VersionInfo::VersionScheme>(m, "VersionScheme") + .value("discover", MOBase::VersionInfo::SCHEME_DISCOVER) + .value("regular", MOBase::VersionInfo::SCHEME_REGULAR) + .value("decimalmark", MOBase::VersionInfo::SCHEME_DECIMALMARK) + .value("numbersandletters", MOBase::VersionInfo::SCHEME_NUMBERSANDLETTERS) + .value("date", MOBase::VersionInfo::SCHEME_DATE) + .value("literal", MOBase::VersionInfo::SCHEME_LITERAL) + + .value("DISCOVER", MOBase::VersionInfo::SCHEME_DISCOVER) + .value("REGULAR", MOBase::VersionInfo::SCHEME_REGULAR) + .value("DECIMAL_MARK", MOBase::VersionInfo::SCHEME_DECIMALMARK) + .value("NUMBERS_AND_LETTERS", MOBase::VersionInfo::SCHEME_NUMBERSANDLETTERS) + .value("DATE", MOBase::VersionInfo::SCHEME_DATE) + .value("LITERAL", MOBase::VersionInfo::SCHEME_LITERAL); + + py::class_<VersionInfo>(m, "VersionInfo") + .def(py::init<>()) + .def(py::init<QString, VersionInfo::VersionScheme>(), "value"_a, + "scheme"_a = VersionInfo::SCHEME_DISCOVER) + // note: order of the two init<> below is important because + // ReleaseType is a simple enum with an implicit int conversion. + .def(py::init<int, int, int, int, VersionInfo::ReleaseType>(), "major"_a, + "minor"_a, "subminor"_a, "subsubminor"_a, + "release_type"_a = VersionInfo::RELEASE_FINAL) + .def(py::init<int, int, int, VersionInfo::ReleaseType>(), "major"_a, + "minor"_a, "subminor"_a, "release_type"_a = VersionInfo::RELEASE_FINAL) + .def("clear", &VersionInfo::clear) + .def("parse", &VersionInfo::parse, "value"_a, + "scheme"_a = VersionInfo::SCHEME_DISCOVER, "is_manual"_a = false) + .def("canonicalString", &VersionInfo::canonicalString) + .def("displayString", &VersionInfo::displayString, "forced_segments"_a = 2) + .def("isValid", &VersionInfo::isValid) + .def("scheme", &VersionInfo::scheme) + .def("__str__", &VersionInfo::canonicalString) + .def(py::self < py::self) + .def(py::self > py::self) + .def(py::self <= py::self) + .def(py::self >= py::self) + .def(py::self != py::self) + .def(py::self == py::self); + } + + void add_executable_classes(py::module_ m) + { + py::class_<ExecutableInfo>(m, "ExecutableInfo") + .def(py::init<const QString&, const FileWrapper&>(), "title"_a, "binary"_a) + .def("withArgument", &ExecutableInfo::withArgument, "argument"_a) + .def("withWorkingDirectory", + wrap_for_directory(&ExecutableInfo::withWorkingDirectory), + "directory"_a) + .def("withSteamAppId", &ExecutableInfo::withSteamAppId, "app_id"_a) + .def("asCustom", &ExecutableInfo::asCustom) + .def("isValid", &ExecutableInfo::isValid) + .def("title", &ExecutableInfo::title) + .def("binary", &ExecutableInfo::binary) + .def("arguments", &ExecutableInfo::arguments) + .def("workingDirectory", &ExecutableInfo::workingDirectory) + .def("steamAppID", &ExecutableInfo::steamAppID) + .def("isCustom", &ExecutableInfo::isCustom); + + py::class_<ExecutableForcedLoadSetting>(m, "ExecutableForcedLoadSetting") + .def(py::init<const QString&, const QString&>(), "process"_a, "library"_a) + .def("withForced", &ExecutableForcedLoadSetting::withForced, "forced"_a) + .def("withEnabled", &ExecutableForcedLoadSetting::withEnabled, "enabled"_a) + .def("enabled", &ExecutableForcedLoadSetting::enabled) + .def("forced", &ExecutableForcedLoadSetting::forced) + .def("library", &ExecutableForcedLoadSetting::library) + .def("process", &ExecutableForcedLoadSetting::process); + + py::class_<IExecutable>(m, "IExecutable") + .def("title", &IExecutable::title) + .def("binaryInfo", &IExecutable::binaryInfo) + .def("arguments", &IExecutable::arguments) + .def("steamAppID", &IExecutable::steamAppID) + .def("workingDirectory", &IExecutable::workingDirectory) + .def("isShownOnToolbar", &IExecutable::isShownOnToolbar) + .def("usesOwnIcon", &IExecutable::usesOwnIcon) + .def("minimizeToSystemTray", &IExecutable::minimizeToSystemTray) + .def("hide", &IExecutable::hide); + + py::class_<IExecutablesList>(m, "IExecutablesList") + .def("executables", + [](IExecutablesList* executablesList) { + return make_generator(executablesList->executables(), + py::return_value_policy::reference); + }) + .def("getByTitle", &IExecutablesList::getByTitle, "title"_a) + .def("getByBinary", &IExecutablesList::getByBinary, "info"_a) + .def("titleExists", &IExecutablesList::contains, "title"_a); + } + + void add_modinterface_classes(py::module_ m) + { + py::enum_<EndorsedState>(m, "EndorsedState", py::arithmetic()) + .value("ENDORSED_FALSE", EndorsedState::ENDORSED_FALSE) + .value("ENDORSED_TRUE", EndorsedState::ENDORSED_TRUE) + .value("ENDORSED_UNKNOWN", EndorsedState::ENDORSED_UNKNOWN) + .value("ENDORSED_NEVER", EndorsedState::ENDORSED_NEVER); + + py::enum_<TrackedState>(m, "TrackedState", py::arithmetic()) + .value("TRACKED_FALSE", TrackedState::TRACKED_FALSE) + .value("TRACKED_TRUE", TrackedState::TRACKED_TRUE) + .value("TRACKED_UNKNOWN", TrackedState::TRACKED_UNKNOWN); + + py::class_<IModInterface>(m, "IModInterface") + .def("name", &IModInterface::name) + .def("absolutePath", &IModInterface::absolutePath) + + .def("comments", &IModInterface::comments) + .def("notes", &IModInterface::notes) + .def("gameName", &IModInterface::gameName) + .def("repository", &IModInterface::repository) + .def("nexusId", &IModInterface::nexusId) + .def("version", &IModInterface::version) + .def("newestVersion", &IModInterface::newestVersion) + .def("ignoredVersion", &IModInterface::ignoredVersion) + .def("installationFile", &IModInterface::installationFile) + .def("converted", &IModInterface::converted) + .def("validated", &IModInterface::validated) + .def("color", &IModInterface::color) + .def("url", &IModInterface::url) + .def("primaryCategory", &IModInterface::primaryCategory) + .def("categories", &IModInterface::categories) + .def("author", &IModInterface::author) + .def("uploader", &IModInterface::uploader) + .def("uploaderUrl", &IModInterface::uploaderUrl) + .def("trackedState", &IModInterface::trackedState) + .def("endorsedState", &IModInterface::endorsedState) + .def("fileTree", &IModInterface::fileTree) + .def("isOverwrite", &IModInterface::isOverwrite) + .def("isBackup", &IModInterface::isBackup) + .def("isSeparator", &IModInterface::isSeparator) + .def("isForeign", &IModInterface::isForeign) + + .def("setVersion", &IModInterface::setVersion, "version"_a) + .def("setNewestVersion", &IModInterface::setNewestVersion, "version"_a) + .def("setIsEndorsed", &IModInterface::setIsEndorsed, "endorsed"_a) + .def("setNexusID", &IModInterface::setNexusID, "nexus_id"_a) + .def("addNexusCategory", &IModInterface::addNexusCategory, "category_id"_a) + .def("addCategory", &IModInterface::addCategory, "name"_a) + .def("removeCategory", &IModInterface::removeCategory, "name"_a) + .def("setGameName", &IModInterface::setGameName, "name"_a) + .def("setUrl", &IModInterface::setUrl, "url"_a) + .def("pluginSetting", &IModInterface::pluginSetting, "plugin_name"_a, + "key"_a, "default"_a = QVariant()) + .def("pluginSettings", &IModInterface::pluginSettings, "plugin_name"_a) + .def("setPluginSetting", &IModInterface::setPluginSetting, "plugin_name"_a, + "key"_a, "value"_a) + .def("clearPluginSettings", &IModInterface::clearPluginSettings, + "plugin_name"_a); + } + + void add_modrepository_classes(py::module_ m) + { + py::class_<IModRepositoryBridge> iModRepositoryBridge(m, + "IModRepositoryBridge"); + iModRepositoryBridge + .def("requestDescription", &IModRepositoryBridge::requestDescription, + "game_name"_a, "mod_id"_a, "user_data"_a) + .def("requestFiles", &IModRepositoryBridge::requestFiles, "game_name"_a, + "mod_id"_a, "user_data"_a) + .def("requestFileInfo", &IModRepositoryBridge::requestFileInfo, + "game_name"_a, "mod_id"_a, "file_id"_a, "user_data"_a) + .def("requestDownloadURL", &IModRepositoryBridge::requestDownloadURL, + "game_name"_a, "mod_id"_a, "file_id"_a, "user_data"_a) + .def("requestToggleEndorsement", + &IModRepositoryBridge::requestToggleEndorsement, "game_name"_a, + "mod_id"_a, "mod_version"_a, "endorse"_a, "user_data"_a); + + py::qt::add_qt_delegate<QObject>(iModRepositoryBridge, "_object"); + + py::class_<ModRepositoryFileInfo>(m, "ModRepositoryFileInfo") + .def(py::init<const ModRepositoryFileInfo&>(), "other"_a) + .def(py::init<QString, int, int>(), "game_name"_a = "", "mod_id"_a = 0, + "file_id"_a = 0) + .def("__str__", &ModRepositoryFileInfo::toString) + .def_static("createFromJson", &ModRepositoryFileInfo::createFromJson, + "data"_a) + .def_readwrite("name", &ModRepositoryFileInfo::name) + .def_readwrite("uri", &ModRepositoryFileInfo::uri) + .def_readwrite("description", &ModRepositoryFileInfo::description) + .def_readwrite("version", &ModRepositoryFileInfo::version) + .def_readwrite("newestVersion", &ModRepositoryFileInfo::newestVersion) + .def_readwrite("categoryID", &ModRepositoryFileInfo::categoryID) + .def_readwrite("modName", &ModRepositoryFileInfo::modName) + .def_readwrite("gameName", &ModRepositoryFileInfo::gameName) + .def_readwrite("modID", &ModRepositoryFileInfo::modID) + .def_readwrite("fileID", &ModRepositoryFileInfo::fileID) + .def_readwrite("fileSize", &ModRepositoryFileInfo::fileSize) + .def_readwrite("fileName", &ModRepositoryFileInfo::fileName) + .def_readwrite("fileCategory", &ModRepositoryFileInfo::fileCategory) + .def_readwrite("fileTime", &ModRepositoryFileInfo::fileTime) + .def_readwrite("repository", &ModRepositoryFileInfo::repository) + .def_readwrite("userData", &ModRepositoryFileInfo::userData) + .def_readwrite("author", &ModRepositoryFileInfo::author) + .def_readwrite("uploader", &ModRepositoryFileInfo::uploader) + .def_readwrite("uploaderUrl", &ModRepositoryFileInfo::uploaderUrl); + } + + void add_guessedstring_classes(py::module_ m) + { + py::enum_<MOBase::EGuessQuality>(m, "GuessQuality") + .value("INVALID", MOBase::GUESS_INVALID) + .value("FALLBACK", MOBase::GUESS_FALLBACK) + .value("GOOD", MOBase::GUESS_GOOD) + .value("META", MOBase::GUESS_META) + .value("PRESET", MOBase::GUESS_PRESET) + .value("USER", MOBase::GUESS_USER); + + py::class_<MOBase::GuessedValue<QString>>(m, "GuessedString") + .def(py::init<>()) + .def(py::init<QString const&, EGuessQuality>(), "value"_a, + "quality"_a = EGuessQuality::GUESS_USER) + .def("update", + py::overload_cast<const QString&>(&GuessedValue<QString>::update), + "value"_a) + .def("update", + py::overload_cast<const QString&, EGuessQuality>( + &GuessedValue<QString>::update), + "value"_a, "quality"_a) + + // Methods to simulate the assignment operator: + .def("reset", + [](GuessedValue<QString>* gv) { + *gv = GuessedValue<QString>(); + return gv; + }) + .def( + "reset", + [](GuessedValue<QString>* gv, const QString& value, EGuessQuality eq) { + *gv = GuessedValue<QString>(value, eq); + return gv; + }, + "value"_a, "quality"_a) + .def( + "reset", + [](GuessedValue<QString>* gv, const GuessedValue<QString>& other) { + *gv = other; + return gv; + }, + "other"_a) + + // use an intermediate lambda because we cannot have a function with a + // non-const reference in Python - in Python, the function should returned a + // bool or the modified value + .def( + "setFilter", + [](GuessedValue<QString>* gv, + std::function<std::variant<QString, bool>(QString const&)> fn) { + gv->setFilter([fn](QString& s) { + auto ret = fn(s); + return std::visit( + [&s](auto v) { + if constexpr (std::is_same_v<decltype(v), QString>) { + s = v; + return true; + } + else if constexpr (std::is_same_v<decltype(v), bool>) { + return v; + } + }, + ret); + }); + }, + "filter"_a) + + // this makes a copy in python but it more practical than + // exposing an iterator + .def("variants", &GuessedValue<QString>::variants) + .def("__str__", &MOBase::GuessedValue<QString>::operator const QString&); + + // implicit conversion from QString - this allows passing Python string to + // function expecting GuessedValue<QString> + py::implicitly_convertible<QString, GuessedValue<QString>>(); + } + + void add_ipluginlist_classes(py::module_ m) + { + py::enum_<IPluginList::PluginState>(m, "PluginState", py::arithmetic()) + .value("missing", IPluginList::STATE_MISSING) + .value("inactive", IPluginList::STATE_INACTIVE) + .value("active", IPluginList::STATE_ACTIVE) + + .value("MISSING", IPluginList::STATE_MISSING) + .value("INACTIVE", IPluginList::STATE_INACTIVE) + .value("ACTIVE", IPluginList::STATE_ACTIVE); + + py::class_<IPluginList>(m, "IPluginList") + .def("state", &IPluginList::state, "name"_a) + .def("priority", &IPluginList::priority, "name"_a) + .def("setPriority", &IPluginList::setPriority, "name"_a, "priority"_a) + .def("loadOrder", &IPluginList::loadOrder, "name"_a) + .def("masters", &IPluginList::masters, "name"_a) + .def("origin", &IPluginList::origin, "name"_a) + .def("onRefreshed", &IPluginList::onRefreshed, "callback"_a) + .def("onPluginMoved", &IPluginList::onPluginMoved, "callback"_a) + + .def("isMasterFlagged", &IPluginList::isMasterFlagged, "name"_a) + .def("hasMasterExtension", &IPluginList::hasMasterExtension, "name"_a) + .def("isMediumFlagged", &IPluginList::isMediumFlagged, "name"_a) + .def("isLightFlagged", &IPluginList::isLightFlagged, "name"_a) + .def("isBlueprintFlagged", &IPluginList::isBlueprintFlagged, "name"_a) + .def("hasLightExtension", &IPluginList::hasLightExtension, "name"_a) + .def("hasNoRecords", &IPluginList::hasNoRecords, "name"_a) + + .def("formVersion", &IPluginList::formVersion, "name"_a) + .def("headerVersion", &IPluginList::headerVersion, "name"_a) + .def("author", &IPluginList::author, "name"_a) + .def("description", &IPluginList::description, "name"_a) + + // Kept but deprecated for backward compatibility: + .def( + "onPluginStateChanged", + [](IPluginList* modList, + const std::function<void(const QString&, IPluginList::PluginStates)>& + fn) { + mo2::python::show_deprecation_warning( + "onPluginStateChanged", + "onPluginStateChanged(Callable[[str, " + "IPluginList.PluginStates], None]) is deprecated, " + "use onPluginStateChanged(Callable[[Dict[str, " + "IPluginList.PluginStates], None]) instead."); + return modList->onPluginStateChanged([fn](auto const& map) { + for (const auto& entry : map) { + fn(entry.first, entry.second); + } + }); + }, + "callback"_a) + .def( + "isMaster", + [](IPluginList* modList, QString const& name) { + mo2::python::show_deprecation_warning( + "isMaster", + "isMaster(str) is deprecated, use isMasterFlagged() or " + "hasMasterExtension() instead."); + return modList->isMasterFlagged(name); + }, + "name"_a) + .def("onPluginStateChanged", &MOBase::IPluginList::onPluginStateChanged, + "callback"_a) + .def("pluginNames", &MOBase::IPluginList::pluginNames) + .def("setState", &MOBase::IPluginList::setState, "name"_a, "state"_a) + .def("setLoadOrder", &MOBase::IPluginList::setLoadOrder, "loadorder"_a); + } + + void add_imodlist_classes(py::module_ m) + { + py::enum_<IModList::ModState>(m, "ModState", py::arithmetic()) + .value("exists", IModList::STATE_EXISTS) + .value("active", IModList::STATE_ACTIVE) + .value("essential", IModList::STATE_ESSENTIAL) + .value("empty", IModList::STATE_EMPTY) + .value("endorsed", IModList::STATE_ENDORSED) + .value("valid", IModList::STATE_VALID) + .value("alternate", IModList::STATE_ALTERNATE) + + .value("EXISTS", IModList::STATE_EXISTS) + .value("ACTIVE", IModList::STATE_ACTIVE) + .value("ESSENTIAL", IModList::STATE_ESSENTIAL) + .value("EMPTY", IModList::STATE_EMPTY) + .value("ENDORSED", IModList::STATE_ENDORSED) + .value("VALID", IModList::STATE_VALID) + .value("ALTERNATE", IModList::STATE_ALTERNATE); + + py::class_<IModList>(m, "IModList") + .def("displayName", &MOBase::IModList::displayName, "name"_a) + .def("allMods", &MOBase::IModList::allMods) + .def("allModsByProfilePriority", + &MOBase::IModList::allModsByProfilePriority, + "profile"_a = static_cast<IProfile*>(nullptr)) + .def("getMod", &MOBase::IModList::getMod, + py::return_value_policy::reference, "name"_a) + .def("removeMod", &MOBase::IModList::removeMod, "mod"_a) + .def("renameMod", &MOBase::IModList::renameMod, + py::return_value_policy::reference, "mod"_a, "name"_a) + + .def("state", &MOBase::IModList::state, "name"_a) + .def("setActive", + py::overload_cast<QStringList const&, bool>( + &MOBase::IModList::setActive), + "names"_a, "active"_a) + .def("setActive", + py::overload_cast<QString const&, bool>(&MOBase::IModList::setActive), + "name"_a, "active"_a) + .def("priority", &MOBase::IModList::priority, "name"_a) + .def("setPriority", &MOBase::IModList::setPriority, "name"_a, "priority"_a) + + // kept but deprecated for backward compatibility + .def( + "onModStateChanged", + [](IModList* modList, + const std::function<void(const QString&, IModList::ModStates)>& fn) { + mo2::python::show_deprecation_warning( + "onModStateChanged", + "onModStateChanged(Callable[[str, IModList.ModStates], None]) " + "is deprecated, " + "use onModStateChanged(Callable[[Dict[str, " + "IModList.ModStates], None]) instead."); + return modList->onModStateChanged([fn](auto const& map) { + for (const auto& entry : map) { + fn(entry.first, entry.second); + } + }); + }, + "callback"_a) + + .def("onModInstalled", &MOBase::IModList::onModInstalled, "callback"_a) + .def("onModRemoved", &MOBase::IModList::onModRemoved, "callback"_a) + .def("onModStateChanged", &MOBase::IModList::onModStateChanged, + "callback"_a) + .def("onModMoved", &MOBase::IModList::onModMoved, "callback"_a); + } + + void add_iorganizer_classes(py::module_ m) + { + // define INVALID_HANDLE_VALUE for startApplication, etc. + m.attr("INVALID_HANDLE_VALUE") = py::int_((std::uintptr_t)INVALID_HANDLE_VALUE); + + py::class_<IOrganizer::FileInfo>(m, "FileInfo") + .def(py::init<>()) + .def_readwrite("filePath", &IOrganizer::FileInfo::filePath) + .def_readwrite("archive", &IOrganizer::FileInfo::archive) + .def_readwrite("origins", &IOrganizer::FileInfo::origins); + + py::class_<IOrganizer>(m, "IOrganizer") + .def("createNexusBridge", &IOrganizer::createNexusBridge, + py::return_value_policy::reference) + .def("instanceName", &IOrganizer::instanceName) + .def("profileName", &IOrganizer::profileName) + .def("profilePath", &IOrganizer::profilePath) + .def("downloadsPath", &IOrganizer::downloadsPath) + .def("overwritePath", &IOrganizer::overwritePath) + .def("basePath", &IOrganizer::basePath) + .def("modsPath", &IOrganizer::modsPath) + .def("appVersion", + [](IOrganizer& o) { + mo2::python::show_deprecation_warning( + "appVersion", "IOrganizer::appVersion() is deprecated, use " + "IOrganizer::version() instead."); +#pragma warning(push) +#pragma warning(disable : 4996) + return o.appVersion(); +#pragma warning(pop) + }) + .def("version", &IOrganizer::version) + .def("createMod", &IOrganizer::createMod, + py::return_value_policy::reference, "name"_a) + .def("getGame", &IOrganizer::getGame, py::return_value_policy::reference, + "name"_a) + .def("modDataChanged", &IOrganizer::modDataChanged, "mod"_a) + .def("isPluginEnabled", + py::overload_cast<IPlugin*>(&IOrganizer::isPluginEnabled, py::const_), + "plugin"_a) + .def("isPluginEnabled", + py::overload_cast<QString const&>(&IOrganizer::isPluginEnabled, + py::const_), + "plugin"_a) + .def("pluginSetting", &IOrganizer::pluginSetting, "plugin_name"_a, "key"_a) + .def("setPluginSetting", &IOrganizer::setPluginSetting, "plugin_name"_a, + "key"_a, "value"_a) + .def("persistent", &IOrganizer::persistent, "plugin_name"_a, "key"_a, + "default"_a = QVariant()) + .def("setPersistent", &IOrganizer::setPersistent, "plugin_name"_a, "key"_a, + "value"_a, "sync"_a = true) + .def("pluginDataPath", &IOrganizer::pluginDataPath) + .def("installMod", wrap_for_filepath<1>(&IOrganizer::installMod), + py::return_value_policy::reference, "filename"_a, + "name_suggestion"_a = "") + .def("resolvePath", wrap_for_filepath(&IOrganizer::resolvePath), + "filename"_a) + .def("listDirectories", &IOrganizer::listDirectories, "directory"_a) + + // "provide multiple overloads of findFiles + .def( + "findFiles", + [](const IOrganizer* o, DirectoryWrapper const& p, + std::function<bool(QString const&)> const& f) { + return o->findFiles(p, f); + }, + "path"_a, "filter"_a) + + // in C++, it is possible to create a QStringList implicitly from + // a single QString, in Python is not possible with the current + // converters in python (and I do not think it is a good idea to + // have it everywhere), but here it is nice to be able to + // pass a single string, so we add an extra overload + // + // important: the order matters, because a Python string can be + // converted to a QStringList since it is a sequence of + // single-character strings: + .def( + "findFiles", + [](const IOrganizer* o, DirectoryWrapper const& p, + const QStringList& gf) { + return o->findFiles(p, gf); + }, + "path"_a, "patterns"_a) + .def( + "findFiles", + [](const IOrganizer* o, DirectoryWrapper const& p, const QString& f) { + return o->findFiles(p, QStringList{f}); + }, + "path"_a, "pattern"_a) + + .def("getFileOrigins", &IOrganizer::getFileOrigins, "filename"_a) + .def("findFileInfos", wrap_for_directory(&IOrganizer::findFileInfos), + "path"_a, "filter"_a) + + .def("virtualFileTree", &IOrganizer::virtualFileTree) + + .def("instanceManager", &IOrganizer::instanceManager, + py::return_value_policy::reference) + .def("downloadManager", &IOrganizer::downloadManager, + py::return_value_policy::reference) + .def("pluginList", &IOrganizer::pluginList, + py::return_value_policy::reference) + .def("modList", &IOrganizer::modList, py::return_value_policy::reference) + .def("executablesList", &IOrganizer::executablesList, + py::return_value_policy::reference) + .def("gameFeatures", &IOrganizer::gameFeatures, + py::return_value_policy::reference) + .def("profile", &IOrganizer::profile) + .def("profileNames", &IOrganizer::profileNames) + .def("getProfile", &IOrganizer::getProfile, "name"_a) + + // custom implementation for startApplication and + // waitForApplication because 1) HANDLE (= void*) is not properly + // converted from/to python, and 2) we need to convert the by-ptr + // argument to a return-tuple for waitForApplication + .def( + "startApplication", + [](IOrganizer* o, const FileWrapper& executable, + const QStringList& args, const DirectoryWrapper& cwd, + const QString& profile, const QString& forcedCustomOverwrite, + bool ignoreCustomOverwrite) -> std::uintptr_t { + return (std::uintptr_t)o->startApplication( + executable, args, cwd, profile, forcedCustomOverwrite, + ignoreCustomOverwrite); + }, + "executable"_a, "args"_a = QStringList(), "cwd"_a = "", + "profile"_a = "", "forcedCustomOverwrite"_a = "", + "ignoreCustomOverwrite"_a = false) + .def( + "waitForApplication", + [](IOrganizer* o, std::uintptr_t handle, bool refresh) { + DWORD returnCode; + bool result = + o->waitForApplication((HANDLE)handle, refresh, &returnCode); + + // we force signed return code because it's probably what's expected + // in Python + return std::make_tuple( + result, static_cast<std::make_signed_t<DWORD>>(returnCode)); + }, + "handle"_a, "refresh"_a = true) + + .def("refresh", &IOrganizer::refresh, "save_changes"_a = true) + .def("managedGame", &IOrganizer::managedGame, + py::return_value_policy::reference) + + .def("onAboutToRun", + py::overload_cast<std::function<bool(QString const&, const QDir&, + const QString&)> const&>( + &IOrganizer::onAboutToRun), + "callback"_a) + .def("onAboutToRun", + py::overload_cast<std::function<bool(QString const&)> const&>( + &IOrganizer::onAboutToRun), + "callback"_a) + .def("onFinishedRun", &IOrganizer::onFinishedRun, "callback"_a) + .def("onUserInterfaceInitialized", &IOrganizer::onUserInterfaceInitialized, + "callback"_a) + .def("onNextRefresh", &IOrganizer::onNextRefresh, "callback"_a, + "immediate_if_possible"_a = true) + .def("onProfileCreated", &IOrganizer::onProfileCreated, "callback"_a) + .def("onProfileRenamed", &IOrganizer::onProfileRenamed, "callback"_a) + .def("onProfileRemoved", &IOrganizer::onProfileRemoved, "callback"_a) + .def("onProfileChanged", &IOrganizer::onProfileChanged, "callback"_a) + + .def("onPluginSettingChanged", &IOrganizer::onPluginSettingChanged, + "callback"_a) + .def( + "onPluginEnabled", + [](IOrganizer* o, std::function<void(const IPlugin*)> const& func) { + o->onPluginEnabled(func); + }, + "callback"_a) + .def( + "onPluginEnabled", + [](IOrganizer* o, QString const& name, + std::function<void()> const& func) { + o->onPluginEnabled(name, func); + }, + "name"_a, "callback"_a) + .def( + "onPluginDisabled", + [](IOrganizer* o, std::function<void(const IPlugin*)> const& func) { + o->onPluginDisabled(func); + }, + "callback"_a) + .def( + "onPluginDisabled", + [](IOrganizer* o, QString const& name, + std::function<void()> const& func) { + o->onPluginDisabled(name, func); + }, + "name"_a, "callback"_a) + + // DEPRECATED: + .def( + "getMod", + [](IOrganizer* o, QString const& name) { + mo2::python::show_deprecation_warning( + "getMod", "IOrganizer::getMod(str) is deprecated, use " + "IModList::getMod(str) instead."); + return o->modList()->getMod(name); + }, + py::return_value_policy::reference, "name"_a) + .def( + "removeMod", + [](IOrganizer* o, IModInterface* mod) { + mo2::python::show_deprecation_warning( + "removeMod", + "IOrganizer::removeMod(IModInterface) is deprecated, use " + "IModList::removeMod(IModInterface) instead."); + return o->modList()->removeMod(mod); + }, + "mod"_a) + .def("modsSortedByProfilePriority", + [](IOrganizer* o) { + mo2::python::show_deprecation_warning( + "modsSortedByProfilePriority", + "IOrganizer::modsSortedByProfilePriority() is deprecated, use " + "IModList::allModsByProfilePriority() instead."); + return o->modList()->allModsByProfilePriority(); + }) + .def( + "refreshModList", + [](IOrganizer* o, bool s) { + mo2::python::show_deprecation_warning( + "refreshModList", + "IOrganizer::refreshModList(bool) is deprecated, use " + "IOrganizer::refresh(bool) instead."); + o->refresh(s); + }, + "save_changes"_a = true) + .def( + "onModInstalled", + [](IOrganizer* organizer, + const std::function<void(QString const&)>& func) { + mo2::python::show_deprecation_warning( + "onModInstalled", + "IOrganizer::onModInstalled(Callable[[str], None]) is " + "deprecated, " + "use IModList::onModInstalled(Callable[[IModInterface], None]) " + "instead."); + return organizer->modList()->onModInstalled( + [func](MOBase::IModInterface* m) { + func(m->name()); + }); + ; + }, + "callback"_a) + + .def_static("getPluginDataPath", &IOrganizer::getPluginDataPath); + } + + void add_iinstance_manager_classes(py::module_ m) + { + py::class_<IInstance, std::shared_ptr<IInstance>>(m, "IInstance") + .def("displayName", &IInstance::displayName) + .def("gameName", &IInstance::gameName) + .def("gameDirectory", &IInstance::gameDirectory) + .def("isPortable", &IInstance::isPortable); + + py::class_<IInstanceManager>(m, "IInstanceManager") + .def("currentInstance", &IInstanceManager::currentInstance) + .def("globalInstancePaths", &IInstanceManager::globalInstancePaths) + .def("getGlobalInstance", &IInstanceManager::getGlobalInstance); + } + + void add_idownload_manager_classes(py::module_ m) + { + py::class_<IDownloadManager>(m, "IDownloadManager") + .def("startDownloadURLs", &IDownloadManager::startDownloadURLs, "urls"_a) + .def("startDownloadNexusFile", &IDownloadManager::startDownloadNexusFile, + "mod_id"_a, "file_id"_a) + .def("startDownloadNexusFileForGame", + &IDownloadManager::startDownloadNexusFileForGame, "game_name"_a, + "mod_id"_a, "file_id"_a) + .def("downloadPath", &IDownloadManager::downloadPath, "id"_a) + .def("onDownloadComplete", &IDownloadManager::onDownloadComplete, + "callback"_a) + .def("onDownloadPaused", &IDownloadManager::onDownloadPaused, "callback"_a) + .def("onDownloadFailed", &IDownloadManager::onDownloadFailed, "callback"_a) + .def("onDownloadRemoved", &IDownloadManager::onDownloadRemoved, + "callback"_a); + } + + void add_iinstallation_manager_classes(py::module_ m) + { + // add this here to get proper typing + py::enum_<IPluginInstaller::EInstallResult>(m, "InstallResult") + .value("SUCCESS", IPluginInstaller::RESULT_SUCCESS) + .value("FAILED", IPluginInstaller::RESULT_FAILED) + .value("CANCELED", IPluginInstaller::RESULT_CANCELED) + .value("MANUAL_REQUESTED", IPluginInstaller::RESULT_MANUALREQUESTED) + .value("NOT_ATTEMPTED", IPluginInstaller::RESULT_NOTATTEMPTED); + + py::class_<IInstallationManager>(m, "IInstallationManager") + .def("getSupportedExtensions", + &IInstallationManager::getSupportedExtensions) + .def("extractFile", &IInstallationManager::extractFile, "entry"_a, + "silent"_a = false) + .def("extractFiles", &IInstallationManager::extractFiles, "entries"_a, + "silent"_a = false) + .def("createFile", &IInstallationManager::createFile, "entry"_a) + + // return a tuple to get back the mod name and the mod ID + .def( + "installArchive", + [](IInstallationManager* m, GuessedValue<QString> modName, + FileWrapper archive, int modId) { + auto result = m->installArchive(modName, archive, modId); + return std::make_tuple(result, static_cast<QString>(modName), + modId); + }, + "mod_name"_a, "archive"_a, "mod_id"_a = 0); + } + + void add_basic_bindings(py::module_ m) + { + add_versioninfo_classes(m); + add_executable_classes(m); + add_guessedstring_classes(m); + + add_ifiletree_bindings(m); + + add_modinterface_classes(m); + add_modrepository_classes(m); + + py::class_<PluginSetting>(m, "PluginSetting") + .def(py::init<const QString&, const QString&, const QVariant&>(), "key"_a, + "description"_a, "default_value"_a) + .def_readwrite("key", &PluginSetting::key) + .def_readwrite("description", &PluginSetting::description) + .def_readwrite("default_value", &PluginSetting::defaultValue); + + py::class_<PluginRequirementFactory>(m, "PluginRequirementFactory") + // pluginDependency + .def_static("pluginDependency", + py::overload_cast<QStringList const&>( + &PluginRequirementFactory::pluginDependency), + "plugins"_a) + .def_static("pluginDependency", + py::overload_cast<QString const&>( + &PluginRequirementFactory::pluginDependency), + "plugin"_a) + // gameDependency + .def_static("gameDependency", + py::overload_cast<QStringList const&>( + &PluginRequirementFactory::gameDependency), + "games"_a) + .def_static("gameDependency", + py::overload_cast<QString const&>( + &PluginRequirementFactory::gameDependency), + "game"_a) + // diagnose + .def_static("diagnose", &PluginRequirementFactory::diagnose, "diagnose"_a) + // basic + .def_static("basic", &PluginRequirementFactory::basic, "checker"_a, + "description"_a); + + py::class_<Mapping>(m, "Mapping") + .def(py::init<>()) + .def(py::init([](QString src, QString dst, bool dir, bool crt) -> Mapping { + return {src, dst, dir, crt}; + }), + "source"_a, "destination"_a, "is_directory"_a, + "create_target"_a = false) + .def_readwrite("source", &Mapping::source) + .def_readwrite("destination", &Mapping::destination) + .def_readwrite("isDirectory", &Mapping::isDirectory) + .def_readwrite("createTarget", &Mapping::createTarget) + .def("__str__", [](Mapping const& m) { + return std::format(L"Mapping({}, {}, {}, {})", m.source.toStdWString(), + m.destination.toStdWString(), m.isDirectory, + m.createTarget); + }); + + // must be done BEFORE imodlist because there is a default argument to a + // IProfile* in the modlist class + py::class_<IProfile, std::shared_ptr<IProfile>>(m, "IProfile") + .def("name", &IProfile::name) + .def("absolutePath", &IProfile::absolutePath) + .def("localSavesEnabled", &IProfile::localSavesEnabled) + .def("localSettingsEnabled", &IProfile::localSettingsEnabled) + .def("invalidationActive", + [](const IProfile* p) { + bool supported; + bool active = p->invalidationActive(&supported); + return std::make_tuple(active, supported); + }) + .def("absoluteIniFilePath", &IProfile::absoluteIniFilePath, "inifile"_a); + + add_ipluginlist_classes(m); + add_imodlist_classes(m); + add_iinstance_manager_classes(m); + add_idownload_manager_classes(m); + add_iinstallation_manager_classes(m); + add_iorganizer_classes(m); + } + +} // namespace mo2::python diff --git a/libs/plugin_python/src/mobase/wrappers/game_features.cpp b/libs/plugin_python/src/mobase/wrappers/game_features.cpp new file mode 100644 index 0000000..655334b --- /dev/null +++ b/libs/plugin_python/src/mobase/wrappers/game_features.cpp @@ -0,0 +1,422 @@ +#include "wrappers.h" + +#include <tuple> + +#include "../pybind11_all.h" + +#include <uibase/ipluginlist.h> +#include <uibase/isavegameinfowidget.h> + +#include <uibase/game_features/bsainvalidation.h> +#include <uibase/game_features/dataarchives.h> +#include <uibase/game_features/gameplugins.h> +#include <uibase/game_features/igamefeatures.h> +#include <uibase/game_features/localsavegames.h> +#include <uibase/game_features/moddatachecker.h> +#include <uibase/game_features/moddatacontent.h> +#include <uibase/game_features/savegameinfo.h> +#include <uibase/game_features/scriptextender.h> +#include <uibase/game_features/unmanagedmods.h> + +#include "pyfiletree.h" + +namespace py = pybind11; + +using namespace MOBase; +using namespace pybind11::literals; + +namespace mo2::python { + + class PyBSAInvalidation : public BSAInvalidation { + public: + bool isInvalidationBSA(const QString& bsaName) override + { + PYBIND11_OVERRIDE_PURE(bool, BSAInvalidation, isInvalidationBSA, bsaName); + } + void deactivate(MOBase::IProfile* profile) override + { + PYBIND11_OVERRIDE_PURE(void, BSAInvalidation, deactivate, profile); + } + void activate(MOBase::IProfile* profile) override + { + PYBIND11_OVERRIDE_PURE(void, BSAInvalidation, activate, profile); + } + bool prepareProfile(MOBase::IProfile* profile) override + { + PYBIND11_OVERRIDE_PURE(bool, BSAInvalidation, prepareProfile, profile); + } + }; + + class PyDataArchives : public DataArchives { + public: + QStringList vanillaArchives() const override + { + PYBIND11_OVERRIDE_PURE(QStringList, DataArchives, vanillaArchives, ); + } + QStringList archives(const MOBase::IProfile* profile) const override + { + PYBIND11_OVERRIDE_PURE(QStringList, DataArchives, archives, profile); + } + void addArchive(MOBase::IProfile* profile, int index, + const QString& archiveName) override + { + PYBIND11_OVERRIDE_PURE(void, DataArchives, addArchive, profile, index, + archiveName); + } + void removeArchive(MOBase::IProfile* profile, + const QString& archiveName) override + { + PYBIND11_OVERRIDE_PURE(void, DataArchives, removeArchive, profile, + archiveName); + } + }; + + class PyGamePlugins : public GamePlugins { + public: + void writePluginLists(const MOBase::IPluginList* pluginList) override + { + PYBIND11_OVERRIDE_PURE(void, GamePlugins, writePluginLists, pluginList); + } + void readPluginLists(MOBase::IPluginList* pluginList) override + { + // TODO: cannot update plugin list or create one from Python so this is + // useless + PYBIND11_OVERRIDE_PURE(void, GamePlugins, readPluginLists, pluginList); + } + QStringList getLoadOrder() override + { + PYBIND11_OVERRIDE_PURE(QStringList, GamePlugins, getLoadOrder, ); + } + bool lightPluginsAreSupported() override + { + PYBIND11_OVERRIDE(bool, GamePlugins, lightPluginsAreSupported, ); + } + bool mediumPluginsAreSupported() override + { + PYBIND11_OVERRIDE(bool, GamePlugins, mediumPluginsAreSupported, ); + } + bool blueprintPluginsAreSupported() override + { + PYBIND11_OVERRIDE(bool, GamePlugins, blueprintPluginsAreSupported, ); + } + }; + + class PyLocalSavegames : public LocalSavegames { + public: + MappingType mappings(const QDir& profileSaveDir) const override + { + PYBIND11_OVERRIDE_PURE(MappingType, LocalSavegames, mappings, + profileSaveDir); + } + bool prepareProfile(MOBase::IProfile* profile) override + { + PYBIND11_OVERRIDE_PURE(bool, LocalSavegames, prepareProfile, profile); + } + }; + + class PyModDataChecker : public ModDataChecker { + public: + CheckReturn + dataLooksValid(std::shared_ptr<const MOBase::IFileTree> fileTree) const override + { + PYBIND11_OVERRIDE_PURE(CheckReturn, ModDataChecker, dataLooksValid, + fileTree); + } + + std::shared_ptr<MOBase::IFileTree> + fix(std::shared_ptr<MOBase::IFileTree> fileTree) const override + { + PYBIND11_OVERRIDE(std::shared_ptr<MOBase::IFileTree>, ModDataChecker, fix, + fileTree); + } + }; + + class PyModDataContent : public ModDataContent { + public: + std::vector<Content> getAllContents() const override + { + PYBIND11_OVERRIDE_PURE(std::vector<Content>, ModDataContent, + getAllContents, ); + ; + } + std::vector<int> + getContentsFor(std::shared_ptr<const MOBase::IFileTree> fileTree) const override + { + PYBIND11_OVERRIDE_PURE(std::vector<int>, ModDataContent, getContentsFor, + fileTree); + } + }; + + class PySaveGameInfo : public SaveGameInfo { + public: + MissingAssets getMissingAssets(MOBase::ISaveGame const& save) const override + { + PYBIND11_OVERRIDE_PURE(MissingAssets, SaveGameInfo, getMissingAssets, + &save); + } + ISaveGameInfoWidget* getSaveGameWidget(QWidget* parent = 0) const override + { + PYBIND11_OVERRIDE_PURE(ISaveGameInfoWidget*, SaveGameInfo, + getSaveGameWidget, parent); + } + }; + + class PyScriptExtender : public ScriptExtender { + public: + QString BinaryName() const override + { + PYBIND11_OVERRIDE_PURE(QString, ScriptExtender, binaryName, ); + } + + QString PluginPath() const override + { + PYBIND11_OVERRIDE_PURE(DirectoryWrapper, ScriptExtender, pluginPath, ); + } + + QString loaderName() const override + { + PYBIND11_OVERRIDE_PURE(QString, ScriptExtender, loaderName, ); + } + + QString loaderPath() const override + { + PYBIND11_OVERRIDE_PURE(FileWrapper, ScriptExtender, loaderPath, ); + } + + QString savegameExtension() const override + { + PYBIND11_OVERRIDE_PURE(QString, ScriptExtender, savegameExtension, ); + } + + bool isInstalled() const override + { + PYBIND11_OVERRIDE_PURE(bool, ScriptExtender, isInstalled, ); + } + + QString getExtenderVersion() const override + { + PYBIND11_OVERRIDE_PURE(QString, ScriptExtender, getExtenderVersion, ); + } + + WORD getArch() const override + { + PYBIND11_OVERRIDE_PURE(WORD, ScriptExtender, getArch, ); + } + }; + + class PyUnmanagedMods : public UnmanagedMods { + public: + QStringList mods(bool onlyOfficial) const override + { + PYBIND11_OVERRIDE_PURE(QStringList, UnmanagedMods, mods, onlyOfficial); + } + QString displayName(const QString& modName) const override + { + PYBIND11_OVERRIDE_PURE(QString, UnmanagedMods, displayName, modName); + } + QFileInfo referenceFile(const QString& modName) const override + { + PYBIND11_OVERRIDE_PURE(FileWrapper, UnmanagedMods, referenceFile, modName); + } + QStringList secondaryFiles(const QString& modName) const override + { + return toQStringList([&] { + PYBIND11_OVERRIDE_PURE(QList<FileWrapper>, UnmanagedMods, + secondaryFiles, modName); + }()); + } + }; + + void add_game_feature_bindings(pybind11::module_ m) + { + // this is just to allow accepting GameFeature in function, we do not expose + // anything from game feature to Python since typeInfo() is useless in Python + // + py::class_<GameFeature, std::shared_ptr<GameFeature>>(m, "GameFeature"); + + // BSAInvalidation + + py::class_<BSAInvalidation, GameFeature, PyBSAInvalidation, + std::shared_ptr<BSAInvalidation>>(m, "BSAInvalidation") + .def(py::init<>()) + .def("isInvalidationBSA", &BSAInvalidation::isInvalidationBSA, "name"_a) + .def("deactivate", &BSAInvalidation::deactivate, "profile"_a) + .def("activate", &BSAInvalidation::activate, "profile"_a); + + // DataArchives + + py::class_<DataArchives, GameFeature, PyDataArchives, + std::shared_ptr<DataArchives>>(m, "DataArchives") + .def(py::init<>()) + .def("vanillaArchives", &DataArchives::vanillaArchives) + .def("archives", &DataArchives::archives, "profile"_a) + .def("addArchive", &DataArchives::addArchive, "profile"_a, "index"_a, + "name"_a) + .def("removeArchive", &DataArchives::removeArchive, "profile"_a, "name"_a); + + // GamePlugins + + py::class_<GamePlugins, GameFeature, PyGamePlugins, + std::shared_ptr<GamePlugins>>(m, "GamePlugins") + .def(py::init<>()) + .def("writePluginLists", &GamePlugins::writePluginLists, "plugin_list"_a) + .def("readPluginLists", &GamePlugins::readPluginLists, "plugin_list"_a) + .def("getLoadOrder", &GamePlugins::getLoadOrder) + .def("lightPluginsAreSupported", &GamePlugins::lightPluginsAreSupported) + .def("mediumPluginsAreSupported", &GamePlugins::mediumPluginsAreSupported) + .def("blueprintPluginsAreSupported", + &GamePlugins::blueprintPluginsAreSupported); + + // LocalSavegames + + py::class_<LocalSavegames, GameFeature, PyLocalSavegames, + std::shared_ptr<LocalSavegames>>(m, "LocalSavegames") + .def(py::init<>()) + .def("mappings", &LocalSavegames::mappings, "profile_save_dir"_a) + .def("prepareProfile", &LocalSavegames::prepareProfile, "profile"_a); + + // ModDataChecker + + py::class_<ModDataChecker, GameFeature, PyModDataChecker, + std::shared_ptr<ModDataChecker>> + pyModDataChecker(m, "ModDataChecker"); + + py::enum_<ModDataChecker::CheckReturn>(pyModDataChecker, "CheckReturn") + .value("INVALID", ModDataChecker::CheckReturn::INVALID) + .value("FIXABLE", ModDataChecker::CheckReturn::FIXABLE) + .value("VALID", ModDataChecker::CheckReturn::VALID) + .export_values(); + + pyModDataChecker.def(py::init<>()) + .def("dataLooksValid", &ModDataChecker::dataLooksValid, "filetree"_a) + .def("fix", &ModDataChecker::fix, "filetree"_a); + + // ModDataContent + py::class_<ModDataContent, GameFeature, PyModDataContent, + std::shared_ptr<ModDataContent>> + pyModDataContent(m, "ModDataContent"); + + py::class_<ModDataContent::Content>(pyModDataContent, "Content") + .def(py::init<int, QString, QString, bool>(), "id"_a, "name"_a, "icon"_a, + "filter_only"_a = false) + .def_property_readonly("id", &ModDataContent::Content::id) + .def_property_readonly("name", &ModDataContent::Content::name) + .def_property_readonly("icon", &ModDataContent::Content::icon) + .def("isOnlyForFilter", &ModDataContent::Content::isOnlyForFilter); + + pyModDataContent.def(py::init<>()) + .def("getAllContents", &ModDataContent::getAllContents) + .def("getContentsFor", &ModDataContent::getContentsFor, "filetree"_a); + + // SaveGameInfo + + py::class_<SaveGameInfo, GameFeature, PySaveGameInfo, + std::shared_ptr<SaveGameInfo>>(m, "SaveGameInfo") + .def(py::init<>()) + .def("getMissingAssets", &SaveGameInfo::getMissingAssets, "save"_a) + .def("getSaveGameWidget", &SaveGameInfo::getSaveGameWidget, + py::return_value_policy::reference, "parent"_a); + + // ScriptExtender + + py::class_<ScriptExtender, GameFeature, PyScriptExtender, + std::shared_ptr<ScriptExtender>>(m, "ScriptExtender") + .def(py::init<>()) + .def("binaryName", &ScriptExtender::BinaryName) + .def("pluginPath", wrap_return_for_directory(&ScriptExtender::PluginPath)) + .def("loaderName", &ScriptExtender::loaderName) + .def("loaderPath", wrap_return_for_filepath(&ScriptExtender::loaderPath)) + .def("savegameExtension", &ScriptExtender::savegameExtension) + .def("isInstalled", &ScriptExtender::isInstalled) + .def("getExtenderVersion", &ScriptExtender::getExtenderVersion) + .def("getArch", &ScriptExtender::getArch); + + // UnmanagedMods + + py::class_<UnmanagedMods, GameFeature, PyUnmanagedMods, + std::shared_ptr<UnmanagedMods>>(m, "UnmanagedMods") + .def(py::init<>()) + .def("mods", &UnmanagedMods::mods, "official_only"_a) + .def("displayName", &UnmanagedMods::displayName, "mod_name"_a) + .def("referenceFile", + wrap_return_for_filepath(&UnmanagedMods::referenceFile), "mod_name"_a) + .def( + "secondaryFiles", + [](UnmanagedMods* m, const QString& modName) -> QList<FileWrapper> { + auto result = m->secondaryFiles(modName); + return {result.begin(), result.end()}; + }, + "mod_name"_a); + } + + void add_igamefeatures_classes(py::module_ m) + { + py::class_<IGameFeatures>(m, "IGameFeatures") + .def("registerFeature", + py::overload_cast<QStringList const&, std::shared_ptr<GameFeature>, + int, bool>(&IGameFeatures::registerFeature), + "games"_a, "feature"_a, "priority"_a, "replace"_a = false) + .def("registerFeature", + py::overload_cast<MOBase::IPluginGame*, std::shared_ptr<GameFeature>, + int, bool>(&IGameFeatures::registerFeature), + "game"_a, "feature"_a, "priority"_a, "replace"_a = false) + .def("registerFeature", + py::overload_cast<std::shared_ptr<GameFeature>, int, bool>( + &IGameFeatures::registerFeature), + "feature"_a, "priority"_a, "replace"_a = false) + .def("unregisterFeature", &IGameFeatures::unregisterFeature, "feature"_a) + .def("unregisterFeatures", &unregister_feature, "feature_type"_a) + .def("gameFeature", &extract_feature, "feature_type"_a, + py ::return_value_policy::reference); + } + +} // namespace mo2::python + +namespace mo2::python { + + class GameFeaturesHelper { + template <class F, std::size_t... Is> + static void helper(F&& f, std::index_sequence<Is...>) + { + (f(static_cast< + std::tuple_element_t<Is, MOBase::details::BaseGameFeaturesP>>( + nullptr)), + ...); + } + + public: + // apply the function f on a null-pointer of type Feature* for each game + // feature + template <class F> + static void apply(F&& f) + { + helper(f, std::make_index_sequence< + std::tuple_size_v<MOBase::details::BaseGameFeaturesP>>{}); + } + }; + + pybind11::object extract_feature(IGameFeatures const& gameFeatures, + pybind11::object type) + { + py::object py_feature = py::none(); + GameFeaturesHelper::apply([&]<class Feature>(Feature*) { + if (py::type::of<Feature>().is(type)) { + py_feature = py::cast(gameFeatures.gameFeature<Feature>(), + py::return_value_policy::reference); + } + }); + return py_feature; + } + + int unregister_feature(MOBase::IGameFeatures& gameFeatures, pybind11::object type) + { + int count = 0; + GameFeaturesHelper::apply([&]<class Feature>(Feature*) { + if (py::type::of<Feature>().is(type)) { + count = gameFeatures.unregisterFeatures<Feature>(); + } + }); + return count; + } + +} // namespace mo2::python diff --git a/libs/plugin_python/src/mobase/wrappers/known_folders.h b/libs/plugin_python/src/mobase/wrappers/known_folders.h new file mode 100644 index 0000000..f74238b --- /dev/null +++ b/libs/plugin_python/src/mobase/wrappers/known_folders.h @@ -0,0 +1,155 @@ +#include <KnownFolders.h> + +namespace mo2::python { + + struct KnownFolder { + const char* name; + KNOWNFOLDERID guid; + }; + + const std::array<KnownFolder, 142> KNOWN_FOLDERS{{ + {"AccountPictures", FOLDERID_AccountPictures}, + {"AddNewPrograms", FOLDERID_AddNewPrograms}, + {"AdminTools", FOLDERID_AdminTools}, + {"AllAppMods", FOLDERID_AllAppMods}, + {"AppCaptures", FOLDERID_AppCaptures}, + {"AppDataDesktop", FOLDERID_AppDataDesktop}, + {"AppDataDocuments", FOLDERID_AppDataDocuments}, + {"AppDataFavorites", FOLDERID_AppDataFavorites}, + {"AppDataProgramData", FOLDERID_AppDataProgramData}, + {"ApplicationShortcuts", FOLDERID_ApplicationShortcuts}, + {"AppsFolder", FOLDERID_AppsFolder}, + {"AppUpdates", FOLDERID_AppUpdates}, + {"CameraRoll", FOLDERID_CameraRoll}, + {"CameraRollLibrary", FOLDERID_CameraRollLibrary}, + {"CDBurning", FOLDERID_CDBurning}, + {"ChangeRemovePrograms", FOLDERID_ChangeRemovePrograms}, + {"CommonAdminTools", FOLDERID_CommonAdminTools}, + {"CommonOEMLinks", FOLDERID_CommonOEMLinks}, + {"CommonPrograms", FOLDERID_CommonPrograms}, + {"CommonStartMenu", FOLDERID_CommonStartMenu}, + {"CommonStartMenuPlaces", FOLDERID_CommonStartMenuPlaces}, + {"CommonStartup", FOLDERID_CommonStartup}, + {"CommonTemplates", FOLDERID_CommonTemplates}, + {"ComputerFolder", FOLDERID_ComputerFolder}, + {"ConflictFolder", FOLDERID_ConflictFolder}, + {"ConnectionsFolder", FOLDERID_ConnectionsFolder}, + {"Contacts", FOLDERID_Contacts}, + {"ControlPanelFolder", FOLDERID_ControlPanelFolder}, + {"Cookies", FOLDERID_Cookies}, + {"CurrentAppMods", FOLDERID_CurrentAppMods}, + {"Desktop", FOLDERID_Desktop}, + {"DevelopmentFiles", FOLDERID_DevelopmentFiles}, + {"Device", FOLDERID_Device}, + {"DeviceMetadataStore", FOLDERID_DeviceMetadataStore}, + {"Documents", FOLDERID_Documents}, + {"DocumentsLibrary", FOLDERID_DocumentsLibrary}, + {"Downloads", FOLDERID_Downloads}, + {"Favorites", FOLDERID_Favorites}, + {"Fonts", FOLDERID_Fonts}, + {"Games", FOLDERID_Games}, + {"GameTasks", FOLDERID_GameTasks}, + {"History", FOLDERID_History}, + {"HomeGroup", FOLDERID_HomeGroup}, + {"HomeGroupCurrentUser", FOLDERID_HomeGroupCurrentUser}, + {"ImplicitAppShortcuts", FOLDERID_ImplicitAppShortcuts}, + {"InternetCache", FOLDERID_InternetCache}, + {"InternetFolder", FOLDERID_InternetFolder}, + {"Libraries", FOLDERID_Libraries}, + {"Links", FOLDERID_Links}, + {"LocalAppData", FOLDERID_LocalAppData}, + {"LocalAppDataLow", FOLDERID_LocalAppDataLow}, + {"LocalDocuments", FOLDERID_LocalDocuments}, + {"LocalDownloads", FOLDERID_LocalDownloads}, + {"LocalizedResourcesDir", FOLDERID_LocalizedResourcesDir}, + {"LocalMusic", FOLDERID_LocalMusic}, + {"LocalPictures", FOLDERID_LocalPictures}, + {"LocalStorage", FOLDERID_LocalStorage}, + {"LocalVideos", FOLDERID_LocalVideos}, + {"Music", FOLDERID_Music}, + {"MusicLibrary", FOLDERID_MusicLibrary}, + {"NetHood", FOLDERID_NetHood}, + {"NetworkFolder", FOLDERID_NetworkFolder}, + {"Objects3D", FOLDERID_Objects3D}, + {"OneDrive", FOLDERID_OneDrive}, + {"OriginalImages", FOLDERID_OriginalImages}, + {"PhotoAlbums", FOLDERID_PhotoAlbums}, + {"Pictures", FOLDERID_Pictures}, + {"PicturesLibrary", FOLDERID_PicturesLibrary}, + {"Playlists", FOLDERID_Playlists}, + {"PrintersFolder", FOLDERID_PrintersFolder}, + {"PrintHood", FOLDERID_PrintHood}, + {"Profile", FOLDERID_Profile}, + {"ProgramData", FOLDERID_ProgramData}, + {"ProgramFiles", FOLDERID_ProgramFiles}, + {"ProgramFilesCommon", FOLDERID_ProgramFilesCommon}, + {"ProgramFilesCommonX64", FOLDERID_ProgramFilesCommonX64}, + {"ProgramFilesCommonX86", FOLDERID_ProgramFilesCommonX86}, + {"ProgramFilesX64", FOLDERID_ProgramFilesX64}, + {"ProgramFilesX86", FOLDERID_ProgramFilesX86}, + {"Programs", FOLDERID_Programs}, + {"Public", FOLDERID_Public}, + {"PublicDesktop", FOLDERID_PublicDesktop}, + {"PublicDocuments", FOLDERID_PublicDocuments}, + {"PublicDownloads", FOLDERID_PublicDownloads}, + {"PublicGameTasks", FOLDERID_PublicGameTasks}, + {"PublicLibraries", FOLDERID_PublicLibraries}, + {"PublicMusic", FOLDERID_PublicMusic}, + {"PublicPictures", FOLDERID_PublicPictures}, + {"PublicRingtones", FOLDERID_PublicRingtones}, + {"PublicUserTiles", FOLDERID_PublicUserTiles}, + {"PublicVideos", FOLDERID_PublicVideos}, + {"QuickLaunch", FOLDERID_QuickLaunch}, + {"Recent", FOLDERID_Recent}, + {"RecordedCalls", FOLDERID_RecordedCalls}, + {"RecordedTVLibrary", FOLDERID_RecordedTVLibrary}, + {"RecycleBinFolder", FOLDERID_RecycleBinFolder}, + {"ResourceDir", FOLDERID_ResourceDir}, + {"RetailDemo", FOLDERID_RetailDemo}, + {"Ringtones", FOLDERID_Ringtones}, + {"RoamedTileImages", FOLDERID_RoamedTileImages}, + {"RoamingAppData", FOLDERID_RoamingAppData}, + {"RoamingTiles", FOLDERID_RoamingTiles}, + {"SampleMusic", FOLDERID_SampleMusic}, + {"SamplePictures", FOLDERID_SamplePictures}, + {"SamplePlaylists", FOLDERID_SamplePlaylists}, + {"SampleVideos", FOLDERID_SampleVideos}, + {"SavedGames", FOLDERID_SavedGames}, + {"SavedPictures", FOLDERID_SavedPictures}, + {"SavedPicturesLibrary", FOLDERID_SavedPicturesLibrary}, + {"SavedSearches", FOLDERID_SavedSearches}, + {"Screenshots", FOLDERID_Screenshots}, + {"SEARCH_CSC", FOLDERID_SEARCH_CSC}, + {"SEARCH_MAPI", FOLDERID_SEARCH_MAPI}, + {"SearchHistory", FOLDERID_SearchHistory}, + {"SearchHome", FOLDERID_SearchHome}, + {"SearchTemplates", FOLDERID_SearchTemplates}, + {"SendTo", FOLDERID_SendTo}, + {"SidebarDefaultParts", FOLDERID_SidebarDefaultParts}, + {"SidebarParts", FOLDERID_SidebarParts}, + {"SkyDrive", FOLDERID_SkyDrive}, + {"SkyDriveCameraRoll", FOLDERID_SkyDriveCameraRoll}, + {"SkyDriveDocuments", FOLDERID_SkyDriveDocuments}, + {"SkyDriveMusic", FOLDERID_SkyDriveMusic}, + {"SkyDrivePictures", FOLDERID_SkyDrivePictures}, + {"StartMenu", FOLDERID_StartMenu}, + {"StartMenuAllPrograms", FOLDERID_StartMenuAllPrograms}, + {"Startup", FOLDERID_Startup}, + {"SyncManagerFolder", FOLDERID_SyncManagerFolder}, + {"SyncResultsFolder", FOLDERID_SyncResultsFolder}, + {"SyncSetupFolder", FOLDERID_SyncSetupFolder}, + {"System", FOLDERID_System}, + {"SystemX86", FOLDERID_SystemX86}, + {"Templates", FOLDERID_Templates}, + {"UserPinned", FOLDERID_UserPinned}, + {"UserProfiles", FOLDERID_UserProfiles}, + {"UserProgramFiles", FOLDERID_UserProgramFiles}, + {"UserProgramFilesCommon", FOLDERID_UserProgramFilesCommon}, + {"UsersFiles", FOLDERID_UsersFiles}, + {"UsersLibraries", FOLDERID_UsersLibraries}, + {"Videos", FOLDERID_Videos}, + {"VideosLibrary", FOLDERID_VideosLibrary}, + {"Windows", FOLDERID_Windows}, + }}; + +} // namespace mo2::python diff --git a/libs/plugin_python/src/mobase/wrappers/pyfiletree.cpp b/libs/plugin_python/src/mobase/wrappers/pyfiletree.cpp new file mode 100644 index 0000000..6dacf26 --- /dev/null +++ b/libs/plugin_python/src/mobase/wrappers/pyfiletree.cpp @@ -0,0 +1,329 @@ +#include "pyfiletree.h" + +#include <tuple> +#include <variant> + +#include "../pybind11_all.h" + +#include <uibase/ifiletree.h> +#include <uibase/log.h> + +namespace py = pybind11; +using namespace MOBase; + +namespace mo2::detail { + + // filetree implementation for testing purpose + // + class PyFileTree : public IFileTree { + public: + using callback_t = std::function<bool(QString, bool)>; + + PyFileTree(std::shared_ptr<const IFileTree> parent, QString name, + callback_t callback) + : FileTreeEntry(parent, name), IFileTree(), m_Callback(callback) + { + } + + std::shared_ptr<FileTreeEntry> addFile(QString name, bool) override + { + if (m_Callback && !m_Callback(name, false)) { + throw UnsupportedOperationException("File rejected by callback."); + } + return IFileTree::addFile(name); + } + + std::shared_ptr<IFileTree> addDirectory(QString name) override + { + if (m_Callback && !m_Callback(name, true)) { + throw UnsupportedOperationException("Directory rejected by callback."); + } + return IFileTree::addDirectory(name); + } + + protected: + std::shared_ptr<IFileTree> + makeDirectory(std::shared_ptr<const IFileTree> parent, + QString name) const override + { + return std::make_shared<PyFileTree>(parent, name, m_Callback); + } + + bool doPopulate([[maybe_unused]] std::shared_ptr<const IFileTree> parent, + std::vector<std::shared_ptr<FileTreeEntry>>&) const override + { + return true; + } + std::shared_ptr<IFileTree> doClone() const override + { + return std::make_shared<PyFileTree>(nullptr, name(), m_Callback); + } + + private: + callback_t m_Callback; + }; + +} // namespace mo2::detail + +#pragma optimize("", off) + +namespace pybind11 { + const void* polymorphic_type_hook<FileTreeEntry>::get(const FileTreeEntry* src, + const std::type_info*& type) + { + if (auto p = dynamic_cast<const IFileTree*>(src)) { + type = &typeid(IFileTree); + return p; + } + return src; + } +} // namespace pybind11 + +namespace mo2::python { + + void add_ifiletree_bindings(pybind11::module_& m) + { + // FileTreeEntry class: + auto fileTreeEntryClass = + py::class_<FileTreeEntry, std::shared_ptr<FileTreeEntry>>(m, + "FileTreeEntry"); + + // IFileTree class: + auto iFileTreeClass = + py::class_<IFileTree, FileTreeEntry, std::shared_ptr<IFileTree>>( + m, "IFileTree", py::multiple_inheritance()); + + // this is FILE_OR_DIRECTORY but as a FileType since we kind of cheat for the + // exposure in Python and this help pybind11 creates proper typing + + const auto FILE_OR_DIRECTORY = static_cast<FileTreeEntry::FileType>( + FileTreeEntry::FILE_OR_DIRECTORY.toInt()); + + // we do not use the enum directly, we will mostly bind the FileTypes + // (with an S) + py::enum_<FileTreeEntry::FileType>(fileTreeEntryClass, "FileTypes", + py::arithmetic{}) + .value("FILE", FileTreeEntry::FileType::FILE) + .value("DIRECTORY", FileTreeEntry::FileType::DIRECTORY) + .value("FILE_OR_DIRECTORY", FILE_OR_DIRECTORY) + .export_values(); + + fileTreeEntryClass + + .def("isFile", &FileTreeEntry::isFile) + .def("isDir", &FileTreeEntry::isDir) + .def("fileType", &FileTreeEntry::fileType) + .def("name", &FileTreeEntry::name) + .def("suffix", &FileTreeEntry::suffix) + .def( + "hasSuffix", + [](FileTreeEntry* entry, QStringList suffixes) { + return entry->hasSuffix(suffixes); + }, + py::arg("suffixes")) + .def( + "hasSuffix", + [](FileTreeEntry* entry, QString suffix) { + return entry->hasSuffix(suffix); + }, + py::arg("suffix")) + .def("parent", py::overload_cast<>(&FileTreeEntry::parent)) + .def("path", &FileTreeEntry::path, py::arg("sep") = "\\") + .def("pathFrom", &FileTreeEntry::pathFrom, py::arg("tree"), + py::arg("sep") = "\\") + + // Mutable operation: + .def("detach", &FileTreeEntry::detach) + .def("moveTo", &FileTreeEntry::moveTo, py::arg("tree")) + + // Special methods: + .def("__eq__", + [](const FileTreeEntry* entry, QString other) { + return entry->compare(other) == 0; + }) + .def("__eq__", + [](const FileTreeEntry* entry, std::shared_ptr<FileTreeEntry> other) { + return entry == other.get(); + }) + + // Special methods for debug: + .def("__repr__", [](const FileTreeEntry* entry) { + return "FileTreeEntry(\"" + entry->name() + "\")"; + }); + + py::enum_<IFileTree::InsertPolicy>(iFileTreeClass, "InsertPolicy") + .value("FAIL_IF_EXISTS", IFileTree::InsertPolicy::FAIL_IF_EXISTS) + .value("REPLACE", IFileTree::InsertPolicy::REPLACE) + .value("MERGE", IFileTree::InsertPolicy::MERGE) + .export_values(); + + py::enum_<IFileTree::WalkReturn>(iFileTreeClass, "WalkReturn") + .value("CONTINUE", IFileTree::WalkReturn::CONTINUE) + .value("STOP", IFileTree::WalkReturn::STOP) + .value("SKIP", IFileTree::WalkReturn::SKIP) + .export_values(); + + // in C++ this is not an inner enum due to the conditional feature of glob(), + // but in Python this makes more sense as a inner enum + py::enum_<GlobPatternType>(iFileTreeClass, "GlobPatternType") + .value("GLOB", GlobPatternType::GLOB) + .value("REGEX", GlobPatternType::REGEX) + .export_values(); + + // Non-mutable operations: + iFileTreeClass.def("exists", + py::overload_cast<QString, IFileTree::FileTypes>( + &IFileTree::exists, py::const_), + py::arg("path"), py::arg("type") = FILE_OR_DIRECTORY); + iFileTreeClass.def( + "find", py::overload_cast<QString, IFileTree::FileTypes>(&IFileTree::find), + py::arg("path"), py::arg("type") = FILE_OR_DIRECTORY); + iFileTreeClass.def("pathTo", &IFileTree::pathTo, py::arg("entry"), + py::arg("sep") = "\\"); + + iFileTreeClass.def( + "walk", + py::overload_cast< + std::function<IFileTree::WalkReturn( + QString const&, std::shared_ptr<const FileTreeEntry>)>, + QString>(&IFileTree::walk, py::const_), + py::arg("callback"), py::arg("sep") = "\\"); + + // the walk() and glob() generator version are free functions in C++ due to the + // conditional nature, but in Python, it makes more sense to have them as method + // of IFileTree directly + + iFileTreeClass.def("walk", [](std::shared_ptr<const IFileTree> tree) { + return make_generator(walk(tree)); + }); + + iFileTreeClass.def( + "glob", + [](std::shared_ptr<const IFileTree> tree, QString pattern, + GlobPatternType patternType) { + return make_generator(glob(tree, pattern, patternType)); + }, + py::arg("pattern"), py::arg("type") = GlobPatternType::GLOB); + + // Kind-of-static operations: + iFileTreeClass.def("createOrphanTree", &IFileTree::createOrphanTree, + py::arg("name") = ""); + + // addFile() and addDirectory throws exception instead of returning null + // pointer in order to have better traces. + iFileTreeClass.def( + "addFile", + [](IFileTree* w, QString path, bool replaceIfExists) { + auto result = w->addFile(path, replaceIfExists); + if (result == nullptr) { + throw std::logic_error("addFile failed"); + } + return result; + }, + py::arg("path"), py::arg("replace_if_exists") = false); + iFileTreeClass.def( + "addDirectory", + [](IFileTree* w, QString path) { + auto result = w->addDirectory(path); + if (result == nullptr) { + throw std::logic_error("addDirectory failed"); + } + return result; + }, + py::arg("path")); + + // Merge needs custom return types depending if the user wants overrides + // or not. A failure is translated into an exception for easier tracing + // and handling. + iFileTreeClass.def( + "merge", + [](IFileTree* p, std::shared_ptr<IFileTree> other, bool returnOverwrites) + -> std::variant<IFileTree::OverwritesType, std::size_t> { + IFileTree::OverwritesType overwrites; + auto result = p->merge(other, returnOverwrites ? &overwrites : nullptr); + if (result == IFileTree::MERGE_FAILED) { + throw std::logic_error("merge failed"); + } + if (returnOverwrites) { + return {overwrites}; + } + return {result}; + }, + py::arg("other"), py::arg("overwrites") = false); + + // Insert and erase returns an iterator, which makes no sense in python, + // so we convert it to bool. Erase is also renamed "remove" since + // "erase" is very C++. + iFileTreeClass.def( + "insert", + [](IFileTree* p, std::shared_ptr<FileTreeEntry> entry, + IFileTree::InsertPolicy insertPolicy) { + return p->insert(entry, insertPolicy) == p->end(); + }, + py::arg("entry"), + py::arg("policy") = IFileTree::InsertPolicy::FAIL_IF_EXISTS); + + iFileTreeClass.def( + "remove", + [](IFileTree* p, QString name) { + return p->erase(name).first != p->end(); + }, + py::arg("name")); + iFileTreeClass.def( + "remove", + [](IFileTree* p, std::shared_ptr<FileTreeEntry> entry) { + return p->erase(entry) != p->end(); + }, + py::arg("entry")); + + iFileTreeClass.def("move", &IFileTree::move, py::arg("entry"), py::arg("path"), + py::arg("policy") = IFileTree::InsertPolicy::FAIL_IF_EXISTS); + iFileTreeClass.def( + "copy", + [](IFileTree* w, std::shared_ptr<FileTreeEntry> entry, QString path, + IFileTree::InsertPolicy insertPolicy) { + auto result = w->copy(entry, path, insertPolicy); + if (result == nullptr) { + throw std::logic_error("copy failed"); + } + return result; + }, + py::arg("entry"), py::arg("path") = "", + py::arg("insert_policy") = IFileTree::InsertPolicy::FAIL_IF_EXISTS); + + iFileTreeClass.def("clear", &IFileTree::clear); + iFileTreeClass.def("removeAll", &IFileTree::removeAll, py::arg("names")); + iFileTreeClass.def("removeIf", &IFileTree::removeIf, py::arg("filter")); + + // Special methods: + iFileTreeClass.def("__getitem__", + py::overload_cast<std::size_t>(&IFileTree::at)); + + iFileTreeClass.def("__iter__", [](IFileTree* tree) { + return py::make_iterator(*tree); + }); + iFileTreeClass.def("__len__", &IFileTree::size); + iFileTreeClass.def( + "__bool__", +[](const IFileTree* tree) { + return !tree->empty(); + }); + iFileTreeClass.def( + "__repr__", +[](const IFileTree* entry) { + return "IFileTree(\"" + entry->name() + "\")"; + }); + } + + void add_make_tree_function(pybind11::module_& m) + { + m.def( + "makeTree", + [](mo2::detail::PyFileTree::callback_t callback) + -> std::shared_ptr<IFileTree> { + return std::make_shared<mo2::detail::PyFileTree>(nullptr, "", callback); + }, + py::arg("callback") = mo2::detail::PyFileTree::callback_t{}); + } + +} // namespace mo2::python + +#pragma optimize("", on) diff --git a/libs/plugin_python/src/mobase/wrappers/pyfiletree.h b/libs/plugin_python/src/mobase/wrappers/pyfiletree.h new file mode 100644 index 0000000..0e94665 --- /dev/null +++ b/libs/plugin_python/src/mobase/wrappers/pyfiletree.h @@ -0,0 +1,34 @@ +#ifndef MO2_PYTHON_FILETREE_H +#define MO2_PYTHON_FILETREE_H + +#include "../pybind11_all.h" + +#include <uibase/ifiletree.h> + +namespace pybind11 { + template <> + struct polymorphic_type_hook<MOBase::FileTreeEntry> { + static const void* get(const MOBase::FileTreeEntry* src, + const std::type_info*& type); + }; +} // namespace pybind11 + +namespace mo2::python { + + /** + * @brief Add bindings for FileTreeEntry andIFileTree to the given module. + * + * @param mobase Module to add the bindings to. + */ + void add_ifiletree_bindings(pybind11::module_& m); + + /** + * @brief Add makeTree() function to the given module, useful for debugging. + * + * @param mobase Module to add the function to. + */ + void add_make_tree_function(pybind11::module_& m); + +} // namespace mo2::python + +#endif diff --git a/libs/plugin_python/src/mobase/wrappers/pyplugins.cpp b/libs/plugin_python/src/mobase/wrappers/pyplugins.cpp new file mode 100644 index 0000000..bcce0df --- /dev/null +++ b/libs/plugin_python/src/mobase/wrappers/pyplugins.cpp @@ -0,0 +1,262 @@ +#include "wrappers.h" + +#include <tuple> + +#include "pyplugins.h" + +namespace py = pybind11; +using namespace pybind11::literals; +using namespace MOBase; + +namespace mo2::python { + + // this one is kind of big so it has its own function + void add_iplugingame_bindings(pybind11::module_ m) + { + py::enum_<IPluginGame::LoadOrderMechanism>(m, "LoadOrderMechanism") + .value("None", IPluginGame::LoadOrderMechanism::None) + .value("FileTime", IPluginGame::LoadOrderMechanism::FileTime) + .value("PluginsTxt", IPluginGame::LoadOrderMechanism::PluginsTxt) + + .value("NONE", IPluginGame::LoadOrderMechanism::None) + .value("FILE_TIME", IPluginGame::LoadOrderMechanism::FileTime) + .value("PLUGINS_TXT", IPluginGame::LoadOrderMechanism::PluginsTxt); + + py::enum_<IPluginGame::SortMechanism>(m, "SortMechanism") + .value("NONE", IPluginGame::SortMechanism::NONE) + .value("MLOX", IPluginGame::SortMechanism::MLOX) + .value("BOSS", IPluginGame::SortMechanism::BOSS) + .value("LOOT", IPluginGame::SortMechanism::LOOT); + + // this does not actually do the conversion, but might be convenient + // for accessing the names for enum bits + py::enum_<IPluginGame::ProfileSetting>(m, "ProfileSetting", py::arithmetic()) + .value("mods", IPluginGame::MODS) + .value("configuration", IPluginGame::CONFIGURATION) + .value("savegames", IPluginGame::SAVEGAMES) + .value("preferDefaults", IPluginGame::PREFER_DEFAULTS) + + .value("MODS", IPluginGame::MODS) + .value("CONFIGURATION", IPluginGame::CONFIGURATION) + .value("SAVEGAMES", IPluginGame::SAVEGAMES) + .value("PREFER_DEFAULTS", IPluginGame::PREFER_DEFAULTS); + + py::class_<IPluginGame, PyPluginGame, IPlugin, + std::unique_ptr<IPluginGame, py::nodelete>>( + m, "IPluginGame", py::multiple_inheritance()) + .def(py::init<>()) + .def("detectGame", &IPluginGame::detectGame) + .def("gameName", &IPluginGame::gameName) + .def("displayGameName", &IPluginGame::displayGameName) + .def("initializeProfile", &IPluginGame::initializeProfile, "directory"_a, + "settings"_a) + .def("listSaves", &IPluginGame::listSaves, "folder"_a) + .def("isInstalled", &IPluginGame::isInstalled) + .def("gameIcon", &IPluginGame::gameIcon) + .def("gameDirectory", &IPluginGame::gameDirectory) + .def("dataDirectory", &IPluginGame::dataDirectory) + .def("modDataDirectory", &IPluginGame::modDataDirectory) + .def("secondaryDataDirectories", &IPluginGame::secondaryDataDirectories) + .def("setGamePath", &IPluginGame::setGamePath, "path"_a) + .def("documentsDirectory", &IPluginGame::documentsDirectory) + .def("savesDirectory", &IPluginGame::savesDirectory) + .def("executables", &IPluginGame::executables) + .def("executableForcedLoads", &IPluginGame::executableForcedLoads) + .def("steamAPPId", &IPluginGame::steamAPPId) + .def("primaryPlugins", &IPluginGame::primaryPlugins) + .def("enabledPlugins", &IPluginGame::enabledPlugins) + .def("gameVariants", &IPluginGame::gameVariants) + .def("setGameVariant", &IPluginGame::setGameVariant, "variant"_a) + .def("binaryName", &IPluginGame::binaryName) + .def("gameShortName", &IPluginGame::gameShortName) + .def("lootGameName", &IPluginGame::lootGameName) + .def("primarySources", &IPluginGame::primarySources) + .def("validShortNames", &IPluginGame::validShortNames) + .def("gameNexusName", &IPluginGame::gameNexusName) + .def("iniFiles", &IPluginGame::iniFiles) + .def("DLCPlugins", &IPluginGame::DLCPlugins) + .def("CCPlugins", &IPluginGame::CCPlugins) + .def("loadOrderMechanism", &IPluginGame::loadOrderMechanism) + .def("sortMechanism", &IPluginGame::sortMechanism) + .def("nexusModOrganizerID", &IPluginGame::nexusModOrganizerID) + .def("nexusGameID", &IPluginGame::nexusGameID) + .def("looksValid", &IPluginGame::looksValid, "directory"_a) + .def("gameVersion", &IPluginGame::gameVersion) + .def("getLauncherName", &IPluginGame::getLauncherName) + .def("getSupportURL", &IPluginGame::getSupportURL) + .def("getModMappings", &IPluginGame::getModMappings); + } + + // multiple installers + void add_iplugininstaller_bindings(pybind11::module_ m) + { + // this is bind but should not be inherited in Python - does not make sense, + // having it makes it simpler to bind the Simple and Custom installers + py::class_<IPluginInstaller, PyPluginInstallerBase<IPluginInstaller>, IPlugin, + std::unique_ptr<IPluginInstaller, py::nodelete>>( + m, "IPluginInstaller", py::multiple_inheritance()) + .def("isArchiveSupported", &IPluginInstaller::isArchiveSupported, "tree"_a) + .def("priority", &IPluginInstaller::priority) + .def("onInstallationStart", &IPluginInstaller::onInstallationStart, + "archive"_a, "reinstallation"_a, "current_mod"_a) + .def("onInstallationEnd", &IPluginInstaller::onInstallationEnd, "result"_a, + "new_mod"_a) + .def("isManualInstaller", &IPluginInstaller::isManualInstaller) + .def("setParentWidget", &IPluginInstaller::setParentWidget, "parent"_a) + .def("setInstallationManager", &IPluginInstaller::setInstallationManager, + "manager"_a) + .def("_parentWidget", + &PyPluginInstallerBase<IPluginInstaller>::parentWidget) + .def("_manager", &PyPluginInstallerBase<IPluginInstaller>::manager, + py::return_value_policy::reference); + + py::class_<IPluginInstallerSimple, PyPluginInstallerSimple, IPluginInstaller, + IPlugin, std::unique_ptr<IPluginInstallerSimple, py::nodelete>>( + m, "IPluginInstallerSimple", py::multiple_inheritance()) + .def(py::init<>()) + + // note: keeping the variant here even if we always return a tuple + // to be consistent with the wrapper and have proper stubs generation. + .def( + "install", + [](IPluginInstallerSimple* p, GuessedValue<QString>& modName, + std::shared_ptr<IFileTree>& tree, QString& version, + int& nexusID) -> PyPluginInstallerSimple::py_install_return_type { + auto result = p->install(modName, tree, version, nexusID); + return std::make_tuple(result, tree, version, nexusID); + }, + "name"_a, "tree"_a, "version"_a, "nexus_id"_a); + + py::class_<IPluginInstallerCustom, PyPluginInstallerCustom, IPluginInstaller, + IPlugin, std::unique_ptr<IPluginInstallerCustom, py::nodelete>>( + m, "IPluginInstallerCustom", py::multiple_inheritance()) + .def(py::init<>()) + .def("isArchiveSupported", &IPluginInstallerCustom::isArchiveSupported, + "archive_name"_a) + .def("supportedExtensions", &IPluginInstallerCustom::supportedExtensions) + .def("install", &IPluginInstallerCustom::install, "mod_name"_a, + "game_name"_a, "archive_name"_a, "version"_a, "nexus_id"_a); + } + + void add_plugins_bindings(pybind11::module_ m) + { + py::class_<IPlugin, PyPlugin, std::unique_ptr<IPlugin, py::nodelete>>( + m, "IPluginBase", py::multiple_inheritance()) + .def(py::init<>()) + .def("init", &IPlugin::init, "organizer"_a) + .def("name", &IPlugin::name) + .def("localizedName", &IPlugin::localizedName) + .def("master", &IPlugin::master) + .def("author", &IPlugin::author) + .def("description", &IPlugin::description) + .def("version", &IPlugin::version) + .def("requirements", &IPlugin::requirements) + .def("settings", &IPlugin::settings) + .def("enabledByDefault", &IPlugin::enabledByDefault); + + py::class_<IPyPlugin, PyPlugin, IPlugin, + std::unique_ptr<IPyPlugin, py::nodelete>>(m, "IPlugin", + py::multiple_inheritance()) + .def(py::init<>()); + + py::class_<IPyPluginFileMapper, PyPluginFileMapper, IPlugin, + std::unique_ptr<IPyPluginFileMapper, py::nodelete>>( + m, "IPluginFileMapper", py::multiple_inheritance()) + .def(py::init<>()) + .def("mappings", &IPluginFileMapper::mappings); + + py::class_<IPyPluginDiagnose, PyPluginDiagnose, IPlugin, + std::unique_ptr<IPyPluginDiagnose, py::nodelete>>( + m, "IPluginDiagnose", py::multiple_inheritance()) + .def(py::init<>()) + .def("activeProblems", &IPluginDiagnose::activeProblems) + .def("shortDescription", &IPluginDiagnose::shortDescription, "key"_a) + .def("fullDescription", &IPluginDiagnose::fullDescription, "key"_a) + .def("hasGuidedFix", &IPluginDiagnose::hasGuidedFix, "key"_a) + .def("startGuidedFix", &IPluginDiagnose::startGuidedFix, "key"_a) + .def("_invalidate", &PyPluginDiagnose::invalidate); + + py::class_<IPluginTool, PyPluginTool, IPlugin, + std::unique_ptr<IPluginTool, py::nodelete>>( + m, "IPluginTool", py::multiple_inheritance()) + .def(py::init<>()) + .def("displayName", &IPluginTool::displayName) + .def("tooltip", &IPluginTool::tooltip) + .def("icon", &IPluginTool::icon) + .def("display", &IPluginTool::display) + .def("setParentWidget", &IPluginTool::setParentWidget) + .def("_parentWidget", &PyPluginTool::parentWidget); + + py::class_<IPluginPreview, PyPluginPreview, IPlugin, + std::unique_ptr<IPluginPreview, py::nodelete>>( + m, "IPluginPreview", py::multiple_inheritance()) + .def(py::init<>()) + .def("supportedExtensions", &IPluginPreview::supportedExtensions) + .def("supportsArchives", &IPluginPreview::supportsArchives) + .def("genFilePreview", &IPluginPreview::genFilePreview, "filename"_a, + "max_size"_a) + .def("genDataPreview", &IPluginPreview::genDataPreview, "file_data"_a, + "filename"_a, "max_size"_a); + + py::class_<IPluginModPage, PyPluginModPage, IPlugin, + std::unique_ptr<IPluginModPage, py::nodelete>>( + m, "IPluginModPage", py::multiple_inheritance()) + .def(py::init<>()) + .def("displayName", &IPluginModPage::displayName) + .def("icon", &IPluginModPage::icon) + .def("pageURL", &IPluginModPage::pageURL) + .def("useIntegratedBrowser", &IPluginModPage::useIntegratedBrowser) + .def("handlesDownload", &IPluginModPage::handlesDownload, "page_url"_a, + "download_url"_a, "fileinfo"_a) + .def("setParentWidget", &IPluginModPage::setParentWidget, "parent"_a) + .def("_parentWidget", &PyPluginModPage::parentWidget); + + add_iplugingame_bindings(m); + add_iplugininstaller_bindings(m); + } + + struct extract_plugins_helper { + QList<QObject*> objects; + + template <class IPluginClass> + void append_if_instance(pybind11::object plugin_obj) + { + if (py::isinstance<IPluginClass>(plugin_obj)) { + objects.append(plugin_obj.cast<IPluginClass*>()); + } + } + }; + + QList<QObject*> extract_plugins(pybind11::object plugin_obj) + { + extract_plugins_helper helper; + + // we need to check the trampoline class for these since the interfaces do not + // extend IPlugin + helper.append_if_instance<IPyPluginFileMapper>(plugin_obj); + helper.append_if_instance<IPyPluginDiagnose>(plugin_obj); + + helper.append_if_instance<IPluginModPage>(plugin_obj); + helper.append_if_instance<IPluginPreview>(plugin_obj); + helper.append_if_instance<IPluginTool>(plugin_obj); + + helper.append_if_instance<IPluginGame>(plugin_obj); + + // we need to check the two installer types because IPluginInstaller does not + // inherit QObject, and the trampoline do not have a common ancestor + helper.append_if_instance<IPluginInstallerSimple>(plugin_obj); + helper.append_if_instance<IPluginInstallerCustom>(plugin_obj); + + if (helper.objects.isEmpty()) { + helper.append_if_instance<IPyPlugin>(plugin_obj); + } + + // tie the lifetime of the Python object to the lifetime of the QObject + for (auto* object : helper.objects) { + py::qt::set_qt_owner(object, plugin_obj); + } + + return helper.objects; + } + +} // namespace mo2::python diff --git a/libs/plugin_python/src/mobase/wrappers/pyplugins.h b/libs/plugin_python/src/mobase/wrappers/pyplugins.h new file mode 100644 index 0000000..17f7ab5 --- /dev/null +++ b/libs/plugin_python/src/mobase/wrappers/pyplugins.h @@ -0,0 +1,527 @@ +#ifndef PYTHON_WRAPPERS_PYPLUGINS_H +#define PYTHON_WRAPPERS_PYPLUGINS_H + +#include "../pybind11_all.h" + +#include <uibase/iinstallationmanager.h> + +#include <uibase/iplugin.h> +#include <uibase/iplugindiagnose.h> +#include <uibase/ipluginfilemapper.h> +#include <uibase/iplugingame.h> +#include <uibase/iplugininstaller.h> +#include <uibase/iplugininstallercustom.h> +#include <uibase/iplugininstallersimple.h> +#include <uibase/ipluginmodpage.h> +#include <uibase/ipluginpreview.h> +#include <uibase/iplugintool.h> + +// these needs to be defined in a header file for automoc - this file is included only +// in pyplugins.cpp +namespace mo2::python { + + using namespace MOBase; + + // we need two base trampoline because IPluginGame has some final methods. + template <class PluginBase> + class PyPluginBaseNoFinal : public PluginBase { + public: + using PluginBase::PluginBase; + + PyPluginBaseNoFinal(PyPluginBaseNoFinal const&) = delete; + PyPluginBaseNoFinal(PyPluginBaseNoFinal&&) = delete; + PyPluginBaseNoFinal& operator=(PyPluginBaseNoFinal const&) = delete; + PyPluginBaseNoFinal& operator=(PyPluginBaseNoFinal&&) = delete; + + bool init(IOrganizer* organizer) override + { + PYBIND11_OVERRIDE_PURE(bool, PluginBase, init, organizer); + } + QString name() const override + { + PYBIND11_OVERRIDE_PURE(QString, PluginBase, name, ); + } + QString localizedName() const override + { + PYBIND11_OVERRIDE(QString, PluginBase, localizedName, ); + } + QString master() const override + { + PYBIND11_OVERRIDE(QString, PluginBase, master, ); + } + QString author() const override + { + PYBIND11_OVERRIDE_PURE(QString, PluginBase, author, ); + } + QString description() const override + { + PYBIND11_OVERRIDE_PURE(QString, PluginBase, description, ); + } + VersionInfo version() const override + { + PYBIND11_OVERRIDE_PURE(VersionInfo, PluginBase, version, ); + } + QList<PluginSetting> settings() const override + { + PYBIND11_OVERRIDE_PURE(QList<PluginSetting>, PluginBase, settings, ); + } + }; + + template <class PluginBase> + class PyPluginBase : public PyPluginBaseNoFinal<PluginBase> { + public: + using PyPluginBaseNoFinal<PluginBase>::PyPluginBaseNoFinal; + + std::vector<std::shared_ptr<const IPluginRequirement>> requirements() const + { + PYBIND11_OVERRIDE(std::vector<std::shared_ptr<const IPluginRequirement>>, + PluginBase, requirements, ); + } + bool enabledByDefault() const override + { + PYBIND11_OVERRIDE(bool, PluginBase, enabledByDefault, ); + } + }; + + // these classes do not inherit IPlugin or QObject so we need intermediate class to + // get proper bindings + class IPyPlugin : public QObject, public IPlugin {}; + class IPyPluginFileMapper : public IPyPlugin, public IPluginFileMapper {}; + class IPyPluginDiagnose : public IPyPlugin, public IPluginDiagnose {}; + + // PyXXX classes - trampoline classes for the plugins + + class PyPlugin : public PyPluginBase<IPyPlugin> { + Q_OBJECT + Q_INTERFACES(MOBase::IPlugin) + }; + + class PyPluginFileMapper : public PyPluginBase<IPyPluginFileMapper> { + Q_OBJECT + Q_INTERFACES(MOBase::IPlugin MOBase::IPluginFileMapper) + public: + MappingType mappings() const override + { + PYBIND11_OVERRIDE_PURE(MappingType, IPyPluginFileMapper, mappings, ); + } + }; + + class PyPluginDiagnose : public PyPluginBase<IPyPluginDiagnose> { + Q_OBJECT + Q_INTERFACES(MOBase::IPlugin MOBase::IPluginDiagnose) + public: + std::vector<unsigned int> activeProblems() const + { + PYBIND11_OVERRIDE_PURE(std::vector<unsigned int>, IPyPluginDiagnose, + activeProblems, ); + } + + QString shortDescription(unsigned int key) const + { + PYBIND11_OVERRIDE_PURE(QString, IPyPluginDiagnose, shortDescription, key); + } + + QString fullDescription(unsigned int key) const + { + PYBIND11_OVERRIDE_PURE(QString, IPyPluginDiagnose, fullDescription, key); + } + + bool hasGuidedFix(unsigned int key) const + { + PYBIND11_OVERRIDE_PURE(bool, IPyPluginDiagnose, hasGuidedFix, key); + } + + void startGuidedFix(unsigned int key) const + { + PYBIND11_OVERRIDE_PURE(void, IPyPluginDiagnose, startGuidedFix, key); + } + + // we need to bring this in public scope + using IPluginDiagnose::invalidate; + }; + + class PyPluginTool : public PyPluginBase<IPluginTool> { + Q_OBJECT + Q_INTERFACES(MOBase::IPlugin MOBase::IPluginTool) + public: + QString displayName() const override + { + PYBIND11_OVERRIDE_PURE(QString, IPluginTool, displayName, ); + } + QString tooltip() const override + { + PYBIND11_OVERRIDE_PURE(QString, IPluginTool, tooltip, ); + } + QIcon icon() const override + { + PYBIND11_OVERRIDE_PURE(QIcon, IPluginTool, icon, ); + } + void setParentWidget(QWidget* widget) override + { + PYBIND11_OVERRIDE(void, IPluginTool, setParentWidget, widget); + } + void display() const override + { + PYBIND11_OVERRIDE_PURE(void, IPluginTool, display, ); + } + + // we need to bring this in public scope + using IPluginTool::parentWidget; + }; + + class PyPluginPreview : public PyPluginBase<IPluginPreview> { + Q_OBJECT + Q_INTERFACES(MOBase::IPlugin MOBase::IPluginPreview) + public: + std::set<QString> supportedExtensions() const override + { + PYBIND11_OVERRIDE_PURE(std::set<QString>, IPluginPreview, + supportedExtensions, ); + } + + bool supportsArchives() const override + { + PYBIND11_OVERRIDE(bool, IPluginPreview, supportsArchives, ); + } + + QWidget* genFilePreview(const QString& fileName, + const QSize& maxSize) const override + { + PYBIND11_OVERRIDE_PURE(QWidget*, IPluginPreview, genFilePreview, fileName, + maxSize); + } + + QWidget* genDataPreview(const QByteArray& fileData, const QString& fileName, + const QSize& maxSize) const override + { + PYBIND11_OVERRIDE(QWidget*, IPluginPreview, genDataPreview, fileData, + fileName, maxSize); + } + }; + + class PyPluginModPage : public PyPluginBase<IPluginModPage> { + Q_OBJECT + Q_INTERFACES(MOBase::IPlugin MOBase::IPluginModPage) + public: + QString displayName() const override + { + PYBIND11_OVERRIDE_PURE(QString, IPluginModPage, displayName, ); + } + + QIcon icon() const override + { + PYBIND11_OVERRIDE_PURE(QIcon, IPluginModPage, icon, ); + } + + QUrl pageURL() const override + { + PYBIND11_OVERRIDE_PURE(QUrl, IPluginModPage, pageURL, ); + } + + bool useIntegratedBrowser() const override + { + PYBIND11_OVERRIDE_PURE(bool, IPluginModPage, useIntegratedBrowser, ); + } + + bool handlesDownload(const QUrl& pageURL, const QUrl& downloadURL, + ModRepositoryFileInfo& fileInfo) const override + { + // TODO: cannot modify fileInfo from Python + PYBIND11_OVERRIDE_PURE(bool, IPluginModPage, handlesDownload, pageURL, + downloadURL, &fileInfo); + } + + void setParentWidget(QWidget* widget) override + { + PYBIND11_OVERRIDE(void, IPluginModPage, setParentWidget, widget); + } + + // we need to bring this in public scope + using IPluginModPage::parentWidget; + }; + + // installers + template <class PluginInstallerBase> + class PyPluginInstallerBase : public PyPluginBase<PluginInstallerBase> { + public: + using PyPluginBase<PluginInstallerBase>::PyPluginBase; + + unsigned int priority() const override + { + PYBIND11_OVERRIDE_PURE(unsigned int, PluginInstallerBase, priority); + } + + bool isManualInstaller() const override + { + PYBIND11_OVERRIDE_PURE(bool, PluginInstallerBase, isManualInstaller, ); + } + + void onInstallationStart(QString const& archive, bool reinstallation, + IModInterface* currentMod) + { + PYBIND11_OVERRIDE(void, PluginInstallerBase, onInstallationStart, archive, + reinstallation, currentMod); + } + + void onInstallationEnd(IPluginInstaller::EInstallResult result, + IModInterface* newMod) + { + PYBIND11_OVERRIDE(void, PluginInstallerBase, onInstallationEnd, result, + newMod); + } + + bool isArchiveSupported(std::shared_ptr<const IFileTree> tree) const override + { + PYBIND11_OVERRIDE_PURE(bool, PluginInstallerBase, isArchiveSupported, tree); + } + + // we need to bring these in public scope + using PluginInstallerBase::manager; + using PluginInstallerBase::parentWidget; + }; + + class PyPluginInstallerCustom + : public PyPluginInstallerBase<IPluginInstallerCustom> { + Q_OBJECT + Q_INTERFACES( + MOBase::IPlugin MOBase::IPluginInstaller MOBase::IPluginInstallerCustom) + public: + bool isArchiveSupported(const QString& archiveName) const + { + PYBIND11_OVERRIDE_PURE(bool, IPluginInstallerCustom, isArchiveSupported, + archiveName); + } + + std::set<QString> supportedExtensions() const + { + PYBIND11_OVERRIDE_PURE(std::set<QString>, IPluginInstallerCustom, + supportedExtensions, ); + } + + EInstallResult install(GuessedValue<QString>& modName, QString gameName, + const QString& archiveName, const QString& version, + int nexusID) override + { + PYBIND11_OVERRIDE_PURE(EInstallResult, IPluginInstallerCustom, install, + &modName, gameName, archiveName, version, nexusID); + } + }; + + class PyPluginInstallerSimple + : public PyPluginInstallerBase<IPluginInstallerSimple> { + Q_OBJECT + Q_INTERFACES( + MOBase::IPlugin MOBase::IPluginInstaller MOBase::IPluginInstallerSimple) + public: + using py_install_return_type = + std::variant<IPluginInstaller::EInstallResult, std::shared_ptr<IFileTree>, + std::tuple<IPluginInstaller::EInstallResult, + std::shared_ptr<IFileTree>, QString, int>>; + + EInstallResult install(GuessedValue<QString>& modName, + std::shared_ptr<IFileTree>& tree, QString& version, + int& nexusID) override + { + const auto result = [&, this]() { + PYBIND11_OVERRIDE_PURE(py_install_return_type, IPluginInstallerSimple, + install, &modName, tree, version, nexusID); + }(); + + return std::visit( + [&tree, &version, &nexusID](auto const& t) { + using type = std::decay_t<decltype(t)>; + if constexpr (std::is_same_v<type, EInstallResult>) { + return t; + } + else if constexpr (std::is_same_v<type, + std::shared_ptr<IFileTree>>) { + tree = t; + return RESULT_SUCCESS; + } + else if constexpr (std::is_same_v< + type, std::tuple<EInstallResult, + std::shared_ptr<IFileTree>, + QString, int>>) { + tree = std::get<1>(t); + version = std::get<2>(t); + nexusID = std::get<3>(t); + return std::get<0>(t); + } + }, + result); + } + }; + + // game + class PyPluginGame : public PyPluginBaseNoFinal<IPluginGame> { + Q_OBJECT + Q_INTERFACES(MOBase::IPlugin MOBase::IPluginGame) + public: + void detectGame() override + { + PYBIND11_OVERRIDE_PURE(void, IPluginGame, detectGame, ); + } + QString gameName() const override + { + PYBIND11_OVERRIDE_PURE(QString, IPluginGame, gameName, ); + } + QString displayGameName() const override + { + PYBIND11_OVERRIDE(QString, IPluginGame, displayGameName, ); + } + void initializeProfile(const QDir& directory, + ProfileSettings settings) const override + { + PYBIND11_OVERRIDE_PURE(void, IPluginGame, initializeProfile, directory, + settings); + } + std::vector<std::shared_ptr<const ISaveGame>> + listSaves(QDir folder) const override + { + PYBIND11_OVERRIDE_PURE(std::vector<std::shared_ptr<const ISaveGame>>, + IPluginGame, listSaves, folder); + } + bool isInstalled() const override + { + PYBIND11_OVERRIDE_PURE(bool, IPluginGame, isInstalled, ); + } + QIcon gameIcon() const override + { + PYBIND11_OVERRIDE_PURE(QIcon, IPluginGame, gameIcon, ); + } + QDir gameDirectory() const override + { + PYBIND11_OVERRIDE_PURE(QDir, IPluginGame, gameDirectory, ); + } + QDir dataDirectory() const override + { + PYBIND11_OVERRIDE_PURE(QDir, IPluginGame, dataDirectory, ); + } + QString modDataDirectory() const override + { + PYBIND11_OVERRIDE(QString, IPluginGame, modDataDirectory, ); + } + QMap<QString, QDir> secondaryDataDirectories() const override + { + using string_dir_map = QMap<QString, QDir>; + PYBIND11_OVERRIDE(string_dir_map, IPluginGame, secondaryDataDirectories, ); + } + void setGamePath(const QString& path) override + { + PYBIND11_OVERRIDE_PURE(void, IPluginGame, setGamePath, path); + } + QDir documentsDirectory() const override + { + PYBIND11_OVERRIDE_PURE(QDir, IPluginGame, documentsDirectory, ); + } + QDir savesDirectory() const override + { + PYBIND11_OVERRIDE_PURE(QDir, IPluginGame, savesDirectory, ); + } + QList<ExecutableInfo> executables() const override + { + PYBIND11_OVERRIDE(QList<ExecutableInfo>, IPluginGame, executables, ); + } + QList<ExecutableForcedLoadSetting> executableForcedLoads() const override + { + PYBIND11_OVERRIDE_PURE(QList<ExecutableForcedLoadSetting>, IPluginGame, + executableForcedLoads, ); + } + QString steamAPPId() const override + { + PYBIND11_OVERRIDE(QString, IPluginGame, steamAPPId, ); + } + QStringList primaryPlugins() const override + { + PYBIND11_OVERRIDE(QStringList, IPluginGame, primaryPlugins, ); + } + QStringList enabledPlugins() const override + { + PYBIND11_OVERRIDE(QStringList, IPluginGame, enabledPlugins, ); + } + QStringList gameVariants() const override + { + PYBIND11_OVERRIDE(QStringList, IPluginGame, gameVariants, ); + } + void setGameVariant(const QString& variant) override + { + PYBIND11_OVERRIDE_PURE(void, IPluginGame, setGameVariant, variant); + } + QString binaryName() const override + { + PYBIND11_OVERRIDE_PURE(QString, IPluginGame, binaryName, ); + } + QString gameShortName() const override + { + PYBIND11_OVERRIDE_PURE(QString, IPluginGame, gameShortName, ); + } + QString lootGameName() const override + { + PYBIND11_OVERRIDE(QString, IPluginGame, lootGameName, ); + } + QStringList primarySources() const override + { + PYBIND11_OVERRIDE(QStringList, IPluginGame, primarySources, ); + } + QStringList validShortNames() const override + { + PYBIND11_OVERRIDE(QStringList, IPluginGame, validShortNames, ); + } + QString gameNexusName() const override + { + PYBIND11_OVERRIDE(QString, IPluginGame, gameNexusName, ); + } + QStringList iniFiles() const override + { + PYBIND11_OVERRIDE(QStringList, IPluginGame, iniFiles, ); + } + QStringList DLCPlugins() const override + { + PYBIND11_OVERRIDE(QStringList, IPluginGame, DLCPlugins, ); + } + QStringList CCPlugins() const override + { + PYBIND11_OVERRIDE(QStringList, IPluginGame, CCPlugins, ); + } + LoadOrderMechanism loadOrderMechanism() const override + { + PYBIND11_OVERRIDE(LoadOrderMechanism, IPluginGame, loadOrderMechanism, ); + } + SortMechanism sortMechanism() const override + { + PYBIND11_OVERRIDE(SortMechanism, IPluginGame, sortMechanism, ); + } + int nexusModOrganizerID() const override + { + PYBIND11_OVERRIDE(int, IPluginGame, nexusModOrganizerID, ); + } + int nexusGameID() const override + { + PYBIND11_OVERRIDE_PURE(int, IPluginGame, nexusGameID, ); + } + bool looksValid(QDir const& dir) const override + { + PYBIND11_OVERRIDE_PURE(bool, IPluginGame, looksValid, dir); + } + QString gameVersion() const override + { + PYBIND11_OVERRIDE_PURE(QString, IPluginGame, gameVersion, ); + } + QString getLauncherName() const override + { + PYBIND11_OVERRIDE_PURE(QString, IPluginGame, getLauncherName, ); + } + QString getSupportURL() const override + { + PYBIND11_OVERRIDE(QString, IPluginGame, getSupportURL, ); + } + QMap<QString, QStringList> getModMappings() const override + { + using vfs_map = QMap<QString, QStringList>; + PYBIND11_OVERRIDE(vfs_map, IPluginGame, getModMappings, ); + } + }; + +} // namespace mo2::python + +#endif diff --git a/libs/plugin_python/src/mobase/wrappers/utils.cpp b/libs/plugin_python/src/mobase/wrappers/utils.cpp new file mode 100644 index 0000000..95111fa --- /dev/null +++ b/libs/plugin_python/src/mobase/wrappers/utils.cpp @@ -0,0 +1,50 @@ +#include "wrappers.h" + +#include "../pybind11_all.h" + +#include <uibase/report.h> +#include <uibase/utility.h> + +#ifdef _WIN32 +#include "known_folders.h" +#endif + +namespace py = pybind11; +using namespace MOBase; + +namespace mo2::python { + + void add_utils_bindings(pybind11::module_ m) + { +#ifdef _WIN32 + py::class_<KnownFolder> pyKnownFolder(m, "KnownFolder"); + for (std::size_t i = 0; i < KNOWN_FOLDERS.size(); ++i) { + pyKnownFolder.attr(KNOWN_FOLDERS[i].name) = py::int_(i); + } + + m.def( + "getKnownFolder", + [](std::size_t knownFolderId, QString what) { + return getKnownFolder(KNOWN_FOLDERS.at(knownFolderId).guid, what); + }, + py::arg("known_folder"), py::arg("what") = ""); + + m.def( + "getOptionalKnownFolder", + [](std::size_t knownFolderId) { + const auto r = + getOptionalKnownFolder(KNOWN_FOLDERS.at(knownFolderId).guid); + return r.isEmpty() ? py::none{} : py::cast(r); + }, + py::arg("known_folder")); +#endif + + 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")); + } + +} // namespace mo2::python diff --git a/libs/plugin_python/src/mobase/wrappers/widgets.cpp b/libs/plugin_python/src/mobase/wrappers/widgets.cpp new file mode 100644 index 0000000..e626540 --- /dev/null +++ b/libs/plugin_python/src/mobase/wrappers/widgets.cpp @@ -0,0 +1,77 @@ +#include "wrappers.h" + +#include "../pybind11_all.h" + +#include <uibase/report.h> + +namespace py = pybind11; +using namespace MOBase; + +namespace mo2::python { + + void add_widget_bindings(pybind11::module_ m) + { + // TaskDialog is also in Windows System. + using TaskDialog = MOBase::TaskDialog; + + // TaskDialog + py::class_<TaskDialogButton>(m, "TaskDialogButton") + .def(py::init<QString, QString, QMessageBox::StandardButton>(), + py::arg("text"), py::arg("description"), py::arg("button")) + .def(py::init<QString, QMessageBox::StandardButton>(), py::arg("text"), + py::arg("button")) + .def_readwrite("text", &TaskDialogButton::text) + .def_readwrite("description", &TaskDialogButton::description) + .def_readwrite("button", &TaskDialogButton::button); + + py::class_<TaskDialog>(m, "TaskDialog") + .def(py::init([](QWidget* parent, QString const& title, QString const& main, + QString const& content, QString const& details, + QMessageBox::Icon icon, + std::vector<TaskDialogButton> const& buttons, + std::variant<QString, std::tuple<QString, QString>> const& + remember) { + auto* dialog = new TaskDialog(parent, title); + dialog->main(main).content(content).details(details).icon(icon); + + for (auto& button : buttons) { + dialog->button(button); + } + + std::visit( + [dialog](auto const& item) { + QString action, file; + if constexpr (std::is_same_v<std::decay_t<decltype(item)>, + QString>) { + action = item; + } + else { + action = std::get<0>(item); + file = std::get<1>(item); + } + dialog->remember(action, file); + }, + remember); + + return dialog; + }), + py::return_value_policy::take_ownership, + py::arg("parent") = static_cast<QWidget*>(nullptr), + py::arg("title") = "", py::arg("main") = "", py::arg("content") = "", + py::arg("details") = "", py::arg("icon") = QMessageBox::NoIcon, + py::arg("buttons") = std::vector<TaskDialogButton>{}, + py::arg("remember") = "") + .def("setTitle", &TaskDialog::title, py::arg("title")) + .def("setMain", &TaskDialog::main, py::arg("main")) + .def("setContent", &TaskDialog::content, py::arg("content")) + .def("setDetails", &TaskDialog::details, py::arg("details")) + .def("setIcon", &TaskDialog::icon, py::arg("icon")) + .def("addButton", &TaskDialog::button, py::arg("button")) + .def("setRemember", &TaskDialog::remember, py::arg("action"), + py::arg("file") = "") + .def("setWidth", &TaskDialog::setWidth, py::arg("width")) + .def("addContent", &TaskDialog::addContent, py::arg("widget")) + .def("exec", &TaskDialog::exec); + } + +} // namespace mo2::python diff --git a/libs/plugin_python/src/mobase/wrappers/wrappers.cpp b/libs/plugin_python/src/mobase/wrappers/wrappers.cpp new file mode 100644 index 0000000..10f043d --- /dev/null +++ b/libs/plugin_python/src/mobase/wrappers/wrappers.cpp @@ -0,0 +1,120 @@ + +#include "wrappers.h" + +#include "../pybind11_all.h" + +#include <QDir> +#include <QIcon> +#include <QString> +#include <QUrl> + +// IOrganizer must be bring here to properly compile the Python bindings of +// plugin requirements +#include <uibase/imoinfo.h> +#include <uibase/isavegame.h> +#include <uibase/isavegameinfowidget.h> +#include <uibase/pluginrequirements.h> + +using namespace pybind11::literals; +namespace py = pybind11; +using namespace MOBase; + +namespace mo2::python { + + class PyPluginRequirement : public IPluginRequirement { + public: + std::optional<Problem> check(IOrganizer* organizer) const override + { + PYBIND11_OVERRIDE_PURE(std::optional<Problem>, IPluginRequirement, check, + organizer); + }; + }; + + class PySaveGame : public ISaveGame { + public: + QString getFilepath() const override + { + PYBIND11_OVERRIDE_PURE(FileWrapper, ISaveGame, getFilepath, ); + } + + QDateTime getCreationTime() const override + { + PYBIND11_OVERRIDE_PURE(QDateTime, ISaveGame, getCreationTime, ); + } + + QString getName() const override + { + PYBIND11_OVERRIDE_PURE(QString, ISaveGame, getName, ); + } + + QString getSaveGroupIdentifier() const override + { + PYBIND11_OVERRIDE_PURE(QString, ISaveGame, getSaveGroupIdentifier, ); + } + + QStringList allFiles() const override + { + return toQStringList([&] { + PYBIND11_OVERRIDE_PURE(QList<FileWrapper>, ISaveGame, allFiles, ); + }()); + } + + ~PySaveGame() { std::cout << "~PySaveGame()" << std::endl; } + }; + + class PySaveGameInfoWidget : public ISaveGameInfoWidget { + public: + // Bring the constructor: + using ISaveGameInfoWidget::ISaveGameInfoWidget; + + void setSave(ISaveGame const& save) override + { + PYBIND11_OVERRIDE_PURE(void, ISaveGameInfoWidget, setSave, &save); + } + + ~PySaveGameInfoWidget() { std::cout << "~PySaveGameInfoWidget()" << std::endl; } + }; + + void add_wrapper_bindings(pybind11::module_ m) + { + // ISaveGame - custom type_caster<> for shared_ptr<> to keep the Python object + // alive when returned from Python (see shared_cpp_owner.h) + + py::class_<ISaveGame, PySaveGame, std::shared_ptr<ISaveGame>>(m, "ISaveGame") + .def(py::init<>()) + .def("getFilepath", wrap_return_for_filepath(&ISaveGame::getFilepath)) + .def("getCreationTime", &ISaveGame::getCreationTime) + .def("getName", &ISaveGame::getName) + .def("getSaveGroupIdentifier", &ISaveGame::getSaveGroupIdentifier) + .def("allFiles", [](ISaveGame* s) -> QList<FileWrapper> { + const auto result = s->allFiles(); + return {result.begin(), result.end()}; + }); + + // ISaveGameInfoWidget - custom holder to keep the Python object alive alongside + // the widget + + py::class_<ISaveGameInfoWidget, PySaveGameInfoWidget, + py::qt::qobject_holder<ISaveGameInfoWidget>> + iSaveGameInfoWidget(m, "ISaveGameInfoWidget"); + iSaveGameInfoWidget.def(py::init<QWidget*>(), "parent"_a = (QWidget*)nullptr) + .def("setSave", &ISaveGameInfoWidget::setSave, "save"_a); + py::qt::add_qt_delegate<QWidget>(iSaveGameInfoWidget, "_widget"); + + // IPluginRequirement - custom type_caster<> for shared_ptr<> to keep the Python + // object alive when returned from Python (see shared_cpp_owner.h) + + py::class_<IPluginRequirement, std::shared_ptr<IPluginRequirement>, + PyPluginRequirement> + iPluginRequirementClass(m, "IPluginRequirement"); + + py::class_<IPluginRequirement::Problem>(iPluginRequirementClass, "Problem") + .def(py::init<QString, QString>(), "short_description"_a, + "long_description"_a = "") + .def("shortDescription", &IPluginRequirement::Problem::shortDescription) + .def("longDescription", &IPluginRequirement::Problem::longDescription); + + iPluginRequirementClass.def("check", &IPluginRequirement::check, "organizer"_a); + } + +} // namespace mo2::python diff --git a/libs/plugin_python/src/mobase/wrappers/wrappers.h b/libs/plugin_python/src/mobase/wrappers/wrappers.h new file mode 100644 index 0000000..43dd574 --- /dev/null +++ b/libs/plugin_python/src/mobase/wrappers/wrappers.h @@ -0,0 +1,109 @@ +#ifndef PYTHON_WRAPPERS_WRAPPERS_H +#define PYTHON_WRAPPERS_WRAPPERS_H + +#include <any> +#include <map> +#include <typeindex> + +#include <pybind11/pybind11.h> + +#include <QList> +#include <QObject> + +#include <uibase/iplugingame.h> + +namespace mo2::python { + + /** + * @brief Add bindings for the various classes in uibase that are not + * wrappers (i.e., cannot be extended from Python). + * + * @param m Python module to add bindings to. + */ + void add_basic_bindings(pybind11::module_ m); + + /** + * @brief Add bindings for the various custom widget classes in uibase that + * cannot be extended from Python. + * + * @param m Python module to add bindings to. + */ + void add_widget_bindings(pybind11::module_ m); + + /** + * @brief Add bindings for the various utilities classes and functions in uibase + * that cannot be extended from Python. + * + * @param m Python module to add bindings to. + */ + void add_utils_bindings(pybind11::module_ m); + + /** + * @brief Add bindings for the uibase wrappers to the given module. uibase + * wrappers include classes from uibase that can be extended from Python but + * are neither plugins nor game features (e.g., ISaveGame). + * + * @param m Python module to add bindings to. + */ + void add_wrapper_bindings(pybind11::module_ m); + + /** + * @brief Add bindings for the various plugin classes in uibase that can be + * extended from Python. + * + * @param m Python module to add bindings to. + */ + void add_plugins_bindings(pybind11::module_ m); + + /** + * @brief Extract plugins from the given object. For each plugin implemented, an + * object is returned. + * + * The returned QObject* are set as owner of the given object so that the Python + * object lifetime does not end immediately after returning to C++. + * + * @param object Python object to extract plugins from. + * + * @return a QObject* for each plugin implemented by the given object. + */ + QList<QObject*> extract_plugins(pybind11::object object); + + /** + * @brief Add bindings for the various game features classes in uibase that + * can be extended from Python. + * + * @param m Python module to add bindings to. + */ + void add_game_feature_bindings(pybind11::module_ m); + + /** + * @brief Add bindings for IGameFeatures. + * + * @param m Python module to add bindings to. + */ + void add_igamefeatures_classes(pybind11::module_ m); + + /** + * @brief Extract the game feature corresponding to the given Python type. + * + * @param gameFeatures Game features to extract the feature from. + * @param type Type of the feature to extract. + * + * @return the feature from the game, or None is the game as no such feature. + */ + pybind11::object extract_feature(MOBase::IGameFeatures const& gameFeatures, + pybind11::object type); + + /** + * @brief Unregister the game feature corresponding to the given Python type. + * + * @param gameFeatures Game features to unregister the feature from. + * @param type Type of the feature to unregister. + * + * @return the feature from the game, or None is the game as no such feature. + */ + int unregister_feature(MOBase::IGameFeatures& gameFeatures, pybind11::object type); + +} // namespace mo2::python + +#endif // PYTHON_WRAPPERS_WRAPPERS_H diff --git a/libs/plugin_python/src/plugin_python_en.ts b/libs/plugin_python/src/plugin_python_en.ts new file mode 100644 index 0000000..9ef7836 --- /dev/null +++ b/libs/plugin_python/src/plugin_python_en.ts @@ -0,0 +1,83 @@ +<?xml version="1.0" encoding="utf-8"?> +<!DOCTYPE TS> +<TS version="2.1" language="en_US"> +<context> + <name>ProxyPython</name> + <message> + <location filename="proxy/proxypython.cpp" line="88"/> + <source>Python Initialization failed</source> + <translation type="unfinished"></translation> + </message> + <message> + <location filename="proxy/proxypython.cpp" line="89"/> + <source>On a previous start the Python Plugin failed to initialize. +Do you want to try initializing python again (at the risk of another crash)? + Suggestion: Select "no", and click the warning sign for further help.Afterwards you have to re-enable the python plugin.</source> + <translation type="unfinished"></translation> + </message> + <message> + <location filename="proxy/proxypython.cpp" line="162"/> + <source>Python Proxy</source> + <translation type="unfinished"></translation> + </message> + <message> + <location filename="proxy/proxypython.cpp" line="172"/> + <source>Proxy Plugin to allow plugins written in python to be loaded</source> + <translation type="unfinished"></translation> + </message> + <message> + <location filename="proxy/proxypython.cpp" line="244"/> + <source>ModOrganizer path contains a semicolon</source> + <translation type="unfinished"></translation> + </message> + <message> + <location filename="proxy/proxypython.cpp" line="246"/> + <source>Python DLL not found</source> + <translation type="unfinished"></translation> + </message> + <message> + <location filename="proxy/proxypython.cpp" line="248"/> + <source>Invalid Python DLL</source> + <translation type="unfinished"></translation> + </message> + <message> + <location filename="proxy/proxypython.cpp" line="250"/> + <source>Initializing Python failed</source> + <translation type="unfinished"></translation> + </message> + <message> + <location filename="proxy/proxypython.cpp" line="252"/> + <location filename="proxy/proxypython.cpp" line="281"/> + <source>invalid problem key %1</source> + <translation type="unfinished"></translation> + </message> + <message> + <location filename="proxy/proxypython.cpp" line="260"/> + <source>The path to Mod Organizer (%1) contains a semicolon.<br>While this is legal on NTFS drives, many applications do not handle it correctly.<br>Unfortunately MO depends on libraries that seem to fall into that group.<br>As a result the python plugin cannot be loaded, and the only solution we can offer is to remove the semicolon or move MO to a path without a semicolon.</source> + <translation type="unfinished"></translation> + </message> + <message> + <location filename="proxy/proxypython.cpp" line="270"/> + <source>The Python plugin DLL was not found, maybe your antivirus deleted it. Re-installing MO2 might fix the problem.</source> + <translation type="unfinished"></translation> + </message> + <message> + <location filename="proxy/proxypython.cpp" line="273"/> + <source>The Python plugin DLL is invalid, maybe your antivirus is blocking it. Re-installing MO2 and adding exclusions for it to your AV might fix the problem.</source> + <translation type="unfinished"></translation> + </message> + <message> + <location filename="proxy/proxypython.cpp" line="278"/> + <source>The initialization of the Python plugin DLL failed, unfortunately without any details.</source> + <translation type="unfinished"></translation> + </message> +</context> +<context> + <name>QObject</name> + <message> + <location filename="runner/error.h" line="76"/> + <source>An unknown exception was thrown in python code.</source> + <translation type="unfinished"></translation> + </message> +</context> +</TS> diff --git a/libs/plugin_python/src/proxy/CMakeLists.txt b/libs/plugin_python/src/proxy/CMakeLists.txt new file mode 100644 index 0000000..ed212a3 --- /dev/null +++ b/libs/plugin_python/src/proxy/CMakeLists.txt @@ -0,0 +1,118 @@ +cmake_minimum_required(VERSION 3.16) + +if(NOT TARGET mo2::uibase) + find_package(mo2-uibase CONFIG REQUIRED) +endif() + +set(PLUGIN_NAME "plugin_python") + +add_library(proxy SHARED proxypython.cpp proxypython.h) +mo2_configure_plugin(proxy + NO_SOURCES + WARNINGS 4 + EXTERNAL_WARNINGS 4 + TRANSLATIONS OFF + EXTRA_TRANSLATIONS + ${CMAKE_CURRENT_SOURCE_DIR}/../runner + ${CMAKE_CURRENT_SOURCE_DIR}/../mobase + ${CMAKE_CURRENT_SOURCE_DIR}/../pybind11-qt) +mo2_default_source_group() +target_link_libraries(proxy PRIVATE runner mo2::uibase) +if(NOT WIN32) + target_compile_definitions(proxy PRIVATE + MO2_PYTHON_PURELIB_DIR="${Python_PURELIB_DIR}" + MO2_PYTHON_PLATLIB_DIR="${Python_PLATLIB_DIR}") +endif() +set_target_properties(proxy PROPERTIES OUTPUT_NAME ${PLUGIN_NAME}) +mo2_install_plugin(proxy FOLDER) + +set(PLUGIN_PYTHON_DIR bin/plugins/${PLUGIN_NAME}) + +# install runner +if(WIN32) + target_link_options(proxy PRIVATE "/DELAYLOAD:runner.dll") + install(FILES $<TARGET_FILE:runner> DESTINATION ${PLUGIN_PYTHON_DIR}/dlls) +endif() + +# translations (custom location) +mo2_add_translations(proxy + TS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../${PLUGIN_NAME}_en.ts + SOURCES + ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/../runner + ${CMAKE_CURRENT_SOURCE_DIR}/../mobase + ${CMAKE_CURRENT_SOURCE_DIR}/../pybind11-qt) + +# install DLLs files needed +set(DLL_DIRS ${PLUGIN_PYTHON_DIR}/dlls) +if(WIN32) + file(GLOB dlls_to_install + ${Python_HOME}/dlls/libffi*.dll + ${Python_HOME}/dlls/sqlite*.dll + ${Python_HOME}/dlls/libssl*.dll + ${Python_HOME}/dlls/libcrypto*.dll + ${Python_HOME}/python${Python_VERSION_MAJOR}*.dll) + install(FILES ${dlls_to_install} DESTINATION ${DLL_DIRS}) +endif() + +# install Python .pyd files +set(PYLIB_DIR ${PLUGIN_PYTHON_DIR}/libs) +if(WIN32) + file(GLOB libs_to_install ${Python_DLL_DIR}/*.pyd) + install(FILES ${libs_to_install} DESTINATION ${PYLIB_DIR}) +endif() + +# generate + install standard library +if(WIN32) + set(pythoncore_zip "${CMAKE_CURRENT_BINARY_DIR}/pythoncore.zip") + add_custom_command( + TARGET proxy POST_BUILD + COMMAND ${Python_EXECUTABLE} + "${CMAKE_CURRENT_SOURCE_DIR}/build_pythoncore.py" + ${pythoncore_zip} + ) + install(FILES ${pythoncore_zip} DESTINATION ${PYLIB_DIR}) +endif() + +# install mobase +install(TARGETS mobase DESTINATION ${PYLIB_DIR}) + +# install PyQt6 +if(WIN32) + install( + DIRECTORY ${CMAKE_BINARY_DIR}/pylibs/PyQt${MO2_QT_VERSION_MAJOR} + DESTINATION ${PYLIB_DIR} + PATTERN "*.pyd" + PATTERN "*.pyi" + PATTERN "__pycache__" EXCLUDE + PATTERN "bindings" EXCLUDE + PATTERN "lupdate" EXCLUDE + PATTERN "Qt6" EXCLUDE + PATTERN "uic" EXCLUDE + ) +endif() + +if(NOT WIN32) + # Build-tree runtime layout expected by proxypython.cpp. + add_custom_command(TARGET proxy POST_BUILD + COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/src/src/plugins" + COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/src/src/plugins/dlls" + COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/src/src/plugins/libs" + COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/src/src/plugins/plugin_python/dlls" + COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_BINARY_DIR}/src/src/plugins/plugin_python/libs" + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "$<TARGET_FILE:proxy>" + "${CMAKE_BINARY_DIR}/src/src/plugins/$<TARGET_FILE_NAME:proxy>" + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "$<TARGET_FILE:runner>" + "${CMAKE_BINARY_DIR}/src/src/plugins/dlls/$<TARGET_FILE_NAME:runner>" + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "$<TARGET_FILE:runner>" + "${CMAKE_BINARY_DIR}/src/src/plugins/plugin_python/dlls/$<TARGET_FILE_NAME:runner>" + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "$<TARGET_FILE:mobase>" + "${CMAKE_BINARY_DIR}/src/src/plugins/libs/$<TARGET_FILE_NAME:mobase>" + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "$<TARGET_FILE:mobase>" + "${CMAKE_BINARY_DIR}/src/src/plugins/plugin_python/libs/$<TARGET_FILE_NAME:mobase>") +endif() diff --git a/libs/plugin_python/src/proxy/build_pythoncore.py b/libs/plugin_python/src/proxy/build_pythoncore.py new file mode 100644 index 0000000..01c42f9 --- /dev/null +++ b/libs/plugin_python/src/proxy/build_pythoncore.py @@ -0,0 +1,15 @@ +import sys +import sysconfig +import zipfile +from pathlib import Path + +_EXCLUDE_MODULES = ["ensurepip", "idlelib", "test", "tkinter", "turtle_demo", "venv"] + +libdir = Path(sysconfig.get_path("stdlib")) +assert libdir.exists() + +with zipfile.PyZipFile(sys.argv[1], optimize=2, mode="w") as fp: + fp.writepy(libdir) # pyright: ignore[reportArgumentType] + for path in libdir.iterdir(): + if path.is_dir() and path.name not in _EXCLUDE_MODULES: + fp.writepy(path) # pyright: ignore[reportArgumentType] diff --git a/libs/plugin_python/src/proxy/proxypython.cpp b/libs/plugin_python/src/proxy/proxypython.cpp new file mode 100644 index 0000000..af897db --- /dev/null +++ b/libs/plugin_python/src/proxy/proxypython.cpp @@ -0,0 +1,368 @@ +/* +Copyright (C) 2022 Sebastian Herbord & MO2 Team. All rights reserved. + +This file is part of python proxy plugin for MO + +python proxy plugin is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Python proxy plugin is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with python proxy plugin. If not, see <http://www.gnu.org/licenses/>. +*/ +#include "proxypython.h" + +#include <filesystem> +#include <cerrno> +#include <cstring> +#include <cstdlib> + +#ifdef _WIN32 +#include <Windows.h> +#else +#include <dlfcn.h> +#endif + +#include <QCoreApplication> +#include <QDirIterator> +#include <QMessageBox> +#include <QWidget> +#include <QtPlugin> + +#include <uibase/log.h> +#include <uibase/utility.h> +#include <uibase/versioninfo.h> + +namespace fs = std::filesystem; +using namespace MOBase; + +// retrieve the path to the folder containing the proxy DLL +fs::path getPluginFolder() +{ +#ifdef _WIN32 + wchar_t path[MAX_PATH]; + HMODULE hm = NULL; + + if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | + GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + (LPCWSTR)&getPluginFolder, &hm) == 0) { + return {}; + } + if (GetModuleFileName(hm, path, sizeof(path)) == 0) { + return {}; + } + + return fs::path(path).parent_path(); +#else + Dl_info info; + if (dladdr((void*)&getPluginFolder, &info) == 0 || info.dli_fname == nullptr) { + return {}; + } + return fs::path(info.dli_fname).parent_path(); +#endif +} + +ProxyPython::ProxyPython() + : m_MOInfo{nullptr}, m_RunnerLib{nullptr}, m_Runner{nullptr}, + m_LoadFailure(FailureType::NONE) +{ +} + +bool ProxyPython::init(IOrganizer* moInfo) +{ + m_MOInfo = moInfo; + + if (m_MOInfo && !m_MOInfo->isPluginEnabled(this)) { + return false; + } + + if (QCoreApplication::applicationDirPath().contains(';')) { + m_LoadFailure = FailureType::SEMICOLON; + return true; + } + + const auto pluginFolder = getPluginFolder(); +#ifdef _WIN32 + const auto pluginDataRoot = + fs::exists(pluginFolder / "plugin_python") ? (pluginFolder / "plugin_python") + : pluginFolder; +#else + // Linux portable installs can contain a legacy Windows "plugin_python" + // payload (pythoncore.zip/.pyd) that is incompatible with our bundled + // Linux runtime. Always use the plugin root on Linux. + const auto pluginDataRoot = pluginFolder; +#endif + + if (pluginFolder.empty()) { +#ifdef _WIN32 + DWORD error = ::GetLastError(); + m_LoadFailure = FailureType::DLL_NOT_FOUND; + log::error("failed to resolve Python proxy directory ({}): {}", error, + qUtf8Printable(windowsErrorString(::GetLastError()))); +#else + m_LoadFailure = FailureType::DLL_NOT_FOUND; + log::error("failed to resolve Python proxy directory: {}", + std::strerror(errno)); +#endif + return false; + } + + if (m_MOInfo && m_MOInfo->persistent(name(), "tryInit", false).toBool()) { + m_LoadFailure = FailureType::INITIALIZATION; + if (QMessageBox::question( + parentWidget(), tr("Python Initialization failed"), + tr("On a previous start the Python Plugin failed to initialize.\n" + "Do you want to try initializing python again (at the risk of " + "another crash)?\n " + "Suggestion: Select \"no\", and click the warning sign for further " + "help.Afterwards you have to re-enable the python plugin."), + QMessageBox::Yes | QMessageBox::No, + QMessageBox::No) == QMessageBox::No) { + // we force enabled here (note: this is a persistent settings since MO2 2.4 + // or something), plugin + // usually should not handle enabled/disabled themselves but this is a base + // plugin so... + m_MOInfo->setPersistent(name(), "enabled", false, true); + return true; + } + } + + if (m_MOInfo) { + m_MOInfo->setPersistent(name(), "tryInit", true); + } + + // load the pythonrunner library, this is done in multiple steps: + // + // 1. we set the dlls/ subfolder (from the plugin) as the DLL directory so Windows + // will look for DLLs in it, this is required to find the Python and libffi DLL, but + // also the runner DLL + // + const auto dllPaths = pluginDataRoot / "dlls"; +#ifdef _WIN32 + if (SetDllDirectoryW(dllPaths.c_str()) == 0) { + DWORD error = ::GetLastError(); + m_LoadFailure = FailureType::DLL_NOT_FOUND; + log::error("failed to add python DLL directory ({}): {}", error, + qUtf8Printable(windowsErrorString(::GetLastError()))); + return false; + } +#endif + + // 2. we create the Python runner, we do not need to use ::LinkLibrary and + // ::GetProcAddress because we use delayed load for the runner DLL (see the + // CMakeLists.txt) + // + m_Runner = mo2::python::createPythonRunner(); + + if (m_Runner) { + const auto libpath = pluginDataRoot / "libs"; +#ifdef _WIN32 + const std::vector<fs::path> paths{ + libpath / "pythoncore.zip", libpath, + std::filesystem::path{IOrganizer::getPluginDataPath().toStdWString()}}; + m_Runner->initialize(paths); +#else + // On Linux, rely on the unpacked stdlib from PYTHONHOME (AppRun sets + // MO2_PYTHON_DIR/PYTHONHOME). Do not prepend pythoncore.zip here: + // forcing zipimport can fail when zlib is unavailable in embedded mode. + std::vector<fs::path> paths{ + libpath, + std::filesystem::path{IOrganizer::getPluginDataPath().toStdWString()}}; + + // Allow portable/AppImage builds to ship Python packages next to the app. + // MO2_PYTHON_DIR (set by AppRun) points to the writable python/ dir + // next to the AppImage; fall back to <exe_dir>/python. + fs::path pythonDir; + const char* envPy = std::getenv("MO2_PYTHON_DIR"); + if (envPy && envPy[0] != '\0') { + pythonDir = fs::path(envPy); + } else { + pythonDir = fs::path(QCoreApplication::applicationDirPath().toStdWString()) / "python"; + } + paths.emplace_back(pythonDir / "site-packages"); + paths.emplace_back(pythonDir); + +#ifdef MO2_PYTHON_PURELIB_DIR + if (std::strlen(MO2_PYTHON_PURELIB_DIR) > 0) { + paths.emplace_back(MO2_PYTHON_PURELIB_DIR); + } +#endif +#ifdef MO2_PYTHON_PLATLIB_DIR + if (std::strlen(MO2_PYTHON_PLATLIB_DIR) > 0 && + std::strcmp(MO2_PYTHON_PLATLIB_DIR, MO2_PYTHON_PURELIB_DIR) != 0) { + paths.emplace_back(MO2_PYTHON_PLATLIB_DIR); + } +#endif + m_Runner->initialize(paths); +#endif + } + + if (m_MOInfo) { + m_MOInfo->setPersistent(name(), "tryInit", false); + } + + // reset DLL directory +#ifdef _WIN32 + SetDllDirectoryW(NULL); +#endif + + if (!m_Runner || !m_Runner->isInitialized()) { + m_LoadFailure = FailureType::INITIALIZATION; + } + else { + m_Runner->addDllSearchPath(pluginDataRoot / "dlls"); + } + + return true; +} + +QString ProxyPython::name() const +{ + return "Python Proxy"; +} + +QString ProxyPython::localizedName() const +{ + return tr("Python Proxy"); +} + +QString ProxyPython::author() const +{ + return "AnyOldName3, Holt59, Silarn, Tannin"; +} + +QString ProxyPython::description() const +{ + return tr("Proxy Plugin to allow plugins written in python to be loaded"); +} + +VersionInfo ProxyPython::version() const +{ + return VersionInfo(3, 0, 0, VersionInfo::RELEASE_FINAL); +} + +QList<PluginSetting> ProxyPython::settings() const +{ + return {}; +} + +QStringList ProxyPython::pluginList(const QDir& pluginPath) const +{ + QDir dir(pluginPath); + dir.setFilter(dir.filter() | QDir::NoDotAndDotDot); + QDirIterator iter(dir); + + // Note: We put python script (.py) and directory names, not the __init__.py + // files in those since it is easier for the runner to import them. + QStringList result; + while (iter.hasNext()) { + QString name = iter.next(); + QFileInfo info = iter.fileInfo(); + const QString baseName = info.fileName(); + + if (info.isFile() && name.endsWith(".py")) { + // Compatibility shims and disabled plugins staged on Linux; they are not + // loaded as MO2 plugins. + if (baseName == "winreg.py" || baseName == "lzokay.py" || + baseName == "FNISPatches.py" || baseName == "FNISTool.py" || + baseName == "FNISToolReset.py") { + continue; + } + result.append(name); + } + else if (info.isDir() && QDir(info.absoluteFilePath()).exists("__init__.py")) { + result.append(name); + } + } + + return result; +} + +QList<QObject*> ProxyPython::load(const QString& identifier) +{ + if (!m_Runner) { + return {}; + } + return m_Runner->load(identifier); +} + +void ProxyPython::unload(const QString& identifier) +{ + if (m_Runner) { + return m_Runner->unload(identifier); + } +} + +std::vector<unsigned int> ProxyPython::activeProblems() const +{ + auto failure = m_LoadFailure; + + // don't know how this could happen but wth + if (m_Runner && !m_Runner->isInitialized()) { + failure = FailureType::INITIALIZATION; + } + + if (failure != FailureType::NONE) { + return {static_cast<std::underlying_type_t<FailureType>>(failure)}; + } + + return {}; +} + +QString ProxyPython::shortDescription(unsigned int key) const +{ + switch (static_cast<FailureType>(key)) { + case FailureType::SEMICOLON: + return tr("ModOrganizer path contains a semicolon"); + case FailureType::DLL_NOT_FOUND: + return tr("Python DLL not found"); + case FailureType::INVALID_DLL: + return tr("Invalid Python DLL"); + case FailureType::INITIALIZATION: + return tr("Initializing Python failed"); + default: + return tr("invalid problem key %1").arg(key); + } +} + +QString ProxyPython::fullDescription(unsigned int key) const +{ + switch (static_cast<FailureType>(key)) { + case FailureType::SEMICOLON: + return tr("The path to Mod Organizer (%1) contains a semicolon.<br>" + "While this is legal on NTFS drives, many applications do not " + "handle it correctly.<br>" + "Unfortunately MO depends on libraries that seem to fall into that " + "group.<br>" + "As a result the python plugin cannot be loaded, and the only " + "solution we can offer is to remove the semicolon or move MO to a " + "path without a semicolon.") + .arg(QCoreApplication::applicationDirPath()); + case FailureType::DLL_NOT_FOUND: + return tr("The Python plugin DLL was not found, maybe your antivirus deleted " + "it. Re-installing MO2 might fix the problem."); + case FailureType::INVALID_DLL: + return tr( + "The Python plugin DLL is invalid, maybe your antivirus is blocking it. " + "Re-installing MO2 and adding exclusions for it to your AV might fix the " + "problem."); + case FailureType::INITIALIZATION: + return tr("The initialization of the Python plugin DLL failed, unfortunately " + "without any details."); + default: + return tr("invalid problem key %1").arg(key); + } +} + +bool ProxyPython::hasGuidedFix(unsigned int) const +{ + return false; +} + +void ProxyPython::startGuidedFix(unsigned int) const {} diff --git a/libs/plugin_python/src/proxy/proxypython.h b/libs/plugin_python/src/proxy/proxypython.h new file mode 100644 index 0000000..e824e2a --- /dev/null +++ b/libs/plugin_python/src/proxy/proxypython.h @@ -0,0 +1,80 @@ +/* +Copyright (C) 2022 Sebastian Herbord & MO2 Team. All rights reserved. + +This file is part of python proxy plugin for MO + +python proxy plugin is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +Python proxy plugin is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with python proxy plugin. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef PROXYPYTHON_H +#define PROXYPYTHON_H + +#include <map> +#include <memory> + +#include <uibase/iplugindiagnose.h> +#include <uibase/ipluginproxy.h> + +#include <pythonrunner.h> + +class ProxyPython : public QObject, + public MOBase::IPluginProxy, + public MOBase::IPluginDiagnose { + Q_OBJECT + Q_INTERFACES(MOBase::IPlugin MOBase::IPluginProxy MOBase::IPluginDiagnose) + Q_PLUGIN_METADATA(IID "org.mo2.ProxyPython") + +public: + ProxyPython(); + + virtual bool init(MOBase::IOrganizer* moInfo); + virtual QString name() const override; + virtual QString localizedName() const override; + virtual QString author() const override; + virtual QString description() const override; + virtual MOBase::VersionInfo version() const override; + virtual QList<MOBase::PluginSetting> settings() const override; + + QStringList pluginList(const QDir& pluginPath) const override; + QList<QObject*> load(const QString& identifier) override; + void unload(const QString& identifier) override; + +public: // IPluginDiagnose + virtual std::vector<unsigned int> activeProblems() const override; + virtual QString shortDescription(unsigned int key) const override; + virtual QString fullDescription(unsigned int key) const override; + virtual bool hasGuidedFix(unsigned int key) const override; + virtual void startGuidedFix(unsigned int key) const override; + +private: + MOBase::IOrganizer* m_MOInfo; +#ifdef _WIN32 + HMODULE m_RunnerLib; +#else + void* m_RunnerLib; +#endif + std::unique_ptr<mo2::python::IPythonRunner> m_Runner; + + enum class FailureType : unsigned int { + NONE = 0, + SEMICOLON = 1, + DLL_NOT_FOUND = 2, + INVALID_DLL = 3, + INITIALIZATION = 4 + }; + + FailureType m_LoadFailure; +}; + +#endif // PROXYPYTHON_H diff --git a/libs/plugin_python/src/pybind11-qt/CMakeLists.txt b/libs/plugin_python/src/pybind11-qt/CMakeLists.txt new file mode 100644 index 0000000..cb742ad --- /dev/null +++ b/libs/plugin_python/src/pybind11-qt/CMakeLists.txt @@ -0,0 +1,60 @@ +cmake_minimum_required(VERSION 3.16) + +find_package(Qt6 REQUIRED COMPONENTS Core Widgets) + +mo2_find_python_executable(PYTHON_EXE) + +add_library(pybind11-qt STATIC) +mo2_configure_target(pybind11-qt + NO_SOURCES + WARNINGS 4 + EXTERNAL_WARNINGS 4 + AUTOMOC OFF + TRANSLATIONS OFF +) +mo2_default_source_group() +target_sources(pybind11-qt + PRIVATE + ./include/pybind11_qt/pybind11_qt_basic.h + ./include/pybind11_qt/pybind11_qt_containers.h + ./include/pybind11_qt/pybind11_qt_enums.h + ./include/pybind11_qt/pybind11_qt_holder.h + ./include/pybind11_qt/pybind11_qt_objects.h + ./include/pybind11_qt/pybind11_qt_qflags.h + ./include/pybind11_qt/pybind11_qt.h + + pybind11_qt_basic.cpp + pybind11_qt_sip.cpp + pybind11_qt_utils.cpp + +) +mo2_target_sources(pybind11-qt + FOLDER src/details + PRIVATE + ./include/pybind11_qt/details/pybind11_qt_enum.h + ./include/pybind11_qt/details/pybind11_qt_qlist.h + ./include/pybind11_qt/details/pybind11_qt_qmap.h + ./include/pybind11_qt/details/pybind11_qt_sip.h + ./include/pybind11_qt/details/pybind11_qt_utils.h +) +target_link_libraries(pybind11-qt PUBLIC pybind11::pybind11 PRIVATE Qt6::Core Qt6::Widgets) +target_include_directories(pybind11-qt PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) + +# this is kind of broken but it only works with this... +target_compile_definitions(pybind11-qt PUBLIC QT_NO_KEYWORDS) + +# we need sip.h for pybind11-qt +add_custom_target(PyQt6-siph DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/sip.h") +set_target_properties(PyQt6-siph PROPERTIES FOLDER autogen) +add_custom_command( + OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/sip.h" + COMMAND ${Python_EXECUTABLE} -m sipbuild.tools.module + --sip-h + --target-dir ${CMAKE_CURRENT_BINARY_DIR} + PyQt${MO2_QT_VERSION_MAJOR}.sip +) +add_dependencies(pybind11-qt PyQt6-siph) + +target_include_directories(pybind11-qt PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) + +add_library(pybind11::qt ALIAS pybind11-qt) diff --git a/libs/plugin_python/src/pybind11-qt/README.md b/libs/plugin_python/src/pybind11-qt/README.md new file mode 100644 index 0000000..784f9a8 --- /dev/null +++ b/libs/plugin_python/src/pybind11-qt/README.md @@ -0,0 +1,64 @@ +# pybind11-qt + +This library contains code to interface pybind11 with Qt and PyQt. + +## Type casters + +The main part of this library is a set of (templated) type casters for Qt types that +can be used by simply importing `pybind11_qt/pybind11_qt.h`. +This provides type casters for: + +- Standard Qt types, such as `QString` or `QVariant`. + - `QString` is equivalent to Python `str` (unicode or not). + - `QVariant` is not exposed but the object is directly converted, similarly to + `std::variant` default type caster. +- Qt containers (`QList`, `QSet`, `QMap`, `QStringList`). + - The `QList` type-caster is more flexible than the standard container type-casters + from pybind11 as it accepts any iterable. +- `QFlags` - Delegates the cast to the underlying type, basically. +- Qt enumerations: a lot of enumerations are provided in + [`pybind11_qt_enums.h`](include/pybind11_qt/pybind11_qt_enums.h) and new ones can be + easily added using the `PYQT_ENUM` macro (inside the header file). +- Qt objects: very few are provided in + [`pybind11_qt_objects.h`](include/pybind11_qt/pybind11_qt_objects.h) and new ones can + be added using the `PYQT_OBJECT` macro (inside the header file). + - Copy-constructible Qt objects are copied when passing from C++ to Python or + vice-versa. + - Non copy-constructible Qt objects, e.g., `QObject` or `QWidget` should always be + exposed as pointer, and their ownership is transferred to C++ when coming from + Python. + +## Qt holder + +The library also provides a `pybind11::qt::qobject_holder` holder for pybind11 that +transfer ownerships of the Python object to the underlying `QObject`. + +This holder is useful when exposing classes inheriting `QObject` (or a child class of +`QObject`) that can be extended to Python. + +The library also provides two `set_qt_owner` functions that can be used to transfer +ownership manually. + +## Qt delegates + +The library provides a `add_qt_delegate` function that can be used to delegate Python +call to Qt functions to C++: + +```cpp +py::class_< + // the C++ class extending QObject to expose + ISaveGameInfoWidget, + + // the trampoline class + PySaveGameInfoWidget, + + // the Qt holder to keep the Python object alive alongside the C++ one + py::qt::qobject_holder<ISaveGameInfoWidget> + +> iSaveGameInfoWidget(m, "ISaveGameInfoWidget"); + +// allow to access most of the class attributes through Python via an overload of +// __getattr__ and add a _widget() method to access the widget itself if needed +// +py::qt::add_qt_delegate<QWidget>(iSaveGameInfoWidget, "_widget"); +``` diff --git a/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/details/pybind11_qt_enum.h b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/details/pybind11_qt_enum.h new file mode 100644 index 0000000..45afd0c --- /dev/null +++ b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/details/pybind11_qt_enum.h @@ -0,0 +1,59 @@ +#ifndef PYTHON_PYBIND11_QT_DETAILS_ENUM_HPP +#define PYTHON_PYBIND11_QT_DETAILS_ENUM_HPP + +#include <type_traits> + +#include <pybind11/pybind11.h> + +#include "pybind11_qt_utils.h" + +namespace pybind11::detail::qt { + + // EnumData, with static members (const char[]) + // - package: name of the Python package containing the enum (e.g., + // PyQt6.QtCore) + // - name: full path to the enum, e.g. Qt.QGlobalColor + // + template <typename T> + struct EnumData; + + // template class for most Qt types that have Python equivalent (QWidget, + // etc.) + // + template <class Enum> + struct qt_enum_caster { + + public: + PYBIND11_TYPE_CASTER(Enum, EnumData<Enum>::package + const_name(".") + + EnumData<Enum>::name); + + bool load(pybind11::handle src, bool) + { + if (PyLong_Check(src.ptr())) { + value = static_cast<Enum>(PyLong_AsLong(src.ptr())); + return true; + } + + auto pyenum = + get_attr_rec(EnumData<Enum>::package.text, EnumData<Enum>::name.text); + + if (isinstance(src, pyenum)) { + value = static_cast<Enum>(src.attr("value").cast<int>()); + return true; + } + + return false; + } + + static pybind11::handle cast(Enum src, + pybind11::return_value_policy /* policy */, + pybind11::handle /* parent */) + { + auto pyenum = + get_attr_rec(EnumData<Enum>::package.text, EnumData<Enum>::name.text); + return pyenum(static_cast<int>(src)); + } + }; +} // namespace pybind11::detail::qt + +#endif diff --git a/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/details/pybind11_qt_qlist.h b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/details/pybind11_qt_qlist.h new file mode 100644 index 0000000..e640725 --- /dev/null +++ b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/details/pybind11_qt_qlist.h @@ -0,0 +1,52 @@ +#ifndef PYTHON_PYBIND11_QT_DETAILS_QLIST_HPP +#define PYTHON_PYBIND11_QT_DETAILS_QLIST_HPP + +#include <pybind11/pybind11.h> +#include <pybind11/stl.h> + +namespace pybind11::detail::qt { + + // helper class for QList to construct from any proper iterable + // + template <typename Type, typename Value> + struct qlist_caster { + using value_conv = make_caster<Value>; + + bool load(handle src, bool convert) + { + if (!isinstance<iterable>(src) || isinstance<bytes>(src) || + isinstance<str>(src)) { + return false; + } + auto s = reinterpret_borrow<iterable>(src); + value.clear(); + + if (isinstance<sequence>(src)) { + value.reserve(s.cast<sequence>().size()); + } + for (auto it : s) { + value_conv conv; + if (!conv.load(it, convert)) { + return false; + } + value.push_back(cast_op<Value&&>(std::move(conv))); + } + return true; + } + + template <typename T> + static handle cast(T&& src, return_value_policy policy, handle parent) + { + return list_caster<QList<Value>, Value>{}.cast(std::forward<T>(src), policy, + parent); + } + + // we type these as "Sequence" even if these can be constructed from Iterable, + // otherwise the return type will be typed as "Iterable" which is problematic + PYBIND11_TYPE_CASTER(Type, const_name("Sequence[") + value_conv::name + + const_name("]")); + }; + +} // namespace pybind11::detail::qt + +#endif diff --git a/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/details/pybind11_qt_qmap.h b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/details/pybind11_qt_qmap.h new file mode 100644 index 0000000..ea743b7 --- /dev/null +++ b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/details/pybind11_qt_qmap.h @@ -0,0 +1,70 @@ +#ifndef PYTHON_PYBIND11_QT_DETAILS_QMAP_HPP +#define PYTHON_PYBIND11_QT_DETAILS_QMAP_HPP + +#include <pybind11/pybind11.h> + +namespace pybind11::detail::qt { + + // helper class for QMap because QMap do not follow the standard std:: maps + // interface, for other containers, the pybind11 built-in xxx_caster works + // + // this code is basically a copy/paste from the pybind11 stl stuff with + // minor modifications + // + template <typename Type, typename Key, typename Value> + struct qmap_caster { + using key_conv = make_caster<Key>; + using value_conv = make_caster<Value>; + + bool load(handle src, bool convert) + { + if (!isinstance<dict>(src)) { + return false; + } + auto d = reinterpret_borrow<dict>(src); + value.clear(); + for (auto it : d) { + key_conv kconv; + value_conv vconv; + if (!kconv.load(it.first.ptr(), convert) || + !vconv.load(it.second.ptr(), convert)) { + return false; + } + value[cast_op<Key&&>(std::move(kconv))] = + cast_op<Value&&>(std::move(vconv)); + } + return true; + } + + template <typename T> + static handle cast(T&& src, return_value_policy policy, handle parent) + { + dict d; + return_value_policy policy_key = policy; + return_value_policy policy_value = policy; + if (!std::is_lvalue_reference<T>::value) { + policy_key = return_value_policy_override<Key>::policy(policy_key); + policy_value = + return_value_policy_override<Value>::policy(policy_value); + } + for (auto it = src.begin(); it != src.end(); ++it) { + auto key = reinterpret_steal<object>( + key_conv::cast(forward_like<T>(it.key()), policy_key, parent)); + auto value = reinterpret_steal<object>(value_conv::cast( + forward_like<T>(it.value()), policy_value, parent)); + if (!key || !value) { + return handle(); + } + d[key] = value; + } + return d.release(); + } + + PYBIND11_TYPE_CASTER(Type, const_name("Dict[") + key_conv::name + + const_name(", ") + value_conv::name + + const_name("]")); + }; + +} // namespace pybind11::detail::qt + +#endif diff --git a/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/details/pybind11_qt_sip.h b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/details/pybind11_qt_sip.h new file mode 100644 index 0000000..556a980 --- /dev/null +++ b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/details/pybind11_qt_sip.h @@ -0,0 +1,221 @@ +#ifndef PYTHON_PYBIND11_QT_DETAILS_SIP_HPP +#define PYTHON_PYBIND11_QT_DETAILS_SIP_HPP + +#include <type_traits> + +#include <pybind11/pybind11.h> + +#include <iostream> +#include <pybind11/iostream.h> + +#include "../pybind11_qt_holder.h" + +struct _sipTypeDef; +typedef struct _sipTypeDef sipTypeDef; + +struct _sipSimpleWrapper; +typedef struct _sipSimpleWrapper sipSimpleWrapper; + +struct _sipWrapper; +typedef struct _sipWrapper sipWrapper; + +namespace pybind11::detail::qt { + + // helper functions to avoid bringing <sip.h> in this header + namespace sip { + + // extract the underlying data if present from the equivalent PyQt object + void* extract_data(PyObject*); + + const sipTypeDef* api_find_type(const char* type); + int api_can_convert_to_type(PyObject* pyObj, const sipTypeDef* td, int flags); + + void api_transfer_to(PyObject* self, PyObject* owner); + void api_transfer_back(PyObject* self); + PyObject* api_convert_from_type(void* cpp, const sipTypeDef* td, + PyObject* transferObj); + } // namespace sip + + template <typename T, class = void> + struct MetaData; + + template <typename T> + struct MetaData<T, std::enable_if_t<std::is_pointer_v<T>>> + : MetaData<std::remove_pointer_t<T>> {}; + + // template class for most Qt types that have Python equivalent (QWidget, + // etc.) + // + template <class QClass> + struct qt_type_caster { + + static constexpr bool is_pointer = std::is_pointer_v<QClass>; + using pointer = std::conditional_t<is_pointer, QClass, QClass*>; + + QClass value; + + public: + static constexpr auto name = MetaData<QClass>::python_name; + + operator pointer() + { + if constexpr (is_pointer) { + return value; + } + else { + return &value; + } + } + + // pybind11 requires operator T&() & and operator T&&() && but here we want to + // use SFINAE with is_pointer so we need to template the operator + // + // having a template <class U> operator U&&() does not work since it will not + // deduce the proper return type for QClass&& or QClass& so we have two separate + // overloads, and in each one, U is actually a reference type (lvalue or rvalue) + // + + template <class U, + std::enable_if_t<std::is_same_v<std::remove_reference_t<U>, QClass> && + std::is_lvalue_reference_v<U> && !is_pointer, + int> = 0> + operator U() + { + return value; + } + + template <class U, + std::enable_if_t<std::is_same_v<std::remove_reference_t<U>, QClass> && + std::is_rvalue_reference_v<U> && !is_pointer, + int> = 0> + operator U() && + { + return std::move(value); + } + + template <typename T> + using cast_op_type = + std::conditional_t<is_pointer, QClass, movable_cast_op_type<T>>; + + bool load(pybind11::handle src, bool) + { + // special check for none for pointer classes + if constexpr (is_pointer) { + if (src.is_none()) { + value = nullptr; + return true; + } + } + + const auto* type = sip::api_find_type(MetaData<QClass>::class_name); + if (type == nullptr) { + return false; + } + if (!sip::api_can_convert_to_type(src.ptr(), type, 0)) { + return false; + } + + // this would transfer responsibility for deconstructing the + // object to C++, but pybind11 assumes l-value converters (such + // as this) don't do that instead, this should be called within + // the wrappers for functions which return deletable pointers. + // + // sipAPI()->api_transfer_to(objPtr, Py_None); + // + void* const data = sip::extract_data(src.ptr()); + + if (data) { + if constexpr (is_pointer) { + value = reinterpret_cast<QClass>(data); + + // transfer ownership + sip::api_transfer_to(src.ptr(), Py_None); + + // tie the py::object to the C++ one + new pybind11::detail::qt::qobject_holder_impl(value); + } + else { + value = *reinterpret_cast<QClass*>(data); + } + return true; + } + else { + return false; + } + } + + template < + typename T, + std::enable_if_t<std::is_same<QClass, std::remove_cv_t<T>>::value, int> = 0> + static handle cast(T* src, return_value_policy policy, handle parent) + { + // note: when QClass is a pointer type, e.g. a QWidget*, T is a + // pointer to pointer, so we can defer to the standard cast() + + if (!src) { + return none().release(); + } + + if (!is_pointer && policy == return_value_policy::take_ownership) { + auto h = cast(std::move(*src), policy, parent); + delete src; + return h; + } + return cast(*src, policy, parent); + } + + static pybind11::handle cast(QClass src, pybind11::return_value_policy policy, + pybind11::handle /* parent */) + { + if constexpr (is_pointer) { + if (!src) { + return none().release(); + } + } + + const sipTypeDef* type = sip::api_find_type(MetaData<QClass>::class_name); + if (type == nullptr) { + return Py_None; + } + + PyObject* sipObj; + void* sipData; + + if constexpr (is_pointer) { + sipData = src; + } + else if (std::is_copy_assignable_v<QClass>) { + // we send to SIP a newly allocated object, and transfer the + // owernship to it + sipData = + new QClass(policy == ::pybind11::return_value_policy::take_ownership + ? std::move(src) + : src); + } + else { + sipData = &src; + } + + sipObj = sip::api_convert_from_type(sipData, type, 0); + + if (sipObj == nullptr) { + return Py_None; + } + + // ensure Python deletes the C++ component + if constexpr (!is_pointer) { + sip::api_transfer_back(sipObj); + } + else { + if (policy == return_value_policy::take_ownership) { + sip::api_transfer_back(sipObj); + } + } + + return sipObj; + } + }; + +} // namespace pybind11::detail::qt + +#endif diff --git a/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/details/pybind11_qt_utils.h b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/details/pybind11_qt_utils.h new file mode 100644 index 0000000..c877c15 --- /dev/null +++ b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/details/pybind11_qt_utils.h @@ -0,0 +1,47 @@ +#ifndef PYTHON_PYBIND11_QT_DETAILS_UTILS_HPP +#define PYTHON_PYBIND11_QT_DETAILS_UTILS_HPP + +#include <string> +#include <string_view> + +#include <pybind11/pybind11.h> + +namespace pybind11::detail::qt { + + /** + * @brief Convert a XXX::YYY compile time string to a XXX.YYY compile time + * string. Only one :: is allowed. + * + */ + template <size_t N> + constexpr descr<N - 2> qt_name_cpp2py(const char (&name)[N]) + { + descr<N - 2> res; + for (std::size_t i = 0, j = 0; i < N - 2; ++i) { + + res.text[i] = name[j]; + + if (res.text[i] == ':') { + res.text[i] = '.'; + j += 2; + } + else { + ++j; + } + } + return res; + } + + /** + * @brief Retrieve the class from the given package at the given path + * + * @param package Name of the module. + * @param path Path to the class/object in the module. + * + * @return the object at the given path in the given module + */ + pybind11::object get_attr_rec(std::string_view package, std::string_view path); + +} // namespace pybind11::detail::qt + +#endif diff --git a/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/pybind11_qt.h b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/pybind11_qt.h new file mode 100644 index 0000000..e803e9f --- /dev/null +++ b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/pybind11_qt.h @@ -0,0 +1,88 @@ +#ifndef PYTHON_PYBIND11_QT_HPP +#define PYTHON_PYBIND11_QT_HPP + +// this header defines many type casters for Qt types, including: +// - basic Qt types such as QString and QVariant - those do not have PyQt6 equivalent +// - QFlags<> class templates +// - containers such as QList<>, QSet<>, etc., the QList<> casters is more flexible than +// the std::vector<> or std::list<> ones as it accepts any iterable +// - many Qt enumeration types (see pybind11_qt_enums) +// - many Qt classes with PyQt6 equivalent +// - copyable type are copied between Python and C++ +// - non-copyable type (QObject, QWidget, QMainWindow) are always owned by the C++ +// side, even when constructed on the Python side, and owned their corresponding +// Python object, e.g., an instance of a class inheriting QWidget created on the +// Python side can be safely used in C++ since the Python object will be owned by +// the C++ QWidget object +// + +#include "pybind11_qt_basic.h" +#include "pybind11_qt_containers.h" +#include "pybind11_qt_enums.h" +#include "pybind11_qt_holder.h" +#include "pybind11_qt_objects.h" +#include "pybind11_qt_qflags.h" + +namespace pybind11::qt { + + /** + * @brief Tie the lifetime of the Python object to the lifetime of the given + * QObject. + * + * @param owner QObject that will own the python object. + * @param child Python object that the QObject will own. + */ + inline void set_qt_owner(QObject* owner, object child) + { + new detail::qt::qobject_holder_impl{owner, child}; + } + + /** + * @brief Tie the lifetime of the given object to the lifetime of the corresponding + * Python object. + * + * This object must have been created from Python and must inherit QObject. + * + * @param object Object to tie. + */ + template <typename Class> + void set_qt_owner(Class* object) + { + static_assert(std::is_base_of_v<QObject, Class>); + new detail::qt::qobject_holder_impl{object}; + } + + /** + * @brief Add Qt "delegate" to the given class. + * + * This function defines two methods: __getattr__ and name, where name will + * simply return the PyQtX object as a QClass* object, while __getattr__ + * will delegate to the underlying QClass object when required. + * + * This allow access to Qt interface for object exposed using pybind11 + * (e.g., signals, methods from QObject or QWidget, etc.). + * + * @param pyclass Python class to define the methods on. + * @param name Name of the method to retrieve the underlying object. + * + * @tparam QClass Name of the Qt class, cannot be deduced. + * @tparam Class Class being wrapped, deduced. + * @tparam Args... Arguments of the class template parameters, deduced. + */ + template <class QClass, class Class, class... Args> + auto& add_qt_delegate(pybind11::class_<Class, Args...>& pyclass, const char* name) + { + return pyclass + .def(name, + [](Class* w) -> QClass* { + return w; + }) + .def( + "__getattr__", +[](Class* w, pybind11::str str) -> pybind11::object { + return pybind11::cast((QClass*)w).attr(str); + }); + } + +} // namespace pybind11::qt + +#endif diff --git a/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/pybind11_qt_basic.h b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/pybind11_qt_basic.h new file mode 100644 index 0000000..911de34 --- /dev/null +++ b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/pybind11_qt_basic.h @@ -0,0 +1,36 @@ +#ifndef PYTHON_PYBIND11_QT_BASIC_HPP +#define PYTHON_PYBIND11_QT_BASIC_HPP + +#include <QString> +#include <QVariant> + +#include <pybind11/pybind11.h> + +namespace pybind11::detail { + + // QString + // + template <> + struct type_caster<QString> { + PYBIND11_TYPE_CASTER(QString, const_name("str")); + + bool load(handle src, bool); + + static handle cast(QString src, return_value_policy policy, handle parent); + }; + + // QVariant - this needs to be defined BEFORE QVariantList + // + template <> + struct type_caster<QVariant> { + public: + PYBIND11_TYPE_CASTER(QVariant, const_name("MoVariant")); + + bool load(handle src, bool); + + static handle cast(QVariant var, return_value_policy policy, handle parent); + }; + +} // namespace pybind11::detail + +#endif diff --git a/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/pybind11_qt_containers.h b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/pybind11_qt_containers.h new file mode 100644 index 0000000..120b5e8 --- /dev/null +++ b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/pybind11_qt_containers.h @@ -0,0 +1,51 @@ +#ifndef PYTHON_PYBIND11_QT_CONTAINERS_HPP +#define PYTHON_PYBIND11_QT_CONTAINERS_HPP + +#include <QList> +#include <QMap> +#include <QSet> + +#include <pybind11/pybind11.h> +#include <pybind11/stl.h> + +// this needs to be included here to get proper QVariantList and QVariantMap +#include "details/pybind11_qt_qlist.h" +#include "details/pybind11_qt_qmap.h" +#include "pybind11_qt_basic.h" + +namespace pybind11::detail { + + // QList + // + template <class T> + struct type_caster<QList<T>> : qt::qlist_caster<QList<T>, T> {}; + + // QSet + // + template <class T> + struct type_caster<QSet<T>> : set_caster<QList<T>, T> {}; + + // QMap + // + template <class K, class V> + struct type_caster<QMap<K, V>> : qt::qmap_caster<QMap<K, V>, K, V> {}; + + // QStringList + // + template <> + struct type_caster<QStringList> : qt::qlist_caster<QStringList, QString> {}; + + // QVariantList + // + template <> + struct type_caster<QVariantList> : qt::qlist_caster<QVariantList, QVariant> {}; + + // QVariantMap + // + template <> + struct type_caster<QVariantMap> : qt::qmap_caster<QVariantMap, QString, QVariant> { + }; + +} // namespace pybind11::detail + +#endif diff --git a/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/pybind11_qt_enums.h b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/pybind11_qt_enums.h new file mode 100644 index 0000000..b58cc41 --- /dev/null +++ b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/pybind11_qt_enums.h @@ -0,0 +1,115 @@ +#ifndef PYTHON_PYBIND11_QT_ENUMS_HPP +#define PYTHON_PYBIND11_QT_ENUMS_HPP + +#include <QColor> +#include <QMessageBox> + +#include "details/pybind11_qt_enum.h" +#include "details/pybind11_qt_utils.h" + +#define PYQT_ENUM(QPackage, QEnum) \ + namespace pybind11::detail { \ + namespace qt { \ + template <> \ + struct EnumData<QEnum> { \ + constexpr static const auto package = \ + const_name("PyQt6.") + const_name(#QPackage); \ + constexpr static const auto name = qt_name_cpp2py(#QEnum); \ + }; \ + } \ + template <> \ + struct type_caster<QEnum> : qt::qt_enum_caster<QEnum> {}; \ + } + +PYQT_ENUM(QtCore, Qt::AlignmentFlag); +PYQT_ENUM(QtCore, Qt::AnchorPoint); +PYQT_ENUM(QtCore, Qt::ApplicationAttribute); +PYQT_ENUM(QtCore, Qt::ApplicationState); +PYQT_ENUM(QtCore, Qt::ArrowType); +PYQT_ENUM(QtCore, Qt::AspectRatioMode); +PYQT_ENUM(QtCore, Qt::Axis); +PYQT_ENUM(QtCore, Qt::BGMode); +PYQT_ENUM(QtCore, Qt::BrushStyle); +PYQT_ENUM(QtCore, Qt::CaseSensitivity); +PYQT_ENUM(QtCore, Qt::CheckState); +PYQT_ENUM(QtCore, Qt::ChecksumType); +PYQT_ENUM(QtCore, Qt::ClipOperation); +PYQT_ENUM(QtCore, Qt::ConnectionType); +PYQT_ENUM(QtCore, Qt::ContextMenuPolicy); +PYQT_ENUM(QtCore, Qt::CoordinateSystem); +PYQT_ENUM(QtCore, Qt::Corner); +PYQT_ENUM(QtCore, Qt::CursorMoveStyle); +PYQT_ENUM(QtCore, Qt::CursorShape); +PYQT_ENUM(QtCore, Qt::DateFormat); +PYQT_ENUM(QtCore, Qt::DayOfWeek); +PYQT_ENUM(QtCore, Qt::DockWidgetArea); +PYQT_ENUM(QtCore, Qt::DropAction); +PYQT_ENUM(QtCore, Qt::Edge); +PYQT_ENUM(QtCore, Qt::EnterKeyType); +PYQT_ENUM(QtCore, Qt::EventPriority); +PYQT_ENUM(QtCore, Qt::FillRule); +PYQT_ENUM(QtCore, Qt::FindChildOption); +PYQT_ENUM(QtCore, Qt::FocusPolicy); +PYQT_ENUM(QtCore, Qt::FocusReason); +PYQT_ENUM(QtCore, Qt::GestureFlag); +PYQT_ENUM(QtCore, Qt::GestureState); +PYQT_ENUM(QtCore, Qt::GestureType); +PYQT_ENUM(QtCore, Qt::GlobalColor); +PYQT_ENUM(QtCore, Qt::HitTestAccuracy); +PYQT_ENUM(QtCore, Qt::ImageConversionFlag); +PYQT_ENUM(QtCore, Qt::InputMethodHint); +PYQT_ENUM(QtCore, Qt::InputMethodQuery); +PYQT_ENUM(QtCore, Qt::ItemDataRole); +PYQT_ENUM(QtCore, Qt::ItemFlag); +PYQT_ENUM(QtCore, Qt::ItemSelectionMode); +PYQT_ENUM(QtCore, Qt::ItemSelectionOperation); +PYQT_ENUM(QtCore, Qt::Key); +PYQT_ENUM(QtCore, Qt::KeyboardModifier); +PYQT_ENUM(QtCore, Qt::LayoutDirection); +PYQT_ENUM(QtCore, Qt::MaskMode); +PYQT_ENUM(QtCore, Qt::MatchFlag); +PYQT_ENUM(QtCore, Qt::Modifier); +PYQT_ENUM(QtCore, Qt::MouseButton); +PYQT_ENUM(QtCore, Qt::MouseEventFlag); +PYQT_ENUM(QtCore, Qt::MouseEventSource); +PYQT_ENUM(QtCore, Qt::NativeGestureType); +PYQT_ENUM(QtCore, Qt::NavigationMode); +PYQT_ENUM(QtCore, Qt::Orientation); +PYQT_ENUM(QtCore, Qt::PenCapStyle); +PYQT_ENUM(QtCore, Qt::PenJoinStyle); +PYQT_ENUM(QtCore, Qt::PenStyle); +PYQT_ENUM(QtCore, Qt::ScreenOrientation); +PYQT_ENUM(QtCore, Qt::ScrollBarPolicy); +PYQT_ENUM(QtCore, Qt::ScrollPhase); +PYQT_ENUM(QtCore, Qt::ShortcutContext); +PYQT_ENUM(QtCore, Qt::SizeHint); +PYQT_ENUM(QtCore, Qt::SizeMode); +PYQT_ENUM(QtCore, Qt::SortOrder); +PYQT_ENUM(QtCore, Qt::TabFocusBehavior); +PYQT_ENUM(QtCore, Qt::TextElideMode); +PYQT_ENUM(QtCore, Qt::TextFlag); +PYQT_ENUM(QtCore, Qt::TextFormat); +PYQT_ENUM(QtCore, Qt::TextInteractionFlag); +PYQT_ENUM(QtCore, Qt::TileRule); +PYQT_ENUM(QtCore, Qt::TimeSpec); +PYQT_ENUM(QtCore, Qt::TimerType); +PYQT_ENUM(QtCore, Qt::ToolBarArea); +PYQT_ENUM(QtCore, Qt::ToolButtonStyle); +PYQT_ENUM(QtCore, Qt::TransformationMode); +PYQT_ENUM(QtCore, Qt::WhiteSpaceMode); +PYQT_ENUM(QtCore, Qt::WidgetAttribute); +PYQT_ENUM(QtCore, Qt::WindowFrameSection); +PYQT_ENUM(QtCore, Qt::WindowModality); +PYQT_ENUM(QtCore, Qt::WindowState); +PYQT_ENUM(QtCore, Qt::WindowType); + +PYQT_ENUM(QtWidgets, QMessageBox::ButtonRole); +PYQT_ENUM(QtWidgets, QMessageBox::DialogCode); +PYQT_ENUM(QtWidgets, QMessageBox::Icon); +PYQT_ENUM(QtWidgets, QMessageBox::PaintDeviceMetric); +PYQT_ENUM(QtWidgets, QMessageBox::RenderFlag); +PYQT_ENUM(QtWidgets, QMessageBox::StandardButton); + +#undef PYQT_ENUM + +#endif diff --git a/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/pybind11_qt_holder.h b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/pybind11_qt_holder.h new file mode 100644 index 0000000..39200d5 --- /dev/null +++ b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/pybind11_qt_holder.h @@ -0,0 +1,59 @@ +#ifndef PYTHON_PYBIND11_QT_HOLDER_HPP +#define PYTHON_PYBIND11_QT_HOLDER_HPP + +#include <QObject> + +#include <pybind11/pybind11.h> + +namespace pybind11::detail::qt { + + class qobject_holder_impl : public QObject { + object p_; + + public: + /** + * @brief Construct a new qobject holder linked to the given QObject and + * maintaining the given python object alive. + * + * @param p Parent of this holder. + * @param o Python object to keep alive. + */ + qobject_holder_impl(QObject* p, object o) : p_{o} { setParent(p); } + + template <class U> + qobject_holder_impl(U* p) + : qobject_holder_impl{p, reinterpret_borrow<object>(cast(p))} + { + } + + ~qobject_holder_impl() + { + gil_scoped_acquire s; + p_ = object(); + } + }; + +} // namespace pybind11::detail::qt + +namespace pybind11::qt { + + template <class Type> + class qobject_holder { + using type = Type; + + type* qobj_; + + public: + qobject_holder(type* qobj) : qobj_{qobj} + { + new detail::qt::qobject_holder_impl(qobj_); + } + + type* get() { return qobj_; } + }; + +} // namespace pybind11::qt + +PYBIND11_DECLARE_HOLDER_TYPE(T, ::pybind11::qt::qobject_holder<T>) + +#endif diff --git a/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/pybind11_qt_objects.h b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/pybind11_qt_objects.h new file mode 100644 index 0000000..e16b5ea --- /dev/null +++ b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/pybind11_qt_objects.h @@ -0,0 +1,62 @@ +#ifndef PYTHON_PYBIND11_QT_OBJECTS_HPP +#define PYTHON_PYBIND11_QT_OBJECTS_HPP + +#include <pybind11/pybind11.h> + +#include <QByteArray> +#include <QDateTime> +#include <QDir> +#include <QFileInfo> +#include <QIcon> +#include <QMainWindow> +#include <QPixmap> +#include <QSize> +#include <QUrl> +#include <QWidget> + +#include "details/pybind11_qt_sip.h" +#include "details/pybind11_qt_utils.h" + +#define PYQT_CLASS(QModule, QClass) \ + namespace pybind11::detail { \ + namespace qt { \ + template <> \ + struct MetaData<QClass> { \ + constexpr static const auto class_name = #QClass; \ + constexpr static const auto python_name = \ + const_name("PyQt6.") + const_name(#QModule) + const_name(".") + \ + const_name(#QClass); \ + }; \ + } \ + template <> \ + struct type_caster<QClass*> \ + : std::conditional_t<std::is_copy_constructible_v<QClass>, \ + type_caster_generic, qt::qt_type_caster<QClass*>> {}; \ + template <> \ + struct type_caster<QClass> \ + : std::conditional_t<std::is_copy_constructible_v<QClass>, \ + qt::qt_type_caster<QClass>, type_caster<QClass*>> {}; \ + } + +// add declarations below to create bindings - the first argument is simply +// the name of the PyQt6 package containing the class, and is only used for +// the python signature + +PYQT_CLASS(QtCore, QByteArray); +PYQT_CLASS(QtCore, QDateTime); +PYQT_CLASS(QtCore, QDir); +PYQT_CLASS(QtCore, QFileInfo); +PYQT_CLASS(QtCore, QObject); +PYQT_CLASS(QtCore, QSize); +PYQT_CLASS(QtCore, QUrl); + +PYQT_CLASS(QtGui, QColor); +PYQT_CLASS(QtGui, QIcon); +PYQT_CLASS(QtGui, QPixmap); + +PYQT_CLASS(QtWidgets, QMainWindow); +PYQT_CLASS(QtWidgets, QWidget); + +#undef METADATA + +#endif diff --git a/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/pybind11_qt_qflags.h b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/pybind11_qt_qflags.h new file mode 100644 index 0000000..f246ed3 --- /dev/null +++ b/libs/plugin_python/src/pybind11-qt/include/pybind11_qt/pybind11_qt_qflags.h @@ -0,0 +1,56 @@ +#ifndef PYTHON_PYBIND11_QT_QFLAGS_HPP +#define PYTHON_PYBIND11_QT_QFLAGS_HPP + +#include <QFlags> + +#include <pybind11/pybind11.h> + +namespace pybind11::detail { + + // QFlags + // + template <class T> + struct type_caster<QFlags<T>> { + PYBIND11_TYPE_CASTER(QFlags<T>, const_name("QFlags[") + make_caster<T>::name + + const_name("]")); + + /** + * Conversion part 1 (Python->C++): convert a PyObject into a QString + * instance or return false upon failure. The second argument + * indicates whether implicit conversions should be applied. + */ + bool load(handle src, bool) + { + PyObject* tmp = PyNumber_Long(src.ptr()); + + if (!tmp) { + return false; + } + + // we do an intermediate extraction to T but this actually + // can contains multiple values + T flag_value = static_cast<T>(PyLong_AsLong(tmp)); + Py_DECREF(tmp); + + value = QFlags<T>(flag_value); + + return !PyErr_Occurred(); + } + + /** + * Conversion part 2 (C++ -> Python): convert an QString instance into + * a Python object. The second and third arguments are used to + * indicate the return value policy and parent object (for + * ``return_value_policy::reference_internal``) and are generally + * ignored by implicit casters. + */ + static handle cast(QFlags<T> const& src, return_value_policy /* policy */, + handle /* parent */) + { + return PyLong_FromLong(static_cast<int>(src)); + } + }; + +} // namespace pybind11::detail + +#endif diff --git a/libs/plugin_python/src/pybind11-qt/pybind11_qt_basic.cpp b/libs/plugin_python/src/pybind11-qt/pybind11_qt_basic.cpp new file mode 100644 index 0000000..d82ec2b --- /dev/null +++ b/libs/plugin_python/src/pybind11-qt/pybind11_qt_basic.cpp @@ -0,0 +1,156 @@ +#include "pybind11_qt/pybind11_qt_basic.h" + +#include <type_traits> + +#include <pybind11/stl/filesystem.h> + +#include "pybind11_qt/details/pybind11_qt_utils.h" + +// need to import containers to get QVariantList and QVariantMap +#include "pybind11_qt/pybind11_qt_containers.h" + +namespace pybind11::detail { + + template <class CharT> + QString qstring_from_stdstring(std::basic_string<CharT> const& s) + { + if constexpr (std::is_same_v<CharT, char>) { + return QString::fromStdString(s); + } + else if constexpr (std::is_same_v<CharT, wchar_t>) { + return QString::fromStdWString(s); + } + else if constexpr (std::is_same_v<CharT, char16_t>) { + return QString::fromStdU16String(s); + } + else if constexpr (std::is_same_v<CharT, char32_t>) { + return QString::fromStdU32String(s); + } + } + + /** + * Conversion part 1 (Python->C++): convert a PyObject into a QString + * instance or return false upon failure. The second argument + * indicates whether implicit conversions should be applied. + */ + bool type_caster<QString>::load(handle src, bool) + { + PyObject* objPtr = src.ptr(); + + if (PyBytes_Check(objPtr)) { + value = QString::fromUtf8(PyBytes_AsString(objPtr)); + return true; + } + else if (PyUnicode_Check(objPtr)) { + switch (PyUnicode_KIND(objPtr)) { + case PyUnicode_1BYTE_KIND: + value = QString::fromUtf8(PyUnicode_AsUTF8(objPtr)); + break; + case PyUnicode_2BYTE_KIND: + value = QString::fromUtf16( + reinterpret_cast<char16_t*>(PyUnicode_2BYTE_DATA(objPtr)), + PyUnicode_GET_LENGTH(objPtr)); + break; + case PyUnicode_4BYTE_KIND: + value = QString::fromUcs4( + reinterpret_cast<char32_t*>(PyUnicode_4BYTE_DATA(objPtr)), + PyUnicode_GET_LENGTH(objPtr)); + break; + default: + return false; + } + + return true; + } + else { + return false; + } + } + + /** + * Conversion part 2 (C++ -> Python): convert an QString instance into + * a Python object. The second and third arguments are used to + * indicate the return value policy and parent object (for + * ``return_value_policy::reference_internal``) and are generally + * ignored by implicit casters. + */ + handle type_caster<QString>::cast(QString src, return_value_policy /* policy */, + handle /* parent */) + { + return PyUnicode_DecodeUTF16(reinterpret_cast<const char*>(src.utf16()), + 2 * src.length(), nullptr, 0); + } + + bool type_caster<QVariant>::load(handle src, bool) + { + // test for string first otherwise PyList_Check also works + if (PyBytes_Check(src.ptr()) || PyUnicode_Check(src.ptr())) { + value = src.cast<QString>(); + return true; + } + else if (PySequence_Check(src.ptr())) { + // we could check if all the elements can be converted to QString + // and store a QStringList in the QVariant but I am not sure that is + // really useful. + value = src.cast<QVariantList>(); + return true; + } + else if (PyMapping_Check(src.ptr())) { + value = src.cast<QVariantMap>(); + return true; + } + else if (src.is(pybind11::none())) { + value = QVariant(); + return true; + } + else if (PyDict_Check(src.ptr())) { + value = src.cast<QVariantMap>(); + return true; + } + // PyBool will also return true for PyLong_Check but not the other way + // around, so the order here is relevant. + else if (PyBool_Check(src.ptr())) { + value = src.cast<bool>(); + return true; + } + else if (PyLong_Check(src.ptr())) { + // QVariant doesn't have long. It has int or long long. Given that + // on m/s, long is 32 bits for 32- and 64- bit code... + value = src.cast<int>(); + return true; + } + else { + return false; + } + } + + handle type_caster<QVariant>::cast(QVariant var, return_value_policy policy, + handle parent) + { + switch (var.typeId()) { + case QMetaType::UnknownType: + return Py_None; + case QMetaType::Int: + return PyLong_FromLong(var.toInt()); + case QMetaType::UInt: + return PyLong_FromUnsignedLong(var.toUInt()); + case QMetaType::Bool: + return PyBool_FromLong(var.toBool()); + case QMetaType::QString: + return type_caster<QString>::cast(var.toString(), policy, parent); + // We need to check for StringList here because these are not considered + // List since List is QList<QVariant> will StringList is QList<QString>: + case QMetaType::QStringList: + return type_caster<QStringList>::cast(var.toStringList(), policy, parent); + case QMetaType::QVariantList: + return type_caster<QVariantList>::cast(var.toList(), policy, parent); + case QMetaType::QVariantMap: + return type_caster<QVariantMap>::cast(var.toMap(), policy, parent); + default: { + PyErr_Format(PyExc_TypeError, "type unsupported: %d", var.userType()); + throw pybind11::error_already_set(); + } + } + } + +} // namespace pybind11::detail diff --git a/libs/plugin_python/src/pybind11-qt/pybind11_qt_sip.cpp b/libs/plugin_python/src/pybind11-qt/pybind11_qt_sip.cpp new file mode 100644 index 0000000..69ad4b8 --- /dev/null +++ b/libs/plugin_python/src/pybind11-qt/pybind11_qt_sip.cpp @@ -0,0 +1,105 @@ +#include "pybind11_qt/details/pybind11_qt_sip.h" + +#include <stdexcept> + +#include <QString> + +#include <sip.h> + +namespace py = pybind11; + +namespace pybind11::detail::qt { + + const sipAPIDef* sipAPI() + { + std::string exception; + static const sipAPIDef* sipApi = nullptr; + if (sipApi == nullptr) { + PyImport_ImportModule("PyQt6.sip"); + + { + auto errorObj = PyErr_Occurred(); + if (errorObj != NULL) { + PyObject *type, *value, *traceback; + PyErr_Fetch(&type, &value, &traceback); + PyErr_NormalizeException(&type, &value, &traceback); + if (traceback != NULL) { + py::handle h_type(type); + py::handle h_val(value); + py::handle h_tb(traceback); + py::object tb(py::module_::import("traceback")); + py::object fmt_exp(tb.attr("format_exception")); + py::object exp_list(fmt_exp(h_type, h_val, h_tb)); + py::object exp_str(py::str("\n").attr("join")(exp_list)); + exception = exp_str.cast<std::string>(); + } + PyErr_Restore(type, value, traceback); + throw std::runtime_error{"Failed to load SIP API: " + exception}; + } + } + + sipApi = (const sipAPIDef*)PyCapsule_Import("PyQt6.sip._C_API", 0); + if (sipApi == NULL) { + auto errorObj = PyErr_Occurred(); + if (errorObj != NULL) { + PyObject *type, *value, *traceback; + PyErr_Fetch(&type, &value, &traceback); + PyErr_NormalizeException(&type, &value, &traceback); + if (traceback != NULL) { + py::handle h_type(type); + py::handle h_val(value); + py::handle h_tb(traceback); + py::object tb(py::module_::import("traceback")); + py::object fmt_exp(tb.attr("format_exception")); + py::object exp_list(fmt_exp(h_type, h_val, h_tb)); + py::object exp_str(py::str("\n").attr("join")(exp_list)); + exception = exp_str.cast<std::string>(); + } + PyErr_Restore(type, value, traceback); + } + throw std::runtime_error{"Failed to load SIP API: " + exception}; + } + } + return sipApi; + } + + namespace sip { + const sipTypeDef* api_find_type(const char* type) + { + return sipAPI()->api_find_type(type); + } + + int api_can_convert_to_type(PyObject* pyObj, const sipTypeDef* td, int flags) + { + return sipAPI()->api_can_convert_to_type(pyObj, td, flags); + } + + void api_transfer_to(PyObject* self, PyObject* owner) + { + sipAPI()->api_transfer_to(self, owner); + } + + void api_transfer_back(PyObject* self) + { + sipAPI()->api_transfer_back(self); + } + + PyObject* api_convert_from_type(void* cpp, const sipTypeDef* td, PyObject*) + { + return sipAPI()->api_convert_from_type(cpp, td, 0); + } + + void* extract_data(PyObject* ptr) + { + if (PyObject_TypeCheck(ptr, sipAPI()->api_simplewrapper_type)) { + return reinterpret_cast<sipSimpleWrapper*>(ptr)->data; + } + else if (PyObject_TypeCheck(ptr, sipAPI()->api_wrapper_type)) { + return reinterpret_cast<sipWrapper*>(ptr)->super.data; + } + return nullptr; + } + + } // namespace sip + +} // namespace pybind11::detail::qt diff --git a/libs/plugin_python/src/pybind11-qt/pybind11_qt_utils.cpp b/libs/plugin_python/src/pybind11-qt/pybind11_qt_utils.cpp new file mode 100644 index 0000000..249bed5 --- /dev/null +++ b/libs/plugin_python/src/pybind11-qt/pybind11_qt_utils.cpp @@ -0,0 +1,11 @@ +#include "pybind11_qt/details/pybind11_qt_utils.h" + +namespace pybind11::detail::qt { + + pybind11::object get_attr_rec(std::string_view package, std::string_view path) + { + + return module_::import("operator") + .attr("attrgetter")(path.data())(module_::import(package.data())); + } +} // namespace pybind11::detail::qt diff --git a/libs/plugin_python/src/pybind11-utils/CMakeLists.txt b/libs/plugin_python/src/pybind11-utils/CMakeLists.txt new file mode 100644 index 0000000..77895b6 --- /dev/null +++ b/libs/plugin_python/src/pybind11-utils/CMakeLists.txt @@ -0,0 +1,23 @@ +cmake_minimum_required(VERSION 3.16) + +add_library(pybind11-utils STATIC + ./include/pybind11_utils/functional.h + ./include/pybind11_utils/generator.h + ./include/pybind11_utils/shared_cpp_owner.h + ./include/pybind11_utils/smart_variant_wrapper.h + ./include/pybind11_utils/smart_variant.h + + functional.cpp +) +mo2_configure_target(pybind11-utils + NO_SOURCES + WARNINGS 4 + EXTERNAL_WARNINGS 4 + AUTOMOC OFF + TRANSLATIONS OFF +) +mo2_default_source_group() +target_link_libraries(pybind11-utils PUBLIC pybind11::pybind11) +target_include_directories(pybind11-utils PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) + +add_library(pybind11::utils ALIAS pybind11-utils) diff --git a/libs/plugin_python/src/pybind11-utils/README.md b/libs/plugin_python/src/pybind11-utils/README.md new file mode 100644 index 0000000..46a9256 --- /dev/null +++ b/libs/plugin_python/src/pybind11-utils/README.md @@ -0,0 +1,50 @@ +# pybind11-utils + +This library contains some utility stuff for `pybind11` + +## smart_variant_wrapper.h + +Expose a function `mo2::python::wrap_arguments` and a template +`mo2::python::smart_variant` that can be used to expose more interesting types Python +than the C++ one, e.g., accept `os.PathLike` and `QFileInfo` when a simple `QString` is +expected. + +A toy example can be found in the test folder at +[`tests/python/test_argument_wrapper.cpp`](../../tests/python/test_argument_wrapper.cpp). + +More concrete examples can be found in +[`mobase/pybind11_all.h`](../mobase/pybind11_all.h) for `FileWrapper` and +`DirectoryWrapper`. + +## functional.h + +TODO: updated version of `<pybind11/functional.h>` that should check the signature +a bit more when creating `std::function` (similar to previous implementation). + +## shared_cpp_owner.h + +Expose a macro `MO2_PYBIND11_SHARED_CPP_HOLDER` that can be used to declare that +`std::shared_ptr<...>` must hold-on their associate Python object. + +```cpp +// use the macro on the type to be exposed (with a trampoline class) +MO2_PYBIND11_SHARED_CPP_HOLDER(ISaveGame) + +// use std::shared_ptr<> as the holder for the class +py::class_<ISaveGame, PySaveGame, std::shared_ptr<ISaveGame>>(...); +``` + +Using the `MO2_PYBIND11_SHARED_CPP_HOLDER` (must be present in all files manipulating +`ISaveGame` between C++ and Python) ensure that the Python instance remains alive +alongside the C++ one. + +The `MO2_PYBIND11_SHARED_CPP_HOLDER` declares a specialization of `type_caster<>` that +alters the `std::shared_ptr` by return a `std::shared_ptr<>` that owns the Python +object (via a `pybind11::object`) but does not release the C++ one - The C++ object is +owned by the Python one, so the relation is as follows: + +- The `std::shared_ptr<X>` manipulated in C++ maintains the `pybind11::object` alive + through a custom deleter but DOES NOT release the C++ object when the reference count + reaches 0. +- The Python object holds a standard `std::shared_ptr<X>` that will release the object + when the reference count reaches 0. diff --git a/libs/plugin_python/src/pybind11-utils/functional.cpp b/libs/plugin_python/src/pybind11-utils/functional.cpp new file mode 100644 index 0000000..4cc7b6c --- /dev/null +++ b/libs/plugin_python/src/pybind11-utils/functional.cpp @@ -0,0 +1,28 @@ +#include "pybind11_utils/functional.h" + +namespace py = pybind11; + +namespace mo2::python::detail { + + bool has_compatible_arity(py::function fn, std::size_t arity) + { + auto inspect = py::module_::import("inspect"); + auto arg_spec = inspect.attr("getfullargspec")(fn); + py::object args = arg_spec.attr("args"), varargs = arg_spec.attr("varargs"), + defaults = arg_spec.attr("defaults"); + + auto args_count = args.is(py::none()) ? 0 : py::len(args); + auto defaults_count = defaults.is(py::none()) ? 0 : py::len(defaults); + + if (inspect.attr("ismethod")(fn).cast<bool>() && py::hasattr(fn, "__self__")) { + --args_count; + } + + auto required_count = args_count - defaults_count; + + return required_count <= arity // cannot require more parameters than given, + && (args_count >= arity || + !varargs.is_none()); // must accept enough parameters. + } + +} // namespace mo2::python::detail diff --git a/libs/plugin_python/src/pybind11-utils/include/pybind11_utils/functional.h b/libs/plugin_python/src/pybind11-utils/include/pybind11_utils/functional.h new file mode 100644 index 0000000..8662d82 --- /dev/null +++ b/libs/plugin_python/src/pybind11-utils/include/pybind11_utils/functional.h @@ -0,0 +1,155 @@ +#ifndef PYTHON_PYBIND11_FUNCTIONAL_H +#define PYTHON_PYBIND11_FUNCTIONAL_H + +#include <pybind11/pybind11.h> + +namespace mo2::python::detail { + + // check if the given function is valid for a C++ function with the + // given arity + // + bool has_compatible_arity(pybind11::function handle, std::size_t arity); + +} // namespace mo2::python::detail + +namespace pybind11::detail { + + // custom type_caster for std::function<> + // + // most of this is from pybind11 except that we also check arity of the function to + // allow overloaded function based on arity of argument + // + template <typename Return, typename... Args> + struct type_caster<std::function<Return(Args...)>> { + using type = std::function<Return(Args...)>; + using retval_type = + conditional_t<std::is_same<Return, void>::value, void_type, Return>; + using function_type = Return (*)(Args...); + + public: + bool load(handle src, bool convert) + { + if (src.is_none()) { + // Defer accepting None to other overloads (if we aren't in convert + // mode): + if (!convert) { + return false; + } + return true; + } + + if (!isinstance<function>(src)) { + return false; + } + + auto func = reinterpret_borrow<function>(src); + + /* + When passing a C++ function as an argument to another C++ + function via Python, every function call would normally involve + a full C++ -> Python -> C++ roundtrip, which can be prohibitive. + Here, we try to at least detect the case where the function is + stateless (i.e. function pointer or lambda function without + captured variables), in which case the roundtrip can be avoided. + */ + if (auto cfunc = func.cpp_function()) { + auto* cfunc_self = PyCFunction_GET_SELF(cfunc.ptr()); + if (isinstance<capsule>(cfunc_self)) { + auto c = reinterpret_borrow<capsule>(cfunc_self); + auto* rec = (function_record*)c; + + while (rec != nullptr) { + if (rec->is_stateless && + same_type(typeid(function_type), + *reinterpret_cast<const std::type_info*>( + rec->data[1]))) { + struct capture { + function_type f; + }; + value = ((capture*)&rec->data)->f; + return true; + } + rec = rec->next; + } + } + // PYPY segfaults here when passing builtin function like sum. + // Raising an fail exception here works to prevent the segfault, but + // only on gcc. See PR #1413 for full details + } + + // !MO2! - check arity + + if (!mo2::python::detail::has_compatible_arity(func, sizeof...(Args))) { + return false; + } + + // !MO2! - everything below is copy/paste from pybind11 + + // ensure GIL is held during functor destruction + struct func_handle { + function f; +#if !(defined(_MSC_VER) && _MSC_VER == 1916 && defined(PYBIND11_CPP17)) + // This triggers a syntax error under very special conditions (very + // weird indeed). + explicit +#endif + func_handle(function&& f_) noexcept + : f(std::move(f_)) + { + } + func_handle(const func_handle& f_) { operator=(f_); } + func_handle& operator=(const func_handle& f_) + { + gil_scoped_acquire acq; + f = f_.f; + return *this; + } + ~func_handle() + { + gil_scoped_acquire acq; + function kill_f(std::move(f)); + } + }; + + // to emulate 'move initialization capture' in C++11 + struct func_wrapper { + func_handle hfunc; + explicit func_wrapper(func_handle&& hf) noexcept : hfunc(std::move(hf)) + { + } + Return operator()(Args... args) const + { + gil_scoped_acquire acq; + object retval(hfunc.f(std::forward<Args>(args)...)); + return retval.template cast<Return>(); + } + }; + + value = func_wrapper(func_handle(std::move(func))); + return true; + } + + template <typename Func> + static handle cast(Func&& f_, return_value_policy policy, handle /* parent */) + { + if (!f_) { + return none().inc_ref(); + } + + auto result = f_.template target<function_type>(); + if (result) { + return cpp_function(*result, policy).release(); + } + return cpp_function(std::forward<Func>(f_), policy).release(); + } + + PYBIND11_TYPE_CASTER(type, const_name("Callable[[") + + concat(make_caster<Args>::name...) + + const_name("], ") + + make_caster<retval_type>::name + + const_name("]")); + }; + +} // namespace pybind11::detail + +#endif diff --git a/libs/plugin_python/src/pybind11-utils/include/pybind11_utils/generator.h b/libs/plugin_python/src/pybind11-utils/include/pybind11_utils/generator.h new file mode 100644 index 0000000..cbf9d18 --- /dev/null +++ b/libs/plugin_python/src/pybind11-utils/include/pybind11_utils/generator.h @@ -0,0 +1,57 @@ +#ifndef PYTHON_PYBIND11_GENERATOR_H +#define PYTHON_PYBIND11_GENERATOR_H + +#include <generator> + +#include <pybind11/pybind11.h> + +namespace mo2::python { + + // the code here is mostly taken from pybind11 itself, and relies on some pybind11 + // internals so might be subject to change when upgrading pybind11 versions + + namespace detail { + template <typename T> + struct generator_state { + std::generator<T> g; + decltype(g.begin()) it; + + generator_state(std::generator<T> gen) : g(std::move(gen)), it(g.begin()) {} + }; + } // namespace detail + + // create a Python generator from a C++ generator + // + template <typename T, typename... Args> + auto make_generator(std::generator<T> g, Args&&... args) + { + using state = detail::generator_state<T>; + + namespace py = pybind11; + if (!py::detail::get_type_info(typeid(state), false)) { + py::class_<state>(py::handle(), "iterator", pybind11::module_local()) + .def("__iter__", + [](state& s) -> state& { + return s; + }) + .def( + "__next__", + [](state& s) -> T { + if (s.it != s.g.end()) { + T v = *s.it; + s.it++; + return v; + } + else { + throw py::stop_iteration(); + } + }, + std::forward<Args>(args)...); + } + + return py::cast(state{std::move(g)}); + } + +} // namespace mo2::python + +#endif diff --git a/libs/plugin_python/src/pybind11-utils/include/pybind11_utils/shared_cpp_owner.h b/libs/plugin_python/src/pybind11-utils/include/pybind11_utils/shared_cpp_owner.h new file mode 100644 index 0000000..02c2111 --- /dev/null +++ b/libs/plugin_python/src/pybind11-utils/include/pybind11_utils/shared_cpp_owner.h @@ -0,0 +1,90 @@ +#ifndef PYTHON_PYBIND11_SHARED_CPP_OWNER_H +#define PYTHON_PYBIND11_SHARED_CPP_OWNER_H + +#include <pybind11/pybind11.h> + +// pybind11 has some issues when a Python classes extend a C++ wrapper since the Python +// object is not kept alive alongside the returned object +// +// there is a pybind11 branch called "smart_holder" that tries to solve this in a very +// complicated way (with many other features) +// +// here, we simply use a custom type_caster<> for the classes we need - see the actual +// definition in mo2::python::detail below +// +// IMPORTANT: this only works for classes that are managed by shared_ptr on the C++ +// side, not Qt object (see pybind11-qt holder for that) +// + +namespace mo2::python::detail { + + template <class Type, class SharedType> + struct shared_cpp_owner_caster + : pybind11::detail::copyable_holder_caster<Type, SharedType> { + + // note that the actual holder type might be different in term of constness + using type = Type; + using holder_type = SharedType; + + using base = pybind11::detail::copyable_holder_caster<Type, SharedType>; + using base::holder; + using base::value; + + // in load, we use the default type_caster<> to extract the shared pointer, then + // we replace it by a custom one + // + // the custom shared_ptr<> holds the py::object BUT does not really manage the + // C++ object because it will ref-count but not delete it + // + // this should work because here it's how it works: + // - the Python object holds a standard shared_ptr<> for the C++ object -> the + // C++ object remains alive as long as the Python one remains alive + // - the C++ object holds a shared_ptr<> that manages the python object -> the + // Python object remains alive as-long as there is a shared_ptr<> on the C++ + // side + // + bool load(pybind11::handle src, bool convert) + { + namespace py = pybind11; + + if (!base::load(src, convert)) { + return false; + } + + holder.reset(holder.get(), [pyobj = py::reinterpret_borrow<py::object>( + src)](auto*) mutable { + py::gil_scoped_acquire s; + pyobj = py::object(); + + // we do NOT delete the object here - if this was the last reference to + // the Python object, the Python object will delete it + }); + + return true; + } + + // cast simply forward to the original type_caster<> + // + static pybind11::handle cast(const holder_type& src, + pybind11::return_value_policy policy, + pybind11::handle parent) + { + return base::cast(src, policy, parent); + } + }; + +} // namespace mo2::python::detail + +#define MO2_PYBIND11_SHARED_CPP_HOLDER(Type) \ + namespace pybind11::detail { \ + template <> \ + struct type_caster<std::shared_ptr<Type>> \ + : mo2::python::detail::shared_cpp_owner_caster<Type, \ + std::shared_ptr<Type>> {}; \ + template <> \ + struct type_caster<std::shared_ptr<const Type>> \ + : mo2::python::detail::shared_cpp_owner_caster< \ + Type, std::shared_ptr<const Type>> {}; \ + } + +#endif diff --git a/libs/plugin_python/src/pybind11-utils/include/pybind11_utils/smart_variant.h b/libs/plugin_python/src/pybind11-utils/include/pybind11_utils/smart_variant.h new file mode 100644 index 0000000..bd7bd9b --- /dev/null +++ b/libs/plugin_python/src/pybind11-utils/include/pybind11_utils/smart_variant.h @@ -0,0 +1,53 @@ +#ifndef PYTHON_PYBIND11_UTILS_SMART_VARIANT_H +#define PYTHON_PYBIND11_UTILS_SMART_VARIANT_H + +#include <variant> + +namespace mo2::python { + + namespace detail { + + // simple template class that should be specialized to expose proper fromXXX + // methods + // + template <class T> + struct smart_variant_converter { + template <class U> + static T from(U&& u) + { + return T{std::forward<U>(u)}; + } + }; + + } // namespace detail + + // a smart_variant is a std::variant that can be automatically converted to any of + // its type via custom operator T() + // + // user should specialize detail::smart_variant_converter to provide proper + // conversions + // + template <class... Args> + struct smart_variant : std::variant<Args...> { + using std::variant<Args...>::variant; + + template <class T, std::enable_if_t< + std::disjunction_v<std::is_same<T, Args>...>, int> = 0> + operator T() const + { + return std::visit( + [](auto const& t) -> T { + if constexpr (std::is_same_v<std::decay_t<decltype(t)>, T>) { + return t; + } + else { + return detail::smart_variant_converter<T>::from(t); + } + }, + *this); + } + }; + +} // namespace mo2::python + +#endif diff --git a/libs/plugin_python/src/pybind11-utils/include/pybind11_utils/smart_variant_wrapper.h b/libs/plugin_python/src/pybind11-utils/include/pybind11_utils/smart_variant_wrapper.h new file mode 100644 index 0000000..123d95c --- /dev/null +++ b/libs/plugin_python/src/pybind11-utils/include/pybind11_utils/smart_variant_wrapper.h @@ -0,0 +1,180 @@ +#ifndef PYTHON_PYBIND11_UTILS_SMART_VARIANT_WRAPPER_H +#define PYTHON_PYBIND11_UTILS_SMART_VARIANT_WRAPPER_H + +#include <functional> +#include <type_traits> + +#include <pybind11/pybind11.h> +#include <pybind11/stl.h> + +#include "smart_variant.h" + +namespace mo2::python { + + namespace detail { + + // simple helper class that expose a ::type attribute which is U is I is in Is, + // V otherwise + template <class U, class V, std::size_t I, class Is> + struct wrap_arg; + + template <class U, class V, std::size_t I, std::size_t... Is> + struct wrap_arg<U, V, I, std::index_sequence<Is...>> { + using type = + std::conditional_t<std::disjunction_v<std::bool_constant<I == Is>...>, + U, V>; + }; + + // helper type for wrap_arg + template <class U, class A, std::size_t I, class Is> + using wrap_arg_t = typename wrap_arg<U, A, I, Is>::type; + + template <class T, std::size_t... Is, std::size_t... AIs, class Fn, class R, + class... Args> + auto wrap_arguments_impl(std::index_sequence<Is...>, Fn&& fn, R (*)(Args...), + std::index_sequence<AIs...>) + { + return [fn = std::forward<Fn>(fn)]( + wrap_arg_t<T, Args, AIs, std::index_sequence<Is...>>... args) { + return std::invoke(fn, std::forward<decltype(args)>(args)...); + }; + } + + template <class T, class... Args, std::size_t... Is> + auto make_convertible_index_sequence(std::index_sequence<Is...>) + { + return std::index_sequence<(std::is_convertible_v<T, Args> ? Is : -1)...>{}; + } + + template <class T, std::size_t... Is, class Fn, class R, class... Args> + auto wrap_arguments_impl(Fn&& fn, R (*sg)(Args...)) + { + if constexpr (sizeof...(Is) == 0) { + return wrap_arguments_impl<T>( + make_convertible_index_sequence<T, Args...>( + std::make_index_sequence<sizeof...(Args)>{}), + std::forward<Fn>(fn), sg, + std::make_index_sequence<sizeof...(Args)>{}); + } + else { + return wrap_arguments_impl<T>( + std::index_sequence<Is...>{}, std::forward<Fn>(fn), sg, + std::make_index_sequence<sizeof...(Args)>{}); + } + } + + template <class T, class Fn, class R, class... Args> + auto wrap_return_impl(Fn&& fn, R (*)(Args...)) + { + return [fn = std::forward<Fn>(fn)](Args... args) { + return T{std::invoke(fn, std::forward<decltype(args)>(args)...)}; + }; + } + + // make_python_function_signature: return a null-pointer with the proper type + // for the given function + + template <class Fn> + struct function_signature { + using type = + pybind11::detail::function_signature_t<std::remove_reference_t<Fn>>; + }; + + template <class R, class... Args> + struct function_signature<R (*)(Args...)> { + using type = R(Args...); + }; + + template <class R, class C, class... Args> + struct function_signature<R (C::*)(Args...)> { + using type = R(C*, Args...); + }; + + template <class R, class C, class... Args> + struct function_signature<R (C::*)(Args...) &> { + using type = R(C*, Args...); + }; + + template <class R, class C, class... Args> + struct function_signature<R (C::*)(Args...) const> { + using type = R(const C*, Args...); + }; + + template <class R, class C, class... Args> + struct function_signature<R (C::*)(Args...) const&> { + using type = R(const C*, Args...); + }; + + template <class Fn> + using function_signature_t = typename function_signature<Fn>::type; + + template <class Type, class... WrappedTypes> + class wrap_type_caster { + using variant_type = std::variant<WrappedTypes...>; + using variant_caster = pybind11::detail::make_caster<variant_type>; + + public: + PYBIND11_TYPE_CASTER(Type, variant_caster::name); + + bool load(pybind11::handle src, bool convert) + { + variant_caster caster; + + if (!caster.load(src, convert)) { + return false; + } + + value = std::visit( + [](auto const& fn) { + return Type(fn); + }, + static_cast<variant_type>(caster)); + return true; + } + + static pybind11::handle cast(const Type& src, + pybind11::return_value_policy policy, + pybind11::handle parent) + { + return variant_caster::cast(variant_type(std::in_place_index<0>, src), + policy, parent); + } + }; + + } // namespace detail + + // wrap the given function-like object to accept T instead of the specified + // arguments at the specified positions + // + // if the list of positions is empty, replace all arguments that can be converted to + // T + // + template <class T, std::size_t... Is, class Fn> + auto wrap_arguments(Fn&& fn) + { + return detail::wrap_arguments_impl<T, Is...>( + std::forward<Fn>(fn), + (mo2::python::detail::function_signature_t<Fn>*)nullptr); + } + + // wrap the given function-like object to return T instead of the specified type + // + template <class T, class Fn> + auto wrap_return(Fn&& fn) + { + return detail::wrap_return_impl<T>( + std::forward<Fn>(fn), + (mo2::python::detail::function_signature_t<Fn>*)nullptr); + } + +} // namespace mo2::python + +namespace pybind11::detail { + + template <class... Args> + struct type_caster<::mo2::python::smart_variant<Args...>> + : variant_caster<::mo2::python::smart_variant<Args...>> {}; + +} // namespace pybind11::detail + +#endif diff --git a/libs/plugin_python/src/runner/CMakeLists.txt b/libs/plugin_python/src/runner/CMakeLists.txt new file mode 100644 index 0000000..d164460 --- /dev/null +++ b/libs/plugin_python/src/runner/CMakeLists.txt @@ -0,0 +1,34 @@ +cmake_minimum_required(VERSION 3.16) + +if(NOT TARGET mo2::uibase) + find_package(mo2-uibase CONFIG REQUIRED) +endif() + +add_library(runner SHARED + error.h + pythonrunner.cpp + pythonrunner.h + pythonutils.h + pythonutils.cpp +) +mo2_configure_target(runner + NO_SOURCES + WARNINGS 4 + EXTERNAL_WARNINGS 4 + AUTOMOC ON + TRANSLATIONS OFF +) +mo2_default_source_group() +target_link_libraries(runner PUBLIC mo2::uibase PRIVATE pybind11::embed pybind11::qt) +target_include_directories(runner PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) +target_compile_definitions(runner PRIVATE RUNNER_BUILD) +if(NOT WIN32 AND Python_SHARED_LIBRARY) + get_filename_component(_pylib_name "${Python_SHARED_LIBRARY}" NAME) + target_compile_definitions(runner PRIVATE + MO2_PYTHON_SHARED_LIBRARY="${_pylib_name}") +endif() + +# proxy will install runner + +# force runner to build mobase +add_dependencies(runner mobase) diff --git a/libs/plugin_python/src/runner/error.h b/libs/plugin_python/src/runner/error.h new file mode 100644 index 0000000..341c7ea --- /dev/null +++ b/libs/plugin_python/src/runner/error.h @@ -0,0 +1,82 @@ +#ifndef ERROR_H +#define ERROR_H + +#include <format> + +#include <QString> + +#include <pybind11/pybind11.h> + +#include <uibase/utility.h> + +namespace pyexcept { + + /** + * @brief Exception to throw when a python implementation does not implement + * a pure virtual function. + */ + class MissingImplementation : public MOBase::Exception { + public: + MissingImplementation(std::string const& className, + std::string const& methodName) + : Exception(QString::fromStdString( + std::format("Python class implementing \"{}\" has no " + "implementation of method \"{}\".", + className, methodName))) + { + } + }; + + /** + * @brief Exception to throw when a python error occurs. + */ + class PythonError : public MOBase::Exception { + public: + /** + * @brief Create a new PythonError, fetching the error message from + * python. If the message cannot be retrieved, `defaultErrorMessage()` + * is used instead. + */ + PythonError(pybind11::error_already_set const& ex) : Exception(ex.what()) {} + + /** + * @brief Create a new PythonError with the given message. + * + * @param message Message for the exception. + */ + PythonError(QString message) : Exception(message) {} + }; + + /** + * @brief Exception to throw when an unknown error occured. This is + * typically thrown from a catch(...) block. + */ + class UnknownException : public MOBase::Exception { + public: + /** + * @brief Create a new UnknownException with the default message. + * + * @see defaultErrorMessage + */ + UnknownException() : Exception(defaultErrorMessage()) {} + + /** + * @brief Create a new UnknownException with the given message. + * + * @param message Message for the exception. + */ + UnknownException(QString message) : Exception(message) {} + + protected: + /** + * + */ + static QString defaultErrorMessage() + { + return QObject::tr("An unknown exception was thrown in python code."); + } + }; + +} // namespace pyexcept + +#endif // ERROR_H diff --git a/libs/plugin_python/src/runner/pythonrunner.cpp b/libs/plugin_python/src/runner/pythonrunner.cpp new file mode 100644 index 0000000..05a0f30 --- /dev/null +++ b/libs/plugin_python/src/runner/pythonrunner.cpp @@ -0,0 +1,415 @@ +#include "pythonrunner.h" + +#ifdef _WIN32 +#pragma warning(disable : 4100) +#pragma warning(disable : 4996) + +#include <Windows.h> +#else +#include <dlfcn.h> +#endif + +#include <algorithm> +#include <cstdlib> +#include <optional> + +#include <QCoreApplication> +#include <QDir> +#include <QFile> + +#include "pybind11_qt/pybind11_qt.h" +#include <pybind11/embed.h> +#include <pybind11/functional.h> +#include <pybind11/stl/filesystem.h> + +#include <uibase/log.h> +#include <uibase/utility.h> + +#include "error.h" +#include "pythonutils.h" + +using namespace MOBase; +namespace py = pybind11; + +namespace mo2::python { + + /** + * + */ + class PythonRunner : public IPythonRunner { + + public: + PythonRunner() = default; + ~PythonRunner() = default; + + QList<QObject*> load(const QString& identifier) override; + void unload(const QString& identifier) override; + + bool initialize(std::vector<std::filesystem::path> const& pythonPaths) override; + void addDllSearchPath(std::filesystem::path const& dllPath) override; + bool isInitialized() const override; + + private: + /** + * @brief Ensure that the given folder is in sys.path. + */ + void ensureFolderInPath(QString folder); + + private: + // for each "identifier" (python file or python module folder), contains the + // list of python objects - this does not keep the objects alive, it simply used + // to unload plugins + std::unordered_map<QString, std::vector<py::handle>> m_PythonObjects; + }; + + std::unique_ptr<IPythonRunner> createPythonRunner() + { + return std::make_unique<PythonRunner>(); + } + + bool PythonRunner::initialize(std::vector<std::filesystem::path> const& pythonPaths) + { + // we only initialize Python once for the whole lifetime of the program, even if + // MO2 is restarted and the proxy or PythonRunner objects are deleted and + // recreated, Python is not re-initialized + // + // in an ideal world, we would initialize Python here (or in the constructor) + // and then finalize it in the destructor + // + // unfortunately, many library, including PyQt6, do not handle properly + // re-initializing the Python interpreter, so we cannot do that and we keep the + // interpreter alive + // + + if (Py_IsInitialized()) { + return true; + } + + std::optional<QByteArray> oldPythonHome; + std::optional<QByteArray> oldPythonPath; + auto restorePythonEnv = [&]() { + if (oldPythonHome.has_value()) { + setenv("PYTHONHOME", oldPythonHome->constData(), 1); + } else { + unsetenv("PYTHONHOME"); + } + if (oldPythonPath.has_value()) { + setenv("PYTHONPATH", oldPythonPath->constData(), 1); + } else { + unsetenv("PYTHONPATH"); + } + }; + + try { + static const char* argv0 = "ModOrganizer.exe"; + +#ifndef _WIN32 +#ifdef MO2_PYTHON_SHARED_LIBRARY + // Ensure libpython symbols are globally visible for extension modules + // loaded later (_struct, PyQt6, etc.). + void* pyHandle = + dlopen(MO2_PYTHON_SHARED_LIBRARY, RTLD_NOW | RTLD_GLOBAL | RTLD_NOLOAD); + if (pyHandle == nullptr) { + pyHandle = dlopen(MO2_PYTHON_SHARED_LIBRARY, RTLD_NOW | RTLD_GLOBAL); + } + if (pyHandle == nullptr) { + MOBase::log::warn("failed to dlopen python shared library '{}': {}", + MO2_PYTHON_SHARED_LIBRARY, dlerror()); + } +#endif +#endif + + // For portable/AppImage builds, set PYTHONHOME so the interpreter + // finds the bundled stdlib instead of looking at system paths. + // MO2_PYTHON_DIR (set by AppRun) points to the writable python/ + // dir next to the AppImage; fall back to <exe_dir>/python. + QString pythonHome; + const char* envPy = std::getenv("MO2_PYTHON_DIR"); + if (envPy && envPy[0] != '\0') { + pythonHome = QString::fromUtf8(envPy); + } else { + pythonHome = QCoreApplication::applicationDirPath() + "/python"; + } + if (const char* v = std::getenv("PYTHONHOME"); v != nullptr) { + oldPythonHome = QByteArray(v); + } + if (const char* v = std::getenv("PYTHONPATH"); v != nullptr) { + oldPythonPath = QByteArray(v); + } + + if (QDir(pythonHome).exists()) { + setenv("PYTHONHOME", pythonHome.toUtf8().constData(), 1); + + const QDir libDir(pythonHome + "/lib"); + const auto pyDirs = + libDir.entryList({"python3.*"}, QDir::Dirs | QDir::NoDotAndDotDot); + if (!pyDirs.isEmpty()) { + const QString pyver = pyDirs.first(); + const QString pyPath = QString("%1/lib/%2:%1/lib/%2/site-packages:%1") + .arg(pythonHome, pyver); + setenv("PYTHONPATH", pyPath.toUtf8().constData(), 1); + } + } + + // Paths we want to prepend/append for MO2 plugin loading. + auto paths = pythonPaths; + + PyConfig config; + PyConfig_InitPythonConfig(&config); + + // from PyBind11 + config.parse_argv = 0; + config.install_signal_handlers = 0; + + // from MO2 + config.site_import = 1; + config.optimization_level = 2; + + py::initialize_interpreter(&config, 1, &argv0, true); + + // Restore process environment after interpreter startup so + // subprocesses (umu/NaK/tools) are not forced onto MO2's Python. + restorePythonEnv(); + + if (!Py_IsInitialized()) { + MOBase::log::error( + "failed to init python: failed to initialize interpreter."); + + if (PyGILState_Check()) { + PyEval_SaveThread(); + } + + return false; + } + + { + for (auto const& path : paths) { + ensureFolderInPath(QString::fromStdString(absolute(path).string())); + } + + py::module_ mainModule = py::module_::import("__main__"); + py::object mainNamespace = mainModule.attr("__dict__"); + mainNamespace["sys"] = py::module_::import("sys"); + mainNamespace["mobase"] = py::module_::import("mobase"); + + mo2::python::configure_python_stream(); + mo2::python::configure_python_logging(mainNamespace["mobase"]); + } + + // we need to release the GIL here - which is what this does + // + // when Python is initialized, the GIl is acquired, and if it is not + // release, trying to acquire it on a different thread will deadlock + PyEval_SaveThread(); + + return true; + } + catch (const py::error_already_set& ex) { + restorePythonEnv(); + MOBase::log::error("failed to init python: {}", ex.what()); + return false; + } + } + + void PythonRunner::addDllSearchPath(std::filesystem::path const& dllPath) + { + py::gil_scoped_acquire lock; +#ifdef _WIN32 + py::module_::import("os").attr("add_dll_directory")(absolute(dllPath)); +#else + // On Linux, there is no add_dll_directory equivalent; prepend the folder to + // sys.path so Python extension modules can be found. + ensureFolderInPath(QString::fromStdString(absolute(dllPath).string())); +#endif + } + + void PythonRunner::ensureFolderInPath(QString folder) + { + py::module_ sys = py::module_::import("sys"); + py::list sysPath = sys.attr("path"); + + // Converting to QStringList for Qt::CaseInsensitive and because .index() + // raise an exception: + const QStringList currentPath = sysPath.cast<QStringList>(); + if (!currentPath.contains(folder, Qt::CaseInsensitive)) { + sysPath.insert(0, folder); + } + } + + QList<QObject*> PythonRunner::load(const QString& identifier) + { + py::gil_scoped_acquire lock; + + const QFileInfo idInfo(identifier); + const QString baseName = idInfo.fileName(); + if (baseName == "winreg.py" || baseName == "lzokay.py") { + log::debug("Skipping Python compatibility shim '{}'.", identifier); + return {}; + } + + // `pluginName` can either be a python file (single-file plugin or a folder + // (whole module). + // + // For whole module, we simply add the parent folder to path, then we load + // the module with a simple py::import, and we retrieve the associated + // __dict__ from which we extract either createPlugin or createPlugins. + // + // For single file, we need to use py::eval_file, and we will use the + // context (global variables) from __main__ (already contains mobase, and + // other required module). Since the context is shared between called of + // `instantiate`, we need to make sure to remove createPlugin(s) from + // previous call. + try { + + // dictionary that will contain createPlugin() or createPlugins(). + py::dict moduleDict; + + if (identifier.endsWith(".py")) { + py::object mainModule = py::module_::import("__main__"); + + // make a copy, otherwise we might end up calling the createPlugin() or + // createPlugins() function multiple time + py::dict moduleNamespace = mainModule.attr("__dict__").attr("copy")(); + + std::string temp = ToString(identifier); + py::eval_file(temp, moduleNamespace).is_none(); + moduleDict = moduleNamespace; + } + else { + // Retrieve the module name: + QStringList parts = identifier.split("/"); + std::string moduleName = ToString(parts.takeLast()); + ensureFolderInPath(parts.join("/")); + + // check if the module is already loaded + py::dict modules = py::module_::import("sys").attr("modules"); + if (modules.contains(moduleName)) { + py::module_ prev = modules[py::str(moduleName)]; + py::module_(prev).reload(); + moduleDict = prev.attr("__dict__"); + } + else { + moduleDict = + py::module_::import(moduleName.c_str()).attr("__dict__"); + } + } + + if (py::len(moduleDict) == 0) { + MOBase::log::error("No plugins found in {}.", identifier); + return {}; + } + + // Create the plugins: + std::vector<py::object> plugins; + + if (moduleDict.contains("createPlugin")) { + plugins.push_back(moduleDict["createPlugin"]()); + } + else if (moduleDict.contains("createPlugins")) { + py::object pyPlugins = moduleDict["createPlugins"](); + if (!py::isinstance<py::sequence>(pyPlugins)) { + MOBase::log::error( + "Plugin {}: createPlugins must return a sequence.", identifier); + } + else { + py::sequence pyList(pyPlugins); + size_t nPlugins = pyList.size(); + for (size_t i = 0; i < nPlugins; ++i) { + plugins.push_back(pyList[i]); + } + } + } + else { + MOBase::log::error("Plugin {}: missing a createPlugin(s) function.", + identifier); + } + + // If we have no plugins, there was an issue, and we already logged the + // problem: + if (plugins.empty()) { + return QList<QObject*>(); + } + + QList<QObject*> allInterfaceList; + + for (py::object pluginObj : plugins) { + + // save to be able to unload it + m_PythonObjects[identifier].push_back(pluginObj); + + QList<QObject*> interfaceList = py::module_::import("mobase.private") + .attr("extract_plugins")(pluginObj) + .cast<QList<QObject*>>(); + + if (interfaceList.isEmpty()) { + MOBase::log::error("Plugin {}: no plugin interface implemented.", + identifier); + } + + // Append the plugins to the main list: + allInterfaceList.append(interfaceList); + } + + return allInterfaceList; + } + catch (const py::error_already_set& ex) { + MOBase::log::error("Failed to import plugin from {}.", identifier); + throw pyexcept::PythonError(ex); + } + } + + void PythonRunner::unload(const QString& identifier) + { + auto it = m_PythonObjects.find(identifier); + if (it != m_PythonObjects.end()) { + + py::gil_scoped_acquire lock; + + if (!identifier.endsWith(".py")) { + + // At this point, the identifier is the full path to the module. + QDir folder(identifier); + + // We want to "unload" (remove from sys.modules) modules that come + // from this plugin (whose __path__ points under this module, + // including the module of the plugin itself). + py::object sys = py::module_::import("sys"); + py::dict modules = sys.attr("modules"); + py::list keys = modules.attr("keys")(); + for (std::size_t i = 0; i < py::len(keys); ++i) { + py::object mod = modules[keys[i]]; + if (PyObject_HasAttrString(mod.ptr(), "__path__")) { + QString mpath = + mod.attr("__path__")[py::int_(0)].cast<QString>(); + + if (!folder.relativeFilePath(mpath).startsWith("..")) { + // If the path is under identifier, we need to unload + // it. + log::debug("Unloading module {} from {} for {}.", + keys[i].cast<std::string>(), mpath, identifier); + + PyDict_DelItem(modules.ptr(), keys[i].ptr()); + } + } + } + } + + // Boost.Python does not handle cyclic garbace collection, so we need to + // release everything hold by the objects before deleting the objects + // themselves (done when erasing from m_PythonObjects). + for (auto& obj : it->second) { + obj.attr("__dict__").attr("clear")(); + } + + log::debug("Deleting {} python objects for {}.", it->second.size(), + identifier); + m_PythonObjects.erase(it); + } + } + + bool PythonRunner::isInitialized() const + { + return Py_IsInitialized() != 0; + } + +} // namespace mo2::python diff --git a/libs/plugin_python/src/runner/pythonrunner.h b/libs/plugin_python/src/runner/pythonrunner.h new file mode 100644 index 0000000..5f9751b --- /dev/null +++ b/libs/plugin_python/src/runner/pythonrunner.h @@ -0,0 +1,53 @@ +#ifndef PYTHONRUNNER_H +#define PYTHONRUNNER_H + +#include <filesystem> +#include <memory> + +#include <QList> +#include <QObject> +#include <QString> +#include <QStringList> + +#ifdef RUNNER_BUILD +#define RUNNER_DLL_EXPORT Q_DECL_EXPORT +#else +#define RUNNER_DLL_EXPORT Q_DECL_IMPORT +#endif + +namespace mo2::python { + + // python runner interface + // + class IPythonRunner { + public: + virtual QList<QObject*> load(const QString& identifier) = 0; + virtual void unload(const QString& identifier) = 0; + + // initialize Python + // + // pythonPaths contains the list of built-in paths for the Python library + // (pythonxxx.zip, etc.), an empty list uses the default Python paths (e.g., the + // PYTHONPATH environment variable) + // + virtual bool + initialize(std::vector<std::filesystem::path> const& pythonPaths = {}) = 0; + + // add a DLL search path + // + virtual void addDllSearchPath(std::filesystem::path const& dllPath) = 0; + + // check if the runner has been initialized, i.e., initialize() has been + // called and succeeded + virtual bool isInitialized() const = 0; + + virtual ~IPythonRunner() {} + }; + + // create the Python runner + // + RUNNER_DLL_EXPORT std::unique_ptr<IPythonRunner> createPythonRunner(); + +} // namespace mo2::python + +#endif // PYTHONRUNNER_H diff --git a/libs/plugin_python/src/runner/pythonutils.cpp b/libs/plugin_python/src/runner/pythonutils.cpp new file mode 100644 index 0000000..c94a50b --- /dev/null +++ b/libs/plugin_python/src/runner/pythonutils.cpp @@ -0,0 +1,155 @@ +#include "pythonutils.h" + +#include <filesystem> +#include <set> +#include <sstream> + +#include <pybind11/eval.h> +#include <pybind11/pybind11.h> + +#include <uibase/log.h> + +namespace py = pybind11; + +namespace mo2::python { + + class PrintWrapper { + MOBase::log::Levels level_; + std::stringstream buffer_; + + public: + PrintWrapper(MOBase::log::Levels level) : level_{level} {} + + void write(std::string_view message) + { + buffer_ << message; + if (buffer_.tellp() != 0 && buffer_.str().back() == '\n') { + const auto full_message = buffer_.str(); + MOBase::log::log(level_, "{}", + full_message.substr(0, full_message.length() - 1)); + buffer_ = std::stringstream{}; + } + } + }; + + /** + * @brief Construct a dynamic Python type. + * + */ + template <class... Args> + pybind11::object make_python_type(std::string_view name, + pybind11::tuple base_classes, Args&&... args) + { + // this is ugly but that's how it's done in C Python + auto type = py::reinterpret_borrow<py::object>((PyObject*)&PyType_Type); + + // create the python class + return type(name, base_classes, py::dict(std::forward<Args>(args)...)); + } + + void configure_python_stream() + { + // create the "MO2Handler" python class + auto printWrapper = make_python_type( + "MO2PrintWrapper", py::make_tuple(), + py::arg("write") = py::cpp_function([](std::string_view message) { + static PrintWrapper wrapper(MOBase::log::Debug); + wrapper.write(message); + }), + py::arg("flush") = py::cpp_function([] {})); + auto errorWrapper = make_python_type( + "MO2ErrorWrapper", py::make_tuple(), + py::arg("write") = py::cpp_function([](std::string_view message) { + static PrintWrapper wrapper(MOBase::log::Error); + wrapper.write(message); + }), + py::arg("flush") = py::cpp_function([] {})); + py::module_ sys = py::module_::import("sys"); + sys.attr("stdout") = printWrapper(); + sys.attr("stderr") = errorWrapper(); + + // this is required to handle exception in Python code OUTSIDE of pybind11 call, + // typically on Qt classes with methods overridden on the Python side + // + // without this, the application will crash instead of properly handling the + // exception as it would do with a py::error_already_set{} + // + // IMPORTANT: sys.attr("excepthook") = sys.attr("__excepthook__") DOES NOT WORK, + // and I have no clue why since the attribute does not seem to get updated (at + // least a print does not show it) + // + sys.attr("excepthook") = + py::eval("lambda x, y, z: sys.__excepthook__(x, y, z)"); + } + + // Small structure to hold the levels - There are copy paste from + // my Python version and I assume these will not change soon: + struct PyLogLevel { + static constexpr int CRITICAL = 50; + static constexpr int ERROR = 40; + static constexpr int WARNING = 30; + static constexpr int INFO = 20; + static constexpr int DEBUG = 10; + }; + + // This is the function we are going to use as our Handler .emit + // method. + void emit_function(py::object record) + { + + // There are other parameters that could be used, but this is minimal + // for now (filename, line number, etc.). + const int level = record.attr("levelno").cast<int>(); + const std::wstring msg = py::str(record.attr("msg")).cast<std::wstring>(); + + switch (level) { + case PyLogLevel::CRITICAL: + case PyLogLevel::ERROR: + MOBase::log::error("{}", msg); + break; + case PyLogLevel::WARNING: + MOBase::log::warn("{}", msg); + break; + case PyLogLevel::INFO: + MOBase::log::info("{}", msg); + break; + case PyLogLevel::DEBUG: + default: // There is a "NOTSET" level in theory: + MOBase::log::debug("{}", msg); + break; + } + }; + + void configure_python_logging(py::module_ mobase) + { + // most of this is dealing with actual Python objects since it is not + // possible to derive from logging.Handler in C++ using pybind11, + // and since a lot of this would require extra register only for this. + + // see also + // https://github.com/pybind/pybind11/issues/1193#issuecomment-429451094 + + // retrieve the logging module and the Handler class. + auto logging = py::module_::import("logging"); + auto Handler = logging.attr("Handler"); + + // create the "MO2Handler" python class + auto MO2Handler = + make_python_type("LogHandler", py::make_tuple(Handler), + py::arg("emit") = py::cpp_function(emit_function)); + + // create the default logger + auto handler = MO2Handler(); + handler.attr("setLevel")(PyLogLevel::DEBUG); + auto logger = logging.attr("getLogger")(py::object(mobase.attr("__name__"))); + logger.attr("setLevel")(PyLogLevel::DEBUG); + + // set mobase attributes + mobase.attr("LogHandler") = MO2Handler; + mobase.attr("logger") = logger; + + logging.attr("root").attr("setLevel")(PyLogLevel::DEBUG); + logging.attr("root").attr("addHandler")(handler); + } + +} // namespace mo2::python diff --git a/libs/plugin_python/src/runner/pythonutils.h b/libs/plugin_python/src/runner/pythonutils.h new file mode 100644 index 0000000..019e782 --- /dev/null +++ b/libs/plugin_python/src/runner/pythonutils.h @@ -0,0 +1,25 @@ +#ifndef PYTHONRUNNER_UTILS_H +#define PYTHONRUNNER_UTILS_H + +#include <string_view> + +#include <pybind11/pybind11.h> + +namespace mo2::python { + + /** + * @brief Configure Python stdout and stderr to log to MO2. + * + */ + void configure_python_stream(); + + /** + * @brief Configure logging for MO2 python plugin. + * + * @param mobase The mobase module. + */ + void configure_python_logging(pybind11::module_ mobase); + +} // namespace mo2::python + +#endif |
