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