Skip to content
This repository was archived by the owner on Aug 4, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/external-file-loader/src/mod_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ void ModManager::CollectPatchableFiles()
std::string start_script = "console.startScript('mods\\"
+ fs::relative(file_path, mods_directory).string()
+ "')";
spdlog::info("Loading ptyhon script {}", start_script);
spdlog::info("Loading python script {}", start_script);
python_scripts_.emplace_back(start_script);
} else {
auto hFile = CreateFileW(file_path.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL,
Expand Down
39 changes: 29 additions & 10 deletions libs/python35/src/python.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,55 @@
#include <string>

static bool scripts_done = false;
void RunModScripts()
{
if (scripts_done)
return;

// Check if it is possible to execute console.startScript command
scripts_done = PyRun_SimpleString("hasattr(console, 'startScript') and callable(getattr(console, 'startScript'))") == 0;
if (!scripts_done) return;

const auto& python_scripts = ModManager::instance().GetPythonScripts();
spdlog::info("Loading python script");
for (const auto& str : python_scripts) {
spdlog::info("Python: {}", str);
int err = PyRun_SimpleString(str.c_str());
if (err < 0) spdlog::info("... failed", str);
}
}

extern "C" void Py_Initialize_S()
{
spdlog::info("Python initialized");
Py_Initialize();

scripts_done = false;
}

extern "C" PyObject* PyRun_StringFlags_S(const char* str, int start, PyObject* globals,
PyObject* locals, PyCompilerFlags* flags)
{
auto* result = PyRun_StringFlags(str, start, globals, locals, flags);
if (!scripts_done) {
scripts_done = true;
const auto& python_scripts = ModManager::instance().GetPythonScripts();
for (const auto& str : python_scripts) {
spdlog::info("Python: {}", str);
PyRun_StringFlags(str.c_str(), start, globals, locals, flags);
}
}
return result;
RunModScripts();
return PyRun_StringFlags(str, start, globals, locals, flags);
}

extern "C" PyObject* PyObject_CallObject_S(PyObject* callable, PyObject* args)
{
RunModScripts();
return PyObject_CallObject(callable, args);
}

extern "C" PyObject* PyObject_Call_S(PyObject* callable, PyObject* args, PyObject* kwargs)
{
RunModScripts();
return PyObject_Call(callable, args, kwargs);
}

extern "C" PyObject* PyEval_CallFunction_S(PyObject* callable, const char* format, ...)
{
RunModScripts();

va_list list;
va_start(list, format);
auto* py_args = Py_VaBuildValue(format, list);
Expand All @@ -50,6 +67,8 @@ extern "C" PyObject* PyEval_CallFunction_S(PyObject* callable, const char* forma

extern "C" PyObject* PyObject_CallFunction_S(PyObject* callable, const char* format, ...)
{
RunModScripts();

va_list list;
va_start(list, format);
auto* py_args = Py_VaBuildValue(format, list);
Expand Down