new tags are now mapped to their own IDs, not an allocated one
[swftools.git] / lib / python / taglist.c
1 #include <Python.h>
2 #undef HAVE_STAT
3 #include "../rfxswf.h"
4 #include "../log.h"
5 #include "./pyutils.h"
6 #include "primitives.h"
7 #include "action.h"
8 #include "tag.h"
9 #include "tagmap.h"
10 #include "taglist.h"
11
12 //----------------------------------------------------------------------------
13 typedef struct {
14     PyObject_HEAD
15     PyObject* taglist;
16     PyObject* tagmap;
17 } TagListObject;
18 //----------------------------------------------------------------------------
19 PyObject * taglist_new()
20 {
21     TagListObject* taglist = PyObject_New(TagListObject, &TagListClass);
22     mylog("+%08x(%d) taglist_new", (int)taglist, taglist->ob_refcnt);
23     taglist->tagmap = tagmap_new();
24     taglist->taglist = PyList_New(0);
25     return (PyObject*)taglist;
26 }
27 //----------------------------------------------------------------------------
28 PyObject * taglist_new2(TAG*tag)
29 {
30     TagListObject* taglist = PyObject_New(TagListObject, &TagListClass);
31     mylog("+%08x(%d) taglist_new2 tag=%08x", (int)taglist, taglist->ob_refcnt, tag);
32     taglist->tagmap = tagmap_new();
33
34     int nr=0;
35     TAG*t = tag;
36     while(t) {nr++;t=t->next;}
37     taglist->taglist = PyList_New(nr);
38     
39     mylog("+%08x(%d) taglist_new2: %d items", (int)taglist, taglist->ob_refcnt, nr);
40
41     nr = 0;
42     t = tag;
43     while(t) {
44         PyObject*newtag = tag_new2(t, taglist->tagmap);
45         if(newtag==NULL) {
46             // pass through exception
47             return NULL;
48         }
49         PyList_SET_ITEM(taglist->taglist,nr,newtag);Py_INCREF(newtag);
50         if(swf_isDefiningTag(t)) {
51             int id = swf_GetDefineID(t);
52             tagmap_addMapping(taglist->tagmap, id, newtag);
53         }
54         nr++;
55         t=t->next;
56     }
57     return (PyObject*)taglist;
58 }
59 //----------------------------------------------------------------------------
60 TAG* taglist_getTAGs(PyObject*self)
61 {
62     if(!PY_CHECK_TYPE(self,&TagListClass)) {
63         PyErr_SetString(PyExc_Exception, setError("Not a taglist (%08x).", self));
64         return 0;
65     }
66     TagListObject*taglist = (TagListObject*)self;
67
68     /* TODO: the tags will be modified by this. We should set mutexes. */
69     
70     int l = PyList_Size(taglist->taglist);
71     int t;
72     TAG* tag = 0;
73     TAG* firstTag = 0;
74     mylog(" %08x(%d) taglist_getTAGs", (int)self, self->ob_refcnt);
75     for(t=0;t<l;t++) {
76         PyObject*item = PyList_GetItem(taglist->taglist, t);
77         tag = tag_getTAG(item, tag, taglist->tagmap);
78         if(!firstTag)
79             firstTag = tag;
80         mylog(" %08x(%d) taglist_getTAGs: added tag %08x", (int)self, self->ob_refcnt, tag);
81     }
82     return firstTag;
83 }
84 //----------------------------------------------------------------------------
85 static PyObject * taglist_foldAll(PyObject* self, PyObject* args)
86 {
87 /*    SWF swf;
88     TagListObject*taglist = (TagListObject*)self;
89     if(!self || !PyArg_ParseTuple(args,"")) 
90         return NULL;
91     swf.firstTag = taglist->firstTag;
92     swf_FoldAll(&swf);
93     taglist->firstTag = swf.firstTag;
94     taglist->lastTag = 0; // FIXME
95     taglist->searchTag = 0;*/
96     return PY_NONE;
97 }
98 //----------------------------------------------------------------------------
99 static PyObject * taglist_unfoldAll(PyObject* self, PyObject* args)
100 {
101     SWF swf;
102 /*    TagListObject*taglist = (TagListObject*)self;
103     if(!self || !PyArg_ParseTuple(args,"")) 
104         return NULL;
105     swf.firstTag = taglist->firstTag;
106     swf_UnFoldAll(&swf);
107     taglist->firstTag = swf.firstTag;
108     taglist->lastTag = 0; // FIXME
109     taglist->searchTag = 0;*/
110     return PY_NONE;
111 }
112 //----------------------------------------------------------------------------
113 static PyObject * taglist_optimizeOrder(PyObject* self, PyObject* args)
114 {
115     SWF swf;
116 /*    TagListObject*taglist = (TagListObject*)self;
117     if(!self || !PyArg_ParseTuple(args,"")) 
118         return NULL;
119     swf.firstTag = taglist->firstTag;
120     swf_UnFoldAll(&swf);
121     taglist->firstTag = swf.firstTag;
122     taglist->lastTag = 0; // FIXME
123     taglist->searchTag = 0;*/
124     return PY_NONE;
125 }
126 //----------------------------------------------------------------------------
127 static void taglist_dealloc(PyObject* self)
128 {
129     TagListObject*taglist = (TagListObject*)self;
130     mylog("-%08x(%d) taglist_dealloc\n", (int)self, self->ob_refcnt);
131     Py_DECREF(taglist->taglist);
132     taglist->taglist = 0;
133     Py_DECREF(taglist->tagmap);
134     taglist->tagmap= 0;
135     PyObject_Del(self);
136 }
137 //----------------------------------------------------------------------------
138 static PyMethodDef taglist_functions[] =
139 {{"foldAll", taglist_foldAll, METH_VARARGS, "fold all sprites (movieclips) in the list"},
140  {"unfoldAll", taglist_unfoldAll, METH_VARARGS, "unfold (expand) all sprites (movieclips) in the list"},
141  {"optimizeOrder", taglist_optimizeOrder, METH_VARARGS, "Reorder the Tag structure"},
142  {NULL, NULL, 0, NULL}
143 };
144
145 static PyObject* taglist_getattr(PyObject * self, char* a)
146 {
147     PyObject* ret = Py_FindMethod(taglist_functions, self, a);
148     mylog(" %08x(%d) taglist_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
149     return ret;
150 }
151 //----------------------------------------------------------------------------
152 static int taglist_length(PyObject * self)
153 {
154     TagListObject*tags = (TagListObject*)self;
155     mylog(" %08x(%d) taglist_length", (int)self, self->ob_refcnt);
156     return PyList_GET_SIZE(tags->taglist);
157 }
158 //----------------------------------------------------------------------------
159 static int taglist_contains(PyObject * self, PyObject * tag)
160 {
161     mylog(" %08x(%d) taglist_contains %08x", (int)self, self->ob_refcnt, tag);
162     TagListObject*taglist = (TagListObject*)self;
163     PyObject*list = taglist->taglist;
164     int l = PyList_Size(list);
165     int t;
166     for(t=0;t<l;t++) {
167         PyObject*item = PyList_GetItem(list, t);
168         if(item == tag) {
169             mylog(" %08x(%d) taglist_contains: yes", (int)self, self->ob_refcnt);
170             return 1;
171         }
172     }
173     mylog(" %08x(%d) taglist_contains: no", (int)self, self->ob_refcnt);
174     return 0;
175 }
176 //----------------------------------------------------------------------------
177 static PyObject * taglist_concat(PyObject * self, PyObject* list)
178 {
179     PyObject*tag = 0;
180     PY_ASSERT_TYPE(self, &TagListClass);
181     TagListObject*taglist = (TagListObject*)self;
182     mylog(" %08x(%d) taglist_concat %08x", (int)self, self->ob_refcnt, list);
183
184     if (PyArg_Parse(list, "O!", &TagClass, &tag)) {
185         mylog(" %08x(%d) taglist_concat: Tag %08x", (int)self, self->ob_refcnt, tag);
186         list = tag_getDependencies(tag);
187         int l = PyList_Size(list);
188         int t;
189         mylog(" %08x(%d) taglist_concat: Tag: %d dependencies", (int)self, self->ob_refcnt, l);
190         for(t=0;t<l;t++) {
191             PyObject*item = PyList_GetItem(list, t);
192             PyObject*_self = taglist_concat(self, item);
193             Py_DECREF(self);
194             self = _self;
195         }
196         if(!taglist_contains(self, tag)) {
197             mylog(" %08x(%d) taglist_concat: Adding Tag %08x", (int)self, self->ob_refcnt, tag);
198             PyList_Append(taglist->taglist, tag);
199         }
200         mylog(" %08x(%d) taglist_concat: done", (int)self, self->ob_refcnt);
201         Py_INCREF(self);
202         return self;
203         /* copy tag, so we don't have to do INCREF(tag) (and don't
204            get problems if the tag is appended to more than one
205            taglist) */
206         /* TODO: handle IDs */
207         /*      
208         TAG*t = tag_getTAG(tag);
209         TAG*nt = 0;
210         mylog("taglist_concat: Tag", (int)self, self->ob_refcnt);
211         // copy tag
212         nt = swf_InsertTag(0, t->id);
213         swf_SetBlock(nt,t->data,t->len);
214         PyObject*newtag = tag_new(taglist->swf, nt);
215         if(swf_isDefiningTag(t)) {
216             int id = swf_GetDefineID(t);
217             PyObject*id = PyLong_FromLong(id);
218             PyDict_SetItem((PyObject*)(taglist->char2id), list, id);
219             Py_INCREF(id);
220             PyDict_SetItem((PyObject*)(taglist->id2char), id, list);
221             Py_INCREF(id);
222         }
223         Py_INCREF(self);
224         return self;*/
225     }
226     PyErr_Clear();
227     if (PyList_Check(list)) {
228         int l = PyList_Size(list);
229         int t;
230         mylog(" %08x(%d) taglist_concat: List", (int)self, self->ob_refcnt);
231         for(t=0;t<l;t++) {
232             PyObject*item = PyList_GetItem(list, t);
233             if(!PY_CHECK_TYPE(item, &TagClass)) {
234                 PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
235                 return 0;
236             }
237             PyObject*_self = taglist_concat(self, item);
238             Py_DECREF(self);
239             self = _self;
240             if(!self)
241                 return 0;
242         }
243         Py_INCREF(self);
244         return self;
245     }
246     PyErr_Clear();
247     if (PY_CHECK_TYPE(list, &TagListClass)) {
248         mylog(" %08x(%d) taglist_concat: TagList", (int)self, self->ob_refcnt);
249         TagListObject*taglist2 = (TagListObject*)list;
250         return taglist_concat(self, taglist2->taglist);
251
252         /*TAG* tags = taglist_getTAGs(self);
253         TAG* tags2 = taglist_getTAGs(list);
254         TAG* tags3;
255         tags3 = swf_Concatenate(tags,tags2);
256         PyObject* newtaglist = taglist_new(tags3);
257         swf_FreeTags(tags3);
258         Py_INCREF(newtaglist);*/
259     }
260     PyErr_Clear();
261
262     PyErr_SetString(PyExc_Exception, setError("taglist concatenation only works with tags and lists (%08x).", list));
263     return 0;
264 }
265 //----------------------------------------------------------------------------
266 static PyObject * taglist_item(PyObject * self, int index)
267 {
268     TagListObject*taglist = (TagListObject*)self;
269     PyObject*tag;
270     mylog(" %08x(%d) taglist_item(%d)", (int)self, self->ob_refcnt, index);
271     tag = PyList_GetItem(taglist->taglist, index);
272     Py_INCREF(tag); //TODO-REF
273     return tag;
274 }
275 static PySequenceMethods taglist_as_sequence =
276 {
277     sq_length: taglist_length, // len(obj)
278     sq_concat: taglist_concat, // obj += [...], obj1+obj2
279     sq_repeat: 0,            // x*n, intargfunc
280     sq_item: taglist_item,  // obj[3]
281     sq_slice: 0,             // x[i:j] intintargfunc
282     sq_ass_item: 0,          // x[i] = y intobjargproc
283     sq_ass_slice: 0,         // x[i:j] = v intintobjargproc
284     sq_contains: taglist_contains,   //???
285 };
286 static PyTypeObject TagListClass = 
287 {
288     PyObject_HEAD_INIT(NULL)
289     0,
290     tp_name: "TagList",
291     tp_basicsize: sizeof(TagListObject),
292     tp_itemsize: 0,
293     tp_dealloc: taglist_dealloc,
294     tp_print: 0,                 // print x
295     tp_getattr: taglist_getattr, // x.attr
296     tp_setattr: 0,               // x.attr = v
297     tp_compare: 0,               // x>y
298     tp_repr: 0,                  // `x`, print x
299     tp_as_number: 0,
300     tp_as_sequence: &taglist_as_sequence,
301 };