| 1 |
/** |
|---|
| 2 |
* use as you like |
|---|
| 3 |
*/ |
|---|
| 4 |
package net.fladdict.display |
|---|
| 5 |
{ |
|---|
| 6 |
import flash.display.Loader; |
|---|
| 7 |
import flash.events.*; |
|---|
| 8 |
import flash.net.*; |
|---|
| 9 |
import flash.system.LoaderContext; |
|---|
| 10 |
import flash.utils.ByteArray; |
|---|
| 11 |
import flash.utils.Endian; |
|---|
| 12 |
|
|---|
| 13 |
/** |
|---|
| 14 |
* Loads both of AVM1 and AVM2 swf as AVM2. |
|---|
| 15 |
*/ |
|---|
| 16 |
public class AVM2Loader extends Loader |
|---|
| 17 |
{ |
|---|
| 18 |
private var _urlLoader:URLLoader; |
|---|
| 19 |
private var _context:LoaderContext; |
|---|
| 20 |
|
|---|
| 21 |
/** |
|---|
| 22 |
* loads both of AVM1 and AVM2 movie as AVM2 movie. |
|---|
| 23 |
*/ |
|---|
| 24 |
override public function load(request:URLRequest, context:LoaderContext=null):void |
|---|
| 25 |
{ |
|---|
| 26 |
_context = context; |
|---|
| 27 |
|
|---|
| 28 |
_urlLoader = new URLLoader(); |
|---|
| 29 |
_urlLoader.dataFormat = URLLoaderDataFormat.BINARY |
|---|
| 30 |
_urlLoader.addEventListener(Event.COMPLETE, _binaryLoaded, false, 0, true); |
|---|
| 31 |
_urlLoader.addEventListener(IOErrorEvent.IO_ERROR, _transferEvent, false, 0, true); |
|---|
| 32 |
_urlLoader.addEventListener(ProgressEvent.PROGRESS, _transferEvent, false, 0, true); |
|---|
| 33 |
_urlLoader.addEventListener(Event.OPEN, _transferEvent, false, 0, true); |
|---|
| 34 |
_urlLoader.addEventListener(HTTPStatusEvent.HTTP_STATUS, _transferEvent, false, 0, true); |
|---|
| 35 |
_urlLoader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, _transferEvent, false, 0, true); |
|---|
| 36 |
_urlLoader.load(request); |
|---|
| 37 |
} |
|---|
| 38 |
|
|---|
| 39 |
private function _binaryLoaded(e:Event):void |
|---|
| 40 |
{ |
|---|
| 41 |
loadBytes(ByteArray(_urlLoader.data), _context); |
|---|
| 42 |
_urlLoader = null |
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
private function _transferEvent(e:Event):void |
|---|
| 46 |
{ |
|---|
| 47 |
dispatchEvent(e); |
|---|
| 48 |
} |
|---|
| 49 |
|
|---|
| 50 |
/** |
|---|
| 51 |
* loads both of AVM1 and AVM2 movie as AVM2 movie. |
|---|
| 52 |
*/ |
|---|
| 53 |
override public function loadBytes(bytes:ByteArray, context:LoaderContext=null):void |
|---|
| 54 |
{ |
|---|
| 55 |
//uncompress if compressed |
|---|
| 56 |
bytes.endian = Endian.LITTLE_ENDIAN; |
|---|
| 57 |
if(bytes[0]==0x43) |
|---|
| 58 |
{ |
|---|
| 59 |
//many thanks for be-interactive.org |
|---|
| 60 |
var compressedBytes:ByteArray = new ByteArray(); |
|---|
| 61 |
compressedBytes.writeBytes(bytes, 8); |
|---|
| 62 |
compressedBytes.uncompress(); |
|---|
| 63 |
|
|---|
| 64 |
bytes.length = 8; |
|---|
| 65 |
bytes.position = 8; |
|---|
| 66 |
bytes.writeBytes(compressedBytes); |
|---|
| 67 |
compressedBytes.length = 0; |
|---|
| 68 |
|
|---|
| 69 |
//flag uncompressed |
|---|
| 70 |
bytes[0] = 0x46; |
|---|
| 71 |
} |
|---|
| 72 |
|
|---|
| 73 |
hackBytes(bytes); |
|---|
| 74 |
super.loadBytes(bytes, context); |
|---|
| 75 |
} |
|---|
| 76 |
|
|---|
| 77 |
//if bytes are AVM1 movie, hack it! |
|---|
| 78 |
private function hackBytes(bytes:ByteArray):void |
|---|
| 79 |
{ |
|---|
| 80 |
if(bytes[4]<0x09) |
|---|
| 81 |
bytes[4] = 0x09; |
|---|
| 82 |
|
|---|
| 83 |
//dirty dirty |
|---|
| 84 |
var imax:int = Math.min(bytes.length, 100); |
|---|
| 85 |
for(var i:int=23; i<imax; i++) |
|---|
| 86 |
{ |
|---|
| 87 |
if(bytes[i-2]==0x44 && bytes[i-1] == 0x11) |
|---|
| 88 |
{ |
|---|
| 89 |
bytes[i] = bytes[i] | 0x08; |
|---|
| 90 |
return |
|---|
| 91 |
} |
|---|
| 92 |
} |
|---|
| 93 |
} |
|---|
| 94 |
} |
|---|
| 95 |
} |
|---|