From: Thomas Perl Date: Sun, 28 Jul 2013 01:58:42 +0000 (+0200) Subject: Cleanup Python integration module X-Git-Tag: tennix-1.2.1~10 X-Git-Url: https://repo.or.cz/w/tennix.git/commitdiff_plain/2b8dd9ee7485caca98b78b5dc58073b56554b168 Cleanup Python integration module --- diff --git a/src/archive.h b/src/archive.h index e2b5ab4..6e44c47 100644 --- a/src/archive.h +++ b/src/archive.h @@ -101,7 +101,7 @@ class TennixArchive { return items[current_item].length; } - int endOfFile() { + bool endOfFile() { return current_item >= header.items; } @@ -109,6 +109,10 @@ class TennixArchive { current_item++; } + void rewind() { + current_item = 0; + } + /* only for building/utility mode: */ void appendItem(char* filename, char* data, uint32_t length); void buildFile(char* filename); diff --git a/src/tennixpy.cc b/src/tennixpy.cc index 8aea008..29b4a5a 100644 --- a/src/tennixpy.cc +++ b/src/tennixpy.cc @@ -32,12 +32,12 @@ #include "archive.h" #include "tennixpy.h" -PyObject* tennixpy_register_bot(PyObject* self, PyObject* bot_class); -PyObject* tennixpy_get_ball_pos(PyObject* self, PyObject* gamestate); -PyObject* tennixpy_get_power(PyObject* self, PyObject* args); -PyObject* tennixpy_get_position(PyObject* self, PyObject* args); -PyObject* tennixpy_create_module(void); - +static void tennixpy_get_bot_name(InputDevice *device, char *dest, int maxlen); +static PyObject* tennixpy_register_bot(PyObject* self, PyObject* bot_class); +static PyObject* tennixpy_get_ball_pos(PyObject* self, PyObject* gamestate); +static PyObject* tennixpy_get_power(PyObject* self, PyObject* args); +static PyObject* tennixpy_get_position(PyObject* self, PyObject* args); +static PyObject* tennixpy_create_module(void); struct InputDevicePython { PyObject* py_bot_class; @@ -46,7 +46,7 @@ struct InputDevicePython { /* This saves our current Python thread state */ -PyThreadState* _py_save; +static PyThreadState *_py_save; static PyMethodDef TennixMethods[] = { {"register_bot", tennixpy_register_bot, METH_O, "Register a new bot"}, @@ -206,7 +206,6 @@ input_add_python_bot(PyObject* bot_class) pydevice->py_bot = NULL; device->user_data = (void*)pydevice; tennixpy_get_bot_name(device, device->name, INPUT_DEVICE_NAME_MAX); - fprintf(stderr, " %s", device->name); } PyObject* tennixpy_register_bot(PyObject* self, PyObject* bot_class) @@ -322,38 +321,26 @@ PyObject* tennixpy_get_position(PyObject* self, PyObject* args) void tennixpy_init(TennixArchive& tnxar) { PyObject* tennix_module; - /*PyObject* bot_module;*/ - /*FILE* fp;*/ char* data; - /*const char* argv[] = {""};*/ - /* Search for modules in CWD */ setenv("PYTHONPATH", ".", 1); PyEval_InitThreads(); Py_Initialize(); tennix_module = tennixpy_create_module(); - /*Py_Main(1, argv);*/ - /*bot_module = PyImport_ImportModule("defaultbot"); - if (bot_module == NULL) { - PyErr_Print(); - } - Py_DECREF(bot_module);*/ - if (tnxar.setItemFilename("defaultbot.py") != 0) { - fprintf(stderr, "Loading computer players:"); - data = tnxar.getItemBytes(); - if (PyRun_SimpleString(data) != 0) { - PyErr_Print(); - } else { - fprintf(stderr, ".\n"); + tnxar.rewind(); + while (!tnxar.endOfFile()) { + const char *filename = tnxar.getItemFilename(); + if (strcmp(".py", filename + strlen(filename) - 3) == 0) { + data = tnxar.getItemBytes(); + if (PyRun_SimpleString(data) != 0) { + PyErr_Print(); + } + free(data); } - free(data); + tnxar.next(); } - /*fp = fopen("defaultbot.py", "r"); - PyRun_SimpleFile(fp, "defaultbot.py"); - fclose(fp);*/ - Py_DECREF(tennix_module); _py_save = PyEval_SaveThread(); } diff --git a/src/tennixpy.h b/src/tennixpy.h index 2a5e463..9cc6751 100644 --- a/src/tennixpy.h +++ b/src/tennixpy.h @@ -32,15 +32,12 @@ #include "archive.h" #include "input.h" -void tennixpy_unregister_bot(InputDevice *device); -void tennixpy_get_bot_name(InputDevice *device, char *dest, int maxlen); - void tennixpy_create_bot(InputDevice *device, GameState *s, int player_id); void tennixpy_destroy_bot(InputDevice *device); -//float tennixpy_bot_get_axis(PyObject* bot, int axis); +void tennixpy_unregister_bot(InputDevice *device); + float tennixpy_bot_get_axis(InputDevice *device, int axis); -//char tennixpy_bot_get_key(PyObject* bot, int key); char tennixpy_bot_get_key(InputDevice *device, int key); void tennixpy_init(TennixArchive& tnxar);