new registry format
[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 "../os.h"
26
27 void as3_import_abc(char*filename)
28 {
29     TAG*tag = swf_InsertTag(0, ST_RAWABC);
30     memfile_t*file = memfile_open(filename);
31     tag->data = file->data;
32     tag->len = file->len;
33     abc_file_t*abc = swf_ReadABC(tag);
34     as3_import_code(abc);
35     swf_FreeABC(abc);
36     memfile_close(file);
37     free(tag);
38 }
39
40 void as3_import_swf(char*filename)
41 {
42     SWF* swf = swf_OpenSWF(filename);
43     if(!swf)
44         return;
45     TAG*tag = swf->firstTag;
46     while(tag) {
47         if(tag->id == ST_DOABC || tag->id == ST_RAWABC) {
48             abc_file_t*abc = swf_ReadABC(tag);
49             as3_import_code(abc);
50             swf_FreeABC(abc);
51         }
52         tag = tag->next;
53     }
54     swf_FreeTags(swf);
55     free(swf);
56 }
57
58 void as3_import_file(char*filename)
59 {
60     FILE*fi = fopen(filename, "rb");
61     if(!fi) return;
62     char head[3];
63     fread(head, 3, 1, fi);
64     fclose(fi);
65     if(!strncmp(head, "FWS", 3) ||
66        !strncmp(head, "FWC", 3))
67         as3_import_swf(filename);
68     else
69         as3_import_abc(filename);
70 }
71
72 static int compare_traits(const void*v1, const void*v2)
73 {
74     trait_t* x1 = *(trait_t**)v1;
75     trait_t* x2 = *(trait_t**)v2;
76     int i = strcmp(x1->name->ns->name, x2->name->ns->name);
77     if(i)
78         return i;
79     return strcmp(x1->name->name, x2->name->name);
80 }
81
82
83 void as3_import_code(abc_file_t*abc)
84 {
85     int t;
86     for(t=0;t<abc->classes->num;t++) {
87         abc_class_t*cls = array_getvalue(abc->classes, t);
88         U8 access = cls->classname->ns->access;
89         if(access==ACCESS_PRIVATE ||
90            access==ACCESS_PACKAGEINTERNAL)
91             continue;
92         if(!strncmp(cls->classname->ns->name, "__AS3", 5))
93             continue;
94
95         const char*package = strdup(cls->classname->ns->name);
96         const char*name = strdup(cls->classname->name);
97                 
98         multiname_list_t*i=cls->interfaces;
99         classinfo_t*c = classinfo_register(access, package, name, list_length(i));
100
101         if(cls->flags & CLASS_FINAL)
102             c->flags |= FLAG_FINAL;
103         if(cls->flags & CLASS_INTERFACE)
104             c->flags |= FLAG_INTERFACE;
105         if(!(cls->flags & CLASS_SEALED))
106             c->flags |= FLAG_DYNAMIC;
107         /*
108           FIXME 
109            int nr = 0;
110         while(i) {
111             c->interfaces[nr++] = i->multiname;
112             i = i->next;
113         }
114         c->superclass = ...
115          
116          */
117       
118         trait_list_t*l=0;
119         char is_static = 0;
120         l = cls->traits;
121         while(l) {
122             trait_t*trait = l->trait;
123             U8 access = trait->name->ns->access;
124             if(access==ACCESS_PRIVATE)
125                 goto cont;
126             const char*name = trait->name->name;
127             if(registry_findmember(c, name, 0))
128                 goto cont;
129             name = strdup(name);
130
131             memberinfo_t*s = 0;
132             if(trait->kind == TRAIT_METHOD) {
133                 s = (memberinfo_t*)methodinfo_register_onclass(c, access, name);
134             } else if(trait->kind == TRAIT_SLOT ||
135                       trait->kind == TRAIT_GETTER) {
136                 s = (memberinfo_t*)varinfo_register_onclass(c, access, name);
137             } else {
138                 goto cont;
139             }
140
141             s->flags = is_static?FLAG_STATIC:0;
142             s->parent = c;
143
144             cont:
145             l = l->next;
146             if(!l && !is_static) {
147                 l = cls->static_traits;
148                 is_static = 1;
149             }
150         }
151     }
152
153 #   define IS_PUBLIC_MEMBER(trait) ((trait)->kind != TRAIT_CLASS)
154
155     /* count public functions */
156     int num_methods=0;
157     for(t=0;t<abc->scripts->num;t++) {
158         trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
159         for(;l;l=l->next) {
160             num_methods += IS_PUBLIC_MEMBER(l->trait);
161         }
162     }
163     trait_t**traits = (trait_t**)malloc(num_methods*sizeof(trait_t*));
164     num_methods=0;
165     for(t=0;t<abc->scripts->num;t++) {
166         trait_list_t*l = ((abc_script_t*)array_getvalue(abc->scripts, t))->traits;
167         for(;l;l=l->next) {
168             if(IS_PUBLIC_MEMBER(l->trait)) {
169                 traits[num_methods++] = l->trait;
170             }
171         }
172     }
173     qsort(traits, num_methods, sizeof(trait_t*), compare_traits);
174     for(t=0;t<num_methods;t++) {
175         trait_t*trait = traits[t];
176         if(IS_PUBLIC_MEMBER(trait)) {
177             U8 access = trait->name->ns->access;
178             const char*package = strdup(trait->name->ns->name);
179             const char*name = strdup(trait->name->name);
180             char np = 0;
181             memberinfo_t*m = 0;
182             if(trait->kind == TRAIT_METHOD) {
183                 m = (memberinfo_t*)methodinfo_register_global(access, package, name);
184             } else {
185                 m = (memberinfo_t*)varinfo_register_global(access, package, name);
186             }
187             m->return_type = 0;
188             m->parent = 0;
189         }
190     }
191 }