implemented asset resolving
[swftools.git] / lib / as3 / import.c
1 /* import.c
2
3    Extension module for the rfxswf library.
4    Part of the swftools package.
5
6    Copyright (c) 2009 Matthias Kramm <kramm@quiss.org>
7  
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.
12
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.
17
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 */
21
22 #include "import.h"
23 #include "abc.h"
24 #include "registry.h"
25 #include "common.h"
26 #include "tokenizer.h"
27 #include "assets.h"
28 #include "../os.h"
29
30 static void import_code(void*_abc, char*filename, int pass, asset_bundle_t*a);
31
32 void as3_import_abc(char*filename)
33 {
34     TAG*tag = swf_InsertTag(0, ST_RAWABC);
35     memfile_t*file = memfile_open(filename);
36     tag->data = file->data;
37     tag->len = file->len;
38     abc_file_t*abc = swf_ReadABC(tag);
39     import_code(abc, filename, 0, 0);
40     import_code(abc, filename, 1, 0);
41     swf_FreeABC(abc);
42     memfile_close(file);
43     free(tag);
44 }
45
46 void as3_import_swf(char*filename)
47 {
48     SWF* swf = swf_OpenSWF(filename);
49     if(!swf)
50         return;
51     swf_FoldAll(swf);
52
53     TAG*tag = swf->firstTag;
54
55     asset_resolver_t* assets =  swf_ParseAssets(swf);
56
57     /* pass 1 */
58     while(tag) {
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);
62             swf_FreeABC(abc);
63         }
64         tag = tag->next;
65     }
66
67     tag = swf->firstTag;
68     /* pass 2 */
69     while(tag) {
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);
74             a->file = abc;
75             registry_add_asset(a);
76             import_code(abc, filename, 1, a);
77         }
78         tag = tag->next;
79     }
80
81     //swf_FreeTags(swf); // FIXME: mem leak
82     free(swf);
83
84 }
85
86 void as3_import_file(char*filename)
87 {
88     FILE*fi = fopen(filename, "rb");
89     if(!fi) return;
90     char head[3];
91     fread(head, 3, 1, fi);
92     fclose(fi);
93     if(!strncmp(head, "FWS", 3) ||
94        !strncmp(head, "CWS", 3)) {
95         as3_import_swf(filename);
96     } else {
97         as3_import_abc(filename);
98     }
99 }
100
101 static int compare_traits(const void*v1, const void*v2)
102 {
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);
106     if(i)
107         return i;
108     return strcmp(x1->name->name, x2->name->name);
109 }
110
111 static classinfo_t*resolve_class(char*filename, char*what, multiname_t*n)
112 {
113     if(!n) return 0;
114     if(!n->name[0]) return 0;
115     if(!strcmp(n->name, "void")) 
116         return &voidclass;
117
118     classinfo_t*c = 0;
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;
123         while(s) {
124             c = (classinfo_t*)registry_find(s->namespace->name, n->name);
125             if(c)
126                 break;
127             s = s->next;
128         }
129     }
130
131     if(!c) {
132         as3_warning("import %s: couldn't resolve %s %s.%s", filename, what, n->ns->name, n->name);
133         return 0;
134     }
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);
137     return c;
138 }
139
140 static void import_code(void*_abc, char*filename, int pass, asset_bundle_t*asset_bundle)
141 {
142     abc_file_t*abc = _abc;
143     int t;
144     if(pass==0) {
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)
150                 continue;
151             //if(!strncmp(cls->classname->ns->name, "__AS3", 5))
152             //    continue;
153
154             const char*package = strdup(cls->classname->ns->name);
155             const char*name = strdup(cls->classname->name);
156
157             multiname_list_t*i=cls->interfaces;
158             classinfo_t*c = classinfo_register(access, package, name, list_length(i));
159             c->flags|=FLAG_ASSET;
160
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;
167         }
168         return;
169     }
170     
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);
176         if(!c) continue;
177
178         if(cls->asset) {
179             c->assets = asset_bundle;
180         }
181
182         int nr = 0;
183         multiname_list_t*i = cls->interfaces;
184         while(i) {
185             c->interfaces[nr++] = resolve_class(filename, "interface", i->multiname);
186             i = i->next;
187         }
188         c->superclass = resolve_class(filename, "superclass", cls->superclass);
189       
190         trait_list_t*l=0;
191         char is_static = 0;
192         l = cls->traits;
193         if(!l) {
194             l = cls->static_traits;
195             is_static = 1;
196         }
197         dict_t*names = dict_new();
198         while(l) {
199             trait_t*trait = l->trait;
200             U8 access = trait->name->ns->access;
201
202             if(access==ACCESS_PRIVATE)
203                 goto cont;
204             const char*name = trait->name->name;
205             char* ns = access==ACCESS_NAMESPACE?strdup(trait->name->ns->name):"";
206         
207             if(registry_findmember(c, ns, name, 0, is_static))
208                 goto cont;
209
210             name = strdup(name);
211
212             memberinfo_t*s = 0;
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);
236                 } else 
237                     goto cont;
238
239             } else {
240                 goto cont;
241             }
242
243             s->flags = is_static?FLAG_STATIC:0;
244             s->flags |= FLAG_BUILTIN;
245             s->parent = c;
246
247             cont:
248             l = l->next;
249             if(!l && !is_static) {
250                 l = cls->static_traits;
251                 is_static = 1;
252             }
253         }
254         dict_destroy(names);
255     }
256
257 #   define IS_PUBLIC_MEMBER(trait) ((trait)->kind != TRAIT_CLASS && (trait)->name->ns->access != ACCESS_PRIVATE)
258
259     /* count public functions */
260     int num_methods=0;
261     for(t=0;t<abc->scripts->num;t++) {
262         trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
263         for(;l;l=l->next) {
264             num_methods += IS_PUBLIC_MEMBER(l->trait);
265         }
266     }
267     trait_t**traits = (trait_t**)malloc(num_methods*sizeof(trait_t*));
268     num_methods=0;
269     for(t=0;t<abc->scripts->num;t++) {
270         trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
271         for(;l;l=l->next) {
272             if(IS_PUBLIC_MEMBER(l->trait)) {
273                 traits[num_methods++] = l->trait;
274             }
275         }
276     }
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);
284             char np = 0;
285             memberinfo_t*m = 0;
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);
289             } else {
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;
295             }
296             m->flags |= FLAG_ASSET;
297             m->parent = 0;
298         }
299     }
300 }
301
302 void as3_import_code(void*_abc)
303 {
304     import_code(_abc, "", 0, 0);
305     import_code(_abc, "", 1, 0);
306 }