PythonEngine.cpp 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "PythonEngine.h"
  2. #include <pybind11/embed.h>
  3. #include <filesystem>
  4. #include <iostream>
  5. #include "../Engine.h"
  6. #include <SDL2/SDL_log.h>
  7. namespace py = pybind11;
  8. PythonEngine& PythonEngine::Get()
  9. {
  10. static PythonEngine instance;
  11. return instance;
  12. }
  13. void PythonEngine::Init()
  14. {
  15. if (initialized_)
  16. {
  17. SDL_Log("[Python] Already initialized");
  18. return;
  19. }
  20. py::initialize_interpreter();
  21. try
  22. {
  23. py::module_::import("sys").attr("path").attr("append")("assets/scripts");
  24. SDL_Log("[Python] scripts/ path added");
  25. }
  26. catch (const py::error_already_set& e)
  27. {
  28. SDL_Log("[Python] Error setting sys.path: %s", e.what());
  29. }
  30. // Release GIL immidiately so game loop can run freely
  31. PyEval_SaveThread();
  32. initialized_ = true;
  33. SDL_Log("[Python] Embedded interpreter ready (managed by scoped_interpreter)");
  34. }
  35. void PythonEngine::Shutdown()
  36. {
  37. if (initialized_) { return; }
  38. PyEval_RestoreThread(PyGILState_GetThisThreadState());
  39. py::finalize_interpreter();
  40. initialized_ = false;
  41. }
  42. void PythonEngine::Exec(const std::string& code)
  43. {
  44. py::gil_scoped_acquire gil;
  45. try
  46. {
  47. py::exec(code);
  48. }
  49. catch (const py::error_already_set& e)
  50. {
  51. SDL_Log("[Python] Error: %s", e.what());
  52. }
  53. catch (const std::exception& e)
  54. {
  55. SDL_Log("[Python] Error: %s", e.what());
  56. }
  57. }