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"
30 static void import_code(void*_abc, char*filename, int pass, asset_bundle_t*a);
32 void as3_import_abc(char*filename)
34 TAG*tag = swf_InsertTag(0, ST_RAWABC);
35 memfile_t*file = memfile_open(filename);
36 tag->data = file->data;
38 abc_file_t*abc = swf_ReadABC(tag);
39 import_code(abc, filename, 0, 0);
40 import_code(abc, filename, 1, 0);
46 void as3_import_swf(char*filename)
48 SWF* swf = swf_OpenSWF(filename);
53 TAG*tag = swf->firstTag;
55 asset_resolver_t* assets = swf_ParseAssets(swf);
59 if(tag->id == ST_DOABC || tag->id == ST_RAWABC) {
60 abc_file_t*abc = swf_ReadABC(tag);
61 import_code(abc, filename, 0, 0);
70 if(tag->id == ST_DOABC || tag->id == ST_RAWABC) {
71 abc_file_t*abc = swf_ReadABC(tag); //FIXME: mem leak
72 swf_ResolveAssets(assets, abc);
73 NEW(asset_bundle_t, a);
75 registry_add_asset(a);
76 import_code(abc, filename, 1, a);
81 //swf_FreeTags(swf); // FIXME: mem leak
86 void as3_import_file(char*filename)
88 FILE*fi = fopen(filename, "rb");
91 fread(head, 3, 1, fi);
93 if(!strncmp(head, "FWS", 3) ||
94 !strncmp(head, "CWS", 3)) {
95 as3_import_swf(filename);
97 as3_import_abc(filename);
101 static int compare_traits(const void*v1, const void*v2)
103 trait_t* x1 = *(trait_t**)v1;
104 trait_t* x2 = *(trait_t**)v2;
105 int i = strcmp(x1->name->ns->name, x2->name->ns->name);
108 return strcmp(x1->name->name, x2->name->name);
111 static classinfo_t*resolve_class(char*filename, char*what, multiname_t*n)
114 if(!n->name[0]) return 0;
115 if(!strcmp(n->name, "void"))
119 if(n->ns && n->ns->name) {
120 c = (classinfo_t*)registry_find(n->ns->name, n->name);
121 } else if(n->namespace_set) {
122 namespace_list_t*s = n->namespace_set->namespaces;
124 c = (classinfo_t*)registry_find(s->namespace->name, n->name);
132 as3_warning("import %s: couldn't resolve %s %s.%s", filename, what, n->ns->name, n->name);
135 if(c->kind != INFOTYPE_CLASS)
136 as3_warning("import %s: %s %s resolves to something that's not a class", filename, what, n->name);
140 static void import_code(void*_abc, char*filename, int pass, asset_bundle_t*asset_bundle)
142 abc_file_t*abc = _abc;
145 for(t=0;t<abc->classes->num;t++) {
146 abc_class_t*cls = array_getvalue(abc->classes, t);
147 U8 access = cls->classname->ns->access;
148 if(access==ACCESS_PRIVATE ||
149 access==ACCESS_PACKAGEINTERNAL)
151 //if(!strncmp(cls->classname->ns->name, "__AS3", 5))
154 const char*package = strdup(cls->classname->ns->name);
155 const char*name = strdup(cls->classname->name);
157 multiname_list_t*i=cls->interfaces;
158 classinfo_t*c = classinfo_register(access, package, name, list_length(i));
159 c->flags|=FLAG_ASSET;
161 if(cls->flags & CLASS_FINAL)
162 c->flags |= FLAG_FINAL;
163 if(cls->flags & CLASS_INTERFACE)
164 c->flags |= FLAG_INTERFACE;
165 if(!(cls->flags & CLASS_SEALED))
166 c->flags |= FLAG_DYNAMIC;
171 for(t=0;t<abc->classes->num;t++) {
172 abc_class_t*cls = array_getvalue(abc->classes, t);
173 const char*package = strdup(cls->classname->ns->name);
174 const char*name = strdup(cls->classname->name);
175 classinfo_t*c = (classinfo_t*)registry_find(package, name);
179 c->assets = asset_bundle;
183 multiname_list_t*i = cls->interfaces;
185 c->interfaces[nr++] = resolve_class(filename, "interface", i->multiname);
188 c->superclass = resolve_class(filename, "superclass", cls->superclass);
194 l = cls->static_traits;
197 dict_t*names = dict_new();
199 trait_t*trait = l->trait;
200 U8 access = trait->name->ns->access;
202 if(access==ACCESS_PRIVATE)
204 const char*name = trait->name->name;
205 char* ns = access==ACCESS_NAMESPACE?strdup(trait->name->ns->name):"";
207 if(registry_findmember(c, ns, name, 0, is_static))
213 if(trait->kind == TRAIT_METHOD) {
214 s = (memberinfo_t*)methodinfo_register_onclass(c, access, ns, name, is_static);
215 s->return_type = resolve_class(filename, "return type", trait->method->return_type);
216 dict_put(names, name, 0);
217 } else if(trait->kind == TRAIT_SLOT) {
218 s = (memberinfo_t*)varinfo_register_onclass(c, access, ns, name, is_static);
219 s->type = resolve_class(filename, "type", trait->type_name);
220 dict_put(names, name, 0);
221 } else if(trait->kind == TRAIT_GETTER) {
222 s = (memberinfo_t*)varinfo_register_onclass(c, access, ns, name, is_static);
223 s->type = resolve_class(filename, "type", trait->method->return_type);
224 dict_put(names, name, 0);
225 } else if(trait->kind == TRAIT_CONST) {
226 /* some variables (e.g. XML.length) are apparently both a method and a slot.
227 needs split of static/non-static first */
228 if(!dict_contains(names, name)) {
229 varinfo_t*v = (varinfo_t*)varinfo_register_onclass(c, access, ns, name, is_static);
230 v->type = resolve_class(filename, "type", trait->type_name);
231 v->flags |= FLAG_CONST;
232 /* leave this alone for now- it blows up the file too much
233 v->value = constant_clone(trait->value);*/
234 s = (memberinfo_t*)v;
235 dict_put(names, name, 0);
243 s->flags = is_static?FLAG_STATIC:0;
244 s->flags |= FLAG_BUILTIN;
249 if(!l && !is_static) {
250 l = cls->static_traits;
257 # define IS_PUBLIC_MEMBER(trait) ((trait)->kind != TRAIT_CLASS && (trait)->name->ns->access != ACCESS_PRIVATE)
259 /* count public functions */
261 for(t=0;t<abc->scripts->num;t++) {
262 trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
264 num_methods += IS_PUBLIC_MEMBER(l->trait);
267 trait_t**traits = (trait_t**)malloc(num_methods*sizeof(trait_t*));
269 for(t=0;t<abc->scripts->num;t++) {
270 trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
272 if(IS_PUBLIC_MEMBER(l->trait)) {
273 traits[num_methods++] = l->trait;
277 qsort(traits, num_methods, sizeof(trait_t*), compare_traits);
278 for(t=0;t<num_methods;t++) {
279 trait_t*trait = traits[t];
280 if(IS_PUBLIC_MEMBER(trait)) {
281 U8 access = trait->name->ns->access;
282 const char*package = strdup(trait->name->ns->name);
283 const char*name = strdup(trait->name->name);
286 if(trait->kind == TRAIT_METHOD) {
287 m = (memberinfo_t*)methodinfo_register_global(access, package, name);
288 m->return_type = resolve_class(filename, "return type", trait->method->return_type);
290 varinfo_t*v = varinfo_register_global(access, package, name);
291 v->type = resolve_class(filename, "type", trait->type_name);
292 v->value = constant_clone(trait->value);
293 v->flags |= trait->kind==TRAIT_CONST?FLAG_CONST:0;
294 m = (memberinfo_t*)v;
296 m->flags |= FLAG_ASSET;
302 void as3_import_code(void*_abc)
304 import_code(_abc, "", 0, 0);
305 import_code(_abc, "", 1, 0);