浏览代码

resource support for As3 target

Simon Krajewski 13 年之前
父节点
当前提交
db43e1320d
共有 4 个文件被更改,包括 76 次插入20 次删除
  1. 41 8
      genas3.ml
  2. 28 3
      std/flash/_std/haxe/Resource.hx
  3. 0 2
      tests/unit/TestResource.hx
  4. 7 7
      tests/unit/unit.hxml

+ 41 - 8
genas3.ml

@@ -113,16 +113,16 @@ let reserved =
 let s_ident n =
 	if Hashtbl.mem reserved n then "_" ^ n else n
 
+let rec create_dir acc = function
+	| [] -> ()
+	| d :: l ->
+		let dir = String.concat "/" (List.rev (d :: acc)) in
+		if not (Sys.file_exists dir) then Unix.mkdir dir 0o755;
+		create_dir (d :: acc) l
+
 let init infos path =
-	let rec create acc = function
-		| [] -> ()
-		| d :: l ->
-			let dir = String.concat "/" (List.rev (d :: acc)) in
-			if not (Sys.file_exists dir) then Unix.mkdir dir 0o755;
-			create (d :: acc) l
-	in
 	let dir = infos.com.file :: fst path in
-	create [] dir;
+	create_dir [] dir;
 	let ch = open_out (String.concat "/" dir ^ "/" ^ snd path ^ ".as") in
 	let imports = Hashtbl.create 0 in
 	Hashtbl.add imports (snd path) [fst path];
@@ -287,6 +287,38 @@ let escape_bin s =
 	done;
 	Buffer.contents b
 
+let generate_resources infos =
+	if Hashtbl.length infos.com.resources <> 0 then begin
+		let dir = (infos.com.file :: ["__res"]) in
+		create_dir [] dir;
+		let add_resource name data =
+			Std.output_file (String.concat "/" (dir @ [name])) data
+		in
+		Hashtbl.iter (fun name data -> add_resource name data) infos.com.resources;
+		let ctx = init infos ([],"__resources__") in
+		spr ctx "\timport flash.utils.Dictionary;\n";
+		spr ctx "\tpublic class __resources__ {\n";
+		spr ctx "\t\tpublic static var list:Dictionary;\n";
+		let inits = ref [] in
+		let k = ref 0 in
+		Hashtbl.iter (fun name _ ->
+			let varname = ("v" ^ (string_of_int !k)) in
+			k := !k + 1;
+			print ctx "\t\t[Embed(source = \"__res/%s\", mimeType = \"application/octet-stream\")]\n" name;
+			print ctx "\t\tpublic static var %s:Class;\n" varname;
+			inits := ("list[\"" ^name^ "\"] = " ^ varname ^ ";") :: !inits;
+		) infos.com.resources;
+		spr ctx "\t\tstatic public function __init__():void {\n";
+		spr ctx "\t\t\tlist = new Dictionary();\n";
+		List.iter (fun init ->
+			print ctx "\t\t\t%s\n" init
+		) !inits;
+		spr ctx "\t\t}\n";
+		spr ctx "\t}\n";
+		spr ctx "}";
+		close ctx;
+	end
+
 let gen_constant ctx p = function
 	| TInt i -> print ctx "%ld" i
 	| TFloat s -> spr ctx s
@@ -1149,6 +1181,7 @@ let generate com =
 	let infos = {
 		com = com;
 	} in
+	generate_resources infos;
 	let ctx = init infos ([],"enum") in
 	generate_base_enum ctx;
 	close ctx;

+ 28 - 3
std/flash/_std/haxe/Resource.hx

@@ -24,6 +24,33 @@
  */
 package haxe;
 
+#if as3
+class Resource {
+	public static function listNames() : Array<String> untyped {
+		return __keys__(__resources__.list);
+	}
+
+	public static function getString( name : String ) : String {
+		var b = resolve(name);
+		return b == null ? null : b.readUTFBytes(b.length);
+	}
+
+	public static function getBytes( name : String ) : haxe.io.Bytes {
+		var b = resolve(name);
+		return b == null ? null : haxe.io.Bytes.ofData(b);
+	}
+	
+	static function resolve( name : String) :flash.utils.ByteArray untyped {
+		var n = __resources__.list[name];
+		if (n == null) return null;
+		return untyped __new__(n);
+	}
+	
+	static function __init__() {
+		untyped __resources__.__init__();
+	}	
+}
+#else
 class Resource {
 
 	static var content : Array<{ name : String }>;
@@ -55,9 +82,7 @@ class Resource {
 	}
 
 	static function __init__() {
-		#if !as3
 		content = untyped __resources__();
-		#end
 	}
-
 }
+#end

+ 0 - 2
tests/unit/TestResource.hx

@@ -4,7 +4,6 @@ class TestResource extends Test {
 
 	static var STR = "Héllo World !";
 
-	#if !as3
 	function testResources() {
 		var names = haxe.Resource.listNames();
 		eq( names.length, 2 );
@@ -31,7 +30,6 @@ class TestResource extends Test {
 		for( i in 0...lasts.length )
 			eq( b.get(b.length - lasts.length + i), lasts[i] );
 	}
-	#end
 
 	#if neko
 	static function main() {

+ 7 - 7
tests/unit/unit.hxml

@@ -42,6 +42,13 @@ params.hxml
 -main unit.Test
 -php php
 
+#as3
+--next
+params.hxml
+-main unit.Test
+-as3 as3
+-cmd mxmlc -static-link-runtime-shared-libraries=true -debug as3/__main__.as --output unit9_as3.swf
+
 #cpp
 --next
 -main RunCpp
@@ -52,13 +59,6 @@ params.hxml
 -main unit.Test
 -cpp cpp
 
-#as3
---next
-params.hxml
--main unit.Test
--as3 as3
--cmd mxmlc -debug as3/__main__.as --output unit9_as3.swf
-
 #java
 --next
 -main RunJava