|
8 | 8 | * |
9 | 9 | * Dice! QQ Dice Robot for TRPG |
10 | 10 | * Copyright (C) 2018-2021 w4123溯洄 |
11 | | - * Copyright (C) 2019-2024 String.Empty |
| 11 | + * Copyright (C) 2019-2025 String.Empty |
12 | 12 | * |
13 | 13 | * This program is free software: you can redistribute it and/or modify it under the terms |
14 | 14 | * of the GNU Affero General Public License as published by the Free Software Foundation, |
@@ -58,7 +58,8 @@ static string py_to_string(PyObject* o) { |
58 | 58 | static string py_to_native_string(PyObject* o) { |
59 | 59 | auto t{ Py_TYPE(o) }; |
60 | 60 | #ifdef _WIN32 |
61 | | - return t == &PyUnicode_Type ? UtoGBK(PyUnicode_AsUnicode(o)) : empty; |
| 61 | + Py_ssize_t l = 0; |
| 62 | + return t == &PyUnicode_Type ? UtoGBK(PyUnicode_AsWideString(o, l)) : empty; |
62 | 63 | #else |
63 | 64 | return t == &PyUnicode_Type ? PyUnicode_AsUTF8(o) : empty; |
64 | 65 | #endif |
@@ -1158,43 +1159,55 @@ PyMODINIT_FUNC PyInit_DiceMaid(){ |
1158 | 1159 | return mod; |
1159 | 1160 | } |
1160 | 1161 | PyGlobal::PyGlobal() { |
1161 | | - PyStatus status; |
| 1162 | + if (Py_IsInitialized())return; |
| 1163 | + PyStatus status; |
| 1164 | + PyPreConfig preconfig; |
| 1165 | + PyPreConfig_InitIsolatedConfig(&preconfig); |
| 1166 | + preconfig.utf8_mode = 1; |
| 1167 | + //preconfig.legacy_windows_fs_encoding = 1; |
| 1168 | + if (PyStatus_Exception(status = Py_PreInitialize(&preconfig))) { |
| 1169 | + console.log("python预初始化失败:" + string(status.err_msg), 0b1); |
| 1170 | + return; |
| 1171 | + } |
1162 | 1172 | PyConfig config; |
1163 | | - PyConfig_InitPythonConfig(&config); |
| 1173 | + PyConfig_InitIsolatedConfig(&config); |
| 1174 | + config._init_main = 0; |
1164 | 1175 | if (std::filesystem::path dirPy{ dirExe / "bin" }; |
1165 | 1176 | std::filesystem::exists(dirPy / "python3.dll") |
1166 | 1177 | || std::filesystem::exists(dirPy / "python3.so") |
1167 | 1178 | || std::filesystem::exists(dirPy = dirExe / "python") |
1168 | | - || std::filesystem::exists(dirPy = dirExe / "python311") |
1169 | | - || std::filesystem::exists(dirPy = dirExe / "py311") |
| 1179 | + || std::filesystem::exists(dirPy = dirExe / "python312") |
| 1180 | + || std::filesystem::exists(dirPy = dirExe / "py312") |
1170 | 1181 | || std::filesystem::exists((dirPy = dirExe) / "python3.dll")) { |
1171 | 1182 | //Py_SetPythonHome(dirPy.wstring().c_str()); |
1172 | 1183 | PyConfig_SetString(&config, &config.home, dirPy.wstring().c_str()); |
1173 | 1184 | //Py_SetProgramName(L"DiceMaid"); |
1174 | 1185 | PyConfig_SetString(&config, &config.program_name, L"DiceMaid"); |
1175 | | - //Py_SetPath((dirPy / "python311.zip").wstring().c_str()); |
1176 | | - PyWideStringList_Append(&config.module_search_paths, (dirPy / "python311.zip").wstring().c_str()); |
| 1186 | + PyWideStringList_Append(&config.module_search_paths, (dirPy / "python312.zip").wstring().c_str()); |
1177 | 1187 | } |
1178 | | - status = PyConfig_Read(&config); |
1179 | 1188 | try { |
1180 | 1189 | static auto import_dice = PyImport_AppendInittab(DiceModuleName, PyInit_DiceMaid); |
1181 | 1190 | if (import_dice) { |
1182 | 1191 | console.log("预载dicemaid模块失败!", 0b1000); |
1183 | 1192 | } |
1184 | | - PyWideStringList_Append(&config.module_search_paths, ((DiceDir / "plugin").wstring() + L"/").c_str()); |
1185 | | - PyWideStringList_Append(&config.module_search_paths, ((dirExe / "Diceki" / "py").wstring() + L"/").c_str()); |
| 1193 | + PyWideStringList_Append(&config.module_search_paths, ((DiceDir / "plugin").wstring()).c_str()); |
| 1194 | + PyWideStringList_Append(&config.module_search_paths, ((dirExe / "Diceki" / "py").wstring()).c_str()); |
1186 | 1195 | config.module_search_paths_set = 1; |
1187 | | - if (!Py_IsInitialized()){//Py_Initialize(); |
1188 | | - status = Py_InitializeFromConfig(&config); |
| 1196 | + if (!PyStatus_Exception(status = PyConfig_Read(&config))) { |
| 1197 | + if (PyStatus_Exception(status = Py_InitializeFromConfig(&config))) |
| 1198 | + console.log("python初始化失败:" + string(status.err_msg), 0b1); |
| 1199 | + else console.log("Python.Initialized", 0); |
1189 | 1200 | PyRun_SimpleString("import sys"); |
1190 | | - PyRun_SimpleString("from dicemaid import *"); |
| 1201 | + //PyRun_SimpleString("from dicemaid import *"); |
| 1202 | + status = _Py_InitializeMain(); |
| 1203 | + } |
| 1204 | + else { |
| 1205 | + console.log("PyConfig_Read error:" + string(status.err_msg), 0b1); |
1191 | 1206 | } |
1192 | 1207 | } |
1193 | 1208 | catch (std::exception& e) { |
1194 | 1209 | console.log("python初始化失败:" + string(e.what()), 0b1); |
1195 | 1210 | } |
1196 | | - if (PyStatus_Exception(status))console.log("python初始化失败:" + string(status.err_msg), 0b1); |
1197 | | - else console.log("Python.Initialized", 0); |
1198 | 1211 | PyConfig_Clear(&config); |
1199 | 1212 | } |
1200 | 1213 | dict<std::pair<PyObject*, std::filesystem::file_time_type>> Py_FileScripts; |
|
0 commit comments