Browse Source

Merge pull request #27 from ousado/genpy

read resources from files
Simon Krajewski 11 years ago
parent
commit
741b7dfb6e
3 changed files with 17 additions and 4 deletions
  1. 3 2
      genpy.ml
  2. 13 1
      std/haxe/Resource.hx
  3. 1 1
      tests/unit/Test.hx

+ 3 - 2
genpy.ml

@@ -1728,7 +1728,7 @@ module Generator = struct
 
 
 	let gen_resources ctx =
 	let gen_resources ctx =
 		if Hashtbl.length ctx.com.resources > 0 then begin
 		if Hashtbl.length ctx.com.resources > 0 then begin
-			spr ctx "def _hx_resources__():\n\treturn {";
+			spr ctx "import os;\ndef _hx_resources__():\n\treturn {";
 			let first = ref true in
 			let first = ref true in
 			Hashtbl.iter (fun k v ->
 			Hashtbl.iter (fun k v ->
 				let prefix = if !first then begin
 				let prefix = if !first then begin
@@ -1737,7 +1737,8 @@ module Generator = struct
 				end else
 				end else
 					","
 					","
 				in
 				in
-				print ctx "%s'%s':'''%s'''" prefix k (Codegen.bytes_serialize v)
+				print ctx "%s'%s': open('%%s.%%s'%%(__file__,'%s'),'rb').read()" prefix k k;
+				Std.output_file (ctx.com.file ^ "." ^ k) v
 			) ctx.com.resources;
 			) ctx.com.resources;
 			spr ctx "}\n"
 			spr ctx "}\n"
 		end
 		end

+ 13 - 1
std/haxe/Resource.hx

@@ -20,6 +20,10 @@
  * DEALINGS IN THE SOFTWARE.
  * DEALINGS IN THE SOFTWARE.
  */
  */
 package haxe;
 package haxe;
+#if python
+import haxe.io.Bytes;
+import haxe.io.BytesData;
+#end
 
 
 /**
 /**
 	Resource can be used to access resources that were added through the
 	Resource can be used to access resources that were added through the
@@ -35,7 +39,7 @@ class Resource {
 	#if (java || cs)
 	#if (java || cs)
 	@:keep static var content : Array<String>;
 	@:keep static var content : Array<String>;
 	#elseif python
 	#elseif python
-	static var content : python.lib.Types.Dict<String, String>;
+	static var content : python.lib.Types.Dict<String, BytesData>;
 	#else
 	#else
 	static var content : Array<{ name : String, data : String, str : String }>;
 	static var content : Array<{ name : String, data : String, str : String }>;
 	#end
 	#end
@@ -98,6 +102,7 @@ class Resource {
 			return new cs.io.NativeInput(str).readAll().toString();
 			return new cs.io.NativeInput(str).readAll().toString();
 		return null;
 		return null;
 		#elseif python
 		#elseif python
+        #if embed_resources
 		for( k in content.keys().iter() )
 		for( k in content.keys().iter() )
 			if( k == name ) {
 			if( k == name ) {
 				var b : haxe.io.Bytes = haxe.crypto.Base64.decode(content.get(k, null));
 				var b : haxe.io.Bytes = haxe.crypto.Base64.decode(content.get(k, null));
@@ -105,6 +110,9 @@ class Resource {
 
 
 			}
 			}
 		return null;
 		return null;
+        #else
+        return content.hasKey(name) ? Bytes.ofData(content.get(name,null)).toString() : null;
+        #end
 		#else
 		#else
 		for( x in content )
 		for( x in content )
 			if( x.name == name ) {
 			if( x.name == name ) {
@@ -140,12 +148,16 @@ class Resource {
 			return new cs.io.NativeInput(str).readAll();
 			return new cs.io.NativeInput(str).readAll();
 		return null;
 		return null;
 		#elseif python
 		#elseif python
+        #if embed_resources
 		for( k in content.keys().iter() )
 		for( k in content.keys().iter() )
 			if( k == name ) {
 			if( k == name ) {
 				var b : haxe.io.Bytes = haxe.crypto.Base64.decode(content.get(k, null));
 				var b : haxe.io.Bytes = haxe.crypto.Base64.decode(content.get(k, null));
 				return b;
 				return b;
 
 
 			}
 			}
+        #else
+        return Bytes.ofData(content.get(name,null));
+        #end
 		return null;
 		return null;
 		#else
 		#else
 		for( x in content )
 		for( x in content )

+ 1 - 1
tests/unit/Test.hx

@@ -257,7 +257,7 @@ class Test #if swf_mark implements mt.Protect #end {
 			new TestEReg(),
 			new TestEReg(),
 			new TestXML(),
 			new TestXML(),
 			new TestMisc(),
 			new TestMisc(),
-			//new TestResource(),
+			new TestResource(),
 			new TestInt64(),
 			new TestInt64(),
 			new TestReflect(),
 			new TestReflect(),
 			new TestSerialize(),
 			new TestSerialize(),