• flex swf和movieclip之前的微妙关系


    开始不清楚, 一直尝试想load图片一样加载一个swftools转换的swf,然后在尝试转换成movieclip的时候,总是报错, avmiMovieClip 不能转换成movieclip之类的。

    但为什么有的swf可以轻松转换成movieclip呢?

    后面我明白这两种movieclip根本就是不同存储格式了,—— 虽然都是swf后缀

    关于movieclip ,我一直不太明白,其实它相当的有用。 

    package
    {
    import flash.display.Loader;
    import flash.errors.EOFError;
    import flash.events.Event;
    import flash.events.IOErrorEvent;
    import flash.events.ProgressEvent;
    import flash.events.SecurityErrorEvent;
    import flash.net.URLRequest;
    import flash.net.URLStream;
    import flash.system.ApplicationDomain;
    import flash.system.LoaderContext;
    import flash.utils.ByteArray;
    import flash.utils.Endian;
    
    import mx.controls.Alert;
    
    
    public class ForcibleLoader
    {
        public function ForcibleLoader()//loader:Loader
        {
            this.loader = new Loader();
            
            _stream = new URLStream();
            _stream.addEventListener(Event.COMPLETE, completeHandler);
            _stream.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            _stream.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
        }
        
        private var _loader:Loader;
        private var _stream:URLStream;
        private var _callback:Function;
        
        public function get loader():Loader
        {
            return _loader;
        }
        
        public function set loader(value:Loader):void
        {
            _loader = value;
        }
        
        public function load(url:String, callback:Function):void
        {
            var request:URLRequest = new URLRequest(url);
            this._callback = callback;
            _stream.load(request);
            
    //        var urlRequest:URLRequest = new URLRequest("adobe1231.swf");
    //        var urlLoader:URLLoader=new URLLoader();
    //        urlLoader.addEventListener(Event.COMPLETE,completeHandler);
    //        urlLoader.dataFormat=URLLoaderDataFormat.BINARY;
            
    
        }
            
        private function completeHandler(event:Event):void
        {
            var inputBytes:ByteArray = new ByteArray();
            _stream.readBytes(inputBytes);
    //        _stream.close();
            inputBytes.endian = Endian.LITTLE_ENDIAN;
            
            if (isCompressed(inputBytes)) {
                uncompress(inputBytes);
            }
            
            var version:uint = uint(inputBytes[3]);
            
    //        if (version < 9) {
    //            updateVersion(inputBytes, 9);
    //        }
    //        if (version > 7) {
    //            flagSWF9Bit(inputBytes);
    //        }
    //        else {
    //            insertFileAttributesTag(inputBytes);
    //        }
            
            if (version <= 9) {
                if (version == 8 || version == 9)
                {
                    flagSWF9Bit(inputBytes);
                }else if (version <= 7)
                {
                    insertFileAttributesTag(inputBytes);
                }
                updateVersion(9, inputBytes);
            }
            
            this.loader = new Loader();
            loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loadProgress)
            loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError)
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete)
            
            
                    var context:LoaderContext = new LoaderContext();
                    context.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);
                    
            loader.loadBytes(inputBytes, context);//new LoaderContext(true, ApplicationDomain.currentDomain));
            //loader.addEventListener(Event.COMPLETE, loaderComplete)
        }
        
        private function loadComplete(evt:Event):void
        {
            Alert.show("sssssssssssss");
            //_callback(loader.content as MovieClip);
        }
        private function loadProgress(evt:ProgressEvent):void
        {
            trace("  ProgressEvent  : " +evt.bytesLoaded + "  / " +evt.bytesTotal);
            if(evt.bytesLoaded == evt.bytesTotal) {
                
                //_callback(loader.content as MovieClip);
            }
        }
        private function onError(evt:IOErrorEvent):void
        {
            Alert.show("  ProgressEvent  ");
        }
        
        
        private function isCompressed(bytes:ByteArray):Boolean
        {
            return bytes[0] == 0x43;
        }
        
        private function uncompress(bytes:ByteArray):void
        {
            var cBytes:ByteArray = new ByteArray();
            cBytes.writeBytes(bytes, 8);
            bytes.length = 8;
            bytes.position = 8;
            cBytes.uncompress();
            bytes.writeBytes(cBytes);
            bytes[0] = 0x46;
            cBytes.length = 0;
        }
        
    //    private function getBodyPosition(bytes:ByteArray):uint
    //    {
    //        var result:uint = 0;
    //        
    //        result += 3; // FWS/CWS
    //        result += 1; // version(byte)
    //        result += 4; // length(32bit-uint)
    //        
    //        var rectNBits:uint = bytes[result] >>> 3;
    //        result += (5 + rectNBits * 4) / 8; // stage(rect)
    //        
    //        result += 2;
    //        
    //        result += 1; // frameRate(byte)
    //        result += 2; // totalFrames(16bit-uint)
    //        
    //        return result;
    //    }
        
            public function flagSWF9Bit(bytes:ByteArray):void
            {
                var pos:int = findFileAttributesPosition(getBodyPosition(bytes), bytes);
                
                if (pos != -1) {
                    bytes[pos + 2] |= 0x08;
                }
                else {
                    insertFileAttributesTag(bytes);
                }
            }
            
            private function findFileAttributesPosition(offset:uint, bytes:ByteArray):int
            {
                bytes.position = offset;
                
                try {
                    for (;;) {
                        var byte:uint = bytes.readShort();
                        var tag:uint = byte >>> 6;
                        if (tag == 69) {
                            return bytes.position - 2;
                        }
                        var length:uint = byte & 0x3f;
                        if (length == 0x3f) {
                            length = bytes.readInt();
                        }
                        bytes.position += length;
                    }
                }
                catch (e:EOFError) {
                }
                
                return -1;
            }
            private function insertFileAttributesTag(bytes:ByteArray):void
            {
                var pos:uint = getBodyPosition(bytes);
                var afterBytes:ByteArray = new ByteArray();
                afterBytes.writeBytes(bytes, pos);
                bytes.length = pos;
                bytes.position = pos;
                bytes.writeByte(0x44);
                bytes.writeByte(0x11);
                bytes.writeByte(0x08);
                bytes.writeByte(0x00);
                bytes.writeByte(0x00);
                bytes.writeByte(0x00);
                bytes.writeBytes(afterBytes);
                afterBytes.length = 0;
            }
            private function getBodyPosition(bytes:ByteArray):uint
            {
                var result:uint = 0;
                
                result += 3; // FWS/CWS
                result += 1; // version(byte)
                result += 4; // length(32bit-uint)
                
                var rectNBits:uint = bytes[result] >>> 3;
                result += (5 + rectNBits * 4) / 8; // stage(rect)
                
                result += 2;
                
                result += 1; // frameRate(byte)
                result += 2; // totalFrames(16bit-uint)
                
                return result;
            }
            
            public function updateVersion(version:uint, b:ByteArray):void
            {
                b[3] = version;
            }
        
    //    private function findFileAttributesPosition(offset:uint, bytes:ByteArray):uint
    //    {
    //        bytes.position = offset;
    //        
    //        try {
    //            for (;;) {
    //            var byte:uint = bytes.readShort();
    //            var tag:uint = byte >>> 6;
    //            if (tag == 69) {
    //            return bytes.position - 2;
    //            }
    //            var length:uint = byte & 0x3f;
    //            if (length == 0x3f) {
    //            length = bytes.readInt();
    //            }
    //            bytes.position += length;
    //            }
    //        }
    //        catch (e:EOFError) {
    //        }
    //        
    //        return NaN;
    //    }
        
    //    private function flagSWF9Bit(bytes:ByteArray):void
    //    {
    //        var pos:uint = findFileAttributesPosition(getBodyPosition(bytes), bytes);
    //        if (!isNaN(pos)) {
    //            bytes[pos + 2] |= 0x08;
    //        }
    //    }
    //    
    //    private function insertFileAttributesTag(bytes:ByteArray):void
    //    {
    //        var pos:uint = getBodyPosition(bytes);
    //        var afterBytes:ByteArray = new ByteArray();
    //        afterBytes.writeBytes(bytes, pos);
    //        bytes.length = pos;
    //        bytes.position = pos;
    //        bytes.writeByte(0x44);
    //        bytes.writeByte(0x11);
    //        bytes.writeByte(0x08);
    //        bytes.writeByte(0x00);
    //        bytes.writeByte(0x00);
    //        bytes.writeByte(0x00);
    //        bytes.writeBytes(afterBytes);
    //        afterBytes.length = 0;
    //    }
    //    
    //    private function updateVersion(bytes:ByteArray, version:uint):void
    //    {
    //        bytes[3] = version;
    //    }
        
        private function ioErrorHandler(event:IOErrorEvent):void
        {
            loader.contentLoaderInfo.dispatchEvent(event.clone());
        }
        
        private function securityErrorHandler(event:SecurityErrorEvent):void
        {
            loader.contentLoaderInfo.dispatchEvent(event.clone());
        }
    }
    }
    View Code

    无论如何, 下面的loadComplete就是没有执行,像是某个地方卡住了一样,但是、、、 我看了progess都是100%的。。 试过很多方式,还是一样结果。

    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete)

    但为什么其他方式就行了呢? 几乎是一样的写法啊?

    package
    {
        import com.hurlant.crypto.Crypto;
        import com.hurlant.crypto.symmetric.ICipher;
        import com.hurlant.crypto.symmetric.IPad;
        import com.hurlant.crypto.symmetric.PKCS5;
        import com.hurlant.util.Hex;
        
        import flash.display.DisplayObject;
        import flash.display.Loader;
        import flash.display.MovieClip;
        import flash.errors.*;
        import flash.events.Event;
        import flash.events.IOErrorEvent;
        import flash.events.ProgressEvent;
        import flash.net.*;
        import flash.system.LoaderContext;
        import flash.utils.ByteArray;
        import flash.utils.Endian;
        
        import mx.controls.Alert;
        import mx.controls.ProgressBar;
        /**
         *队列加载类                  
         * @author Ryan_Leung
         * 
         */     
        public class LoaderManager
        {
    //        public var _order:int = 0;
            public var _loadOrder:int = 0;
            public var loadOrder_from:int = 0;
            public var loadOrder_to:int = 0;
            private var _callBack:Function;
            public var _paths:Array = [];
            private var _load:Loader;
            private var _progressBar:ProgressBar;
            private var _fristloadswfs:int;
            
            
            private var loadedSWFs:Array = [];
            
            private var noloadSWFs:Array = [];
            
            
            public var mc:MovieClip = null;
            
            public var bookdecrypt:String = "00000000";
            private var stream:URLStream = null;
            
            public function LoaderManager(progressBar:ProgressBar, fristloadswfs:int)
            {
                _loadOrder = 0;
                loadOrder_from = 0;
                loadOrder_to = fristloadswfs;
                _progressBar = progressBar;
                _fristloadswfs = fristloadswfs;
            }
            public function addPath(path:String):void
            {
                for(var i:int = 0 ; i < _paths.length ; i++)
                {
                    if(_paths.indexOf(path) > -1)
                        return;
                }
                _paths.push(path);
            }
            public function getPath(pageNum:int):String 
            {
                if(pageNum > _paths.length)
                {
                    return "";
                }
                var ss:String =  _paths[pageNum]  ;
                return ss;
                //_load.load(new URLRequest(loadedSWFs[_loadOrder]) , new LoaderContext(true));
            }
             
            /**
             *  通过paths数组的索引找到路径path加载swf
             *  加载某一个swf时候,需要同时确保其前后6个swf都已经加载上, 否则有问题。
             * 
             */        
            public function loadSwfById(fromOrder:int,toOrder:int, callBack:Function):void {
                _loadOrder = fromOrder;
                loadOrder_from = fromOrder;
                loadOrder_to = toOrder; 
                start(callBack);// // call back when the load is finished! 调用了两次, 不知道合理不合理
            }
            
            /**
             * 在所有资源路径添加完之后、开始加载
             */        
            public function start(callBack:Function):void 
            {    
                
    //            stream = new URLStream();
    //            stream.addEventListener(Event.COMPLETE,completeHandler_s);
    //            stream.addEventListener(ProgressEvent.PROGRESS,progressHandler);
    //            stream.addEventListener(IOErrorEvent.IO_ERROR,ioerrorHandler);
                if(stream != null)
                {
                    stream.close();
                    stream = null;
                }
                if(this.pbuf != null)
                {
                    this.pbuf.clear();
                    
                }
                _load = new Loader();
                if(this.loader != null)
                {
                    try {
                        this.loader.close();
                    } catch (err:Error) {
                        trace(err.message);
                    }
                    this.loader = null;
                    
                }
                
                _callBack = callBack;
                loading();
            }
            
            private var urlReq:URLRequest = null;
            public var buffer:ByteArray = null;
            private var isencrty:Boolean = false;
            private var pbuf:ByteArray = new ByteArray();
            private var loaderCtx:LoaderContext = null;
            public var loader:Loader = null;
            private function progressHandler(event:ProgressEvent):void
            {
                //trace("progress ……");
                this.stream.readBytes(this.pbuf,this.pbuf.length);
                
            }
            public function isCompressed(bytes:ByteArray):Boolean
            {
                return bytes[0] == 0x43;
            }
            private function ioerrorHandler(event:IOErrorEvent):void
            {
                trace("ioerror");
                trace(event.text);
            }
            private function uncompress(bytes:ByteArray):void
            {
                var cBytes:ByteArray = new ByteArray();
                cBytes.writeBytes(bytes, 8);
                bytes.length = 8;
                bytes.position = 8;
                cBytes.uncompress();
                bytes.writeBytes(cBytes);
                bytes[0] = 0x46;
                cBytes.length = 0;
            }
            private function completeHandler_s(event:Event):void
            {
                trace("Stream complete");
                //解密            
                
                bookdecrypt ="s3@74$*K";
                var decryptLen:int = this.pbuf.readInt();
                var decryptStr:String = this.pbuf.readUTFBytes(decryptLen);
                //trace(decryptLen,decryptStr);
                var kdata:ByteArray = Hex.toArray(Hex.fromString(bookdecrypt));
                var data:ByteArray = Hex.toArray(decryptStr);
                var name:String = "simple-des-ecb";
                var pad:IPad = new PKCS5;
                var mode:ICipher = Crypto.getCipher(name,kdata,pad);
                pad.setBlockSize(mode.getBlockSize());
                mode.decrypt(data);
                
                if(data==null)
                {
                    onLoadComplete(event);
                    return;
                }
                this.buffer.endian = Endian.LITTLE_ENDIAN;
                this.buffer.writeBytes(data);
                //trace(Hex.fromArray(data));
                this.pbuf.readBytes(this.buffer,this.buffer.length);
                //            
                
                
                //              this.buffer = this.pbuf;
                
                if(isCompressed(this.buffer)) {
                    //trace("uncompress");
                    uncompress(this.buffer);
                }    
                var version:uint = uint(this.buffer[3]);
                //trace(version,"ver");
                if (version <= 9) {
                    if (version == 8 || version == 9)
                    {
                        flagSWF9Bit(this.buffer);
                    }else if (version <= 7)
                    {
                        insertFileAttributesTag(this.buffer);
                    }
                    updateVersion(9,this.buffer);
                }
                //var result:DisplayObject = loader.content as DisplayObject;
                //ResourceBox.setResource(_paths[_loadOrder] , result);
                if(_loadOrder ==0)
                {
                    //_loadOrder =_loadOrder+1;
                    
                }
                //loading(); 
                //_loadOrder++;
                
                this.loader = new Loader();
                this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE,swfCompleteHandler);
                this.loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS,swfProgress);
                this.loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR , onError);
                this.loader.loadBytes(this.buffer,this.loaderCtx);
                //this.loader.loadBytes(this.buffer);
                
            
                trace("正加载文件:"+_loadOrder);
                    
                 
                loadedSWFs.push(this.loader);
                
            }
            private function onLoadComplete(event:Event):void
            {
                if (stream.connected)
                {
                    var b:int;
                    try {
                        do {
                            b = stream.readByte();
                            pbuf.writeByte(b);
                        } while (true);
                    } catch (e:EOFError) {
                        //Alert.show(bytes.length.toString());
                    }
                }
                this.buffer.writeBytes(buffer);
                this.loader.loadBytes(buffer);
            }
            private function swfProgress(event:ProgressEvent):void
            {
                //trace("swf progress");
                //trace("正加载文件:"+_loadOrder);
                if(mc != null)
                {
                    //trace(mc.framesLoaded,mc.totalFrames);
                }
            }
            private function swfCompleteHandler(event:Event):void
            {
                trace("swf complete");
                
                //event.target.removeEventListener(Event.COMPLETE, this.swfCompleteHandler);
                this.mc =  event.target.content as MovieClip;
                //this.loader.loadBytes(this.buffer,this.loaderCtx);
                //var result:DisplayObject = loader.content as DisplayObject;
                //ResourceBox.setResource(_paths[_loadOrder] , result);
                
    //            if(_loadOrder == 0 || _progressBar.visible == true)    // ?? 
    //            {
    //                _progressBar.visible = false;
                    _callBack(mc);
    //            }
                _loadOrder=_loadOrder+1;
                
    //             _loadOrder =_loadOrder+1;  ??
                trace("已加载文件:"+_loadOrder);
                //loading();
                /*test  */
                //this.mc.trackAsMenu=true;
                //flash.utils.setTimeout(tt,1000);
                
            }
            
            public function flagSWF9Bit(bytes:ByteArray):void
            {
                var pos:int = findFileAttributesPosition(getBodyPosition(bytes), bytes);
                
                if (pos != -1) {
                    bytes[pos + 2] |= 0x08;
                }
                else {
                    insertFileAttributesTag(bytes);
                }
            }
            private function findFileAttributesPosition(offset:uint, bytes:ByteArray):int
            {
                bytes.position = offset;
                
                try {
                    for (;;) {
                        var byte:uint = bytes.readShort();
                        var tag:uint = byte >>> 6;
                        if (tag == 69) {
                            return bytes.position - 2;
                        }
                        var length:uint = byte & 0x3f;
                        if (length == 0x3f) {
                            length = bytes.readInt();
                        }
                        bytes.position += length;
                    }
                }
                catch (e:EOFError) {
                }
                
                return -1;
            }
            private function insertFileAttributesTag(bytes:ByteArray):void
            {
                var pos:uint = getBodyPosition(bytes);
                var afterBytes:ByteArray = new ByteArray();
                afterBytes.writeBytes(bytes, pos);
                bytes.length = pos;
                bytes.position = pos;
                bytes.writeByte(0x44);
                bytes.writeByte(0x11);
                bytes.writeByte(0x08);
                bytes.writeByte(0x00);
                bytes.writeByte(0x00);
                bytes.writeByte(0x00);
                bytes.writeBytes(afterBytes);
                afterBytes.length = 0;
            }
            private function getBodyPosition(bytes:ByteArray):uint
            {
                var result:uint = 0;
                
                result += 3; // FWS/CWS
                result += 1; // version(byte)
                result += 4; // length(32bit-uint)
                
                var rectNBits:uint = bytes[result] >>> 3;
                result += (5 + rectNBits * 4) / 8; // stage(rect)
                
                result += 2;
                
                result += 1; // frameRate(byte)
                result += 2; // totalFrames(16bit-uint)
                
                return result;
            }
            
            public function updateVersion(version:uint, b:ByteArray):void
            {
                b[3] = version;
            }
            
            
            private function loading():void 
            {
                if(_loadOrder < loadOrder_from || _loadOrder > loadOrder_to ||    // 加载文件数现定于  loadOrder_from、loadOrder_to之间
                    _loadOrder > _paths.length - 1  )
                {
                    
                    _progressBar.visible = false;
                    
                    _callBack(true);// ??
                    return;
                }else
                {
                    
                }
            
                //            _load.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS , onProgress);
                //            _load.contentLoaderInfo.addEventListener(Event.COMPLETE , completeHandler);
                //            _load.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR , onError);
                //            
                
                if(this.buffer != null)
                {
                    this.buffer.clear();
                }
                else
                {
                    this.buffer = new ByteArray();
                }
                if(this.stream!=null)
                {
                    this.stream.close();
                }
                
                this.stream = new URLStream();
                this.stream.addEventListener(Event.COMPLETE,completeHandler_s);
                this.stream.addEventListener(ProgressEvent.PROGRESS,progressHandler);
                this.stream.addEventListener(IOErrorEvent.IO_ERROR,ioerrorHandler);
                
                this.urlReq = new URLRequest(_paths[_loadOrder]);
                trace(_paths[_loadOrder]);
                this.stream.load(this.urlReq);
            //    _load.load(new URLRequest(_paths[_loadOrder]) , new LoaderContext(true));
            }
            private function onError(evt:IOErrorEvent):void
            {
                Alert.show(evt.toString() , "加载出错");
            }
            private function onProgress(evt:ProgressEvent):void
            {
                //trace("LoaderManager加载进度" + evt.bytesLoaded / evt.bytesTotal);
                _progressBar.label = "加载中......";
                _progressBar.setProgress(evt.bytesLoaded , evt.bytesTotal);
            }
    //        private function completeHandler(evt:Event):void 
    //        {
    //            var result:DisplayObject = _load.content as DisplayObject;
    //            ResourceBox.setResource(_paths[_loadOrder] , result);
    //            if(_loadOrder >= _paths.length - 1)
    //            {
    //                return;
    //            }
    //            if(_loadOrder == 0 || _progressBar.visible == true)
    //            {
    //                _progressBar.visible = false;
    //                _callBack();
    //            }
    //         
    //             
    //        }
             
        }    
    }
    View Code
  • 相关阅读:
    [USACO 2012 Feb B]Moo
    [Atcoder ARC124] XOR Matching 2-小思维 | 暴力
    loj数列分块入门
    2019牛客暑期多校2-Partition problem深搜
    Codeforces 1554C
    [USACO 2012 Feb G]Cow Coupons----贪心&带悔(看完稳AC)
    Codeforces 220B-Little Elephant and Array-扫描线 & 树状数组
    [AtCoder ARC098] Donation| 建图 | 树型dp
    关于幂等性以及怎么实现幂等性
    【OOM】解决思路
  • 原文地址:https://www.cnblogs.com/FlyAway2013/p/3661724.html
Copyright © 2020-2023  润新知