added isShape() tag function.
[swftools.git] / lib / python / tag.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
11 //----------------------------------------------------------------------------
12
13 typedef struct _TagObject {
14     PyObject_HEAD
15     tag_internals_t internals;
16 } TagObject;
17
18 //----------------------------------------------------------------------------
19 static PyMethodDef generic_methods[] = 
20 {
21   {NULL, NULL, 0, NULL}
22 };
23 static tag_internals_t generic_tag =
24 {
25     parse: 0,
26     dealloc: 0,
27     fillTAG: 0,
28     tagfunctions: generic_methods,
29     datasize: 0,
30 };
31 //----------------------------------------------------------------------------
32
33 static struct tag_parser {
34     int id;
35     tag_internals_t*spec;
36     struct tag_parser* next;
37 } tag_parsers[1024];
38 static char parsers_initialized = 0;
39
40 void register_tag(int id, tag_internals_t*spec)
41 {
42     assert(id>=0 && id<1024);
43     if(!parsers_initialized) {
44         memset(tag_parsers, 0, sizeof(tag_parsers));
45         parsers_initialized = 1;
46     }
47     tag_parsers[id].id = id;
48     tag_parsers[id].spec = spec;
49 };
50
51 static tag_internals_t* get_parser(int id)
52 {
53     if(parsers_initialized<2) {
54         int t;
55         struct tag_parser*last = &tag_parsers[0];
56         for(t=0;t<1024;t++) {
57             if(tag_parsers[t].spec) {
58                 last->next = &tag_parsers[t];
59                 last = &tag_parsers[t];
60             }
61         }
62         parsers_initialized = 2;
63     }
64     assert(id>=0 && id<1024);
65     return tag_parsers[id].spec;
66 }
67
68 //----------------------------------------------------------------------------
69 static void tag_dealloc(PyObject * self)
70 {
71     TagObject*tag = (TagObject*)self;
72     if(tag->internals.tag)
73         mylog("-%08x(%d) tag_dealoc [%s]\n", (int)self, self->ob_refcnt, swf_TagGetName(tag->internals.tag));
74     else
75         mylog("-%08x(%d) tag_dealoc [?]\n", (int)self, self->ob_refcnt);
76     if(tag->internals.dealloc) {
77         if(!tag->internals.data)
78             mylog("-%08x(%d) tag_dealoc: Warning: calling dealloc without any data(?)\n", (int)self, self->ob_refcnt);
79         tag->internals.dealloc(&tag->internals);
80     }
81     if(tag->internals.data) {
82         free(tag->internals.data);
83         tag->internals.data = 0;
84     }
85     if(tag->internals.tag) {
86         swf_DeleteTag(tag->internals.tag);
87         tag->internals.tag = 0;
88     }
89     Py_DECREF(tag->internals.tagmap);
90     tag->internals.tagmap = 0;
91     PyObject_Del(self);
92 }
93 //----------------------------------------------------------------------------
94 static int fillTAG(PyObject*self) 
95 {
96     TagObject*tag = (TagObject*)self;
97     if(tag->internals.tag)
98         return 1;
99     if(!tag->internals.fillTAG) {
100         PyErr_SetString(PyExc_Exception, setError("No way to fill TAG with data"));
101         return 0;
102     }
103     if(!tag->internals.fillTAG(&tag->internals)) {
104         return 0; // pass through exception
105     }
106     if(!tag->internals.tag) {
107         PyErr_SetString(PyExc_Exception, setError("Couldn't fill tag"));
108         return 0;
109     }
110     return 1;
111 }
112 //----------------------------------------------------------------------------
113 static PyObject* tag_isShape(PyObject * _self, PyObject*args)
114 {
115     TagObject*self = (TagObject*)_self;
116     if(!PyArg_ParseTuple(args, ""))
117         return NULL;
118     if(!fillTAG((PyObject*)self))
119         return NULL;
120     return PyInt_FromLong(swf_isShapeTag(self->internals.tag));
121 }
122 //----------------------------------------------------------------------------
123 static PyMethodDef common_tagfunctions[] =
124 {{"isShape", tag_isShape, METH_VARARGS, "tests whether the tag is a shape tag"},
125  {NULL, NULL, 0, NULL}
126 };
127
128 static PyObject* tag_getattr(PyObject * self, char* a)
129 {
130     TagObject*tag = (TagObject*)self;
131     PyObject* ret = NULL;
132     int t;
133
134     /* -- fields -- */
135     if(!strcmp(a, "tagid")) {
136         if(!fillTAG(self))
137             return 0;
138         return Py_BuildValue("i", tag->internals.tag->id);
139     }
140     if(!strcmp(a, "name")) {
141         if(!fillTAG(self))
142             return 0;
143         char* name = swf_TagGetName(tag->internals.tag);
144         return Py_BuildValue("s", name);
145     }
146     if(tag->internals.getattr) {
147         PyObject* ret = tag->internals.getattr(&tag->internals, a);
148         if(ret) return ret;
149     }
150     
151     /* search for a tag specific function */
152     if(tag->internals.tagfunctions) {
153         mylog(" %08x(%d) tag_getattr: tag has specific functions\n", (int)self, self->ob_refcnt);
154         ret = Py_FindMethod(tag->internals.tagfunctions, self, a);
155         if(ret) return ret;
156         PyErr_Clear();
157         ret = FindMethodMore(ret, common_tagfunctions, self, a);
158         mylog(" %08x(%d) tag_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
159         if(ret) return ret;
160         PyErr_Clear();
161     }
162   
163     ret = Py_FindMethod(common_tagfunctions, self, a);
164
165     mylog(" %08x(%d) tag_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
166     return ret;
167 }
168 static int tag_setattr(PyObject * _self, char* a, PyObject * o)
169 {
170     TagObject*self= (TagObject*)_self;
171     /* a setattr will almost certainly change the tag data,
172        so delete the tag */
173     if(self->internals.tag) {
174         swf_DeleteTag(self->internals.tag);
175         self->internals.tag = 0;
176     }
177     if(self->internals.setattr) {
178         int ret = self->internals.setattr(&self->internals, a, o);
179         return ret;
180     }
181     return 1;
182 }
183 //----------------------------------------------------------------------------
184 //                     Tag Constructors
185 //----------------------------------------------------------------------------
186 PyObject* tag_new(tag_internals_t*tag_internals)
187 {
188     TagObject*tag = PyObject_New(TagObject, &TagClass);
189     mylog("+%08x(%d) tag_new\n", (int)tag, tag->ob_refcnt);
190     memcpy(&tag->internals, tag_internals, sizeof(tag_internals_t));
191     if(tag->internals.datasize) {
192         tag->internals.data = malloc(tag->internals.datasize);
193         memset(tag->internals.data , 0, tag->internals.datasize);
194     } else {
195         tag->internals.data = 0;
196     }
197     tag->internals.tag = 0;
198     tag->internals.tagmap = tagmap_new();
199
200     return (PyObject*)tag;
201 }
202 PyObject* tag_new2(TAG*t, PyObject* tagmap)
203 {
204     TagObject*tag = PyObject_New(TagObject, &TagClass);
205     mylog("+%08x(%d) tag_new2 tag=%08x id=%d (%s)\n", (int)tag, tag->ob_refcnt, t, t->id, swf_TagGetName(t));
206     
207     PyObject*mytagmap = tagmap_new();
208
209     int num = swf_GetNumUsedIDs(t);
210     if(num) { // tag has dependencies
211         int * positions = malloc(num*sizeof(int));
212         swf_GetUsedIDs(t, positions);
213         int i;
214         for(i=0;i<num;i++) {
215             int id = GET16(&t->data[positions[i]]);
216             PyObject*obj = tagmap_id2obj(tagmap, id);
217             if(obj==NULL) {
218                 PyErr_SetString(PyExc_Exception, setError("TagID %d not defined", id));
219                 return NULL;
220             }
221             //mylog("+%08x(%d) tag_new2 handling id %d at %d/%d\n", (int)tag, tag->ob_refcnt, id, positions[i], t->len);
222             //mylog("+%08x(%d) tag_new2 add dependency %d to id %d, object %08x(%d)\n", (int)tag, tag->ob_refcnt, i, id, obj, obj->ob_refcnt);
223             tagmap_addMapping(mytagmap, id, obj);
224         }
225         free(positions);
226     }
227
228     tag_internals_t*spec = get_parser(t->id);
229     if(spec) {
230         memcpy(&tag->internals, spec, sizeof(tag_internals_t));
231     } else {
232         memcpy(&tag->internals, &generic_tag, sizeof(tag_internals_t));
233     }
234     if(tag->internals.datasize) {
235         tag->internals.data = malloc(tag->internals.datasize);
236         memset(tag->internals.data, 0, tag->internals.datasize);
237     } else {
238         tag->internals.data = 0;
239     }
240     tag->internals.tag = swf_InsertTag(0, t->id);
241     swf_SetBlock(tag->internals.tag, t->data, t->len);
242     tag->internals.tagmap = mytagmap;
243
244     // call tag->internals.init()?
245
246     return (PyObject*)tag;
247 }
248 //----------------------------------------------------------------------------
249 /* serialize */
250 TAG* tag_getTAG(PyObject*self, TAG*prevTag, PyObject*tagmap)
251 {
252     TagObject*tag = (TagObject*)self;
253
254     if(!fillTAG(self))
255         return 0;
256     mylog(" %08x(%d) tag_getTAG: tag=%08x id=%d (%s)", (int)self, self->ob_refcnt, tag->internals.tag, tag->internals.tag->id, swf_TagGetName(tag->internals.tag));
257
258     TAG* t = swf_InsertTag(prevTag, tag->internals.tag->id);
259     swf_SetBlock(t, tag->internals.tag->data, tag->internals.tag->len);
260     
261     if(swf_isDefiningTag(t)) {
262         int newid = tagmap_add(tagmap, self);
263         swf_SetDefineID(t, newid);
264     }
265
266     int num = swf_GetNumUsedIDs(t);
267     if(num) { // tag has dependencies
268         int * positions = malloc(num*sizeof(int));
269         swf_GetUsedIDs(t, positions);
270         int i;
271         for(i=0;i<num;i++) {
272             int id = GET16(&t->data[positions[i]]);
273             PyObject* obj =  tagmap_id2obj(tag->internals.tagmap, id);
274             if(obj==NULL) {
275                 PyErr_SetString(PyExc_Exception, setError("Internal error: id %d not known in taglist:"));
276                 free(positions);
277                 return 0;
278             }
279             //int newid = tagmap_obj2id(tag->internals.tagmap, obj);
280             int newid = tagmap_obj2id(tagmap, obj);
281             if(newid>=0) {
282                 mylog(" %08x(%d) tag_getTAG: dependency %d) %d->%08x -> assigning(%08x) id %d", (int)self, self->ob_refcnt, i, id, obj, tagmap, newid);
283             } else {
284                 /* TODO: this is only needed for sprites, so maybe it should throw an
285                    exception otherwise */
286                 newid = tagmap_add(tagmap, obj);
287                 mylog(" %08x(%d) tag_getTAG: added dependency %d) %d->%08x -> assigning(%08x) id %d", (int)self, self->ob_refcnt, i, id, obj, tagmap, newid);
288             }
289             PUT16(&t->data[positions[i]], newid);
290         }
291         free(positions);
292     }
293     return t;
294 }
295 //----------------------------------------------------------------------------
296 tag_internals_t* tag_getinternals(PyObject*self)
297 {
298     TagObject*tag = (TagObject*)self;
299     mylog(" %08x(%d) tag_getInternals\n", (int)self, self->ob_refcnt);
300     return &tag->internals;
301 }
302 //----------------------------------------------------------------------------
303 PyObject* tag_getDependencies(PyObject*self)
304 {
305     TagObject*tag = (TagObject*)self;
306     mylog(" %08x(%d) tag_getDependencies\n", (int)self, self->ob_refcnt);
307     return tagmap_getObjectList(tag->internals.tagmap);
308 }
309 //----------------------------------------------------------------------------
310 int tag_print(PyObject * self, FILE * fi, int flags)
311 {
312     TagObject*tag = (TagObject*)self;
313     mylog(" %08x(%d) tag_print flags=%08x\n", (int)self, self->ob_refcnt, flags);
314     if(!fillTAG(self))
315         return -1;
316     //fprintf(fi, "tag-%08x-%d-%s", (int)tag->internals.tag, tag->internals.tag->id, swf_TagGetName(tag->internals.tag));
317     fprintf(fi, "%s", swf_TagGetName(tag->internals.tag));
318     return 0;
319 }
320 //----------------------------------------------------------------------------
321 PyTypeObject TagClass = 
322 {
323     PyObject_HEAD_INIT(NULL)
324     0,
325     tp_name: "Tag",
326     tp_basicsize: sizeof(TagObject),
327     tp_itemsize: 0,
328     tp_dealloc: tag_dealloc,
329     tp_print: tag_print,
330     tp_getattr: tag_getattr,
331     tp_setattr: tag_setattr,
332 };