some fixes, added "verbose" constant.
[swftools.git] / lib / python / SWF.c
index a24a5f0..2a5b903 100644 (file)
@@ -62,6 +62,9 @@ typedef struct {
     TAG*firstTag;
     TAG*searchTag;
     TAG*lastTag;
+    PyDictObject* char2id;
+    PyDictObject* id2char;
+    U16 currentID;
 } TagListObject;
 
 typedef struct {
@@ -104,6 +107,9 @@ static PyObject* f_create(PyObject* self, PyObject* args, PyObject* kwargs)
     swf->taglist->firstTag = 0;
     swf->taglist->searchTag = 0;
     swf->taglist->lastTag = 0;
+    swf->taglist->currentID = 1;
+    swf->taglist->char2id = (PyDictObject*)PyDict_New();
+    swf->taglist->id2char = (PyDictObject*)PyDict_New();
 
     if(swf->swf.fileVersion>=6)
        swf->swf.compressed = 1;
@@ -145,6 +151,11 @@ static PyObject* f_load(PyObject* self, PyObject* args)
     swf->taglist->firstTag = swf->swf.firstTag;
     swf->taglist->searchTag = swf->swf.firstTag;
     swf->taglist->lastTag = swf->swf.firstTag;
+    while(swf->taglist->lastTag->next)
+       swf->taglist->lastTag = swf->taglist->lastTag->next;
+    swf->taglist->currentID = 1;
+    swf->taglist->char2id = (PyDictObject*)PyDict_New();
+    swf->taglist->id2char = (PyDictObject*)PyDict_New();
     swf->swf.firstTag = 0;
 
     mylog("load %08x -> %08x\n", (int)self, (int)swf);
@@ -276,13 +287,6 @@ static PyMethodDef swf_functions[] =
  {NULL, NULL, 0, NULL}
 };
  
-static PyMethodDef taglist_functions[] =
-{{"foldAll", taglist_foldAll, METH_VARARGS, "fold all sprites (movieclips) in the list"},
- {"unfoldAll", taglist_unfoldAll, METH_VARARGS, "unfold (expand) all sprites (movieclips) in the list"},
- {"optimizeOrder", taglist_optimizeOrder, METH_VARARGS, "Reorder the Tag structure"},
- {NULL, NULL, 0, NULL}
-};
-
 //----------------------------------------------------------------------------
 static void swf_dealloc(PyObject* self)
 {
@@ -414,6 +418,13 @@ err:
 }
 
 //----------------------------------------------------------------------------
+static PyMethodDef taglist_functions[] =
+{{"foldAll", taglist_foldAll, METH_VARARGS, "fold all sprites (movieclips) in the list"},
+ {"unfoldAll", taglist_unfoldAll, METH_VARARGS, "unfold (expand) all sprites (movieclips) in the list"},
+ {"optimizeOrder", taglist_optimizeOrder, METH_VARARGS, "Reorder the Tag structure"},
+ {NULL, NULL, 0, NULL}
+};
+
 static PyObject* taglist_getattr(PyObject * self, char* a)
 {
     PyObject* ret = Py_FindMethod(taglist_functions, self, a);
@@ -439,7 +450,7 @@ static PyObject * taglist_concat(PyObject * self, PyObject* list)
 {
     TagObject*tag;
     TagListObject*taglist = (TagListObject*)self;
-    mylog("taglist_concat %08x(%d)", (int)self, self->ob_refcnt);
+    mylog("taglist_concat %08x(%d) %08x", (int)self, self->ob_refcnt, list);
        
     if (PyArg_Parse(list, "O!", &TagClass, &tag)) {
        /* copy tag, so we don't have to do INCREF(tag) (and don't
@@ -452,21 +463,31 @@ static PyObject * taglist_concat(PyObject * self, PyObject* list)
        if(!taglist->firstTag) {
            taglist->firstTag = taglist->searchTag = taglist->lastTag;
        }
+       if(swf_isDefiningTag(tag->tag)) {
+           PyObject*id = PyLong_FromLong(taglist->currentID);
+           PyDict_SetItem((PyObject*)(taglist->char2id), list, id);
+           Py_INCREF(id);
+           PyDict_SetItem((PyObject*)(taglist->id2char), id, list);
+           Py_INCREF(id);
+       }
        Py_INCREF(self);
        return self;
     }
     PyErr_Clear();
     if (PyList_Check(list)) {
+       int l = PyList_Size(list);
+       int t;
        mylog("taglist_concat: PythonList", (int)self, self->ob_refcnt);
+       for(t=0;t<l;t++) {
+           PyObject*item = PyList_GetItem(list, t);
+           self = taglist_concat(self, item);
+           if(!self)
+               return 0;
+       }
        Py_INCREF(self);
        return self;
     }
-    return 0;
-}
-//----------------------------------------------------------------------------
-static int taglist_contains(PyObject * self, PyObject* other)
-{
-    mylog("taglist_contains %08x(%d)", (int)self, self->ob_refcnt);
+    PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
     return 0;
 }
 //----------------------------------------------------------------------------
@@ -477,9 +498,9 @@ static PyObject * taglist_item(PyObject * self, int index)
     TAG*tag;
     TagObject*tagobject;
     int i = 0;
-    mylog("taglist_item %08x(%d) [%d]", (int)self, self->ob_refcnt, i);
+    mylog("taglist_item %08x(%d) [%d]", (int)self, self->ob_refcnt, index);
 
-    if(i<0) {
+    if(index<0) {
        PyErr_SetString(PyExc_Exception, setError("Negative Indices not supported."));
        return NULL;
     }
@@ -487,9 +508,18 @@ static PyObject * taglist_item(PyObject * self, int index)
     tag = taglist->firstTag;
     while(tag && i<index) {
        tag = tag->next;
+       i++;
     }
     if(!tag || i != index) {
-       PyErr_SetString(PyExc_Exception, setError("No Tag at position %d", index));
+       if(index> i+10) {
+           PyErr_SetString(PyExc_Exception, setError("No Tag at position %d", index));
+           return NULL;
+       }
+
+       mylog("taglist_item %08x(%d)->IndexError (%d)", (int)self, self->ob_refcnt, index);
+
+       Py_INCREF(PyExc_IndexError);
+       PyErr_SetObject(PyExc_IndexError, Py_None);
        return NULL;
     }
 
@@ -562,16 +592,18 @@ static PyObject* tag_getattr(PyObject * self, char* a)
     for(t=0;t<sizeof(tagfunctions)/sizeof(tagfunctions[0]);t++)
     {
        if(id==tagfunctions[t].id) {
-           mylog("taglist_getattr: id %d found\n", id);
+           mylog("tag_getattr: id %d found\n", id);
            ret = Py_FindMethod(tagfunctions[t].f, self, a);
-           break;
+           if(!ret) return ret;
+           ret = FindMethodMore(ret, common_tagfunctions, self, a);
+           mylog("tag_getattr %08x(%d) %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
+           return ret;
        }
     }
+   
+    ret = Py_FindMethod(common_tagfunctions, self, a);
 
-    /* search in the functions common to all tags */
-    FindMethodMore(ret, common_tagfunctions, self, a);
-
-    mylog("taglist_getattr %08x(%d) %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
+    mylog("tag_getattr %08x(%d) %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
     return ret;
 }
 //----------------------------------------------------------------------------
@@ -637,6 +669,24 @@ static PyObject* f_DefineFont(PyObject* self, PyObject* args, PyObject* kwargs)
     return (PyObject*)tag;
 }
 //----------------------------------------------------------------------------
+static PyObject* f_Protect(PyObject* self, PyObject* args, PyObject* kwargs)
+{
+    static char *kwlist[] = {"password", NULL};
+    char*password = 0;
+    TagObject*tag;
+
+    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwlist, &password))
+       return NULL;
+
+    tag = TagObject_New();
+    tag->tag = swf_InsertTag(0, ST_PROTECT);
+    if(password) {
+       swf_SetPassword(tag->tag, password);
+    }
+    mylog("f_Protect %08x -> %08x\n", (int)self, (int)tag);
+    return (PyObject*)tag;
+}
+//----------------------------------------------------------------------------
 static PyObject* f_DefineText(PyObject* self, PyObject* args, PyObject* kwargs)
 {
     static char *kwlist[] = {"font", "text", "size", "color", NULL};
@@ -673,7 +723,7 @@ static PyObject* f_DefineText(PyObject* self, PyObject* args, PyObject* kwargs)
 //----------------------------------------------------------------------------
 static PyObject* f_PlaceObject(PyObject* self, PyObject* args, PyObject* kwargs)
 {
-    static char *kwlist[] = {"character", "depth", "matrix", "colortransform", "ratio", "name", "clipdepth", "action"};
+    static char *kwlist[] = {"character", "depth", "matrix", "colortransform", "ratio", "name", "clipdepth", "action", NULL};
     TagObject*tag;
     
     TagObject*character = 0;
@@ -741,7 +791,7 @@ static PySequenceMethods taglist_as_sequence =
     sq_slice: 0,             // x[i:j] intintargfunc
     sq_ass_item: 0,          // x[i] = y intobjargproc
     sq_ass_slice: 0,         // x[i:j] = v intintobjargproc
-    sq_contains: taglist_contains, //???
+    sq_contains: 0,          //???
 };
 static PyTypeObject TagListClass = 
 {
@@ -772,8 +822,19 @@ static PyTypeObject TagClass =
 };
 //----------------------------------------------------------------------------
 
+static PyObject* module_verbose(PyObject* self, PyObject* args)
+{
+    if (!PyArg_ParseTuple(args,"i", &verbose)) 
+       return NULL;
+    return Py_BuildValue("s", 0);
+}
+
+
 static PyMethodDef SWFMethods[] = 
 {
+    /* Module functions */
+    {"verbose", module_verbose, METH_VARARGS, "Set the module verbosity"},
+
     /* SWF creation*/
     {"load", f_load, METH_VARARGS, "Load a SWF from disc."},
     {"create", (PyCFunction)f_create, METH_KEYWORDS, "Create a new SWF from scratch."},
@@ -787,6 +848,7 @@ static PyMethodDef SWFMethods[] =
 
     /* TAGS */
     {"SetBackgroundColor", (PyCFunction)f_SetBackgroundColor, METH_KEYWORDS, "Create a SetBackGroundColor Tag."},
+    {"Protect", (PyCFunction)f_Protect, METH_KEYWORDS, "Create a Protect Tag."},
     {"DefineFont", (PyCFunction)f_DefineFont, METH_KEYWORDS, "Create a DefineFont Tag."},
     {"DefineText", (PyCFunction)f_DefineText, METH_KEYWORDS, "Create a DefineText Tag."},
     {"PlaceObject", (PyCFunction)f_PlaceObject, METH_KEYWORDS, "Create a PlaceObject Tag."},