Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2009-04-15 14:57:28 +0400
committerCampbell Barton <ideasman42@gmail.com>2009-04-15 14:57:28 +0400
commite8f4d9322112e64677df11ba834a26159874b332 (patch)
treea26e20a97d0c6b79c06da995b6b64c01a0fb057c /source/gameengine/Ketsji/KX_GameObject.h
parent19c869ab64a0e14727217c7d221e03d32a614132 (diff)
Disable using KX_GameObjects in python that have been removed from the scene (zombie objects) by raising a RuntimeError when accessing methods, attributes or passing to a function.
Common cases of this are when python references an object from the AddObject actuator that has ended, or a scene has been loaded and the old objects freed. This means some scripts will raise errors now in certain cases but better give the error early rather then failing silently with strange hard to track down behavior & crashes. Added "isValid" attribute for checking objects are in a scene. At the moment it uses the SceneGraph Node to check of the objects valid but it might be better to do this in a more generic way so scenes, meshes etc also have this check.
Diffstat (limited to 'source/gameengine/Ketsji/KX_GameObject.h')
-rw-r--r--source/gameengine/Ketsji/KX_GameObject.h110
1 files changed, 11 insertions, 99 deletions
diff --git a/source/gameengine/Ketsji/KX_GameObject.h b/source/gameengine/Ketsji/KX_GameObject.h
index 94192580859..0bf3e60f34b 100644
--- a/source/gameengine/Ketsji/KX_GameObject.h
+++ b/source/gameengine/Ketsji/KX_GameObject.h
@@ -51,7 +51,6 @@
#include "SCA_LogicManager.h" /* for ConvertPythonToGameObject to search object names */
#define KX_OB_DYNAMIC 1
-
//Forward declarations.
struct KX_ClientObjectInfo;
class KX_RayCast;
@@ -61,6 +60,10 @@ class PHY_IGraphicController;
class PHY_IPhysicsEnvironment;
struct Object;
+/* utility conversion function */
+bool ConvertPythonToGameObject(PyObject * value, KX_GameObject **object, bool py_none_ok);
+bool ValidPythonToGameObject(KX_GameObject *object);
+
/**
* KX_GameObject is the main class for dynamic objects.
*/
@@ -811,104 +814,15 @@ public:
virtual PyObject* py_getattro(PyObject *attr);
virtual int py_setattro(PyObject *attr, PyObject *value); // py_setattro method
- virtual PyObject* py_repr(void) { return PyString_FromString(GetName().ReadPtr()); }
-
- /* we need our own getattr and setattr types */
- /* See m_attrlist definition for rules on how this works */
- static PyObject *py_base_getattro_gameobject(PyObject * self, PyObject *attr)
+ virtual PyObject* py_repr(void)
{
- PyObject *object= ((KX_GameObject *) self)->py_getattro(attr);
-
- if (object==NULL && ((KX_GameObject *) self)->m_attrlist) {
- /* backup the exception incase the attr doesnt exist in the dict either */
- PyObject *err_type, *err_value, *err_tb;
- PyErr_Fetch(&err_type, &err_value, &err_tb);
-
- object= PyDict_GetItem(((KX_GameObject *) self)->m_attrlist, attr);
- if (object) {
- Py_INCREF(object);
-
- PyErr_Clear();
- Py_XDECREF( err_type );
- Py_XDECREF( err_value );
- Py_XDECREF( err_tb );
- }
- else {
- PyErr_Restore(err_type, err_value, err_tb); /* use the error from the parent function */
- }
- }
- return object;
- }
-
- static int py_base_setattro_gameobject(PyObject * self, PyObject *attr, PyObject *value)
- {
- int ret;
-
- /* Delete the item */
- if (value==NULL)
- {
- ret= ((PyObjectPlus*) self)->py_delattro(attr);
-
- if (ret != 0) /* CValue attribute failed, try KX_GameObject m_attrlist dict */
- {
- if (((KX_GameObject *) self)->m_attrlist)
- {
- /* backup the exception incase the attr doesnt exist in the dict either */
- PyObject *err_type, *err_value, *err_tb;
- PyErr_Fetch(&err_type, &err_value, &err_tb);
-
- if (PyDict_DelItem(((KX_GameObject *) self)->m_attrlist, attr) == 0)
- {
- ret= 0;
- PyErr_Clear();
- Py_XDECREF( err_type );
- Py_XDECREF( err_value );
- Py_XDECREF( err_tb );
- }
- else {
- PyErr_Restore(err_type, err_value, err_tb); /* use the error from the parent function */
- }
- }
- }
- return ret;
- }
-
-
- ret= ((PyObjectPlus*) self)->py_setattro(attr, value);
-
- if (ret==PY_SET_ATTR_SUCCESS) {
- /* remove attribute in our own dict to avoid double ups */
- if (((KX_GameObject *) self)->m_attrlist) {
- if (PyDict_DelItem(((KX_GameObject *) self)->m_attrlist, attr) != 0)
- PyErr_Clear();
- }
- }
-
- if (ret==PY_SET_ATTR_COERCE_FAIL) {
- /* CValue attribute exists, remove and add dict value */
- ((KX_GameObject *) self)->RemoveProperty(STR_String(PyString_AsString(attr)));
- ret= PY_SET_ATTR_MISSING;
- }
-
- if (ret==PY_SET_ATTR_MISSING) {
- /* Lazy initialization */
- if (((KX_GameObject *) self)->m_attrlist==NULL)
- ((KX_GameObject *) self)->m_attrlist = PyDict_New();
-
- if (PyDict_SetItem(((KX_GameObject *) self)->m_attrlist, attr, value)==0) {
- PyErr_Clear();
- ret= PY_SET_ATTR_SUCCESS;
- }
- else {
- PyErr_Format(PyExc_AttributeError, "failed assigning value to KX_GameObject internal dictionary");
- ret= PY_SET_ATTR_FAIL;
- }
- }
-
- return ret;
+ if (ValidPythonToGameObject(this)==false)
+ return NULL;
+ return PyString_FromString(GetName().ReadPtr());
}
-
+ static PyObject *py_base_getattro_gameobject(PyObject * self, PyObject *attr);
+ static int py_base_setattro_gameobject(PyObject * self, PyObject *attr, PyObject *value);
KX_PYMETHOD_NOARGS(KX_GameObject,GetPosition);
KX_PYMETHOD_O(KX_GameObject,SetPosition);
@@ -979,6 +893,7 @@ public:
static PyObject* pyattr_get_state(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
static int pyattr_set_state(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
static PyObject* pyattr_get_meshes(void* self_v, const KX_PYATTRIBUTE_DEF *attrdef);
+ static PyObject* pyattr_get_is_valid(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
/* for dir(), python3 uses __dir__() */
static PyObject* pyattr_get_dir_dict(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
@@ -1012,8 +927,5 @@ private :
};
-/* utility conversion function */
-bool ConvertPythonToGameObject(PyObject * value, KX_GameObject **object, bool py_none_ok);
-
#endif //__KX_GAMEOBJECT