save and load configuration actuator, (option in game actuator menu)

saves a marshal'd GameLogic.globalDict to the blendfile path with the blend extension replaced with bgeconf

Use this in YoFrankie to save keyboard layout and graphics quality settings.
This commit is contained in:
Campbell Barton
2008-09-12 02:15:16 +00:00
parent 75841bc97f
commit 0dea748e01
8 changed files with 158 additions and 48 deletions

View File

@@ -98,7 +98,6 @@ extern "C"
#include "GHOST_IEventConsumer.h"
#include "GHOST_IWindow.h"
#include "GHOST_Rect.h"
#include "marshal.h"
static void frameTimerProc(GHOST_ITimerTask* task, GHOST_TUns64 time);
@@ -685,15 +684,7 @@ bool GPG_Application::startEngine(void)
initMathutils();
/* Restore the dict */
if (m_pyGlobalDictString) {
PyObject* pyGlobalDict = PyMarshal_ReadObjectFromString(m_pyGlobalDictString, m_pyGlobalDictString_Length);
if (pyGlobalDict) {
PyDict_SetItemString(PyModule_GetDict(gameLogic), "globalDict", pyGlobalDict); // Same as importing the module.
} else {
PyErr_Clear();
printf("Error could not marshall string\n");
}
}
loadGamePythonConfig(m_pyGlobalDictString, m_pyGlobalDictString_Length);
m_sceneconverter->ConvertScene(
startscenename,
@@ -731,36 +722,12 @@ bool GPG_Application::startEngine(void)
void GPG_Application::stopEngine()
{
// get the python dict and convert to a string for future use
{
SetPyGlobalDictMarshal(NULL, 0);
PyObject* gameLogic = PyImport_ImportModule("GameLogic");
if (gameLogic) {
PyObject* pyGlobalDict = PyDict_GetItemString(PyModule_GetDict(gameLogic), "globalDict"); // Same as importing the module
if (pyGlobalDict) {
#ifdef Py_MARSHAL_VERSION
PyObject* pyGlobalDictMarshal = PyMarshal_WriteObjectToString( pyGlobalDict, 2); // Py_MARSHAL_VERSION == 2 as of Py2.5
#else
PyObject* pyGlobalDictMarshal = PyMarshal_WriteObjectToString( pyGlobalDict );
#endif
if (pyGlobalDictMarshal) {
m_pyGlobalDictString_Length = PyString_Size(pyGlobalDictMarshal);
// for testing only
// PyObject_Print(pyGlobalDictMarshal, stderr, 0);
m_pyGlobalDictString = static_cast<char *> (malloc(m_pyGlobalDictString_Length));
memcpy(m_pyGlobalDictString, PyString_AsString(pyGlobalDictMarshal), m_pyGlobalDictString_Length);
} else {
printf("Error, GameLogic.globalDict could not be marshal'd\n");
}
Py_DECREF(gameLogic);
} else {
printf("Error, GameLogic.globalDict was removed\n");
}
} else {
printf("Error, GameLogic failed to import GameLogic.globalDict will be lost\n");
}
}
char *marshal_buffer;
m_pyGlobalDictString_Length = saveGamePythonConfig(&marshal_buffer);
if (m_pyGlobalDictString_Length) {
m_pyGlobalDictString = static_cast<char *> (malloc(m_pyGlobalDictString_Length));
memcpy(m_pyGlobalDictString, marshal_buffer, m_pyGlobalDictString_Length);
}
// when exiting the mainloop
exitGamePythonScripting();