From 735a6e04f2c74cf061c2212628689d052f6100e6 Mon Sep 17 00:00:00 2001 From: Kraust Date: Sat, 8 Jun 2024 22:14:40 -0400 Subject: [PATCH] Testing something. --- .gitignore | 1 + CMakeLists.txt | 5 +++ OSCRUI/callbacks.py | 2 +- shim/CMakeLists.txt | 8 ++++ shim/main.cpp | 91 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 CMakeLists.txt create mode 100644 shim/CMakeLists.txt create mode 100644 shim/main.cpp diff --git a/.gitignore b/.gitignore index bee8a64..ce8c7f3 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ __pycache__ +build diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..94704ea --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.29) +set(NAME "OSCR") +project(${NAME}) + +add_subdirectory(shim) diff --git a/OSCRUI/callbacks.py b/OSCRUI/callbacks.py index bcbd741..9bcbb73 100644 --- a/OSCRUI/callbacks.py +++ b/OSCRUI/callbacks.py @@ -37,7 +37,7 @@ def save_combat(self, combat_num: int): filename = combat.map if combat.difficulty is not None and combat.difficulty != '': filename += ' ' + combat.difficulty - filename += f' {combat.start_time.strftime('%Y-%m-%d %H.%M')}.log' + filename += f' {combat.start_time.strftime("%Y-%m-%d %H.%M")}.log' base_dir = f'{os.path.dirname(self.entry.text())}/{filename}' if not base_dir: base_dir = self.app_dir diff --git a/shim/CMakeLists.txt b/shim/CMakeLists.txt new file mode 100644 index 0000000..c360e7c --- /dev/null +++ b/shim/CMakeLists.txt @@ -0,0 +1,8 @@ +find_package(Python COMPONENTS Interpreter Development) +find_package(Qt6 REQUIRED COMPONENTS Core Widgets) + +qt_add_executable(${NAME} main.cpp) + +target_link_libraries(${NAME} PUBLIC Python::Python) +target_link_libraries(${NAME} PUBLIC Qt6::Core) +target_link_libraries(${NAME} PUBLIC Qt6::Widgets) diff --git a/shim/main.cpp b/shim/main.cpp new file mode 100644 index 0000000..ea12cc7 --- /dev/null +++ b/shim/main.cpp @@ -0,0 +1,91 @@ +#include +#include + +#include +#include +#include + +// nix-shell -p cmake gdb qt6.full + +class PythonQt { +public: + PythonQt() { + const char *fname = "main.py"; + PyObject *obj = Py_BuildValue("s", fname); + FILE *fp = _Py_fopen_obj(obj, "rb"); + if (NULL == fp) { + throw std::runtime_error("Failed to open main.py"); + } + + int ret = PyRun_SimpleFile(fp, fname); + fclose(fp); + Py_Finalize(); + if (ret) { + PyErr_Print(); + throw std::runtime_error("Failed to run main.py"); + } + } +}; + +static int init_python(int argc, char **argv) { + const char *executable = "venv/bin/python"; + const char *home = "venv"; + size_t size; + PyConfig config; + PyStatus status; + + PyConfig_InitIsolatedConfig(&config); + + status = PyConfig_SetBytesString(&config, &config.executable, executable); + if (PyStatus_Exception(status)) { + throw std::runtime_error("Failed to set executable venv"); + } + + status = PyConfig_SetBytesString(&config, &config.home, home); + if (PyStatus_Exception(status)) { + throw std::runtime_error("Failed to set executable venv"); + } + + Py_InitializeFromConfig(&config); + PyConfig_Clear(&config); + return 0; +} + +static int init_qt(int argc, char **argv) { + QApplication app(argc, argv); + QMainWindow win; + win.setObjectName("OSCR"); + + PythonQt code; + app.exec(); + + return 0; +} + +static int run(int argc, char **argv) { + int ret = 0; + + ret = init_python(argc, argv); + if (ret) { + throw std::runtime_error("Failed to initialize python"); + } + + ret = init_qt(argc, argv); + if (ret) { + throw std::runtime_error("Failed to initialize qt"); + } + + return 0; +} + +int main(int argc, char **argv) { + int ret = 0; + try { + ret = run(argc, argv); + } catch (std::exception &e) { + std::cout << e.what() << std::endl; + } catch (...) { + std::cout << "Failed to parse exception" << std::endl; + } + return ret; +}