6 #include "primitives.h"
11 //----------------------------------------------------------------------------
13 typedef struct _TagObject {
15 tag_internals_t internals;
18 //----------------------------------------------------------------------------
19 static PyMethodDef generic_methods[] =
23 static tag_internals_t generic_tag =
28 tagfunctions: generic_methods,
31 //----------------------------------------------------------------------------
33 static struct tag_parser {
36 struct tag_parser* next;
38 static char parsers_initialized = 0;
40 void register_tag(int id, tag_internals_t*spec)
42 assert(id>=0 && id<1024);
43 if(!parsers_initialized) {
44 memset(tag_parsers, 0, sizeof(tag_parsers));
45 parsers_initialized = 1;
47 tag_parsers[id].id = id;
48 tag_parsers[id].spec = spec;
51 static tag_internals_t* get_parser(int id)
53 if(parsers_initialized<2) {
55 struct tag_parser*last = &tag_parsers[0];
57 if(tag_parsers[t].spec) {
58 last->next = &tag_parsers[t];
59 last = &tag_parsers[t];
62 parsers_initialized = 2;
64 assert(id>=0 && id<1024);
65 return tag_parsers[id].spec;
68 //----------------------------------------------------------------------------
69 static void tag_dealloc(PyObject * self)
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));
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);
81 if(tag->internals.data) {
82 free(tag->internals.data);
83 tag->internals.data = 0;
85 if(tag->internals.tag) {
86 swf_DeleteTag(tag->internals.tag);
87 tag->internals.tag = 0;
89 Py_DECREF(tag->internals.tagmap);
90 tag->internals.tagmap = 0;
93 //----------------------------------------------------------------------------
94 static int fillTAG(PyObject*self)
96 TagObject*tag = (TagObject*)self;
97 if(tag->internals.tag)
99 if(!tag->internals.fillTAG) {
100 PyErr_SetString(PyExc_Exception, setError("No way to fill TAG with data"));
103 if(!tag->internals.fillTAG(&tag->internals)) {
104 return 0; // pass through exception
106 if(!tag->internals.tag) {
107 PyErr_SetString(PyExc_Exception, setError("Couldn't fill tag"));
112 //----------------------------------------------------------------------------
113 static PyObject* tag_isShape(PyObject * _self, PyObject*args)
115 TagObject*self = (TagObject*)_self;
116 if(!PyArg_ParseTuple(args, "")) return NULL;
117 if(!fillTAG((PyObject*)self)) return NULL;
118 return PyInt_FromLong(swf_isShapeTag(self->internals.tag));
120 static PyObject* tag_isFont(PyObject * _self, PyObject*args)
122 TagObject*self = (TagObject*)_self;
123 if(!PyArg_ParseTuple(args, "")) return NULL;
124 if(!fillTAG((PyObject*)self)) return NULL;
125 int id = self->internals.tag->id;
127 if(id == ST_DEFINEFONT || id == ST_DEFINEFONT2)
129 return PyInt_FromLong(isfont);
131 static PyObject* tag_isImage(PyObject * _self, PyObject*args)
133 TagObject*self = (TagObject*)_self;
134 if(!PyArg_ParseTuple(args, "")) return NULL;
135 if(!fillTAG((PyObject*)self)) return NULL;
136 return PyInt_FromLong(swf_isImageTag(self->internals.tag));
138 static PyObject* tag_isDefiningTag(PyObject * _self, PyObject*args)
140 TagObject*self = (TagObject*)_self;
141 if(!PyArg_ParseTuple(args, "")) return NULL;
142 if(!fillTAG((PyObject*)self)) return NULL;
143 return PyInt_FromLong(swf_isDefiningTag(self->internals.tag));
145 static PyObject* tag_isPlacement(PyObject * _self, PyObject*args)
147 TagObject*self = (TagObject*)_self;
148 if(!PyArg_ParseTuple(args, "")) return NULL;
149 if(!fillTAG((PyObject*)self)) return NULL;
150 return PyInt_FromLong((self->internals.tag->id == ST_PLACEOBJECT ||
151 self->internals.tag->id == ST_PLACEOBJECT2));
153 static PyObject* tag_getBBox(PyObject * _self, PyObject*args)
155 TagObject*self = (TagObject*)_self;
156 if(!PyArg_ParseTuple(args, "")) return NULL;
157 if(!fillTAG((PyObject*)self)) return NULL;
158 return f_BBox2(swf_GetDefineBBox(self->internals.tag));
160 static PyObject* tag_setBBox(PyObject * _self, PyObject*args)
162 TagObject*self = (TagObject*)_self;
164 if(!PyArg_ParseTuple(args, "O!", &BBoxClass, &bbox)) return NULL;
165 if(!fillTAG((PyObject*)self)) return NULL;
166 swf_SetDefineBBox(self->internals.tag, bbox_getSRECT(bbox));
169 //----------------------------------------------------------------------------
170 static PyMethodDef common_tagfunctions[] =
171 {{"isShape", tag_isShape, METH_VARARGS, "tests whether the tag is a shape tag"},
172 {"isImage", tag_isImage, METH_VARARGS, "tests whether the tag is an image"},
173 {"isFont", tag_isFont, METH_VARARGS, "tests whether the tag is a font"},
174 {"isDefiningTag", tag_isDefiningTag, METH_VARARGS, "tests whether the tag is a defining tag"},
175 {"isPlacement", tag_isPlacement, METH_VARARGS, "tests whether the tag is a placement"},
176 {"getBBox", tag_getBBox, METH_VARARGS, "get's the tags bounding box"},
177 {"setBBox", tag_setBBox, METH_VARARGS, "set's the tags bounding box"},
178 {NULL, NULL, 0, NULL}
181 static PyObject* tag_getattr(PyObject * self, char* a)
183 TagObject*tag = (TagObject*)self;
184 PyObject* ret = NULL;
188 if(!strcmp(a, "tagid")) {
191 return Py_BuildValue("i", tag->internals.tag->id);
193 if(!strcmp(a, "name")) {
196 char* name = swf_TagGetName(tag->internals.tag);
197 return Py_BuildValue("s", name);
199 if(tag->internals.getattr) {
200 PyObject* ret = tag->internals.getattr(&tag->internals, a);
204 /* search for a tag specific function */
205 if(tag->internals.tagfunctions) {
206 mylog(" %08x(%d) tag_getattr: tag has specific functions\n", (int)self, self->ob_refcnt);
207 ret = Py_FindMethod(tag->internals.tagfunctions, self, a);
210 ret = FindMethodMore(ret, common_tagfunctions, self, a);
211 mylog(" %08x(%d) tag_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
216 ret = Py_FindMethod(common_tagfunctions, self, a);
218 mylog(" %08x(%d) tag_getattr %s: %08x\n", (int)self, self->ob_refcnt, a, ret);
221 static int tag_setattr(PyObject * _self, char* a, PyObject * o)
223 TagObject*self= (TagObject*)_self;
224 /* a setattr will almost certainly change the tag data,
226 if(self->internals.tag) {
227 swf_DeleteTag(self->internals.tag);
228 self->internals.tag = 0;
230 if(self->internals.setattr) {
231 int ret = self->internals.setattr(&self->internals, a, o);
236 //----------------------------------------------------------------------------
238 //----------------------------------------------------------------------------
239 PyObject* tag_new(tag_internals_t*tag_internals)
241 TagObject*tag = PyObject_New(TagObject, &TagClass);
242 mylog("+%08x(%d) tag_new\n", (int)tag, tag->ob_refcnt);
243 memcpy(&tag->internals, tag_internals, sizeof(tag_internals_t));
244 if(tag->internals.datasize) {
245 tag->internals.data = malloc(tag->internals.datasize);
246 memset(tag->internals.data , 0, tag->internals.datasize);
248 tag->internals.data = 0;
250 tag->internals.tag = 0;
251 tag->internals.tagmap = tagmap_new();
253 return (PyObject*)tag;
255 PyObject* tag_new2(TAG*t, PyObject* tagmap)
257 TagObject*tag = PyObject_New(TagObject, &TagClass);
258 mylog("+%08x(%d) tag_new2 tag=%08x id=%d (%s)\n", (int)tag, tag->ob_refcnt, t, t->id, swf_TagGetName(t));
260 PyObject*mytagmap = tagmap_new();
262 int num = swf_GetNumUsedIDs(t);
263 if(num) { // tag has dependencies
264 int * positions = malloc(num*sizeof(int));
265 swf_GetUsedIDs(t, positions);
268 int id = GET16(&t->data[positions[i]]);
269 PyObject*obj = tagmap_id2obj(tagmap, id);
271 PyErr_SetString(PyExc_Exception, setError("TagID %d not defined", id));
274 //mylog("+%08x(%d) tag_new2 handling id %d at %d/%d\n", (int)tag, tag->ob_refcnt, id, positions[i], t->len);
275 //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);
276 tagmap_addMapping(mytagmap, id, obj);
281 tag_internals_t*spec = get_parser(t->id);
283 memcpy(&tag->internals, spec, sizeof(tag_internals_t));
285 memcpy(&tag->internals, &generic_tag, sizeof(tag_internals_t));
287 if(tag->internals.datasize) {
288 tag->internals.data = malloc(tag->internals.datasize);
289 memset(tag->internals.data, 0, tag->internals.datasize);
291 tag->internals.data = 0;
293 tag->internals.tag = swf_InsertTag(0, t->id);
294 swf_SetBlock(tag->internals.tag, t->data, t->len);
295 tag->internals.tagmap = mytagmap;
297 // call tag->internals.init()?
299 return (PyObject*)tag;
301 //----------------------------------------------------------------------------
303 TAG* tag_getTAG(PyObject*self, TAG*prevTag, PyObject*tagmap)
305 TagObject*tag = (TagObject*)self;
309 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));
311 TAG* t = swf_InsertTag(prevTag, tag->internals.tag->id);
312 swf_SetBlock(t, tag->internals.tag->data, tag->internals.tag->len);
314 if(swf_isDefiningTag(t)) {
315 int newid = tagmap_add(tagmap, self);
316 swf_SetDefineID(t, newid);
319 int num = swf_GetNumUsedIDs(t);
320 if(num) { // tag has dependencies
321 int * positions = malloc(num*sizeof(int));
322 swf_GetUsedIDs(t, positions);
325 int id = GET16(&t->data[positions[i]]);
326 PyObject* obj = tagmap_id2obj(tag->internals.tagmap, id);
328 PyErr_SetString(PyExc_Exception, setError("Internal error: id %d not known in taglist:"));
332 //int newid = tagmap_obj2id(tag->internals.tagmap, obj);
333 int newid = tagmap_obj2id(tagmap, obj);
335 mylog(" %08x(%d) tag_getTAG: dependency %d) %d->%08x -> assigning(%08x) id %d", (int)self, self->ob_refcnt, i, id, obj, tagmap, newid);
337 /* TODO: this is only needed for sprites, so maybe it should throw an
338 exception otherwise */
339 newid = tagmap_add(tagmap, obj);
340 mylog(" %08x(%d) tag_getTAG: added dependency %d) %d->%08x -> assigning(%08x) id %d", (int)self, self->ob_refcnt, i, id, obj, tagmap, newid);
342 PUT16(&t->data[positions[i]], newid);
348 //----------------------------------------------------------------------------
349 tag_internals_t* tag_getinternals(PyObject*self)
351 TagObject*tag = (TagObject*)self;
352 mylog(" %08x(%d) tag_getInternals\n", (int)self, self->ob_refcnt);
353 return &tag->internals;
355 //----------------------------------------------------------------------------
356 PyObject* tag_getDependencies(PyObject*self)
358 TagObject*tag = (TagObject*)self;
359 mylog(" %08x(%d) tag_getDependencies\n", (int)self, self->ob_refcnt);
360 return tagmap_getObjectList(tag->internals.tagmap);
362 //----------------------------------------------------------------------------
363 int tag_print(PyObject * self, FILE * fi, int flags)
365 TagObject*tag = (TagObject*)self;
366 mylog(" %08x(%d) tag_print flags=%08x\n", (int)self, self->ob_refcnt, flags);
369 //fprintf(fi, "tag-%08x-%d-%s", (int)tag->internals.tag, tag->internals.tag->id, swf_TagGetName(tag->internals.tag));
370 fprintf(fi, "%s", swf_TagGetName(tag->internals.tag));
373 //----------------------------------------------------------------------------
374 PyTypeObject TagClass =
376 PyObject_HEAD_INIT(NULL)
379 tp_basicsize: sizeof(TagObject),
381 tp_dealloc: tag_dealloc,
383 tp_getattr: tag_getattr,
384 tp_setattr: tag_setattr,