3 Extension module for the rfxswf library.
4 Part of the swftools package.
6 Copyright (c) 2009 Matthias Kramm <kramm@quiss.org>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
26 #include "tokenizer.h"
34 static void import_code(void*_abc, char*filename, int pass, asset_bundle_t*a);
36 void as3_import_abc(char*filename)
38 TAG*tag = swf_InsertTag(0, ST_RAWABC);
39 memfile_t*file = memfile_open(filename);
40 tag->data = file->data;
42 abc_file_t*abc = swf_ReadABC(tag);
43 import_code(abc, filename, 0, 0);
44 import_code(abc, filename, 1, 0);
50 typedef struct _deps {
55 void as3_import_swf2(reader_t*r, char*filename, dict_t*deps)
58 if(swf_ReadSWF2(r, &_swf)<0)
62 TAG*tag = swf->firstTag;
64 asset_resolver_t* assets = swf_ParseAssets(swf);
66 asset_bundle_list_t* asset_bundles = list_new();
68 dict_t*name2bundle = dict_new();
71 if(tag->id == ST_DOABC || tag->id == ST_RAWABC) {
72 abc_file_t*abc = swf_ReadABC(tag);
73 import_code(abc, filename, 0, 0);
74 NEW(asset_bundle_t, a);
77 dict_put(name2bundle, abc->name, a);
79 list_append(asset_bundles, a);
85 asset_bundle_list_t*b = asset_bundles;
88 if(tag->id == ST_DOABC || tag->id == ST_RAWABC) {
89 asset_bundle_t*a = b->asset_bundle;
90 abc_file_t*abc = a->file;
91 swf_ResolveAssets(assets, abc);
92 if(deps && abc->name) {
93 deps_t*d = dict_lookup(deps, abc->name);
96 asset_bundle_t*other = dict_lookup(name2bundle, d->name);
97 list_append(a->dependencies, other);
102 registry_add_asset(a);
103 import_code(abc, filename, 1, a);
109 dict_destroy(name2bundle);
110 list_free(asset_bundles);
112 //swf_FreeTags(swf); // FIXME: mem leak
115 void as3_import_swf(char*filename)
118 reader_init_filereader2(&reader, filename);
119 as3_import_swf2(&reader, filename, 0);
120 reader.dealloc(&reader);
124 typedef struct _catalog_state {
136 const char* fa(catalog_state_t*state, xmlattribute_t*attr, const char*name)
139 if(!strcmp(attr->name, name)) return attr->value;
142 syntaxerror("error parsing %s: attribute %s missing", state->xml_filename, name);
145 void catalog_start_tag(xmlconsumer_t*c, char*name, xmlattribute_t*attr)
147 catalog_state_t*state = (catalog_state_t*)c->internal;
148 if(!strcmp(name, "libraries")) {
149 state->in_libraries = 1;
150 } else if(!strcmp(name, "library")) {
151 state->library = strdup(fa(state, attr, "path"));
152 } else if(!strcmp(name, "script")) {
153 state->script = strdup(fa(state, attr, "name"));
154 } else if(!strcmp(name, "def")) {
155 dict_put(state->id2script, strdup(fa(state, attr, "id")), strdup(state->script));
156 } else if(!strcmp(name, "dep")) {
158 d->name = strdup(fa(state, attr, "id"));
159 d->next = state->current_deps;
160 state->current_deps = d;
163 void catalog_data(xmlconsumer_t*c, char*data, int len)
165 catalog_state_t*state = (catalog_state_t*)c->internal;
167 void catalog_end_tag(xmlconsumer_t*c, char*name)
169 catalog_state_t*state = (catalog_state_t*)c->internal;
170 if(!strcmp(name, "libraries")) {
171 state->in_libraries = 0;
172 } else if(!strcmp(name, "library")) {
173 ZZIP_FILE*file = zzip_file_open(state->dir, state->library, 0);
175 DICT_ITERATE_DATA(state->deps,deps_t*,deps) {
177 char*script = dict_lookup(state->id2script, deps->name);
179 //as3_warning("when importing %s: depencency %s referenced in catalog.xml, but not found.", state->xml_filename, deps->name);
187 as3_warning("when importing %s: %s referenced in catalog.xml, but not found.", state->xml_filename, state->library);
190 reader_init_zzipreader(&r, file);
191 as3_import_swf2(&r, state->library, state->deps);
193 zzip_file_close(file);
195 dict_destroy(state->deps);
197 free(state->library);
199 } else if(!strcmp(name, "script")) {
200 dict_put(state->deps, state->script, state->current_deps);
202 state->current_deps = 0;
206 void as3_import_zipfile(char*filename)
208 ZZIP_DIR*dir = zzip_opendir(filename);
209 if(!dir) as3_error("Error reading %s\n", filename);
210 ZZIP_FILE*file = zzip_file_open(dir, "catalog.xml", 0);
212 reader_init_zzipreader(&r, file);
215 catalog_state_t state;
216 memset(&state, 0, sizeof(state));
218 state.xml_filename = filename;
219 state.name2deps = dict_new();
220 state.id2script = dict_new();
221 state.deps = dict_new();
222 c.start_tag = catalog_start_tag;
223 c.data= catalog_data;
224 c.end_tag = catalog_end_tag;
232 void as3_import_zipfile(char*filename)
234 as3_warning("No zipfile support compiled in- can't import %s\n", filename);
238 void as3_import_file(char*filename)
240 FILE*fi = fopen(filename, "rb");
243 fread(head, 3, 1, fi);
245 if(!strncmp(head, "FWS", 3) ||
246 !strncmp(head, "CWS", 3)) {
247 as3_import_swf(filename);
248 } else if(!strncmp(head, "PK", 2)) {
249 as3_import_zipfile(filename);
251 as3_import_abc(filename);
255 static int compare_traits(const void*v1, const void*v2)
257 trait_t* x1 = *(trait_t**)v1;
258 trait_t* x2 = *(trait_t**)v2;
259 int i = strcmp(x1->name->ns->name, x2->name->ns->name);
262 return strcmp(x1->name->name, x2->name->name);
265 static classinfo_t*resolve_class(char*filename, char*what, multiname_t*n)
268 if(!n->name[0]) return 0;
269 if(!strcmp(n->name, "void"))
273 if(n->ns && n->ns->name) {
274 c = (classinfo_t*)registry_find(n->ns->name, n->name);
275 } else if(n->namespace_set) {
276 namespace_list_t*s = n->namespace_set->namespaces;
278 c = (classinfo_t*)registry_find(s->namespace->name, n->name);
286 as3_warning("import %s: couldn't resolve %s %s.%s", filename, what, n->ns->name, n->name);
289 if(c->kind != INFOTYPE_CLASS)
290 as3_warning("import %s: %s %s resolves to something that's not a class", filename, what, n->name);
294 static void import_code(void*_abc, char*filename, int pass, asset_bundle_t*asset_bundle)
296 abc_file_t*abc = _abc;
299 for(t=0;t<abc->classes->num;t++) {
300 abc_class_t*cls = array_getvalue(abc->classes, t);
301 U8 access = cls->classname->ns->access;
302 if(access==ACCESS_PRIVATE ||
303 access==ACCESS_PACKAGEINTERNAL)
305 //if(!strncmp(cls->classname->ns->name, "__AS3", 5))
308 const char*package = strdup(cls->classname->ns->name);
309 const char*name = strdup(cls->classname->name);
311 multiname_list_t*i=cls->interfaces;
312 classinfo_t*c = classinfo_register(access, package, name, list_length(i));
313 c->flags|=FLAG_BUILTIN;
315 if(cls->flags & CLASS_FINAL)
316 c->flags |= FLAG_FINAL;
317 if(cls->flags & CLASS_INTERFACE)
318 c->flags |= FLAG_INTERFACE;
319 if(!(cls->flags & CLASS_SEALED))
320 c->flags |= FLAG_DYNAMIC;
325 for(t=0;t<abc->classes->num;t++) {
326 abc_class_t*cls = array_getvalue(abc->classes, t);
327 const char*package = strdup(cls->classname->ns->name);
328 const char*name = strdup(cls->classname->name);
329 classinfo_t*c = (classinfo_t*)registry_find(package, name);
332 c->assets = asset_bundle;
335 multiname_list_t*i = cls->interfaces;
337 c->interfaces[nr++] = resolve_class(filename, "interface", i->multiname);
340 c->superclass = resolve_class(filename, "superclass", cls->superclass);
346 l = cls->static_traits;
349 dict_t*names = dict_new();
351 trait_t*trait = l->trait;
352 U8 access = trait->name->ns->access;
354 if(access==ACCESS_PRIVATE)
356 const char*name = trait->name->name;
357 char* ns = access==ACCESS_NAMESPACE?strdup(trait->name->ns->name):"";
359 if(registry_findmember(c, ns, name, 0, is_static))
365 if(trait->kind == TRAIT_METHOD) {
366 s = (memberinfo_t*)methodinfo_register_onclass(c, access, ns, name, is_static);
367 s->return_type = resolve_class(filename, "return type", trait->method->return_type);
368 dict_put(names, name, 0);
369 } else if(trait->kind == TRAIT_SLOT) {
370 s = (memberinfo_t*)varinfo_register_onclass(c, access, ns, name, is_static);
371 s->type = resolve_class(filename, "type", trait->type_name);
372 dict_put(names, name, 0);
373 } else if(trait->kind == TRAIT_GETTER) {
374 s = (memberinfo_t*)varinfo_register_onclass(c, access, ns, name, is_static);
375 s->type = resolve_class(filename, "type", trait->method->return_type);
376 dict_put(names, name, 0);
377 } else if(trait->kind == TRAIT_CONST) {
378 /* some variables (e.g. XML.length) are apparently both a method and a slot.
379 needs split of static/non-static first */
380 if(!dict_contains(names, name)) {
381 varinfo_t*v = (varinfo_t*)varinfo_register_onclass(c, access, ns, name, is_static);
382 v->type = resolve_class(filename, "type", trait->type_name);
383 v->flags |= FLAG_CONST;
384 /* leave this alone for now- it blows up the file too much
385 v->value = constant_clone(trait->value);*/
386 s = (memberinfo_t*)v;
387 dict_put(names, name, 0);
395 s->flags = is_static?FLAG_STATIC:0;
396 s->flags |= FLAG_BUILTIN;
401 if(!l && !is_static) {
402 l = cls->static_traits;
409 # define IS_PUBLIC_MEMBER(trait) ((trait)->kind != TRAIT_CLASS && (trait)->name->ns->access != ACCESS_PRIVATE)
411 /* count public functions */
413 for(t=0;t<abc->scripts->num;t++) {
414 trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
416 num_methods += IS_PUBLIC_MEMBER(l->trait);
419 trait_t**traits = (trait_t**)malloc(num_methods*sizeof(trait_t*));
421 for(t=0;t<abc->scripts->num;t++) {
422 trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
424 if(IS_PUBLIC_MEMBER(l->trait)) {
425 traits[num_methods++] = l->trait;
429 qsort(traits, num_methods, sizeof(trait_t*), compare_traits);
430 for(t=0;t<num_methods;t++) {
431 trait_t*trait = traits[t];
432 if(IS_PUBLIC_MEMBER(trait)) {
433 U8 access = trait->name->ns->access;
434 const char*package = strdup(trait->name->ns->name);
435 const char*name = strdup(trait->name->name);
438 if(trait->kind == TRAIT_METHOD) {
439 m = (memberinfo_t*)methodinfo_register_global(access, package, name);
440 m->return_type = resolve_class(filename, "return type", trait->method->return_type);
442 varinfo_t*v = varinfo_register_global(access, package, name);
443 v->type = resolve_class(filename, "type", trait->type_name);
444 v->value = constant_clone(trait->value);
445 v->flags |= trait->kind==TRAIT_CONST?FLAG_CONST:0;
446 m = (memberinfo_t*)v;
448 m->flags |= FLAG_BUILTIN;
454 void as3_import_code(void*_abc)
456 import_code(_abc, "", 0, 0);
457 import_code(_abc, "", 1, 0);