Skip to content

Commit

Permalink
base64-encode resource names when writing to file system (see HaxeFou…
Browse files Browse the repository at this point in the history
  • Loading branch information
Simn committed Feb 26, 2015
1 parent 789c022 commit 3060ca7
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 19 deletions.
5 changes: 3 additions & 2 deletions genas3.ml
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ let generate_resources infos =
let dir = (infos.com.file :: ["__res"]) in
create_dir [] dir;
let add_resource name data =
let name = Base64.str_encode name in
let ch = open_out_bin (String.concat "/" (dir @ [name])) in
output_string ch data;
close_out ch
Expand All @@ -344,9 +345,9 @@ let generate_resources infos =
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\t[Embed(source = \"__res/%s\", mimeType = \"application/octet-stream\")]\n" (Base64.str_encode name);
print ctx "\t\tpublic static var %s:Class;\n" varname;
inits := ("list[\"" ^name^ "\"] = " ^ varname ^ ";") :: !inits;
inits := ("list[\"" ^ Ast.s_escape name ^ "\"] = " ^ varname ^ ";") :: !inits;
) infos.com.resources;
spr ctx "\t\tstatic public function __init__():void {\n";
spr ctx "\t\t\tlist = new Dictionary();\n";
Expand Down
3 changes: 2 additions & 1 deletion gencs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ let rec is_cs_basic_type t =
| TInst( { cl_path = (["haxe"], "Int32") }, [] )
| TInst( { cl_path = (["haxe"], "Int64") }, [] )
| TAbstract ({ a_path = (["cs"], "Int64") },[])
| TAbstract ({ a_path = (["cs"], "UInt64") },[])
| TAbstract ({ a_path = (["cs"], "UInt64") },[])
| TAbstract ({ a_path = ([], "Int") },[])
| TAbstract ({ a_path = ([], "Float") },[])
| TAbstract ({ a_path = ([], "Bool") },[]) ->
Expand Down Expand Up @@ -3018,6 +3018,7 @@ let configure gen =
gen.gcon.file ^ "/src/Resources"
in
Hashtbl.iter (fun name v ->
let name = Base64.str_encode name in
let full_path = src ^ "/" ^ name in
mkdir_from_path full_path;

Expand Down
2 changes: 1 addition & 1 deletion genjava.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2256,7 +2256,7 @@ let configure gen =
let res = ref [] in
Hashtbl.iter (fun name v ->
res := { eexpr = TConst(TString name); etype = gen.gcon.basic.tstring; epos = Ast.null_pos } :: !res;

let name = Base64.str_encode name in
let full_path = gen.gcon.file ^ "/src/" ^ name in
mkdir_from_path full_path;

Expand Down
5 changes: 3 additions & 2 deletions genpy.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2137,8 +2137,9 @@ module Generator = struct
end else
","
in
print ctx "%s'%s': open('%%s.%%s'%%(__file__,'%s'),'rb').read()" prefix k k;
Std.output_file (ctx.com.file ^ "." ^ k) v
let k_enc = Base64.str_encode k in
print ctx "%s\"%s\": open('%%s.%%s'%%(__file__,'%s'),'rb').read()" prefix (Ast.s_escape k) k_enc;
Std.output_file (ctx.com.file ^ "." ^ k_enc) v
) ctx.com.resources;
spr ctx "}"
end
Expand Down
2 changes: 2 additions & 0 deletions std/cs/_std/haxe/Resource.hx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ package haxe;
}

public static function getString( name : String ) : String {
name = haxe.crypto.Base64.encode(haxe.io.Bytes.ofString(name));
var path = getPaths().get(name);
var str = cs.Lib.toNativeType(haxe.Resource).Assembly.GetManifestResourceStream(path);
if (str != null)
Expand All @@ -53,6 +54,7 @@ package haxe;
}

public static function getBytes( name : String ) : haxe.io.Bytes {
name = haxe.crypto.Base64.encode(haxe.io.Bytes.ofString(name));
var path = getPaths().get(name);
var str = cs.Lib.toNativeType(haxe.Resource).Assembly.GetManifestResourceStream(path);
if (str != null)
Expand Down
2 changes: 2 additions & 0 deletions std/java/_std/haxe/Resource.hx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ package haxe;
}

public static function getString( name : String ) : String {
name = haxe.crypto.Base64.encode(haxe.io.Bytes.ofString(name));
var stream = cast(Resource, java.lang.Class<Dynamic>).getResourceAsStream("/" + name);
if (stream == null)
return null;
Expand All @@ -38,6 +39,7 @@ package haxe;
}

public static function getBytes( name : String ) : haxe.io.Bytes {
name = haxe.crypto.Base64.encode(haxe.io.Bytes.ofString(name));
var stream = cast(Resource, java.lang.Class<Dynamic>).getResourceAsStream("/" + name);
if (stream == null)
return null;
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/compile-each.hxml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
-debug
-cp src
-cp "C:\Program Files\The Haxe Effect\src/dev/null"
-resource res1.txt
-resource res2.bin
-resource res1.txt@res?!%[]))("'£1.txt
-resource res2.bin@res?!%[]))("'£1.bin
-dce full
-D analyzer
22 changes: 11 additions & 11 deletions tests/unit/src/unit/TestResource.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ class TestResource extends Test {
function testResources() {
var names = haxe.Resource.listNames();
eq( names.length, 2 );
if( names[0] == "res1.txt" )
eq( names[1], "res2.bin" );
if( names[0] == "res?!%[]))(\"'£1.txt" )
eq( names[1], "res?!%[]))(\"'£1.bin" );
else {
eq( names[0], "res2.bin" );
eq( names[1], "res1.txt" );
eq( names[0], "res?!%[]))(\"'£1.bin" );
eq( names[1], "res?!%[]))(\"'£1.txt" );
}
eq( haxe.Resource.getString("res1.txt"), STR );
eq( haxe.Resource.getString("res?!%[]))(\"'£1.txt"), STR );
#if (neko || php)
// allow binary strings
eq( haxe.Resource.getBytes("res2.bin").sub(0,9).toString(), "MZ\x90\x00\x03\x00\x00\x00\x04" );
eq( haxe.Resource.getBytes("res?!%[]))(\"'£1.bin").sub(0,9).toString(), "MZ\x90\x00\x03\x00\x00\x00\x04" );
#else
// cut until first \0
eq( haxe.Resource.getString("res2.bin").substr(0,2), "MZ" );
eq( haxe.Resource.getString("res?!%[]))(\"'£1.bin").substr(0,2), "MZ" );
#end
eq( haxe.Resource.getBytes("res1.txt").compare(haxe.io.Bytes.ofString(STR)), 0 );
var b = haxe.Resource.getBytes("res2.bin");
eq( haxe.Resource.getBytes("res?!%[]))(\"'£1.txt").compare(haxe.io.Bytes.ofString(STR)), 0 );
var b = haxe.Resource.getBytes("res?!%[]))(\"'£1.bin");
var firsts = [0x4D,0x5A,0x90,0x00,0x03,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0xB8];
var lasts = [0xD6,0x52,0x03,0x1A,0x2C,0x4E,0x45,0x4B,0x4F,0x00,0x1C,0x00,0x00];
for( i in 0...firsts.length )
Expand All @@ -33,10 +33,10 @@ class TestResource extends Test {

#if neko
static function main() {
var ch = sys.io.File.write("res1.txt",true);
var ch = sys.io.File.write("res?!%[]))(\"'£1.txt",true);
ch.writeString(STR);
ch.close();
var ch = sys.io.File.write("res2.bin",true);
var ch = sys.io.File.write("res?!%[]))(\"'£1.bin",true);
ch.writeString("Héllo");
ch.writeByte(0);
ch.writeString("World");
Expand Down

0 comments on commit 3060ca7

Please sign in to comment.