-
Notifications
You must be signed in to change notification settings - Fork 10
/
proxypython.cpp
290 lines (243 loc) · 8.84 KB
/
proxypython.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
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 <Windows.h>
#include <QCoreApplication>
#include <QDirIterator>
#include <QMessageBox>
#include <QWidget>
#include <QtPlugin>
#include <log.h>
#include <utility.h>
#include <versioninfo.h>
namespace fs = std::filesystem;
using namespace MOBase;
// retrieve the path to the folder containing the proxy DLL
fs::path getPluginFolder()
{
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();
}
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();
if (pluginFolder.empty()) {
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
// will look for DLLs in it, this is required to find the Python and libffi DLL, but
// also the runner DLL
//
const auto dllPaths = pluginFolder / "dlls";
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;
}
// 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 = pluginFolder / "libs";
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()) {
m_LoadFailure = FailureType::INITIALIZATION;
}
else {
m_Runner->addDllSearchPath(pluginFolder / "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();
if (info.isFile() && name.endsWith(".py")) {
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 {}