Skip to content

Commit

Permalink
Testing something.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kraust committed Jun 9, 2024
1 parent d99097d commit 735a6e0
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
__pycache__
build
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cmake_minimum_required(VERSION 3.29)
set(NAME "OSCR")
project(${NAME})

add_subdirectory(shim)
2 changes: 1 addition & 1 deletion OSCRUI/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions shim/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
91 changes: 91 additions & 0 deletions shim/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#include <exception>
#include <iostream>

#include <Python.h>
#include <QApplication>
#include <QMainWindow>

// 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;
}

0 comments on commit 735a6e0

Please sign in to comment.