自从AS3引入ByteArray之后,可以玩的花样更多了,下面这种把SWF植入AS3的用法虽不常用,但是绝对可以满足初级代码控的需求。
思路就是把SWF或者PNG/JPG的资源二进制文件流,以ASCII CODE的方式放置在一个AS数组,运行时写入到ByteArray中去,再用loadBytes加载到场景。
这种方式比较适合把一些小的SWF文件嵌入到AS代码中去,不建议植入大文件或者大量植入。
这种方式比较适合把一些小的SWF文件嵌入到AS代码中去,不建议植入大文件或者大量植入。
原理:
代码
/* generic by swf2class */
/* original file: as2laucher.swf*/
package
{
import flash.display.Loader;
import flash.utils.ByteArray;
public class Test extends Loader
{
public function Test()
{
super();
var bytes:Array=[67,87,83,9,73,11,0,0,120,-100,-83,
86,75,83,-29,70,16,110,61,108,11,99/*,...*/];
var bin:ByteArray=new ByteArray();
for(var i:int=0;i<bytes.length-1;bin.writeByte(bytes[i++]));
this.loadBytes(bin);
}
}
}
/* generic by swf2class */
/* original file: as2laucher.swf*/
package
{
import flash.display.Loader;
import flash.utils.ByteArray;
public class Test extends Loader
{
public function Test()
{
super();
var bytes:Array=[67,87,83,9,73,11,0,0,120,-100,-83,
86,75,83,-29,70,16,110,61,108,11,99/*,...*/];
var bin:ByteArray=new ByteArray();
for(var i:int=0;i<bytes.length-1;bin.writeByte(bytes[i++]));
this.loadBytes(bin);
}
}
}
基于命令行的swf2class代码:
代码
/**
* swf2class
*
* by jackfu
*
* build:
* vc: cl.exe swf2class.c -o swf2class.exe
* gcc: gcc swf2class.c -o swf2class
*
* usage:
* swf2class "swf file" "namespace" "Classname" > Classname.as
*/
#include <stdio.h>
int main(int argc, char** argv)
{
FILE *fp;
char buffer[1];
char *pfile; // swf filename
char *pns; // namespace
char *pclass; // class name
if (argc<4)
{
printf("usage:\n\t%s swf namespace classname\n", argv[0]);
return 0;
}
pfile = argv[1];
pns = argv[2];
pclass = argv[3];
fp = fopen(pfile, "rb");
if (fp)
{
printf("/* generic by swf2class */\n/* original file: %s*/\n\n", pfile);
printf("package %s\n{\n\timport flash.display.Loader;\n\timport flash.utils.ByteArray;\n\n\tpublic class %s extends Loader\n\t{\n\t\tpublic function %s()\n\t\t{\n\t\t\tsuper();\n\t\t\tvar bytes:Array=[", pns, pclass, pclass);
while(!feof(fp))
{
fread(buffer, sizeof(char), 1, fp );
printf("%d,", buffer[0]);
}
printf("];\n\t\t\tvar bin:ByteArray=new ByteArray();for(var i:int=0;i<bytes.length-1;bin.writeByte(bytes[i++]));\n\t\t\tthis.loadBytes(bin);\n\t\t}\n\t}\n}\n");
fclose(fp);
}
return 0;
}
* swf2class
*
* by jackfu
*
* build:
* vc: cl.exe swf2class.c -o swf2class.exe
* gcc: gcc swf2class.c -o swf2class
*
* usage:
* swf2class "swf file" "namespace" "Classname" > Classname.as
*/
#include <stdio.h>
int main(int argc, char** argv)
{
FILE *fp;
char buffer[1];
char *pfile; // swf filename
char *pns; // namespace
char *pclass; // class name
if (argc<4)
{
printf("usage:\n\t%s swf namespace classname\n", argv[0]);
return 0;
}
pfile = argv[1];
pns = argv[2];
pclass = argv[3];
fp = fopen(pfile, "rb");
if (fp)
{
printf("/* generic by swf2class */\n/* original file: %s*/\n\n", pfile);
printf("package %s\n{\n\timport flash.display.Loader;\n\timport flash.utils.ByteArray;\n\n\tpublic class %s extends Loader\n\t{\n\t\tpublic function %s()\n\t\t{\n\t\t\tsuper();\n\t\t\tvar bytes:Array=[", pns, pclass, pclass);
while(!feof(fp))
{
fread(buffer, sizeof(char), 1, fp );
printf("%d,", buffer[0]);
}
printf("];\n\t\t\tvar bin:ByteArray=new ByteArray();for(var i:int=0;i<bytes.length-1;bin.writeByte(bytes[i++]));\n\t\t\tthis.loadBytes(bin);\n\t\t}\n\t}\n}\n");
fclose(fp);
}
return 0;
}
可以考虑用于核心代码的加密