Created
May 10, 2015 14:11
-
-
Save zwetan/30dbe8da53e30005965c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/as3shebang -- | |
import flash.utils.*; | |
import shell.*; | |
import C.stdlib.*; | |
/* NOTE: | |
how to embed binaries in plain text | |
in our case how to embed ABC libraries in shell scripts | |
one solution is to parse the ABC binary data | |
and generate hex strings that we can parse at runtime | |
usage: ./make_hexdata path/to/lib.abc namelib | |
for example | |
./make_hexdata ansilib.abc ansilib | |
will generate the file ansilib.inc | |
*/ | |
if( Program.argv.length < 2 ) | |
{ | |
trace( "not enough arguments" ); | |
trace( "usage: ./make_hexdata path/to/lib.abc namelib" ); | |
exit( 0 ); | |
} | |
var FILENAME:String = Program.argv[0]; | |
var CLASSNAME:String = Program.argv[1]; | |
if( FILENAME == "" ) | |
{ | |
trace( "FILENAME is empty" ); | |
exit( 0 ); | |
} | |
if( CLASSNAME == "" ) | |
{ | |
trace( "CLASSNAME is empty" ); | |
exit( 0 ); | |
} | |
function byteToHex( bytes:* ):String | |
{ | |
var b:uint = bytes.readUnsignedByte(); | |
var h:String = b.toString(16); | |
return (h.length == 2) ? h: "0"+h; | |
} | |
var bytes:* = FileSystem.readByteArray( FILENAME ); | |
bytes.position = 0; | |
var output:String = ""; | |
output += "\n"; | |
output += "import flash.utils.*;\n"; | |
output += "import shell.*;\n"; | |
output += "\n"; | |
output += "function " + CLASSNAME + "():*\n"; | |
output += "{\n"; | |
output += " var hex:String = \""; | |
var max_row:uint = 80; | |
var total:uint = 0; | |
var row:uint = 0; | |
while( bytes.bytesAvailable ) | |
{ | |
output += byteToHex( bytes ); | |
total += 2; | |
row += 2; | |
if( row > max_row ) | |
{ | |
output += "\";\n"; | |
output += " hex += \""; | |
row = 0; | |
} | |
} | |
output += "\";\n"; | |
output += " var bytes:* = new ByteArray();\n"; | |
output += "\n"; | |
output += " var i:uint;\n"; | |
output += " var len:uint = hex.length;\n"; | |
output += " for( i = 0; i < len; i += 2 )\n"; | |
output += " {\n"; | |
output += " var b:String = hex.substr( i , 2 );\n"; | |
output += " var result:uint = uint( \"0x\" + b );\n"; | |
output += " bytes.writeByte( result );\n"; | |
output += " }\n"; | |
output += " bytes.position = 0;\n"; | |
output += " return bytes;\n"; | |
output += "}\n"; | |
output += "\n"; | |
output += "\n"; | |
FileSystem.write( CLASSNAME + ".inc", output ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment