Compare commits

..
24 Commits
Author SHA1 Message Date
pre-commit-ci[bot] e9d55a50a7 [pre-commit.ci] Pre-commit autoupdate. (#159)
updates:
- [github.com/pre-commit/mirrors-clang-format: v22.1.2 → v22.1.5](https://github.com/pre-commit/mirrors-clang-format/compare/v22.1.2...v22.1.5)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-07-08 15:54:09 +02:00
Jonathan Feenstra 17840a0d97 Fix typo in "Python Initialization failed" message (#158) 2026-06-04 09:01:06 +02:00
Mikaël Capelle 0b0f34b2da Change IExecutablesList.titleExists to contains to match UIBase name. (#157) 2026-05-08 12:54:40 +02:00
Jeremy Rimpo 1720aeedf8 Update vcpkg targets (#155) 2026-04-15 09:31:56 +02:00
Jeremy Rimpo 1c8ab3f1c1 Add blueprint prefix to game plugin (#156) 2026-04-15 09:01:02 +02:00
pre-commit-ci[bot] 5003bf75a7 [pre-commit.ci] Pre-commit autoupdate. (#153)
updates:
- [github.com/pre-commit/mirrors-clang-format: v21.1.8 → v22.1.2](https://github.com/pre-commit/mirrors-clang-format/compare/v21.1.8...v22.1.2)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-04-07 09:00:58 +02:00
Jonathan Feenstra b1ecd89c50 Add instance manager to plugin API (#152) 2026-02-08 10:28:25 +01:00
Jonathan Feenstra 076244faab Add executables list to plugin API (#151) 2026-01-31 16:49:51 +01:00
Jonathan Feenstra b81646562c Add instanceName and profiles methods to plugin API (#150) 2026-01-11 17:57:34 +01:00
pre-commit-ci[bot] 648db4db99 [pre-commit.ci] Pre-commit autoupdate. (#149)
updates:
- [github.com/pre-commit/mirrors-clang-format: v21.1.2 → v21.1.8](https://github.com/pre-commit/mirrors-clang-format/compare/v21.1.2...v21.1.8)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-01-05 19:32:00 +01:00
pre-commit-ci[bot] 15d74e5e76 [pre-commit.ci] Pre-commit autoupdate. (#148)
updates:
- [github.com/pre-commit/pre-commit-hooks: v5.0.0 → v6.0.0](https://github.com/pre-commit/pre-commit-hooks/compare/v5.0.0...v6.0.0)
- [github.com/pre-commit/mirrors-clang-format: v20.1.7 → v21.1.2](https://github.com/pre-commit/mirrors-clang-format/compare/v20.1.7...v21.1.2)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-10-07 15:00:39 +02:00
Mikaël Capelle 4f3a9891fa Add missing IPlugin inheritance in installer bindings. (#147) 2025-09-30 20:22:52 +02:00
Jonathan Feenstra 6bed428d04 Extend Python plugin API with author and uploader data (#145) 2025-08-18 07:55:42 +02:00
Mikaël Capelle f16e51867a Allow non-cache build in CI when Azure variables are not available. (#146) 2025-08-17 12:00:35 +02:00
Mikaël Capelle c05027b653 Update following move of IFileTree glob() and walk() to free function. (#144) 2025-07-08 19:33:04 +02:00
pre-commit-ci[bot] 5726e65504 [pre-commit.ci] Pre-commit autoupdate. (#143)
* [pre-commit.ci] Pre-commit autoupdate.

updates:
- [github.com/pre-commit/mirrors-clang-format: v19.1.5 → v20.1.7](https://github.com/pre-commit/mirrors-clang-format/compare/v19.1.5...v20.1.7)

* [pre-commit.ci] Auto fixes from pre-commit.com hooks.

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2025-07-07 20:38:02 +02:00
Mikaël Capelle b0d2b48524 Add bindings for IFileTree glob() and walk() generators (#142)
* Adding binding for new IFileTree glob and walk.
* Fix issues with Version binding.
2025-07-06 19:38:16 +02:00
Mikaël Capelle 00a6d5969b Use exact Python version and fix missing DLLs for plugin. (#141) 2025-07-02 08:16:47 +02:00
Mikaël Capelle 1010bfda55 Move to VCPKG (#133) 2025-05-29 11:03:51 +02:00
Jeremy Rimpo 9a788c9f3f Add game interfaces (#140)
- modDataDirectory defines mod directory that maps to 'Data'
- getModMappings allows mapping directories to multiple locations
2025-05-21 18:43:47 +02:00
Jonathan Feenstra 84bf90d575 Add form and header versions to IPluginList (#139) 2025-01-31 20:07:33 +01:00
Jonathan Feenstra 8c47cea270 Add startDownloadNexusFileForGame (#138) 2025-01-02 14:11:06 +01:00
Jeremy RimpoandJonathan Feenstra 5cdf0f6f19 Blueprint support (#136)
---------

Co-authored-by: Jonathan Feenstra <26406078+JonathanFeenstra@users.noreply.github.com>
2024-10-03 09:45:05 +02:00
Jonathan Feenstra 4409375fd8 Add author and description methods to IPluginList (#135) 2024-09-28 19:58:34 +02:00
36 changed files with 757 additions and 442 deletions
+13 -30
View File
@@ -2,54 +2,37 @@ name: Build & Test Plugin Python
on:
push:
branches: master
branches: [master]
pull_request:
types: [opened, synchronize, reopened]
env:
VCPKG_BINARY_SOURCES: "clear;x-gha,readwrite"
VCPKG_BINARY_SOURCES: ${{ vars.AZ_BLOB_VCPKG_URL != '' && format('clear;x-azblob,{0},{1},readwrite', vars.AZ_BLOB_VCPKG_URL, secrets.AZ_BLOB_SAS) || '' }}
jobs:
build:
runs-on: windows-2022
steps:
# https://learn.microsoft.com/en-us/vcpkg/consume/binary-caching-github-actions-cache
- name: Export GitHub Actions cache environment variables
uses: actions/github-script@v7
with:
script: |
core.exportVariable('ACTIONS_CACHE_URL', process.env.ACTIONS_CACHE_URL || '');
core.exportVariable('ACTIONS_RUNTIME_TOKEN', process.env.ACTIONS_RUNTIME_TOKEN || '');
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- name: Install Qt
uses: jurplel/install-qt-action@v3
- name: Configure Plugin Python
id: configure-plugin-python
uses: ModOrganizer2/build-with-mob-action@master
with:
setup-python: false
version: 6.7.1
modules:
cache: true
- uses: actions/checkout@v4
- name: "Set environmental variables"
shell: bash
run: |
echo "VCPKG_ROOT=$VCPKG_INSTALLATION_ROOT" >> $GITHUB_ENV
- name: Configure Plugin Python build
shell: pwsh
run: |
cmake --preset vs2022-windows-standalone `
"-DCMAKE_PREFIX_PATH=${env:QT_ROOT_DIR}\msvc2019_64" `
-DPLUGIN_PYTHON_TESTING=ON
mo2-dependencies: uibase
mo2-skip-build: true
- name: Build Plugin Python
working-directory: ${{ steps.configure-plugin-python.outputs.working-directory }}
run: cmake --build vsbuild --config RelWithDebInfo --verbose `
--target python-tests --target runner-tests --target proxy
- name: Test Plugin Python
working-directory: ${{ steps.configure-plugin-python.outputs.working-directory }}
run: ctest --test-dir vsbuild -C RelWithDebInfo --output-on-failure
- name: Install Plugin Python
working-directory: ${{ steps.configure-plugin-python.outputs.working-directory }}
run: cmake --build vsbuild --config RelWithDebInfo --target INSTALL
+20
View File
@@ -0,0 +1,20 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v6.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-merge-conflict
- id: check-case-conflict
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v22.1.5
hooks:
- id: clang-format
'types_or': [c++, c]
ci:
autofix_commit_msg: "[pre-commit.ci] Auto fixes from pre-commit.com hooks."
autofix_prs: true
autoupdate_commit_msg: "[pre-commit.ci] Pre-commit autoupdate."
autoupdate_schedule: quarterly
submodules: false
+18 -5
View File
@@ -4,14 +4,27 @@ cmake_policy(SET CMP0144 NEW)
project(plugin_python CXX)
set(Python_FIND_VIRTUALENV STANDARD)
# we need mo2-cmake to obtain the Python version, but mo2-cmake will set
# CMAKE_MAP_IMPORTED_CONFIG_* which will trigger a tons of CMP0111 warnings for Python
# below so we need to reset these before finding Python and then reset them after
find_package(mo2-cmake CONFIG REQUIRED)
set(_CMAKE_MAP_IMPORTED_CONFIG_MINSIZEREL ${CMAKE_MAP_IMPORTED_CONFIG_MINSIZEREL})
set(_CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBINFO ${CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBINFO})
set(_CMAKE_MAP_IMPORTED_CONFIG_RELEASE ${CMAKE_MAP_IMPORTED_CONFIG_RELEASE})
set(CMAKE_MAP_IMPORTED_CONFIG_MINSIZEREL "")
set(CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBINFO "")
set(CMAKE_MAP_IMPORTED_CONFIG_RELEASE "")
# find Python before include mo2-cmake, otherwise this will trigger a bunch of CMP0111
# due to the imported configuration mapping variables defined in mo2.cmake
find_package(Python ${MO2_PYTHON_VERSION} COMPONENTS Interpreter Development REQUIRED)
find_package(Python ${MO2_PYTHON_VERSION} EXACT COMPONENTS Interpreter Development REQUIRED)
find_package(pybind11 CONFIG REQUIRED)
find_package(mo2-cmake CONFIG REQUIRED)
set(CMAKE_MAP_IMPORTED_CONFIG_MINSIZEREL ${_CMAKE_MAP_IMPORTED_CONFIG_MINSIZEREL})
set(CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBINFO ${_CMAKE_MAP_IMPORTED_CONFIG_RELWITHDEBINFO})
set(CMAKE_MAP_IMPORTED_CONFIG_RELEASE ${_CMAKE_MAP_IMPORTED_CONFIG_RELEASE})
get_filename_component(Python_HOME ${Python_EXECUTABLE} PATH)
set(Python_DLL_DIR "${Python_HOME}/DLLs")
@@ -26,8 +39,8 @@ set(Python_VERSION_SHORT ${Python_VERSION_MAJOR}${Python_VERSION_MINOR})
add_subdirectory(src)
# tests (if requested)
set(PLUGIN_PYTHON_TESTING ${BUILD_TESTING} CACHE BOOL "build tests for plugin_python")
if (PLUGIN_PYTHON_TESTING)
set(BUILD_TESTING ${BUILD_TESTING} CACHE BOOL "build tests for plugin_python")
if (BUILD_TESTING)
enable_testing()
add_subdirectory(tests)
endif()
+1 -12
View File
@@ -5,9 +5,6 @@
#include <tuple>
#include <variant>
#include <uibase/exceptions.h>
#include <uibase/versioning.h>
#include <pybind11/embed.h>
#include "pybind11_all.h"
@@ -27,13 +24,6 @@ PYBIND11_MODULE(mobase, m)
m.add_object("PyQt6.QtGui", py::module_::import("PyQt6.QtGui"));
m.add_object("PyQt6.QtWidgets", py::module_::import("PyQt6.QtWidgets"));
// exceptions
//
py::register_exception<Exception>(m, "MO2Exception");
py::register_exception<InvalidNXMLinkException>(m, "InvalidNXMLinkException");
py::register_exception<IncompatibilityException>(m, "IncompatibilityException");
py::register_exception<InvalidVersionException>(m, "InvalidVersionException");
// bindings
//
mo2::python::add_basic_bindings(m);
@@ -79,8 +69,7 @@ PYBIND11_MODULE(mobase, m)
//
m.add_object(
"MoVariant",
py::eval(
"None | bool | int | str | float | list[object] | dict[str, object]"));
py::eval("None | bool | int | str | list[object] | dict[str, object]"));
// same thing for GameFeatureType
//
+1
View File
@@ -13,6 +13,7 @@
#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"
+96 -74
View File
@@ -3,15 +3,19 @@
#include "../pybind11_all.h"
#include <format>
#include <memory>
#include <pybind11_utils/generator.h>
#include <uibase/executableinfo.h>
#include <uibase/extensions/extension.h>
#include <uibase/extensions/iextensionlist.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>
@@ -56,7 +60,24 @@ namespace mo2::python {
.value("NO_METADATA", Version::FormatMode::NoMetadata)
.value("CONDENSED",
static_cast<Version::FormatMode>(Version::FormatCondensed.toInt()))
.export_values();
.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,
@@ -88,8 +109,11 @@ namespace mo2::python {
.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::FormatCondensed)
.def("__str__", &Version::string)
.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)
@@ -179,6 +203,27 @@ namespace mo2::python {
.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("contains", &IExecutablesList::contains, "title"_a);
}
void add_modinterface_classes(py::module_ m)
@@ -213,6 +258,9 @@ namespace mo2::python {
.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)
@@ -280,7 +328,10 @@ namespace mo2::python {
.def_readwrite("fileCategory", &ModRepositoryFileInfo::fileCategory)
.def_readwrite("fileTime", &ModRepositoryFileInfo::fileTime)
.def_readwrite("repository", &ModRepositoryFileInfo::repository)
.def_readwrite("userData", &ModRepositoryFileInfo::userData);
.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)
@@ -360,38 +411,6 @@ namespace mo2::python {
py::implicitly_convertible<QString, GuessedValue<QString>>();
}
void add_iextensionlist_classes(py::module_ m)
{
// TODO: add all bindings here
py::class_<IExtension>(m, "IExtension");
py::class_<IExtensionList>(m, "IExtensionList")
.def("installed", &IExtensionList::installed, "identifier"_a)
.def(
"enabled",
py::overload_cast<const QString&>(&IExtensionList::enabled, py::const_),
"identifier"_a)
.def(
"__getitem__",
[](IExtensionList const& self,
std::variant<std::size_t, QString> const& index) {
return std::visit(
[&self](auto&& value) {
if constexpr (std::is_same_v<std::decay_t<decltype(value)>,
std::size_t>) {
return self.at(value);
}
else {
return self.get(value);
}
},
index);
},
"index"_a, py::return_value_policy::reference)
.def("__len__", &IExtensionList::size);
}
void add_ipluginlist_classes(py::module_ m)
{
py::enum_<IPluginList::PluginState>(m, "PluginState", py::arithmetic())
@@ -417,9 +436,15 @@ namespace mo2::python {
.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",
@@ -538,6 +563,7 @@ namespace mo2::python {
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)
@@ -620,16 +646,20 @@ namespace mo2::python {
.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("extensionList", &IOrganizer::extensionList,
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, 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
@@ -773,12 +803,29 @@ namespace mo2::python {
.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)
@@ -830,37 +877,12 @@ namespace mo2::python {
add_modinterface_classes(m);
add_modrepository_classes(m);
py::class_<Setting>(m, "Setting")
.def(py::init([](const QString& name, const QString& description,
const QVariant& defaultValue) {
mo2::python::show_deprecation_warning(
"Setting(key, description, default)",
"Setting(key, description, default) is deprecated, use "
"Setting(name, title, description, default) instead.");
return Setting(name, description, defaultValue);
}),
"key"_a, "description"_a, "default_value"_a)
.def(py::init<const QString&, const QString&, const QString&,
const QVariant&>(),
"name"_a, "title"_a, "description"_a, "default_value"_a)
.def(py::init<const QString&, const QString&, const QString&,
const QString&, const QVariant&>(),
"name"_a, "title"_a, "description"_a, "group"_a, "default_value"_a)
.def_property_readonly("name", &Setting::name)
.def_property_readonly("title", &Setting::title)
.def_property_readonly("description", &Setting::description)
.def_property_readonly("group", &Setting::group)
.def_property_readonly("default_value", &Setting::defaultValue);
// deprecated alias
m.attr("PluginSetting") = m.attr("Setting");
py::class_<SettingGroup>(m, "SettingGroup")
.def(py::init<const QString&, const QString&, const QString&>(), "name"_a,
"title"_a, "description"_a)
.def_property_readonly("name", &SettingGroup::name)
.def_property_readonly("title", &SettingGroup::title)
.def_property_readonly("description", &SettingGroup::description);
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
@@ -906,7 +928,7 @@ namespace mo2::python {
// must be done BEFORE imodlist because there is a default argument to a
// IProfile* in the modlist class
py::class_<IProfile>(m, "IProfile")
py::class_<IProfile, std::shared_ptr<IProfile>>(m, "IProfile")
.def("name", &IProfile::name)
.def("absolutePath", &IProfile::absolutePath)
.def("localSavesEnabled", &IProfile::localSavesEnabled)
@@ -919,9 +941,9 @@ namespace mo2::python {
})
.def("absoluteIniFilePath", &IProfile::absoluteIniFilePath, "inifile"_a);
add_iextensionlist_classes(m);
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);
+9 -3
View File
@@ -89,11 +89,15 @@ namespace mo2::python {
}
bool lightPluginsAreSupported() override
{
PYBIND11_OVERRIDE_PURE(bool, GamePlugins, lightPluginsAreSupported, );
PYBIND11_OVERRIDE(bool, GamePlugins, lightPluginsAreSupported, );
}
bool mediumPluginsAreSupported() override
{
PYBIND11_OVERRIDE_PURE(bool, GamePlugins, mediumPluginsAreSupported, );
PYBIND11_OVERRIDE(bool, GamePlugins, mediumPluginsAreSupported, );
}
bool blueprintPluginsAreSupported() override
{
PYBIND11_OVERRIDE(bool, GamePlugins, blueprintPluginsAreSupported, );
}
};
@@ -259,7 +263,9 @@ namespace mo2::python {
.def("readPluginLists", &GamePlugins::readPluginLists, "plugin_list"_a)
.def("getLoadOrder", &GamePlugins::getLoadOrder)
.def("lightPluginsAreSupported", &GamePlugins::lightPluginsAreSupported)
.def("mediumPluginsAreSupported", &GamePlugins::mediumPluginsAreSupported);
.def("mediumPluginsAreSupported", &GamePlugins::mediumPluginsAreSupported)
.def("blueprintPluginsAreSupported",
&GamePlugins::blueprintPluginsAreSupported);
// LocalSavegames
+31 -6
View File
@@ -49,7 +49,7 @@ namespace mo2::detail {
return std::make_shared<PyFileTree>(parent, name, m_Callback);
}
bool doPopulate(std::shared_ptr<const IFileTree> parent,
bool doPopulate([[maybe_unused]] std::shared_ptr<const IFileTree> parent,
std::vector<std::shared_ptr<FileTreeEntry>>&) const override
{
return true;
@@ -83,7 +83,6 @@ namespace mo2::python {
void add_ifiletree_bindings(pybind11::module_& m)
{
// FileTreeEntry class:
auto fileTreeEntryClass =
py::class_<FileTreeEntry, std::shared_ptr<FileTreeEntry>>(m,
@@ -164,6 +163,13 @@ namespace mo2::python {
.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>(
@@ -175,10 +181,29 @@ namespace mo2::python {
iFileTreeClass.def("pathTo", &IFileTree::pathTo, py::arg("entry"),
py::arg("sep") = "\\");
// Note: walk() would probably be better as a generator in python, but
// it is likely impossible to construct from the C++ walk() method.
iFileTreeClass.def("walk", &IFileTree::walk, py::arg("callback"),
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,
+10 -4
View File
@@ -55,6 +55,7 @@ namespace mo2::python {
.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)
@@ -75,6 +76,7 @@ namespace mo2::python {
.def("iniFiles", &IPluginGame::iniFiles)
.def("DLCPlugins", &IPluginGame::DLCPlugins)
.def("CCPlugins", &IPluginGame::CCPlugins)
.def("blueprintPrefix", &IPluginGame::blueprintPrefix)
.def("loadOrderMechanism", &IPluginGame::loadOrderMechanism)
.def("sortMechanism", &IPluginGame::sortMechanism)
.def("nexusModOrganizerID", &IPluginGame::nexusModOrganizerID)
@@ -82,7 +84,8 @@ namespace mo2::python {
.def("looksValid", &IPluginGame::looksValid, "directory"_a)
.def("gameVersion", &IPluginGame::gameVersion)
.def("getLauncherName", &IPluginGame::getLauncherName)
.def("getSupportURL", &IPluginGame::getSupportURL);
.def("getSupportURL", &IPluginGame::getSupportURL)
.def("getModMappings", &IPluginGame::getModMappings);
}
// multiple installers
@@ -109,7 +112,7 @@ namespace mo2::python {
py::return_value_policy::reference);
py::class_<IPluginInstallerSimple, PyPluginInstallerSimple, IPluginInstaller,
std::unique_ptr<IPluginInstallerSimple, py::nodelete>>(
IPlugin, std::unique_ptr<IPluginInstallerSimple, py::nodelete>>(
m, "IPluginInstallerSimple", py::multiple_inheritance())
.def(py::init<>())
@@ -126,7 +129,7 @@ namespace mo2::python {
"name"_a, "tree"_a, "version"_a, "nexus_id"_a);
py::class_<IPluginInstallerCustom, PyPluginInstallerCustom, IPluginInstaller,
std::unique_ptr<IPluginInstallerCustom, py::nodelete>>(
IPlugin, std::unique_ptr<IPluginInstallerCustom, py::nodelete>>(
m, "IPluginInstallerCustom", py::multiple_inheritance())
.def(py::init<>())
.def("isArchiveSupported", &IPluginInstallerCustom::isArchiveSupported,
@@ -144,9 +147,12 @@ namespace mo2::python {
.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("settingGroups", &IPlugin::settingGroups)
.def("enabledByDefault", &IPlugin::enabledByDefault);
py::class_<IPyPlugin, PyPlugin, IPlugin,
+27 -8
View File
@@ -45,19 +45,25 @@ namespace mo2::python {
{
PYBIND11_OVERRIDE(QString, PluginBase, localizedName, );
}
std::vector<std::shared_ptr<const IPluginRequirement>>
requirements() const override
QString master() const override
{
PYBIND11_OVERRIDE(std::vector<std::shared_ptr<const IPluginRequirement>>,
PluginBase, requirements, );
PYBIND11_OVERRIDE(QString, PluginBase, master, );
}
QList<Setting> settings() const override
QString author() const override
{
PYBIND11_OVERRIDE_PURE(QList<Setting>, PluginBase, settings, );
PYBIND11_OVERRIDE_PURE(QString, PluginBase, author, );
}
QList<SettingGroup> settingGroups() const override
QString description() const override
{
PYBIND11_OVERRIDE(QList<SettingGroup>, PluginBase, settingGroups, );
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, );
}
};
@@ -391,6 +397,10 @@ namespace mo2::python {
{
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>;
@@ -473,6 +483,10 @@ namespace mo2::python {
{
PYBIND11_OVERRIDE(QStringList, IPluginGame, CCPlugins, );
}
QString blueprintPrefix() const override
{
PYBIND11_OVERRIDE(QString, IPluginGame, blueprintPrefix, );
}
LoadOrderMechanism loadOrderMechanism() const override
{
PYBIND11_OVERRIDE(LoadOrderMechanism, IPluginGame, loadOrderMechanism, );
@@ -505,6 +519,11 @@ namespace mo2::python {
{
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
+52 -9
View File
@@ -4,30 +4,73 @@
<context>
<name>ProxyPython</name>
<message>
<location filename="proxy/proxypython.cpp" line="170"/>
<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 &quot;no&quot;, 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.&lt;br&gt;While this is legal on NTFS drives, many applications do not handle it correctly.&lt;br&gt;Unfortunately MO depends on libraries that seem to fall into that group.&lt;br&gt;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="180"/>
<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="183"/>
<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="188"/>
<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>
<message>
<location filename="proxy/proxypython.cpp" line="191"/>
<source>no failure</source>
<translation type="unfinished"></translation>
</message>
</context>
<context>
<name>QObject</name>
+13 -15
View File
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.16)
find_package(mo2-uibase CONFIG REQUIRED)
set(PROXY_NAME "python")
set(PLUGIN_NAME "plugin_python")
add_library(proxy SHARED proxypython.cpp proxypython.h)
mo2_configure_plugin(proxy
@@ -16,23 +16,18 @@ mo2_configure_plugin(proxy
${CMAKE_CURRENT_SOURCE_DIR}/../pybind11-qt)
mo2_default_source_group()
target_link_libraries(proxy PRIVATE runner mo2::uibase)
set_target_properties(proxy PROPERTIES OUTPUT_NAME "python_proxy")
set_target_properties(proxy PROPERTIES OUTPUT_NAME ${PLUGIN_NAME})
mo2_install_plugin(proxy FOLDER)
set(PROXY_PYTHON_DIR ${MO2_INSTALL_BIN}/proxies/python)
set(PLUGIN_PYTHON_DIR bin/plugins/${PLUGIN_NAME})
# install runner and proxy
install(FILES $<TARGET_FILE:proxy> DESTINATION ${PROXY_PYTHON_DIR})
install(FILES $<TARGET_FILE:runner> DESTINATION ${PROXY_PYTHON_DIR}/dlls)
# install PDB
install(FILES $<TARGET_PDB_FILE:proxy> DESTINATION pdb)
# delay loading since the dll is not in the standard folder
# install runner
target_link_options(proxy PRIVATE "/DELAYLOAD:runner.dll")
install(FILES $<TARGET_FILE:runner> DESTINATION ${PLUGIN_PYTHON_DIR}/dlls)
# translations (custom location)
mo2_add_translations(proxy
TS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../plugin_python_en.ts
TS_FILE ${CMAKE_CURRENT_SOURCE_DIR}/../${PLUGIN_NAME}_en.ts
SOURCES
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../runner
@@ -40,14 +35,17 @@ mo2_add_translations(proxy
${CMAKE_CURRENT_SOURCE_DIR}/../pybind11-qt)
# install DLLs files needed
set(DLL_DIRS ${PROXY_PYTHON_DIR}/dlls)
set(DLL_DIRS ${PLUGIN_PYTHON_DIR}/dlls)
file(GLOB dlls_to_install
# ${PYTHON_BUILD_PATH}/libffi*.dll
${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})
# install Python .pyd files
set(PYLIB_DIR ${PROXY_PYTHON_DIR}/libs)
set(PYLIB_DIR ${PLUGIN_PYTHON_DIR}/libs)
file(GLOB libs_to_install ${Python_DLL_DIR}/*.pyd)
install(FILES ${libs_to_install} DESTINATION ${PYLIB_DIR})
+146 -50
View File
@@ -53,26 +53,58 @@ fs::path getPluginFolder()
return fs::path(path).parent_path();
}
ProxyPython::ProxyPython() : m_RunnerLib{nullptr}, m_Runner{nullptr} {}
bool ProxyPython::initialize(QString& errorMessage)
ProxyPython::ProxyPython()
: m_MOInfo{nullptr}, m_RunnerLib{nullptr}, m_Runner{nullptr},
m_LoadFailure(FailureType::NONE)
{
errorMessage = "";
}
bool ProxyPython::init(IOrganizer* moInfo)
{
m_MOInfo = moInfo;
if (m_MOInfo && !m_MOInfo->isPluginEnabled(this)) {
return false;
}
if (QCoreApplication::applicationDirPath().contains(';')) {
errorMessage = failureMessage(FailureType::SEMICOLON);
m_LoadFailure = FailureType::SEMICOLON;
return true;
}
const auto pluginFolder = getPluginFolder();
if (pluginFolder.empty()) {
DWORD error = ::GetLastError();
errorMessage = failureMessage(FailureType::DLL_NOT_FOUND);
DWORD error = ::GetLastError();
m_LoadFailure = FailureType::DLL_NOT_FOUND;
log::error("failed to resolve Python proxy directory ({}): {}", error,
qUtf8Printable(windowsErrorString(::GetLastError())));
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
@@ -81,8 +113,8 @@ bool ProxyPython::initialize(QString& errorMessage)
//
const auto dllPaths = pluginFolder / "dlls";
if (SetDllDirectoryW(dllPaths.c_str()) == 0) {
DWORD error = ::GetLastError();
errorMessage = failureMessage(FailureType::DLL_NOT_FOUND);
DWORD error = ::GetLastError();
m_LoadFailure = FailureType::DLL_NOT_FOUND;
log::error("failed to add python DLL directory ({}): {}", error,
qUtf8Printable(windowsErrorString(::GetLastError())));
return false;
@@ -96,16 +128,21 @@ bool ProxyPython::initialize(QString& errorMessage)
if (m_Runner) {
const auto libpath = pluginFolder / "libs";
const std::vector<fs::path> paths{libpath / "pythoncore.zip", libpath};
const std::vector<fs::path> paths{
libpath / "pythoncore.zip", libpath,
std::filesystem::path{IOrganizer::getPluginDataPath().toStdWString()}};
m_Runner->initialize(paths);
}
if (m_MOInfo) {
m_MOInfo->setPersistent(name(), "tryInit", false);
}
// reset DLL directory
SetDllDirectoryW(NULL);
if (!m_Runner || !m_Runner->isInitialized()) {
errorMessage = failureMessage(FailureType::INITIALIZATION);
return false;
m_LoadFailure = FailureType::INITIALIZATION;
}
else {
m_Runner->addDllSearchPath(pluginFolder / "dlls");
@@ -114,58 +151,110 @@ bool ProxyPython::initialize(QString& errorMessage)
return true;
}
QList<QList<QObject*>> ProxyPython::load(const PluginExtension& extension)
QString ProxyPython::name() const
{
if (!m_Runner) {
return {};
}
if (extension.autodetect()) {
log::debug("{}: automatic plugin detection is not supported for Python plugins",
extension.metadata().name());
return {};
}
m_ExtensionModules[&extension] = {};
QList<QList<QObject*>> plugins;
for (auto& [moduleName, modulePath] : extension.plugins()) {
m_ExtensionModules[&extension].push_back({moduleName, modulePath});
plugins.append(m_Runner->load(moduleName, modulePath));
}
return plugins;
return "Python Proxy";
}
void ProxyPython::unload(const PluginExtension& extension)
QString ProxyPython::localizedName() const
{
if (!m_Runner) {
return;
}
return tr("Python Proxy");
}
if (auto it = m_ExtensionModules.find(&extension); it != m_ExtensionModules.end()) {
for (auto& [moduleName, modulePath] : it->second) {
m_Runner->unload(moduleName, modulePath);
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();
if (info.isFile() && name.endsWith(".py")) {
result.append(name);
}
else if (info.isDir() && QDir(info.absoluteFilePath()).exists("__init__.py")) {
result.append(name);
}
m_ExtensionModules.erase(it);
}
return result;
}
void ProxyPython::unloadAll()
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) {
for (auto& [ext, modules] : m_ExtensionModules) {
for (auto& [moduleName, modulePath] : modules) {
m_Runner->unload(moduleName, modulePath);
}
}
return m_Runner->unload(identifier);
}
m_ExtensionModules.clear();
}
QString ProxyPython::failureMessage(FailureType key)
std::vector<unsigned int> ProxyPython::activeProblems() const
{
switch (key) {
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 "
@@ -188,6 +277,13 @@ QString ProxyPython::failureMessage(FailureType key)
return tr("The initialization of the Python plugin DLL failed, unfortunately "
"without any details.");
default:
return tr("no failure");
return tr("invalid problem key %1").arg(key);
}
}
bool ProxyPython::hasGuidedFix(unsigned int) const
{
return false;
}
void ProxyPython::startGuidedFix(unsigned int) const {}
+28 -17
View File
@@ -23,27 +23,45 @@ along with python proxy plugin. If not, see <http://www.gnu.org/licenses/>.
#include <map>
#include <memory>
#include <uibase/extensions/ipluginloader.h>
#include <uibase/iplugindiagnose.h>
#include <Windows.h>
#include <uibase/ipluginproxy.h>
#include <pythonrunner.h>
class ProxyPython : public MOBase::IPluginLoader {
class ProxyPython : public QObject,
public MOBase::IPluginProxy,
public MOBase::IPluginDiagnose {
Q_OBJECT
Q_INTERFACES(MOBase::IPluginLoader)
Q_INTERFACES(MOBase::IPlugin MOBase::IPluginProxy MOBase::IPluginDiagnose)
Q_PLUGIN_METADATA(IID "org.mo2.ProxyPython")
public:
ProxyPython();
bool initialize(QString& errorMessage) override;
QList<QList<QObject*>> load(const MOBase::PluginExtension& extension) override;
void unload(const MOBase::PluginExtension& extension) override;
void unloadAll() override;
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;
HMODULE m_RunnerLib;
std::unique_ptr<mo2::python::IPythonRunner> m_Runner;
enum class FailureType : unsigned int {
NONE = 0,
SEMICOLON = 1,
@@ -52,14 +70,7 @@ private:
INITIALIZATION = 4
};
static QString failureMessage(FailureType failureType);
private:
HMODULE m_RunnerLib;
std::unique_ptr<mo2::python::IPythonRunner> m_Runner;
std::unordered_map<const MOBase::PluginExtension*,
std::vector<std::pair<std::string, std::filesystem::path>>>
m_ExtensionModules;
FailureType m_LoadFailure;
};
#endif // PROXYPYTHON_H
+1 -48
View File
@@ -2,17 +2,12 @@
#include <type_traits>
#include <QColor>
#include <QIcon>
#include <QPixmap>
#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"
#include "pybind11_qt/pybind11_qt_objects.h"
namespace pybind11::detail {
@@ -86,18 +81,7 @@ namespace pybind11::detail {
2 * src.length(), nullptr, 0);
}
template <class T>
bool tryCast(QVariant& value, handle src, bool implicit)
{
type_caster<T> caster;
if (caster.load(src, implicit)) {
value = caster.value;
return true;
}
return false;
}
bool type_caster<QVariant>::load(handle src, bool implicit)
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())) {
@@ -135,15 +119,6 @@ namespace pybind11::detail {
value = src.cast<int>();
return true;
}
else if (PyFloat_Check(src.ptr())) {
value = src.cast<double>();
return true;
}
else if (tryCast<QColor>(value, src, implicit) ||
tryCast<QIcon>(value, src, implicit) ||
tryCast<QPixmap>(value, src, implicit)) {
return true;
}
else {
return false;
}
@@ -156,25 +131,13 @@ namespace pybind11::detail {
case QMetaType::UnknownType:
return Py_None;
case QMetaType::Int:
case QMetaType::Long:
return PyLong_FromLong(var.toInt());
case QMetaType::LongLong:
return PyLong_FromLongLong(var.toLongLong());
case QMetaType::UInt:
case QMetaType::ULong:
return PyLong_FromUnsignedLong(var.toUInt());
case QMetaType::ULongLong:
return PyLong_FromUnsignedLongLong(var.toULongLong());
case QMetaType::Float:
return PyFloat_FromDouble(var.toFloat());
case QMetaType::Double:
return PyFloat_FromDouble(var.toDouble());
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:
@@ -183,16 +146,6 @@ namespace pybind11::detail {
return type_caster<QVariantList>::cast(var.toList(), policy, parent);
case QMetaType::QVariantMap:
return type_caster<QVariantMap>::cast(var.toMap(), policy, parent);
case QMetaType::QColor:
return type_caster<QColor>::cast(var.value<QColor>(), policy, parent);
case QMetaType::QIcon:
return type_caster<QIcon>::cast(var.value<QIcon>(), policy, parent);
case QMetaType::QPixmap:
return type_caster<QPixmap>::cast(var.value<QPixmap>(), policy, parent);
default: {
PyErr_Format(PyExc_TypeError, "type unsupported: %d", var.userType());
throw pybind11::error_already_set();
+1
View File
@@ -2,6 +2,7 @@ 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
@@ -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
@@ -91,7 +91,7 @@ namespace mo2::python {
};
template <class R, class C, class... Args>
struct function_signature<R (C::*)(Args...)&> {
struct function_signature<R (C::*)(Args...) &> {
using type = R(C*, Args...);
};
+1 -2
View File
@@ -21,8 +21,7 @@ target_link_libraries(runner PUBLIC mo2::uibase PRIVATE pybind11::embed pybind11
target_include_directories(runner PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_compile_definitions(runner PRIVATE RUNNER_BUILD)
# proxy will install runner but we install the PDB
install(FILES $<TARGET_PDB_FILE:runner> DESTINATION pdb)
# proxy will install runner
# force runner to build mobase
add_dependencies(runner mobase)
+120 -68
View File
@@ -36,14 +36,24 @@ namespace mo2::python {
PythonRunner() = default;
~PythonRunner() = default;
QList<QList<QObject*>> load(std::string_view moduleName,
std::filesystem::path const& modulePath) override;
void unload(std::string_view moduleName,
std::filesystem::path const& modulePath) override;
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()
@@ -152,44 +162,72 @@ namespace mo2::python {
py::module_::import("os").attr("add_dll_directory")(absolute(dllPath));
}
QList<QList<QObject*>> PythonRunner::load(std::string_view name,
const std::filesystem::path& pythonModule)
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;
// `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 {
// some needed import
auto sys = py::module_::import("sys");
auto importlib_util = py::module_::import("importlib.util");
// dictionary that will contain createPlugin() or createPlugins().
py::dict moduleDict;
// check if the module is already loaded
py::dict modules = sys.attr("modules");
py::module_ pymodule;
if (modules.contains(name)) {
pymodule = modules[py::str(name)];
pymodule.reload();
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 {
// load the module
auto spec =
importlib_util.attr("spec_from_file_location")(name, pythonModule);
// Retrieve the module name:
QStringList parts = identifier.split("/");
std::string moduleName = ToString(parts.takeLast());
ensureFolderInPath(parts.join("/"));
if (Py_IsNone(spec.ptr())) {
MOBase::log::error("failed to load Python plugin '{}' from '{}'",
name, pythonModule);
return {};
// 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__");
}
pymodule = importlib_util.attr("module_from_spec")(spec);
sys.attr("modules")[py::str(name)] = pymodule;
spec.attr("loader").attr("exec_module")(pymodule);
}
py::dict moduleDict = pymodule.attr("__dict__");
if (py::len(moduleDict) == 0) {
MOBase::log::error("no plugins found in {}", pythonModule);
MOBase::log::error("No plugins found in {}.", identifier);
return {};
}
@@ -202,8 +240,8 @@ namespace mo2::python {
else if (moduleDict.contains("createPlugins")) {
py::object pyPlugins = moduleDict["createPlugins"]();
if (!py::isinstance<py::sequence>(pyPlugins)) {
MOBase::log::error("{}: createPlugins must return a sequence",
pythonModule);
MOBase::log::error(
"Plugin {}: createPlugins must return a sequence.", identifier);
}
else {
py::sequence pyList(pyPlugins);
@@ -214,26 +252,30 @@ namespace mo2::python {
}
}
else {
MOBase::log::error("{}: missing createPlugin(s) function",
pythonModule);
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 we have no plugins, there was an issue, and we already logged the
// problem:
if (plugins.empty()) {
return {};
return QList<QObject*>();
}
QList<QList<QObject*>> allInterfaceList;
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("{}: no plugin interface implemented.",
pythonModule);
MOBase::log::error("Plugin {}: no plugin interface implemented.",
identifier);
}
// Append the plugins to the main list:
@@ -243,47 +285,57 @@ namespace mo2::python {
return allInterfaceList;
}
catch (const py::error_already_set& ex) {
MOBase::log::error("failed to import plugin from {}", pythonModule);
MOBase::log::error("Failed to import plugin from {}.", identifier);
throw pyexcept::PythonError(ex);
}
}
void PythonRunner::unload(std::string_view moduleName,
std::filesystem::path const& modulePath)
void PythonRunner::unload(const QString& identifier)
{
py::gil_scoped_acquire lock;
auto it = m_PythonObjects.find(identifier);
if (it != m_PythonObjects.end()) {
// at this point, the identifier is the full path to the module.
QDir folder(modulePath);
py::gil_scoped_acquire lock;
// 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 (!identifier.endsWith(".py")) {
if (!folder.relativeFilePath(mpath).startsWith("..")) {
// if the path is under identifier, we need to unload it
log::debug("unloading module {} from {}",
keys[i].cast<std::string>(), mpath);
// At this point, the identifier is the full path to the module.
QDir folder(identifier);
PyDict_DelItem(modules.ptr(), keys[i].ptr());
// 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());
}
}
}
}
}
// for simple Python file - not really used anymore, but actually used in
// testing - we need to remove using the module name
//
py::str pyModuleName(moduleName);
if (modules.contains(pyModuleName)) {
PyDict_DelItem(modules.ptr(), pyModuleName.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);
}
}

Some files were not shown because too many files have changed in this diff Show More