17
17
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
/* $Id: action.c,v 1.10 2004/03/30 15:07:28 strk Exp $ */
20
/* $Id: action.c,v 1.24 2008/09/28 16:53:58 krechert Exp $ */
22
27
#include "action.h"
23
#include "outputblock.h"
24
32
#include "libming.h"
27
SWFAction newSWFAction()
29
return newSWFOutputBlock(newSWFOutput(), SWF_DOACTION);
33
#include "character.h"
34
#include "movieclip.h"
35
#include "actioncompiler/compile.h"
36
#include "actiontypes.h"
46
struct SWFBlock_s block;
47
ActionInputType inputType;
57
struct SWFInitAction_s
59
struct SWFBlock_s block;
65
static char *readActionFile(FILE *file)
69
SWFInput input = newSWFInput_file(file);
70
len = SWFInput_length(input);
71
script = (char *)malloc(len + 1);
72
if(SWFInput_read(input, (unsigned char *)script, len) != len)
74
SWF_warn("readActionFile failed\n");
78
destroySWFInput(input);
83
void SWFOutput_writeAction(SWFOutput out, SWFAction action)
88
if(action->out == NULL)
90
SWF_warn("SWFAction: compile action first\n");
94
len = SWFOutput_getLength(action->out);
95
data = SWFOutput_getBuffer(action->out);
96
SWFOutput_writeBuffer(out, data, len);
100
* Compiles the current script stored in this SWFAction instance.
101
* returns 0 on success, -1 otherwise.
102
* the length of the compiled bytecode is storen in the length pointer (if not NULL).
104
int SWFAction_compile(SWFAction action,
105
int swfVersion /* target SWF version */,
106
int *length /* output length */)
112
if(action->out != NULL)
115
*length = SWFOutput_getLength(action->out);
119
switch(action->inputType)
122
script = action->input.script;
125
script = readActionFile(action->input.file);
130
if(script != NULL && swfVersion == 4)
132
swf4ParseInit(script, action->debug, swfVersion);
133
parserError = swf4parse((void *)&b);
135
else if (script != NULL)
137
swf5ParseInit(script, action->debug, swfVersion);
138
parserError = swf5parse((void *)&b);
143
// if INPUT_FILE script was allocated by readActionFile()
144
if(action->inputType == INPUT_FILE)
147
action->out = newSWFOutput();
151
SWFOutput_writeBuffer(action->out, b->buffer, bufferLength(b));
155
SWF_warn("Parser error: writing empty block\n");
157
SWFOutput_writeUInt8(action->out, SWFACTION_END);
159
*length = SWFOutput_getLength(action->out);
168
* Returns the compiled bytecode.
169
* If not already compiled the script will compiled for SWF V7.
171
* Returns NULL in case of an error. Length pointer stores the length of
172
* the compiled bytecode.
174
byte *SWFAction_getByteCode(SWFAction action, int *length)
180
if(action->out == NULL)
182
SWF_warn("SWFAction_getByteCode: please use SWFAction_compile first\n");
183
SWF_warn("auto-compiling as SWF 7 code now...\n");
184
ret = SWFAction_compile(action, 7, (int *)length);
192
return SWFOutput_getBuffer(action->out);
196
completeSWFAction(SWFBlock block)
199
SWFAction action = (SWFAction)block;
200
SWFAction_compile(action, block->swfVersion, &length);
205
completeSWFInitAction(SWFBlock block)
207
SWFInitAction init = (SWFInitAction)block;
210
SWFAction_compile(init->action, block->swfVersion, &len);
216
writeSWFActionToMethod(SWFBlock block, SWFByteOutputMethod method, void* data)
218
SWFOutput out = ((SWFAction)block)->out;
219
SWFOutput_writeToMethod(out, method, data);
223
writeSWFInitActionToMethod(SWFBlock block, SWFByteOutputMethod method, void* data)
225
SWFInitAction init = (SWFInitAction)block;
226
methodWriteUInt16(init->spriteId, method, data);
227
SWFOutput_writeToMethod(init->action->out, method, data);
231
* Destroys a SWFAction instance
33
233
void destroySWFAction(SWFAction action)
35
destroySWFOutputBlock(action);
39
SWFAction newSWFAction_fromOutput(SWFOutput out)
41
return newSWFOutputBlock(out, SWF_DOACTION);
238
switch(action->inputType)
241
fclose(action->input.file);
244
free(action->input.script);
251
destroySWFOutput(action->out);
256
void destroySWFInitAction(SWFInitAction init)
262
destroySWFMovieClip(init->clip);
263
destroySWFAction(init->action);
268
static SWFAction createEmptyAction()
270
SWFAction action = (SWFAction)malloc(sizeof(struct SWFAction_s));
272
SWFBlockInit(BLOCK(action));
273
BLOCK(action)->type = SWF_DOACTION;
274
BLOCK(action)->writeBlock = writeSWFActionToMethod;
275
BLOCK(action)->complete = completeSWFAction;
276
BLOCK(action)->dtor = (destroySWFBlockMethod) destroySWFAction;
277
action->inputType = INPUT_EMPTY;
284
* enable verbose compiler output
286
* Set debug value to 1 get very! verbose compile messages.
289
int SWFAction_setDebug(SWFAction a, int debug /*debug switch*/)
300
* Creates a new SWFAction object.
301
* Takes a String containing AS[2] source code.
303
* returns a SWFAction instance.
305
SWFAction newSWFAction(const char *script)
307
SWFAction action = createEmptyAction();
308
action->inputType = INPUT_SCRIPT;
309
action->input.script = strdup(script);
315
* Creates a new SWFAction object.
316
* Takes a filename pointing to a file containing AS[2] source code.
318
* return a SWFAction instance.
320
SWFAction newSWFAction_fromFile(const char *filename)
322
SWFAction action = createEmptyAction();
323
action->inputType = INPUT_FILE;
324
action->input.file = fopen(filename, "r");
325
if(action->input.file == NULL)
327
destroySWFAction(action);
333
SWFMovieClip SWFInitAction_getMovieClip(SWFInitAction action)
339
* create a InitAction block with a given sprite's character id
341
* This function creates a InitAction block with a given sprite's character id.
344
SWFInitAction newSWFInitAction_withId(SWFAction action, int id /* mc character id */)
346
SWFInitAction init = (SWFInitAction)malloc(sizeof(struct SWFInitAction_s));
347
SWFBlockInit(BLOCK(init));
348
BLOCK(init)->writeBlock = writeSWFInitActionToMethod;
349
BLOCK(init)->complete = completeSWFInitAction;
350
BLOCK(init)->dtor = (destroySWFBlockMethod) destroySWFInitAction;
351
BLOCK(init)->type = SWF_INITACTION;
352
init->clip = NULL; // use external clip
354
init->action = action;
359
* create a InitAction block
361
* This function creates a InitAction block and defines an empty sprite/mc
362
* which is not placed. This functions is usefull for defining classes.
364
SWFInitAction newSWFInitAction(SWFAction action)
366
SWFInitAction init = (SWFInitAction)malloc(sizeof(struct SWFInitAction_s));
367
SWFBlockInit(BLOCK(init));
368
BLOCK(init)->writeBlock = writeSWFInitActionToMethod;
369
BLOCK(init)->complete = completeSWFInitAction;
370
BLOCK(init)->dtor = (destroySWFBlockMethod) destroySWFInitAction;
371
BLOCK(init)->type = SWF_INITACTION;
372
init->clip = newSWFMovieClip();
373
init->spriteId = CHARACTERID(init->clip);
374
init->action = action;
378
SWFInitAction newSWFInitAction_MovieClip(SWFMovieClip clip, SWFAction action)
380
SWFInitAction init = (SWFInitAction)malloc(sizeof(struct SWFInitAction_s));
381
SWFBlockInit(BLOCK(init));
382
BLOCK(init)->writeBlock = writeSWFInitActionToMethod;
383
BLOCK(init)->complete = completeSWFInitAction;
384
BLOCK(init)->dtor = (destroySWFBlockMethod) destroySWFInitAction;
385
BLOCK(init)->type = SWF_INITACTION;
386
init->spriteId = CHARACTERID(clip);
387
init->clip = NULL; // use external clip
388
init->action = action;
46
393
* Local variables: