以前自己写编辑器的时候需要读取一个flash生成的swf文件 然后得到swf中的链接名字
就是这个样子,然后我从网上找了好多例子都没有很好的,最后用swf的文件结构实现了读取swf中链接的工具
今天有一个朋友也遇到了这个需求,处于方便写篇帖子,以后有需要的朋友都可以参考下
AnalysisSwf.as类
package
{import flash.display.ActionScriptVersion;
import flash.display.Bitmap;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLRequest;
import flash.utils.ByteArray;
import flash.utils.Endian;
import flashx.textLayout.elements.InlineGraphicElement;
/**
*解析swf文件链接
*@author 七公子_崛
*/
public class AnalysisSwf extends Sprite
{
private static var intance:AnalysisSwf;
public static function getInstance():AnalysisSwf{
if(!intance) intance = new AnalysisSwf();
return intance;
}
private var _swfByteArray:ByteArray = new ByteArray;
private const COMPRESSED:String = "CWS";
private var _swfSize:int;
private var _frameRate:int;
private var _frameTotal:int;
/***版本信息***/
private var _version:int;
/***swf中链接名字***/
private var _classList:Array;
/***解析完毕回调的方法***/
private var analysisCompleteFun:Function;
/***swf***/
private var swfMovieClip:MovieClip;
/***swf的在本机计算机上地址***/
private var swfPath:String;
/***用来加载swf***/
private var loader:Loader = new Loader();
public function AnalysisSwf()
{
super();
}
/**
* 开始解析swf文件
* @param _byte swf二进制
* @param _fun 解析完毕回调方法
*/
public function startAnalysis(_byte:ByteArray,_fun:Function):void{
analysisCompleteFun = _fun;
var tempByteArray:ByteArray = _byte;
//是否 压缩
var compressed:String = tempByteArray.readUTFBytes(3);
//swf 版本
_version = tempByteArray.readByte();
//
var length:uint = tempByteArray.readUnsignedInt();
tempByteArray.position = 8;
tempByteArray.readBytes(_swfByteArray);
if(compressed == COMPRESSED)
{
_swfByteArray.uncompress();
}
_swfByteArray.endian = Endian.LITTLE_ENDIAN;
// 解析 swf 宽度 高度 数据 rect 数据
_swfSize = _swfByteArray.readUnsignedByte()>>3;
_swfByteArray.position = Math.ceil((_swfSize*4)/8+5);// 计算 rect 结束位置
trace("当前ByteArr游标",_swfByteArray.position);
_frameRate = _swfByteArray.readShort()/256;//读取帧频 因为低8位是小数,所以需要除以2的8次方
_frameTotal = _swfByteArray.readShort();//读取 总帧数
trace("compressed:",compressed,"swf_version:",_version,"frameRate:",_frameRate,"frameTotal:",_frameTotal);
parseTagType();
}
private function parseTagType():void
{
//设置读取数据的字节顺序为倒序(以字节为单位)
_swfByteArray.endian = Endian.LITTLE_ENDIAN;
while(_swfByteArray.bytesAvailable)
{
var tagHead:int = _swfByteArray.readShort();
var tagType:int = tagHead>>6;
//0x3F 00111111
var tagLength:int = tagHead & 0x3F;
if(tagLength == 63) //如果tag 是长类型
{
tagLength = _swfByteArray.readUnsignedInt();
}
// 解析 symbolClass tag
if(tagType == 76)
{
parseSymbolClass(tagLength);
}
else
{
_swfByteArray.position += tagLength;
}
}
}
private function parseSymbolClass(length:int):void
{
_classList = [];
var classNum:int = _swfByteArray.readShort();
while(classNum --)
{
var classId:int = _swfByteArray.readUnsignedShort();
// trace("classId之后的位置是"+_swfByteArray.position);
var char:int = _swfByteArray.readByte();
var name:String = "";
while(char)
{
name += String.fromCharCode(char);
char = _swfByteArray.readByte();
}
trace("导出类名为"+name);
_classList.push(name);
}
analysisCompleteFun(_classList);
}
protected function waitSwf(event:Event):void
{
}
}
}
startAnalysis方法中需要传入载入swf的二进制,然后和一个解析完毕的回调方法 回调方法中的参数是一个数组,里面存放了所有找到的链接名字