Merge of itasc branch. Project files, scons and cmake should be working. Makefile...
[blender.git] / source / gameengine / Ketsji / KX_GameActuator.cpp
index ada6e3e..71980a3 100644 (file)
@@ -3,15 +3,12 @@
 *
 * $Id$
 *
- * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
+ * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
  * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version. The Blender
- * Foundation also sells licenses for use in proprietary software under
- * the Blender License.  See http://www.blender.org/BL/ for information
- * about this.
+ * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -29,7 +26,7 @@
  *
  * Contributor(s): none yet.
  *
- * ***** END GPL/BL DUAL LICENSE BLOCK *****
+ * ***** END GPL LICENSE BLOCK *****
 */
 
 #include "SCA_IActuator.h"
@@ -37,6 +34,7 @@
 //#include <iostream>
 #include "KX_Scene.h"
 #include "KX_KetsjiEngine.h"
+#include "KX_PythonInit.h" /* for config load/saving */
 
 #ifdef HAVE_CONFIG_H
 #include <config.h>
@@ -51,9 +49,8 @@ KX_GameActuator::KX_GameActuator(SCA_IObject *gameobj,
                                                                   const STR_String& filename,
                                                                   const STR_String& loadinganimationname,
                                                                   KX_Scene* scene,
-                                                                  KX_KetsjiEngine* ketsjiengine,
-                                                                  PyTypeObject* T)
-                                                                  : SCA_IActuator(gameobj, T)
+                                                                  KX_KetsjiEngine* ketsjiengine)
+                                                                  : SCA_IActuator(gameobj, KX_ACT_GAME)
 {
        m_mode = mode;
        m_filename = filename;
@@ -75,8 +72,6 @@ CValue* KX_GameActuator::GetReplica()
 {
        KX_GameActuator* replica = new KX_GameActuator(*this);
        replica->ProcessReplica();
-       // this will copy properties and so on...
-       CValue::AddDataToReplica(replica);
        
        return replica;
 }
@@ -85,7 +80,7 @@ CValue* KX_GameActuator::GetReplica()
 
 bool KX_GameActuator::Update()
 {
-       bool result = false;    
+       // bool result = false;  /*unused*/
        bool bNegativeEvent = IsNegativeEvent();
        RemoveAllEvents();
 
@@ -128,6 +123,73 @@ bool KX_GameActuator::Update()
                        }
                        break;
                }
+       case KX_GAME_SAVECFG:
+               {
+                       if (m_ketsjiengine)
+                       {
+                               char mashal_path[512];
+                               char *marshal_buffer = NULL;
+                               unsigned int marshal_length;
+                               FILE *fp = NULL;
+                               
+                               pathGamePythonConfig(mashal_path);
+                               marshal_length = saveGamePythonConfig(&marshal_buffer);
+                               
+                               if (marshal_length && marshal_buffer) {
+                                       fp = fopen(mashal_path, "wb");
+                                       if (fp) {
+                                               if (fwrite(marshal_buffer, 1, marshal_length, fp) != marshal_length) {
+                                                       printf("Warning: could not write marshal data\n");
+                                               }
+                                               fclose(fp);
+                                       } else {
+                                               printf("Warning: could not open marshal file\n");
+                                       }
+                               } else {
+                                       printf("Warning: could not create marshal buffer\n");
+                               }
+                               if (marshal_buffer)
+                                       delete [] marshal_buffer;
+                       }
+                       break;
+               }
+       case KX_GAME_LOADCFG:
+               {
+                       if (m_ketsjiengine)
+                       {
+                               char mashal_path[512];
+                               char *marshal_buffer;
+                               int marshal_length;
+                               FILE *fp = NULL;
+                               int result;
+                               
+                               pathGamePythonConfig(mashal_path);
+                               
+                               fp = fopen(mashal_path, "rb");
+                               if (fp) {
+                                       // obtain file size:
+                                       fseek (fp , 0 , SEEK_END);
+                                       marshal_length = ftell(fp);
+                                       rewind(fp);
+                                       
+                                       marshal_buffer = (char*) malloc (sizeof(char)*marshal_length);
+                                       
+                                       result = fread (marshal_buffer, 1, marshal_length, fp);
+                                       
+                                       if (result == marshal_length) {
+                                               loadGamePythonConfig(marshal_buffer, marshal_length);
+                                       } else {
+                                               printf("warning: could not read all of '%s'\n", mashal_path);
+                                       }
+                                       
+                                       free(marshal_buffer);
+                                       fclose(fp);
+                               } else {
+                                       printf("warning: could not open '%s'\n", mashal_path);
+                               }
+                       }
+                       break;
+               }
        default:
                ; /* do nothing? this is an internal error !!! */
        }
@@ -145,77 +207,34 @@ bool KX_GameActuator::Update()
 
 /* Integration hooks ------------------------------------------------------- */
 PyTypeObject KX_GameActuator::Type = {
-       PyObject_HEAD_INIT(&PyType_Type)
-               0,
-               "KX_GameActuator",
-               sizeof(KX_GameActuator),
-               0,
-               PyDestructor,
-               0,
-               __getattr,
-               __setattr,
-               0, //&MyPyCompare,
-               __repr,
-               0, //&cvalue_as_number,
-               0,
-               0,
-               0,
-               0
+       PyVarObject_HEAD_INIT(NULL, 0)
+       "KX_GameActuator",
+       sizeof(PyObjectPlus_Proxy),
+       0,
+       py_base_dealloc,
+       0,
+       0,
+       0,
+       0,
+       py_base_repr,
+       0,0,0,0,0,0,0,0,0,
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+       0,0,0,0,0,0,0,
+       Methods,
+       0,
+       0,
+       &SCA_IActuator::Type,
+       0,0,0,0,0,0,
+       py_base_new
 };
 
-
-
-PyParentObject KX_GameActuator::Parents[] =
-{
-       &KX_GameActuator::Type,
-               &SCA_IActuator::Type,
-               &SCA_ILogicBrick::Type,
-               &CValue::Type,
-               NULL
-};
-
-
-
 PyMethodDef KX_GameActuator::Methods[] =
 {
-       {"getFile",     (PyCFunction) KX_GameActuator::sPyGetFile, METH_VARARGS, GetFile_doc},
-       {"setFile", (PyCFunction) KX_GameActuator::sPySetFile, METH_VARARGS, SetFile_doc},
        {NULL,NULL} //Sentinel
 };
 
-/* getFile */
-char KX_GameActuator::GetFile_doc[] = 
-"getFile()\n"
-"get the name of the file to start.\n";
-PyObject* KX_GameActuator::PyGetFile(PyObject* self, PyObject* args, PyObject* kwds)
-{      
-       return PyString_FromString(m_filename);
-}
-
-/* setFile */
-char KX_GameActuator::SetFile_doc[] =
-"setFile(name)\n"
-"set the name of the file to start.\n";
-PyObject* KX_GameActuator::PySetFile(PyObject* self, PyObject* args, PyObject* kwds)
-{
-       char* new_file;
-       
-       if (!PyArg_ParseTuple(args, "s", &new_file))
-       {
-               return NULL;
-       }
-       
-       m_filename = STR_String(new_file);
-
-       Py_Return;
-
-}
-       
-
-
-PyObject* KX_GameActuator::_getattr(const STR_String& attr)
-{
-       _getattr_up(SCA_IActuator);
-}
-
-
+PyAttributeDef KX_GameActuator::Attributes[] = {
+       KX_PYATTRIBUTE_STRING_RW("fileName",0,100,false,KX_GameActuator,m_filename),
+       KX_PYATTRIBUTE_INT_RW("mode", KX_GAME_NODEF+1, KX_GAME_MAX-1, true, KX_GameActuator, m_mode),
+       { NULL }        //Sentinel
+};