as3compile: small bugfixes, added documentation
[swftools.git] / src / as3compile.c
1 /* as3compile.c
2    Compiles ActionScript 3.0 (.as) files into .swf files.
3
4    Part of the swftools package.
5
6    Copyright (c) 2008/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 <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <unistd.h>
27 #include "../lib/rfxswf.h"
28 #include "../lib/args.h"
29 #include "../lib/q.h"
30 #include "../lib/os.h"
31
32 static char * filename = 0;
33 static char * outputname = 0;
34 static int override_outputname = 0;
35 static int do_cgi = 0;
36 static double framerate = 25.0;
37 static double width = 400;
38 static double height = 300;
39 static int flashversion = 9;
40 static int verbose = 1;
41 static char local_with_networking = 0;
42 static char local_with_filesystem = 0;
43 static char*mainclass = 0;
44
45 static struct options_t options[] = {
46 {"h", "help"},
47 {"V", "version"},
48 {"v", "verbose"},
49 {"q", "quiet"},
50 {"C", "cgi"},
51 {"R", "resolve"},
52 {"D", "define"},
53 {"X", "width"},
54 {"Y", "height"},
55 {"r", "rate"},
56 {"l", "library"},
57 {"I", "include"},
58 {"N", "local-with-network"},
59 {"L", "local-with-filesystem"},
60 {"T", "flashversion"},
61 {"o", "output"},
62 {0,0}
63 };
64
65 int args_callback_option(char*name,char*val)
66 {
67     if(!strcmp(name, "V")) {
68         printf("swfc - part of %s %s\n", PACKAGE, VERSION);
69         exit(0);
70     }
71     else if(!strcmp(name, "o")) {
72         outputname = val;
73         override_outputname = 1;
74         return 1;
75     }
76     else if(!strcmp(name, "r")) {
77         framerate = atof(val);
78         return 1;
79     }
80     else if(!strcmp(name, "M")) {
81         mainclass = val;
82         return 1;
83     }
84     else if(!strcmp(name, "v")) {
85         verbose++;
86         return 0;
87     }
88     else if(!strcmp(name, "q")) {
89         verbose--;
90         return 0;
91     }
92     else if(!strcmp(name, "X")) {
93         width = atof(val);
94         return 1;
95     }
96     else if(!strcmp(name, "Y")) {
97         height = atof(val);
98         return 1;
99     }
100     else if(!strcmp(name, "T")) {
101         flashversion = atoi(val);
102         return 1;
103     }
104     else if(!strcmp(name, "C")) {
105         do_cgi = 1;
106         return 0;
107     }
108     else if(!strcmp(name, "l")) {
109         as3_import_file(val);
110         return 1;
111     }
112     else if(!strcmp(name, "I")) {
113         as3_add_include_dir(val);
114         return 1;
115     }
116     else if(!strcmp(name, "R")) {
117         as3_set_option("recurse","1");
118         return 0;
119     }
120     else if(!strcmp(name, "D")) {
121         as3_set_define(val);
122         return 1;
123     }
124     else if (!strcmp(name, "N"))
125     {
126         local_with_networking = 1;
127         return 0;
128     }
129     else if (!strcmp(name, "L"))
130     {
131         local_with_filesystem = 1;
132         return 0;
133     }
134     else {
135         printf("Unknown option: -%s\n", name);
136         exit(1);
137     }
138     return 0;
139 }
140 int args_callback_longoption(char*name,char*val)
141 {
142     return args_long2shortoption(options, name, val);
143 }
144 void args_callback_usage(char *name)
145 {
146     printf("\n");
147     printf("Usage: %s file.as [-o file.swf] \n", name);
148     printf("\n");
149     printf("-h , --help                    Print short help message and exit\n");
150     printf("-V , --version                 Print version info and exit\n");
151     printf("-v , --verbose                 Increase verbosity\n");
152     printf("-q , --quiet                   Decrease verbosity\n");
153     printf("-C , --cgi                     Output to stdout (for use in CGI environments)\n");
154     printf("-R , --resolve                 Try to resolve undefined classes automatically.\n");
155     printf("-D , --define <namespace::variable>    Set a compile time variable (for doing conditional compilation)\n");
156     printf("-X , --width                   Set target SWF width\n");
157     printf("-Y , --height                  Set target SWF width\n");
158     printf("-r , --rate                    Set target SWF framerate\n");
159     printf("-l , --library <file>          Include library file <file>. <file> can be an .abc or .swf file.\n");
160     printf("-I , --include <dir>           Add additional include dir <dir>.\n");
161     printf("-N , --local-with-network      Make output file \"local with networking\"\n");
162     printf("-L , --local-with-filesystem     Make output file \"local with filesystem\"\n");
163     printf("-T , --flashversion <num>      Set target SWF flash version to <num>.\n");
164     printf("-o , --output <filename>       Set output file to <filename>.\n");
165     printf("\n");
166 }
167 int args_callback_command(char*name,char*val)
168 {
169     if(filename) {
170         fprintf(stderr, "Only one file allowed. You supplied at least two. (%s and %s)\n",
171                  filename, name);
172     }
173     filename = name;
174     return 0;
175 }
176
177 void writeSWF(SWF*swf)
178 {
179     int fi = -1;
180     if(do_cgi || !strcmp(filename, "-"))
181         fi = fileno(stdout);
182     else
183         fi = open(outputname, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0644);
184     if(fi<0) {
185         fprintf(stderr, "couldn't create output file %s\n", filename);
186         exit(1);
187     }
188     if(do_cgi) {
189         if(swf_WriteCGI(swf)<0) {
190             fprintf(stderr, "WriteCGI failed.\n");
191             exit(1);
192         }
193     } else {
194         if(swf_WriteSWF(fi, swf)<0) {
195             fprintf(stderr, "WriteSWF() failed.\n");
196             exit(1);
197         }
198     }
199 }
200
201 int main (int argc,char ** argv)
202 {
203     char buf[512];
204     char*currentdir = getcwd(buf, 512);
205     if(!currentdir) {
206         as3_warning("Could not determine the current directory");
207     } else {
208         as3_add_include_dir(currentdir);
209     }
210     registry_init();
211
212     int t;
213     processargs(argc, argv);
214     as3_setverbosity(verbose);
215
216     if(!filename) {
217         args_callback_usage(argv[0]);
218         exit(1);
219     }
220     if(!outputname) {
221         outputname = stripFilename(filename, ".swf");
222         //as3_warning("output name not given, writing to %s", outputname);
223     }
224
225     as3_parse_file(filename);
226     void*code = as3_getcode();
227
228     SWF swf;
229     memset(&swf, 0, sizeof(swf));
230     swf.fileVersion = flashversion;
231     swf.frameRate = framerate*0x100;
232     swf.movieSize.xmin = 0;
233     swf.movieSize.ymin = 0;
234     swf.movieSize.xmax = width*20;
235     swf.movieSize.ymax = height*20;
236     TAG*tag = swf.firstTag = swf_InsertTag(0, ST_DOABC);
237     swf_WriteABC(tag, code);
238
239     if(!mainclass)
240         mainclass = as3_getglobalclass();
241     if(mainclass) {
242         tag = swf_InsertTag(tag, ST_SYMBOLCLASS);
243         swf_SetU16(tag, 1);
244         swf_SetU16(tag, 0);
245         swf_SetString(tag, as3_getglobalclass());
246     } else {
247         as3_warning("no global public MovieClip subclass");
248     }
249     
250     as3_destroy();
251
252     tag = swf_InsertTag(tag, ST_SHOWFRAME);
253     tag = swf_InsertTag(tag, ST_END);
254
255     swf_FreeABC(code);
256     
257     if(local_with_filesystem)
258         swf.fileAttributes &= ~FILEATTRIBUTE_USENETWORK;
259     if(local_with_networking)
260         swf.fileAttributes |= FILEATTRIBUTE_USENETWORK;
261
262     writeSWF(&swf);
263     swf_FreeTags(&swf);
264
265     return 0;
266 }
267