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