5ab8db2986d4f780bdb0d171dad0a24d6406c9c6
[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 "PyObjectPlus.h"
37 #include "compile.h"
38 #include "eval.h"
39 #include <algorithm>
40
41
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45
46 // initialize static member variables
47 SCA_PythonController* SCA_PythonController::m_sCurrentController = NULL;
48
49
50 SCA_PythonController::SCA_PythonController(SCA_IObject* gameobj,
51                                                                                    PyTypeObject* T)
52         : SCA_IController(gameobj, T),
53         m_bytecode(NULL),
54         m_bModified(true),
55         m_pythondictionary(NULL)
56 {
57 }
58
59 /*
60 //debugging
61 CValue*         SCA_PythonController::AddRef()
62 {
63         //printf("AddRef refcount = %i\n",GetRefCount());
64         return CValue::AddRef();
65 }
66 int                     SCA_PythonController::Release()
67 {
68         //printf("Release refcount = %i\n",GetRefCount());
69         return CValue::Release();
70 }
71 */
72
73
74
75 SCA_PythonController::~SCA_PythonController()
76 {
77         if (m_bytecode)
78         {
79                 //
80                 //printf("released python byte script\n");
81                 Py_DECREF(m_bytecode);
82         }
83         
84         if (m_pythondictionary)
85         {
86                 // break any circular references in the dictionary
87                 PyDict_Clear(m_pythondictionary);
88                 Py_DECREF(m_pythondictionary);
89         }
90 }
91
92
93
94 CValue* SCA_PythonController::GetReplica()
95 {
96         SCA_PythonController* replica = new SCA_PythonController(*this);
97         // Copy the compiled bytecode if possible.
98         Py_XINCREF(replica->m_bytecode);
99         replica->m_bModified = replica->m_bytecode == NULL;
100         
101         // The replica->m_pythondictionary is stolen - replace with a copy.
102         if (m_pythondictionary)
103                 replica->m_pythondictionary = PyDict_Copy(m_pythondictionary);
104                 
105         /*
106         // The other option is to incref the replica->m_pythondictionary -
107         // the replica objects can then share data.
108         if (m_pythondictionary)
109                 Py_INCREF(replica->m_pythondictionary);
110         */
111         
112         // this will copy properties and so on...
113         CValue::AddDataToReplica(replica);
114
115         return replica;
116 }
117
118
119
120 void SCA_PythonController::SetScriptText(const STR_String& text)
121
122         m_scriptText = text;
123         m_bModified = true;
124 }
125
126
127
128 void SCA_PythonController::SetScriptName(const STR_String& name)
129 {
130         m_scriptName = name;
131 }
132
133
134
135 void SCA_PythonController::SetDictionary(PyObject*      pythondictionary)
136 {
137         if (m_pythondictionary)
138         {
139                 PyDict_Clear(m_pythondictionary);
140                 Py_DECREF(m_pythondictionary);
141         }
142         m_pythondictionary = PyDict_Copy(pythondictionary); /* new reference */
143 }
144
145 int SCA_PythonController::IsTriggered(class SCA_ISensor* sensor)
146 {
147         if (std::find(m_triggeredSensors.begin(), m_triggeredSensors.end(), sensor) != 
148                 m_triggeredSensors.end())
149                 return 1;
150         return 0;
151 }
152
153 #if 0
154 static const char* sPyGetCurrentController__doc__;
155 #endif
156
157
158 PyObject* SCA_PythonController::sPyGetCurrentController(PyObject* self)
159 {
160         m_sCurrentController->AddRef();
161         return m_sCurrentController;
162 }
163
164 #if 0
165 static const char* sPyAddActiveActuator__doc__;
166 #endif
167
168 PyObject* SCA_PythonController::sPyAddActiveActuator(
169           
170                 PyObject* self, 
171                 PyObject* args)
172 {
173         
174         PyObject* ob1;
175         int activate;
176         if (!PyArg_ParseTuple(args, "Oi", &ob1,&activate))
177                 return NULL;
178
179         // for safety, todo: only allow for registered actuators (pointertable)
180         // we don't want to crash gameengine/blender by python scripts
181         std::vector<SCA_IActuator*> lacts =  m_sCurrentController->GetLinkedActuators();
182         
183         std::vector<SCA_IActuator*>::iterator it;
184         bool found = false;
185         CValue* act = (CValue*)ob1;
186
187         for(it = lacts.begin(); it!= lacts.end(); it++) {
188                 if( static_cast<SCA_IActuator*>(act) == (*it) ) {
189                         found=true;
190                         break;
191                 }
192         }
193         if(found){
194                 CValue* boolval = new CBoolValue(activate!=0);
195                 m_sCurrentLogicManager->AddActiveActuator((SCA_IActuator*)act,boolval);
196                 boolval->Release();
197         }
198         Py_RETURN_NONE;
199 }
200
201
202 const char* SCA_PythonController::sPyGetCurrentController__doc__ = "getCurrentController()";
203 const char* SCA_PythonController::sPyAddActiveActuator__doc__= "addActiveActuator(actuator,bool)";
204 const char SCA_PythonController::GetActuators_doc[] = "getActuator";
205
206 PyTypeObject SCA_PythonController::Type = {
207         PyObject_HEAD_INIT(&PyType_Type)
208                 0,
209                 "SCA_PythonController",
210                 sizeof(SCA_PythonController),
211                 0,
212                 PyDestructor,
213                 0,
214                 __getattr,
215                 __setattr,
216                 0, //&MyPyCompare,
217                 __repr,
218                 0, //&cvalue_as_number,
219                 0,
220                 0,
221                 0,
222                 0
223 };
224
225 PyParentObject SCA_PythonController::Parents[] = {
226         &SCA_PythonController::Type,
227         &SCA_IController::Type,
228         &CValue::Type,
229         NULL
230 };
231 PyMethodDef SCA_PythonController::Methods[] = {
232         {"getActuators", (PyCFunction) SCA_PythonController::sPyGetActuators, METH_NOARGS, (PY_METHODCHAR)SCA_PythonController::GetActuators_doc},
233         {"getActuator", (PyCFunction) SCA_PythonController::sPyGetActuator, METH_O, (PY_METHODCHAR)SCA_PythonController::GetActuator_doc},
234         {"getSensors", (PyCFunction) SCA_PythonController::sPyGetSensors, METH_NOARGS, (PY_METHODCHAR)SCA_PythonController::GetSensors_doc},
235         {"getSensor", (PyCFunction) SCA_PythonController::sPyGetSensor, METH_O, (PY_METHODCHAR)SCA_PythonController::GetSensor_doc},
236         //Deprecated functions ------>
237         {"setScript", (PyCFunction) SCA_PythonController::sPySetScript, METH_O},
238         {"getScript", (PyCFunction) SCA_PythonController::sPyGetScript, METH_NOARGS},
239         {"getState", (PyCFunction) SCA_PythonController::sPyGetState, METH_NOARGS},
240         //<----- Deprecated
241         {NULL,NULL} //Sentinel
242 };
243
244
245 bool SCA_PythonController::Compile()
246 {
247         //printf("py script modified '%s'\n", m_scriptName.Ptr());
248         
249         // if a script already exists, decref it before replace the pointer to a new script
250         if (m_bytecode)
251         {
252                 Py_DECREF(m_bytecode);
253                 m_bytecode=NULL;
254         }
255         // recompile the scripttext into bytecode
256         m_bytecode = Py_CompileString(m_scriptText.Ptr(), m_scriptName.Ptr(), Py_file_input);
257         m_bModified=false;
258         
259         if (m_bytecode)
260         {
261                 
262                 return true;
263         }
264         else {
265                 // didn't compile, so instead of compile, complain
266                 // something is wrong, tell the user what went wrong
267                 printf("Python compile error from controller \"%s\": \n", GetName().Ptr());
268                 //PyRun_SimpleString(m_scriptText.Ptr());
269                 PyErr_Print();
270                 
271                 /* Added in 2.48a, the last_traceback can reference Objects for example, increasing
272                  * their user count. Not to mention holding references to wrapped data.
273                  * This is especially bad when the PyObject for the wrapped data is free'd, after blender 
274                  * has alredy dealocated the pointer */
275                 PySys_SetObject( (char *)"last_traceback", Py_None);
276                 PyErr_Clear(); /* just to be sure */
277                 
278                 return false;
279         }
280 }
281
282 void SCA_PythonController::Trigger(SCA_LogicManager* logicmgr)
283 {
284         m_sCurrentController = this;
285         m_sCurrentLogicManager = logicmgr;
286         
287         if (m_bModified)
288         {
289                 if (Compile()==false) // sets m_bModified to false
290                         return;
291         }
292         if (!m_bytecode) {
293                 return;
294         }
295                 
296         
297                 /*
298                  * This part here with excdict is a temporary patch
299                  * to avoid python/gameengine crashes when python
300                  * inadvertently holds references to game objects
301                  * in global variables.
302                  * 
303                  * The idea is always make a fresh dictionary, and
304                  * destroy it right after it is used to make sure
305                  * python won't hold any gameobject references.
306                  * 
307                  * Note that the PyDict_Clear _is_ necessary before
308                  * the Py_DECREF() because it is possible for the
309                  * variables inside the dictionary to hold references
310                  * to the dictionary (ie. generate a cycle), so we
311                  * break it by hand, then DECREF (which in this case
312                  * should always ensure excdict is cleared).
313                  */
314
315         PyObject *excdict= PyDict_Copy(m_pythondictionary);
316         PyObject* resultobj = PyEval_EvalCode((PyCodeObject*)m_bytecode,
317                 excdict, excdict);
318
319         if (resultobj)
320         {
321                 Py_DECREF(resultobj);
322         }
323         else
324         {
325                 // something is wrong, tell the user what went wrong
326                 printf("Python script error from controller \"%s\": \n", GetName().Ptr());
327                 PyErr_Print();
328                 
329                 /* Added in 2.48a, the last_traceback can reference Objects for example, increasing
330                  * their user count. Not to mention holding references to wrapped data.
331                  * This is especially bad when the PyObject for the wrapped data is free'd, after blender 
332                  * has alredy dealocated the pointer */
333                 PySys_SetObject( (char *)"last_traceback", Py_None);
334                 PyErr_Clear(); /* just to be sure */
335                 
336                 //PyRun_SimpleString(m_scriptText.Ptr());
337         }
338
339         // clear after PyErrPrint - seems it can be using
340         // something in this dictionary and crash?
341         PyDict_Clear(excdict);
342         Py_DECREF(excdict);
343         m_triggeredSensors.erase(m_triggeredSensors.begin(), m_triggeredSensors.end());
344         m_sCurrentController = NULL;
345 }
346
347
348
349 PyObject* SCA_PythonController::_getattr(const char *attr)
350 {
351         if (!strcmp(attr,"state")) {
352                 return PyInt_FromLong(m_statemask);
353         }
354         if (!strcmp(attr,"script")) {
355                 return PyString_FromString(m_scriptText);
356         }
357         _getattr_up(SCA_IController);
358 }
359
360 int SCA_PythonController::_setattr(const char *attr, PyObject *value)
361 {
362         if (!strcmp(attr,"state")) {
363                 PyErr_SetString(PyExc_AttributeError, "state is read only");
364                 return 1;
365         }
366         if (!strcmp(attr,"script")) {
367                 char *scriptArg = PyString_AsString(value);
368                 
369                 if (scriptArg==NULL) {
370                         PyErr_SetString(PyExc_TypeError, "expected a string (script name)");
371                         return -1;
372                 }
373         
374                 /* set scripttext sets m_bModified to true, 
375                         so next time the script is needed, a reparse into byte code is done */
376                 this->SetScriptText(scriptArg);
377                 
378                 return 1;
379         }
380         return SCA_IController::_setattr(attr, value);
381 }
382
383
384 PyObject* SCA_PythonController::PyGetActuators(PyObject* self)
385 {
386         PyObject* resultlist = PyList_New(m_linkedactuators.size());
387         for (unsigned int index=0;index<m_linkedactuators.size();index++)
388         {
389                 PyList_SET_ITEM(resultlist,index,m_linkedactuators[index]->AddRef());
390         }
391
392         return resultlist;
393 }
394
395 const char SCA_PythonController::GetSensor_doc[] = 
396 "GetSensor (char sensorname) return linked sensor that is named [sensorname]\n";
397 PyObject*
398 SCA_PythonController::PyGetSensor(PyObject* self, PyObject* value)
399 {
400
401         char *scriptArg = PyString_AsString(value);
402         if (scriptArg==NULL) {
403                 PyErr_SetString(PyExc_TypeError, "expected a string (sensor name)");
404                 return NULL;
405         }
406         
407         for (unsigned int index=0;index<m_linkedsensors.size();index++)
408         {
409                 SCA_ISensor* sensor = m_linkedsensors[index];
410                 STR_String realname = sensor->GetName();
411                 if (realname == scriptArg)
412                 {
413                         return sensor->AddRef();
414                 }
415         }
416         
417         char emsg[96];
418         PyOS_snprintf( emsg, sizeof( emsg ), "Unable to find requested sensor \"%s\"", scriptArg );
419         PyErr_SetString(PyExc_AttributeError, emsg);
420         return NULL;
421 }
422
423
424
425 const char SCA_PythonController::GetActuator_doc[] = 
426 "GetActuator (char sensorname) return linked actuator that is named [actuatorname]\n";
427 PyObject*
428 SCA_PythonController::PyGetActuator(PyObject* self, PyObject* value)
429 {
430
431         char *scriptArg = PyString_AsString(value);
432         if (scriptArg==NULL) {
433                 PyErr_SetString(PyExc_TypeError, "expected a string (actuator name)");
434                 return NULL;
435         }
436         
437         for (unsigned int index=0;index<m_linkedactuators.size();index++)
438         {
439                 SCA_IActuator* actua = m_linkedactuators[index];
440                 STR_String realname = actua->GetName();
441                 if (realname == scriptArg)
442                 {
443                         return actua->AddRef();
444                 }
445         }
446         
447         char emsg[96];
448         PyOS_snprintf( emsg, sizeof( emsg ), "Unable to find requested actuator \"%s\"", scriptArg );
449         PyErr_SetString(PyExc_AttributeError, emsg);
450         return NULL;
451 }
452
453
454 const char SCA_PythonController::GetSensors_doc[]   = "getSensors returns a list of all attached sensors";
455 PyObject*
456 SCA_PythonController::PyGetSensors(PyObject* self)
457 {
458         PyObject* resultlist = PyList_New(m_linkedsensors.size());
459         for (unsigned int index=0;index<m_linkedsensors.size();index++)
460         {
461                 PyList_SET_ITEM(resultlist,index,m_linkedsensors[index]->AddRef());
462         }
463         
464         return resultlist;
465 }
466
467 /* 1. getScript */
468 PyObject* SCA_PythonController::PyGetScript(PyObject* self)
469 {
470         ShowDeprecationWarning("getScript()", "the script property");
471         return PyString_FromString(m_scriptText);
472 }
473
474 /* 2. setScript */
475 PyObject* SCA_PythonController::PySetScript(PyObject* self, PyObject* value)
476 {
477         char *scriptArg = PyString_AsString(value);
478         
479         ShowDeprecationWarning("setScript()", "the script property");
480         
481         if (scriptArg==NULL) {
482                 PyErr_SetString(PyExc_TypeError, "expected a string (script name)");
483                 return NULL;
484         }
485         
486         /* set scripttext sets m_bModified to true, 
487                 so next time the script is needed, a reparse into byte code is done */
488
489         this->SetScriptText(scriptArg);
490         
491         Py_RETURN_NONE;
492 }
493
494 /* 1. getScript */
495 PyObject* SCA_PythonController::PyGetState(PyObject* self)
496 {
497         ShowDeprecationWarning("getState()", "the state property");
498         return PyInt_FromLong(m_statemask);
499 }
500
501 /* eof */