Browse Source

store resources in binary tags

Nicolas Cannasse 14 years ago
parent
commit
835d2101e4
4 changed files with 87 additions and 6 deletions
  1. 1 0
      doc/CHANGES.txt
  2. 9 2
      genswf.ml
  3. 14 4
      genswf9.ml
  4. 63 0
      std/flash9/_std/haxe/Resource.hx

+ 1 - 0
doc/CHANGES.txt

@@ -26,6 +26,7 @@
 	all : optimized macro engine (speed x2)
 	all : added -D macrotimes support
 	all : added untyped __this__ support (prepare for 'this' spec. change)
+	flash9 : store resources in bytes tag instead of bytecode
 
 2011-01-30: 2.07
 	all : fixed completion support with --remap

+ 9 - 2
genswf.ml

@@ -811,8 +811,15 @@ let build_swf9 com file swc =
 		) code in
 		[tag (TActionScript3 (None,As3hlparse.flatten inits))]
 	) in
-	let clips = [tag (TF9Classes [{ f9_cid = None; f9_classname = boot_name }])] in
-	code @ clips
+	let cid = ref 0 in
+	let classes = ref [{ f9_cid = None; f9_classname = boot_name }] in
+	let res = Hashtbl.fold (fun name data acc ->
+		incr cid;
+		classes := { f9_cid = Some !cid; f9_classname = s_type_path (Genswf9.resource_path name) } :: !classes;
+		tag (TBinaryData (!cid,data)) :: acc
+	) com.resources [] in
+	let clips = [tag (TF9Classes (List.rev !classes))] in
+	res @ code @ clips
 
 let merge com file priority (h1,tags1) (h2,tags2) =
   (* prioritize header+bgcolor for first swf *)

+ 14 - 4
genswf9.ml

@@ -1357,9 +1357,7 @@ and gen_call ctx retval e el r =
 			incr count;
 			write ctx (HString "name");
 			write ctx (HString name);
-			write ctx (HString "data");
-			write ctx (HString (Codegen.bytes_serialize data));
-			write ctx (HObject 2);
+			write ctx (HObject 1);
 		) ctx.com.resources;
 		write ctx (HArray !count)
 	| TLocal { v_name = "__vmem_set__" }, [{ eexpr = TConst (TInt code) };e1;e2] ->
@@ -2171,6 +2169,17 @@ let generate_type ctx t =
 	| TTypeDecl _ ->
 		None
 
+let resource_path name = 
+	(["_res"],"_" ^ String.concat "_" (ExtString.String.nsplit name "."))
+
+let generate_resource ctx name =	
+	let c = mk_class (resource_path name) null_pos in
+	c.cl_super <- Some (mk_class (["flash";"utils"],"ByteArray") null_pos,[]);
+	let t = TClassDecl c in
+	match generate_type ctx t with
+	| Some (m,f) -> (t,m,f)
+	| None -> assert false
+
 let generate com boot_name =
 	let ctx = {
 		com = com;
@@ -2208,11 +2217,12 @@ let generate com boot_name =
 	else
 		com.types
 	in
+	let res = Hashtbl.fold (fun name _ acc -> generate_resource ctx name :: acc) com.resources [] in
 	let classes = List.fold_left (fun acc t ->
 		match generate_type ctx t with
 		| None -> acc
 		| Some (m,f) -> (t,m,f) :: acc
-	) [] types in
+	) res types in
 	List.rev classes
 
 ;;

+ 63 - 0
std/flash9/_std/haxe/Resource.hx

@@ -0,0 +1,63 @@
+/*
+ * Copyright (c) 2005-2008, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+package haxe;
+
+class Resource {
+
+	static var content : Array<{ name : String }>;
+
+	public static function listNames() : Array<String> {
+		var names = new Array();
+		for( x in content )
+			names.push(x.name);
+		return names;
+	}
+
+	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 {
+		try untyped {
+			var c = __as__(__global__["flash.utils.getDefinitionByName"]("_res._"+name.split(".").join("_")),Class);
+			return __new__(c);
+		} catch( e : Dynamic ) {
+			return null;
+		}
+	}
+
+	static function __init__() {
+		#if !as3
+		content = untyped __resources__();
+		#end
+	}
+
+}