Merged 15170:15635 from trunk (no conflicts or even merges)
[blender.git] / source / gameengine / GameLogic / SCA_PythonController.cpp
1 /**
2  * Execute Python scripts
3  *
4  * $Id$
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23  * All rights reserved.
24  *
25  * The Original Code is: all of this file.
26  *
27  * Contributor(s): none yet.
28  *
29  * ***** END GPL LICENSE BLOCK *****
30  */
31
32 #include "SCA_PythonController.h"
33 #include "SCA_LogicManager.h"
34 #include "SCA_ISensor.h"
35 #include "SCA_IActuator.h"
36 #include "compile.h"
37 #include "eval.h"
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 // initialize static member variables
44 SCA_PythonController* SCA_PythonController::m_sCurrentController = NULL;
45
46
47 SCA_PythonController::SCA_PythonController(SCA_IObject* gameobj,
48                                                                                    PyTypeObject* T)
49         : SCA_IController(gameobj, T),
50         m_bytecode(NULL),
51         m_bModified(true),
52         m_pythondictionary(NULL)
53 {
54 }
55
56 /*
57 //debugging
58 CValue*         SCA_PythonController::AddRef()
59 {
60         //printf("AddRef refcount = %i\n",GetRefCount());
61         return CValue::AddRef();
62 }
63 int                     SCA_PythonController::Release()
64 {
65         //printf("Release refcount = %i\n",GetRefCount());
66         return CValue::Release();
67 }
68 */
69
70
71
72 SCA_PythonController::~SCA_PythonController()
73 {
74         if (m_bytecode)
75         {
76                 //
77                 //printf("released python byte script\n");
78                 Py_DECREF(m_bytecode);
79         }
80         
81         if (m_pythondictionary)
82         {
83                 // break any circular references in the dictionary
84                 PyDict_Clear(m_pythondictionary);
85                 Py_DECREF(m_pythondictionary);
86         }
87 }
88
89
90
91 CValue* SCA_PythonController::GetReplica()
92 {
93         SCA_PythonController* replica = new SCA_PythonController(*this);
94         // Copy the compiled bytecode if possible.
95         Py_XINCREF(replica->m_bytecode);
96         replica->m_bModified = replica->m_bytecode == NULL;
97         
98         // The replica->m_pythondictionary is stolen - replace with a copy.
99         if (m_pythondictionary)
100                 replica->m_pythondictionary = PyDict_Copy(m_pythondictionary);
101                 
102         /*
103         // The other option is to incref the replica->m_pythondictionary -
104         // the replica objects can then share data.
105         if (m_pythondictionary)
106                 Py_INCREF(replica->m_pythondictionary);
107         */
108         
109         // this will copy properties and so on...
110         CValue::AddDataToReplica(replica);
111
112         return replica;
113 }
114
115
116
117 void SCA_PythonController::SetScriptText(const STR_String& text)
118
119         m_scriptText = text;
120         m_bModified = true;
121 }
122
123
124
125 void SCA_PythonController::SetScriptName(const STR_String& name)
126 {
127         m_scriptName = name;
128 }
129
130
131
132 void SCA_PythonController::SetDictionary(PyObject*      pythondictionary)
133 {
134         if (m_pythondictionary)
135         {
136                 PyDict_Clear(m_pythondictionary);
137                 Py_DECREF(m_pythondictionary);
138         }
139         m_pythondictionary = PyDict_Copy(pythondictionary); /* new reference */
140 }
141
142 #if 0
143 static char* sPyGetCurrentController__doc__;
144 #endif
145
146
147 PyObject* SCA_PythonController::sPyGetCurrentController(PyObject* self)
148 {
149         m_sCurrentController->AddRef();
150         return m_sCurrentController;
151 }
152
153 #if 0
154 static char* sPyAddActiveActuator__doc__;
155 #endif
156
157 PyObject* SCA_PythonController::sPyAddActiveActuator(
158           
159                 PyObject* self, 
160                 PyObject* args)
161 {
162         
163         PyObject* ob1;
164         int activate;
165         if (!PyArg_ParseTuple(args, "Oi", &ob1,&activate))
166                 return NULL;
167
168         // for safety, todo: only allow for registered actuators (pointertable)
169         // we don't want to crash gameengine/blender by python scripts
170         std::vector<SCA_IActuator*> lacts =  m_sCurrentController->GetLinkedActuators();
171         
172         std::vector<SCA_IActuator*>::iterator it;
173         bool found = false;
174         CValue* act = (CValue*)ob1;
175
176         for(it = lacts.begin(); it!= lacts.end(); it++) {
177                 if( static_cast<SCA_IActuator*>(act) == (*it) ) {
178                         found=true;
179                         break;
180                 }
181         }
182         if(found){
183                 CValue* boolval = new CBoolValue(activate!=0);
184                 m_sCurrentLogicManager->AddActiveActuator((SCA_IActuator*)act,boolval);
185                 boolval->Release();
186         }
187         Py_RETURN_NONE;
188 }
189
190
191 char* SCA_PythonController::sPyGetCurrentController__doc__ = "getCurrentController()";
192 char* SCA_PythonController::sPyAddActiveActuator__doc__= "addActiveActuator(actuator,bool)";
193 char SCA_PythonController::GetActuators_doc[] = "getActuator";
194
195 PyTypeObject SCA_PythonController::Type = {
196         PyObject_HEAD_INIT(&PyType_Type)
197                 0,
198                 "SCA_PythonController",
199                 sizeof(SCA_PythonController),
200                 0,
201                 PyDestructor,
202                 0,
203                 __getattr,
204                 __setattr,
205                 0, //&MyPyCompare,
206                 __repr,
207                 0, //&cvalue_as_number,
208                 0,
209                 0,
210                 0,
211                 0
212 };
213
214 PyParentObject SCA_PythonController::Parents[] = {
215         &SCA_PythonController::Type,
216         &SCA_IController::Type,
217         &CValue::Type,
218         NULL
219 };
220 PyMethodDef SCA_PythonController::Methods[] = {
221         {"getActuators", (PyCFunction) SCA_PythonController::sPyGetActuators, METH_NOARGS, SCA_PythonController::GetActuators_doc},
222         {"getActuator", (PyCFunction) SCA_PythonController::sPyGetActuator, METH_O, SCA_PythonController::GetActuator_doc},
223         {"getSensors", (PyCFunction) SCA_PythonController::sPyGetSensors, METH_NOARGS, SCA_PythonController::GetSensors_doc},
224         {"getSensor", (PyCFunction) SCA_PythonController::sPyGetSensor, METH_O, SCA_PythonController::GetSensor_doc},
225         {"getScript", (PyCFunction) SCA_PythonController::sPyGetScript, METH_NOARGS},
226         {"setScript", (PyCFunction) SCA_PythonController::sPySetScript, METH_O},
227         {"getState", (PyCFunction) SCA_PythonController::sPyGetState, METH_NOARGS},
228         {NULL,NULL} //Sentinel
229 };
230
231
232 void SCA_PythonController::Trigger(SCA_LogicManager* logicmgr)
233 {
234         m_sCurrentController = this;
235         m_sCurrentLogicManager = logicmgr;
236         
237         if (m_bModified)
238         {
239                 // if a script already exists, decref it before replace the pointer to a new script
240                 if (m_bytecode)
241                 {
242                         Py_DECREF(m_bytecode);
243                         m_bytecode=NULL;
244                 }
245                 // recompile the scripttext into bytecode
246                 m_bytecode = Py_CompileString(m_scriptText.Ptr(), m_scriptName.Ptr(), Py_file_input);
247                 if (!m_bytecode)
248                 {
249                         // didn't compile, so instead of compile, complain
250                         // something is wrong, tell the user what went wrong
251                         printf("PYTHON SCRIPT ERROR:\n");
252                         //PyRun_SimpleString(m_scriptText.Ptr());
253                         PyErr_Print();
254                         return;
255                 }
256                 m_bModified=false;
257         }
258         
259                 /*
260                  * This part here with excdict is a temporary patch
261                  * to avoid python/gameengine crashes when python
262                  * inadvertently holds references to game objects
263                  * in global variables.
264                  * 
265                  * The idea is always make a fresh dictionary, and
266                  * destroy it right after it is used to make sure
267                  * python won't hold any gameobject references.
268                  * 
269                  * Note that the PyDict_Clear _is_ necessary before
270                  * the Py_DECREF() because it is possible for the
271                  * variables inside the dictionary to hold references
272                  * to the dictionary (ie. generate a cycle), so we
273                  * break it by hand, then DECREF (which in this case
274                  * should always ensure excdict is cleared).
275                  */
276 /*      PyObject *excdict= myPyDict_Copy(m_pythondictionary);
277         struct _object* resultobj = PyEval_EvalCode((PyCodeObject*)m_bytecode,
278                 excdict, 
279                 excdict
280                 );
281         PyDict_Clear(excdict);
282         Py_DECREF(excdict);*/
283
284
285 #if 1
286         PyObject *excdict= PyDict_Copy(m_pythondictionary);
287         PyObject* resultobj = PyEval_EvalCode((PyCodeObject*)m_bytecode,
288                 excdict, 
289                 excdict
290                 );
291         PyDict_Clear(excdict);
292         Py_DECREF(excdict);
293 #else
294
295         PyObject* resultobj = PyEval_EvalCode((PyCodeObject*)m_bytecode,
296                 m_pythondictionary, 
297                 m_pythondictionary
298                 );
299
300 #endif
301
302         if (resultobj)
303         {
304                 Py_DECREF(resultobj);
305         } else
306         {
307                 // something is wrong, tell the user what went wrong
308                 printf("PYTHON SCRIPT ERROR:\n");
309                 PyErr_Print();
310                 //PyRun_SimpleString(m_scriptText.Ptr());
311         }
312
313         m_sCurrentController = NULL;
314 }
315
316
317
318 PyObject* SCA_PythonController::_getattr(const STR_String& attr)
319 {
320         _getattr_up(SCA_IController);
321 }
322
323
324
325 PyObject* SCA_PythonController::PyGetActuators(PyObject* self)
326 {
327         PyObject* resultlist = PyList_New(m_linkedactuators.size());
328         for (unsigned int index=0;index<m_linkedactuators.size();index++)
329         {
330                 PyList_SET_ITEM(resultlist,index,m_linkedactuators[index]->AddRef());
331         }
332
333         return resultlist;
334 }
335
336 char SCA_PythonController::GetSensor_doc[] = 
337 "GetSensor (char sensorname) return linked sensor that is named [sensorname]\n";
338 PyObject*
339 SCA_PythonController::PyGetSensor(PyObject* self, PyObject* value)
340 {
341
342         char *scriptArg = PyString_AsString(value);
343         if (scriptArg==NULL) {
344                 PyErr_SetString(PyExc_TypeError, "expected a string (sensor name)");
345                 return NULL;
346         }
347         
348         for (unsigned int index=0;index<m_linkedsensors.size();index++)
349         {
350                 SCA_ISensor* sensor = m_linkedsensors[index];
351                 STR_String realname = sensor->GetName();
352                 if (realname == scriptArg)
353                 {
354                         return sensor->AddRef();
355                 }
356         }
357         
358         char emsg[96];
359         PyOS_snprintf( emsg, sizeof( emsg ), "Unable to find requested sensor \"%s\"", scriptArg );
360         PyErr_SetString(PyExc_AttributeError, emsg);
361         return NULL;
362 }
363
364
365
366 char SCA_PythonController::GetActuator_doc[] = 
367 "GetActuator (char sensorname) return linked actuator that is named [actuatorname]\n";
368 PyObject*
369 SCA_PythonController::PyGetActuator(PyObject* self, PyObject* value)
370 {
371
372         char *scriptArg = PyString_AsString(value);
373         if (scriptArg==NULL) {
374                 PyErr_SetString(PyExc_TypeError, "expected a string (actuator name)");
375                 return NULL;
376         }
377         
378         for (unsigned int index=0;index<m_linkedactuators.size();index++)
379         {
380                 SCA_IActuator* actua = m_linkedactuators[index];
381                 STR_String realname = actua->GetName();
382                 if (realname == scriptArg)
383                 {
384                         return actua->AddRef();
385                 }
386         }
387         
388         char emsg[96];
389         PyOS_snprintf( emsg, sizeof( emsg ), "Unable to find requested actuator \"%s\"", scriptArg );
390         PyErr_SetString(PyExc_AttributeError, emsg);
391         return NULL;
392 }
393
394
395 char SCA_PythonController::GetSensors_doc[]   = "getSensors returns a list of all attached sensors";
396 PyObject*
397 SCA_PythonController::PyGetSensors(PyObject* self)
398 {
399         PyObject* resultlist = PyList_New(m_linkedsensors.size());
400         for (unsigned int index=0;index<m_linkedsensors.size();index++)
401         {
402                 PyList_SET_ITEM(resultlist,index,m_linkedsensors[index]->AddRef());
403         }
404         
405         return resultlist;
406 }
407
408 /* 1. getScript */
409 PyObject* SCA_PythonController::PyGetScript(PyObject* self)
410 {
411         return PyString_FromString(m_scriptText);
412 }
413
414 /* 2. setScript */
415 PyObject* SCA_PythonController::PySetScript(PyObject* self, PyObject* value)
416 {
417         char *scriptArg = PyString_AsString(value);
418         if (scriptArg==NULL) {
419                 PyErr_SetString(PyExc_TypeError, "expected a string (script name)");
420                 return NULL;
421         }
422         
423         /* set scripttext sets m_bModified to true, 
424                 so next time the script is needed, a reparse into byte code is done */
425
426         this->SetScriptText(scriptArg);
427         
428         Py_RETURN_NONE;
429 }
430
431 /* 1. getScript */
432 PyObject* SCA_PythonController::PyGetState(PyObject* self)
433 {
434         return PyInt_FromLong(m_statemask);
435 }
436
437 /* eof */