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_BUILTIN;
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);
178 c->assets = asset_bundle;
181 multiname_list_t*i = cls->interfaces;
183 c->interfaces[nr++] = resolve_class(filename, "interface", i->multiname);
186 c->superclass = resolve_class(filename, "superclass", cls->superclass);
192 l = cls->static_traits;
195 dict_t*names = dict_new();
197 trait_t*trait = l->trait;
198 U8 access = trait->name->ns->access;
200 if(access==ACCESS_PRIVATE)
202 const char*name = trait->name->name;
203 char* ns = access==ACCESS_NAMESPACE?strdup(trait->name->ns->name):"";
205 if(registry_findmember(c, ns, name, 0, is_static))
211 if(trait->kind == TRAIT_METHOD) {
212 s = (memberinfo_t*)methodinfo_register_onclass(c, access, ns, name, is_static);
213 s->return_type = resolve_class(filename, "return type", trait->method->return_type);
214 dict_put(names, name, 0);
215 } else if(trait->kind == TRAIT_SLOT) {
216 s = (memberinfo_t*)varinfo_register_onclass(c, access, ns, name, is_static);
217 s->type = resolve_class(filename, "type", trait->type_name);
218 dict_put(names, name, 0);
219 } else if(trait->kind == TRAIT_GETTER) {
220 s = (memberinfo_t*)varinfo_register_onclass(c, access, ns, name, is_static);
221 s->type = resolve_class(filename, "type", trait->method->return_type);
222 dict_put(names, name, 0);
223 } else if(trait->kind == TRAIT_CONST) {
224 /* some variables (e.g. XML.length) are apparently both a method and a slot.
225 needs split of static/non-static first */
226 if(!dict_contains(names, name)) {
227 varinfo_t*v = (varinfo_t*)varinfo_register_onclass(c, access, ns, name, is_static);
228 v->type = resolve_class(filename, "type", trait->type_name);
229 v->flags |= FLAG_CONST;
230 /* leave this alone for now- it blows up the file too much
231 v->value = constant_clone(trait->value);*/
232 s = (memberinfo_t*)v;
233 dict_put(names, name, 0);
241 s->flags = is_static?FLAG_STATIC:0;
242 s->flags |= FLAG_BUILTIN;
247 if(!l && !is_static) {
248 l = cls->static_traits;
255 # define IS_PUBLIC_MEMBER(trait) ((trait)->kind != TRAIT_CLASS && (trait)->name->ns->access != ACCESS_PRIVATE)
257 /* count public functions */
259 for(t=0;t<abc->scripts->num;t++) {
260 trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
262 num_methods += IS_PUBLIC_MEMBER(l->trait);
265 trait_t**traits = (trait_t**)malloc(num_methods*sizeof(trait_t*));
267 for(t=0;t<abc->scripts->num;t++) {
268 trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
270 if(IS_PUBLIC_MEMBER(l->trait)) {
271 traits[num_methods++] = l->trait;
275 qsort(traits, num_methods, sizeof(trait_t*), compare_traits);
276 for(t=0;t<num_methods;t++) {
277 trait_t*trait = traits[t];
278 if(IS_PUBLIC_MEMBER(trait)) {
279 U8 access = trait->name->ns->access;
280 const char*package = strdup(trait->name->ns->name);
281 const char*name = strdup(trait->name->name);
284 if(trait->kind == TRAIT_METHOD) {
285 m = (memberinfo_t*)methodinfo_register_global(access, package, name);
286 m->return_type = resolve_class(filename, "return type", trait->method->return_type);
288 varinfo_t*v = varinfo_register_global(access, package, name);
289 v->type = resolve_class(filename, "type", trait->type_name);
290 v->value = constant_clone(trait->value);
291 v->flags |= trait->kind==TRAIT_CONST?FLAG_CONST:0;
292 m = (memberinfo_t*)v;
294 m->flags |= FLAG_BUILTIN;
300 void as3_import_code(void*_abc)
302 import_code(_abc, "", 0, 0);
303 import_code(_abc, "", 1, 0);