From c37b8042ebc8f1d2c59786df498e5fd8117c1446 Mon Sep 17 00:00:00 2001 From: Igor Karatayev Date: Mon, 2 Jan 2023 23:55:16 +0300 Subject: [PATCH] Fixed loading of start_script.py - it will be loaded as fast as possible - original hook was not wokring, now it try until console.startScript is available --- libs/external-file-loader/src/mod_manager.cc | 2 +- libs/python35/src/python.cc | 39 +++++++++++++++----- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/libs/external-file-loader/src/mod_manager.cc b/libs/external-file-loader/src/mod_manager.cc index 7d5a7da..597d9d7 100644 --- a/libs/external-file-loader/src/mod_manager.cc +++ b/libs/external-file-loader/src/mod_manager.cc @@ -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, diff --git a/libs/python35/src/python.cc b/libs/python35/src/python.cc index 29b1911..672898e 100644 --- a/libs/python35/src/python.cc +++ b/libs/python35/src/python.cc @@ -7,38 +7,55 @@ #include 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); @@ -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);