mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-07-22 18:39:18 +02:00
master branch unification: cross-platform python interpreter
This commit is contained in:
3
EU07.cpp
3
EU07.cpp
@@ -459,7 +459,6 @@ int main(int argc, char *argv[])
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
TPythonInterpreter::killInstance();
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
Console::Off(); // wyłączenie konsoli (komunikacji zwrotnej)
|
Console::Off(); // wyłączenie konsoli (komunikacji zwrotnej)
|
||||||
SafeDelete( pConsole );
|
SafeDelete( pConsole );
|
||||||
@@ -469,5 +468,7 @@ int main(int argc, char *argv[])
|
|||||||
glfwDestroyWindow(window);
|
glfwDestroyWindow(window);
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
|
|
||||||
|
TPythonInterpreter::killInstance();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -567,13 +567,6 @@ global_settings::ConfigParse(cParser &Parser) {
|
|||||||
UITextColor = UITextColor / 255.0f;
|
UITextColor = UITextColor / 255.0f;
|
||||||
UITextColor.a = 1.0f;
|
UITextColor.a = 1.0f;
|
||||||
}
|
}
|
||||||
else if (token == "pyscreenrendererpriority")
|
|
||||||
{
|
|
||||||
// priority of python screen renderer
|
|
||||||
Parser.getTokens();
|
|
||||||
Parser >> token;
|
|
||||||
TPythonInterpreter::getInstance()->setScreenRendererPriority(token.c_str());
|
|
||||||
}
|
|
||||||
else if( token == "input.gamepad" ) {
|
else if( token == "input.gamepad" ) {
|
||||||
// czy grupować eventy o tych samych nazwach
|
// czy grupować eventy o tych samych nazwach
|
||||||
Parser.getTokens();
|
Parser.getTokens();
|
||||||
|
|||||||
81
PyInt.cpp
81
PyInt.cpp
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
#include "renderer.h"
|
#include "renderer.h"
|
||||||
#include "model3d.h"
|
#include "Model3d.h"
|
||||||
#include "Train.h"
|
#include "Train.h"
|
||||||
#include "Logs.h"
|
#include "Logs.h"
|
||||||
|
|
||||||
@@ -11,20 +11,30 @@ TPythonInterpreter *TPythonInterpreter::_instance = NULL;
|
|||||||
|
|
||||||
//#define _PY_INT_MORE_LOG
|
//#define _PY_INT_MORE_LOG
|
||||||
|
|
||||||
|
#ifdef __GNUC__
|
||||||
|
#pragma GCC diagnostic ignored "-Wwrite-strings"
|
||||||
|
#endif
|
||||||
|
|
||||||
TPythonInterpreter::TPythonInterpreter()
|
TPythonInterpreter::TPythonInterpreter()
|
||||||
{
|
{
|
||||||
WriteLog("Loading Python ...");
|
WriteLog("Loading Python ...");
|
||||||
|
#ifdef _WIN32
|
||||||
if (sizeof(void*) == 8)
|
if (sizeof(void*) == 8)
|
||||||
Py_SetPythonHome("python64");
|
Py_SetPythonHome("python64");
|
||||||
else
|
else
|
||||||
Py_SetPythonHome("python");
|
Py_SetPythonHome("python");
|
||||||
|
#elif __linux__
|
||||||
|
if (sizeof(void*) == 8)
|
||||||
|
Py_SetPythonHome("linuxpython64");
|
||||||
|
else
|
||||||
|
Py_SetPythonHome("linuxpython");
|
||||||
|
#endif
|
||||||
Py_Initialize();
|
Py_Initialize();
|
||||||
_main = PyImport_ImportModule("__main__");
|
_main = PyImport_ImportModule("__main__");
|
||||||
if (_main == NULL)
|
if (_main == NULL)
|
||||||
{
|
{
|
||||||
WriteLog("Cannot import Python module __main__");
|
WriteLog("Cannot import Python module __main__");
|
||||||
}
|
}
|
||||||
_screenRendererPriority = THREAD_PRIORITY_NORMAL; // domyslny priorytet normalny
|
|
||||||
PyObject *cStringModule = PyImport_ImportModule("cStringIO");
|
PyObject *cStringModule = PyImport_ImportModule("cStringIO");
|
||||||
_stdErr = NULL;
|
_stdErr = NULL;
|
||||||
if (cStringModule == NULL)
|
if (cStringModule == NULL)
|
||||||
@@ -111,7 +121,7 @@ FILE *TPythonInterpreter::_getFile( std::string const &lookupPath, std::string c
|
|||||||
#endif // _PY_INT_MORE_LOG
|
#endif // _PY_INT_MORE_LOG
|
||||||
if( nullptr != file ) { return file; }
|
if( nullptr != file ) { return file; }
|
||||||
}
|
}
|
||||||
std::string sourcefilepath = "python\\local\\" + className + ".py";
|
std::string sourcefilepath = "python/local/" + className + ".py";
|
||||||
FILE *file = fopen( sourcefilepath.c_str(), "r" );
|
FILE *file = fopen( sourcefilepath.c_str(), "r" );
|
||||||
#ifdef _PY_INT_MORE_LOG
|
#ifdef _PY_INT_MORE_LOG
|
||||||
WriteLog( sourceFilePath );
|
WriteLog( sourceFilePath );
|
||||||
@@ -136,7 +146,7 @@ FILE *TPythonInterpreter::_getFile( std::string const &lookupPath, std::string c
|
|||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
char *basePath = "python\\local\\";
|
char *basePath = "python/local/";
|
||||||
sourceFilePath = (char *)calloc(strlen(basePath) + strlen(className) + 4, sizeof(char));
|
sourceFilePath = (char *)calloc(strlen(basePath) + strlen(className) + 4, sizeof(char));
|
||||||
strcat(sourceFilePath, basePath);
|
strcat(sourceFilePath, basePath);
|
||||||
strcat(sourceFilePath, className);
|
strcat(sourceFilePath, className);
|
||||||
@@ -233,38 +243,6 @@ PyObject *TPythonInterpreter::newClass(std::string const &className, PyObject *a
|
|||||||
return object;
|
return object;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TPythonInterpreter::setScreenRendererPriority(const char *priority)
|
|
||||||
{
|
|
||||||
if (strncmp(priority, "normal", 6) == 0)
|
|
||||||
{
|
|
||||||
_screenRendererPriority = THREAD_PRIORITY_NORMAL;
|
|
||||||
//#ifdef _PY_INT_MORE_LOG
|
|
||||||
WriteLog("Python screen renderer priority: Normal");
|
|
||||||
//#endif // _PY_INT_MORE_LOG
|
|
||||||
}
|
|
||||||
else if (strncmp(priority, "lower", 5) == 0)
|
|
||||||
{
|
|
||||||
_screenRendererPriority = THREAD_PRIORITY_BELOW_NORMAL;
|
|
||||||
//#ifdef _PY_INT_MORE_LOG
|
|
||||||
WriteLog("Python screen renderer priority: Lower");
|
|
||||||
//#endif // _PY_INT_MORE_LOG
|
|
||||||
}
|
|
||||||
else if (strncmp(priority, "lowest", 6) == 0)
|
|
||||||
{
|
|
||||||
_screenRendererPriority = THREAD_PRIORITY_LOWEST;
|
|
||||||
//#ifdef _PY_INT_MORE_LOG
|
|
||||||
WriteLog("Python screen renderer priority: Lowest");
|
|
||||||
//#endif // _PY_INT_MORE_LOG
|
|
||||||
}
|
|
||||||
else if (strncmp(priority, "idle", 4) == 0)
|
|
||||||
{
|
|
||||||
_screenRendererPriority = THREAD_PRIORITY_IDLE;
|
|
||||||
//#ifdef _PY_INT_MORE_LOG
|
|
||||||
WriteLog("Python screen renderer priority: Idle");
|
|
||||||
//#endif // _PY_INT_MORE_LOG
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
TPythonScreenRenderer::TPythonScreenRenderer(int textureId, PyObject *renderer)
|
TPythonScreenRenderer::TPythonScreenRenderer(int textureId, PyObject *renderer)
|
||||||
{
|
{
|
||||||
_textureId = textureId;
|
_textureId = textureId;
|
||||||
@@ -379,6 +357,10 @@ void TPythonScreenRenderer::render(PyObject *trainState)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef __GNUC__
|
||||||
|
#pragma GCC diagnostic pop
|
||||||
|
#endif
|
||||||
|
|
||||||
TPythonScreenRenderer::~TPythonScreenRenderer()
|
TPythonScreenRenderer::~TPythonScreenRenderer()
|
||||||
{
|
{
|
||||||
#ifdef _PY_INT_MORE_LOG
|
#ifdef _PY_INT_MORE_LOG
|
||||||
@@ -419,8 +401,9 @@ void TPythonScreens::reset(void *train)
|
|||||||
if (_thread != NULL)
|
if (_thread != NULL)
|
||||||
{
|
{
|
||||||
// WriteLog("Awaiting python thread to end");
|
// WriteLog("Awaiting python thread to end");
|
||||||
WaitForSingleObject(_thread, INFINITE);
|
_thread->join();
|
||||||
_thread = NULL;
|
delete _thread;
|
||||||
|
_thread = nullptr;
|
||||||
}
|
}
|
||||||
_terminationFlag = false;
|
_terminationFlag = false;
|
||||||
_cleanupReadyFlag = false;
|
_cleanupReadyFlag = false;
|
||||||
@@ -496,6 +479,7 @@ void TPythonScreens::update()
|
|||||||
void TPythonScreens::setLookupPath(std::string const &path)
|
void TPythonScreens::setLookupPath(std::string const &path)
|
||||||
{
|
{
|
||||||
_lookupPath = path;
|
_lookupPath = path;
|
||||||
|
std::replace(_lookupPath.begin(), _lookupPath.end(), '\\', '/');
|
||||||
}
|
}
|
||||||
|
|
||||||
TPythonScreens::TPythonScreens()
|
TPythonScreens::TPythonScreens()
|
||||||
@@ -553,7 +537,11 @@ void TPythonScreens::run()
|
|||||||
_renderReadyFlag = true;
|
_renderReadyFlag = true;
|
||||||
while (!_cleanupReadyFlag && !_terminationFlag)
|
while (!_cleanupReadyFlag && !_terminationFlag)
|
||||||
{
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
Sleep(100);
|
Sleep(100);
|
||||||
|
#elif __linux__
|
||||||
|
usleep(100*1000);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
if (_terminationFlag)
|
if (_terminationFlag)
|
||||||
{
|
{
|
||||||
@@ -568,33 +556,20 @@ void TPythonScreens::finish()
|
|||||||
_thread = NULL;
|
_thread = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
DWORD WINAPI ScreenRendererThread(LPVOID lpParam)
|
void ScreenRendererThread(TPythonScreens* renderer)
|
||||||
{
|
{
|
||||||
TPythonScreens *renderer = (TPythonScreens *)lpParam;
|
|
||||||
renderer->run();
|
renderer->run();
|
||||||
renderer->finish();
|
renderer->finish();
|
||||||
#ifdef _PY_INT_MORE_LOG
|
#ifdef _PY_INT_MORE_LOG
|
||||||
WriteLog("Python Screen Renderer Thread Ends");
|
WriteLog("Python Screen Renderer Thread Ends");
|
||||||
#endif // _PY_INT_MORE_LOG
|
#endif // _PY_INT_MORE_LOG
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void TPythonScreens::start()
|
void TPythonScreens::start()
|
||||||
{
|
{
|
||||||
if (_screens.size() > 0)
|
if (_screens.size() > 0)
|
||||||
{
|
{
|
||||||
_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ScreenRendererThread, this,
|
_thread = new std::thread(ScreenRendererThread, this);
|
||||||
CREATE_SUSPENDED, reinterpret_cast<LPDWORD>(&_threadId));
|
|
||||||
if (_thread != NULL)
|
|
||||||
{
|
|
||||||
SetThreadPriority(_thread,
|
|
||||||
TPythonInterpreter::getInstance()->getScreenRendererPriotity());
|
|
||||||
if (ResumeThread(_thread) != (DWORD)-1)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
WriteLog("Python Screen Renderer Thread Did Not Start");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
21
PyInt.h
21
PyInt.h
@@ -1,9 +1,13 @@
|
|||||||
#ifndef PyIntH
|
#ifndef PyIntH
|
||||||
#define PyIntH
|
#define PyIntH
|
||||||
|
|
||||||
#include <vector>
|
#ifdef _POSIX_C_SOURCE
|
||||||
#include <set>
|
#undef _POSIX_C_SOURCE
|
||||||
#include <string>
|
#endif
|
||||||
|
|
||||||
|
#ifdef _XOPEN_SOURCE
|
||||||
|
#undef _XOPEN_SOURCE
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
#undef _DEBUG // bez tego macra Py_DECREF powoduja problemy przy linkowaniu
|
#undef _DEBUG // bez tego macra Py_DECREF powoduja problemy przy linkowaniu
|
||||||
@@ -12,7 +16,7 @@
|
|||||||
#else
|
#else
|
||||||
#include "Python.h"
|
#include "Python.h"
|
||||||
#endif
|
#endif
|
||||||
#include "classes.h"
|
#include "Classes.h"
|
||||||
|
|
||||||
#define PyGetFloat(param) PyFloat_FromDouble(param >= 0 ? param : -param)
|
#define PyGetFloat(param) PyFloat_FromDouble(param >= 0 ? param : -param)
|
||||||
#define PyGetFloatS(param) PyFloat_FromDouble(param)
|
#define PyGetFloatS(param) PyFloat_FromDouble(param)
|
||||||
@@ -35,7 +39,6 @@ class TPythonInterpreter
|
|||||||
TPythonInterpreter();
|
TPythonInterpreter();
|
||||||
~TPythonInterpreter() {}
|
~TPythonInterpreter() {}
|
||||||
static TPythonInterpreter *_instance;
|
static TPythonInterpreter *_instance;
|
||||||
int _screenRendererPriority = 0;
|
|
||||||
// std::set<const char *, ltstr> _classes;
|
// std::set<const char *, ltstr> _classes;
|
||||||
std::set<std::string> _classes;
|
std::set<std::string> _classes;
|
||||||
PyObject *_main;
|
PyObject *_main;
|
||||||
@@ -52,11 +55,6 @@ class TPythonInterpreter
|
|||||||
*/ bool loadClassFile( std::string const &lookupPath, std::string const &className );
|
*/ bool loadClassFile( std::string const &lookupPath, std::string const &className );
|
||||||
PyObject *newClass( std::string const &className );
|
PyObject *newClass( std::string const &className );
|
||||||
PyObject *newClass( std::string const &className, PyObject *argsTuple );
|
PyObject *newClass( std::string const &className, PyObject *argsTuple );
|
||||||
int getScreenRendererPriotity()
|
|
||||||
{
|
|
||||||
return _screenRendererPriority;
|
|
||||||
};
|
|
||||||
void setScreenRendererPriority(const char *priority);
|
|
||||||
void handleError();
|
void handleError();
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -83,8 +81,7 @@ class TPythonScreens
|
|||||||
bool _cleanupReadyFlag;
|
bool _cleanupReadyFlag;
|
||||||
bool _renderReadyFlag;
|
bool _renderReadyFlag;
|
||||||
bool _terminationFlag;
|
bool _terminationFlag;
|
||||||
void *_thread;
|
std::thread *_thread;
|
||||||
unsigned int _threadId;
|
|
||||||
std::vector<TPythonScreenRenderer *> _screens;
|
std::vector<TPythonScreenRenderer *> _screens;
|
||||||
std::string _lookupPath;
|
std::string _lookupPath;
|
||||||
void *_train;
|
void *_train;
|
||||||
|
|||||||
Reference in New Issue
Block a user