Skip to content

Instantly share code, notes, and snippets.

@propella
Created August 6, 2009 04:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save propella/163142 to your computer and use it in GitHub Desktop.
Save propella/163142 to your computer and use it in GitHub Desktop.
(*
Generate SWF from a ActionScript bytecode file.
abc2swf filename.abc [width height]
build:
ocamlc -g -I ocaml -I ocaml/swflib ocaml/extLib.cma ocaml/swflib/swflib.cma -o abc2swf abc2swf.ml
*)
open Swf
let read_abc (infile:string) : tag =
let ch = IO.input_channel (open_in_bin infile) in
let contents = IO.read_all ch in
IO.close_in ch;
let out = IO.output_string () in
IO.write_i32 out 0x0; (* flags = 0x0 *)
IO.write_string out infile; (* name = infile *)
IO.nwrite out contents;
let data = IO.close_out out in
{ tid = 82; textended = false; tdata = TUnknown (82, data) }
let make_swf (abc:tag) (width:int) (height:int) (classname:string) : swf =
let header : header = {
h_version = 4;
h_size = ({rect_nbits = 16; left = 0; right = width * 20; top = 0; bottom = height * 20});
h_fps = 1;
h_frame_count = 1;
h_compressed = false;
} in
let body : tag list = [
{ tid = 0; textended = false; tdata = TSandbox (SBUnknown 0b00001000) };
abc;
{ tid = 0; textended = false; tdata = TF9Classes [{f9_cid = Some 0; f9_classname = classname}]};
{ tid = 0; textended = false; tdata = TShowFrame };
{ tid = 0; textended = false; tdata = TEnd };
] in
(header, body)
let write_swf (swf:swf) (outfile:string) =
let ch = IO.output_channel (open_out_bin outfile) in
write ch swf;
IO.close_out ch
let main2swf infile width height =
let classname = Filename.chop_extension infile in
let outfile = classname ^ ".swf" in
let abc = read_abc infile in
let swf = make_swf abc width height classname in
write_swf swf outfile
let main args = match args with
| [| _; infile; width; height; |] -> main2swf infile (int_of_string width) (int_of_string height)
| [| _; infile; |] -> main2swf infile 400 300
| _ -> failwith "Usage: abc2swf filename.abc [width] [height]"
;;
SwfParser.init (fun a -> a) (fun a -> a);;
main Sys.argv;;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment