Last active
June 23, 2018 05:11
-
-
Save zwetan/1f91e0271018f745b01a43eb54845b1a to your computer and use it in GitHub Desktop.
Detect AIR SWF version
This file contains 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
package | |
{ | |
public class Test | |
{ | |
public function Test() | |
{ | |
trace( "SWF version = " + _parseSWFVersion() ); | |
} | |
private function _getAIRSWF():String | |
{ | |
var na:NativeApplication = NativeApplication.nativeApplication; | |
var swf_file:String = ""; | |
if( na ) | |
{ | |
var descriptor:XML = na.applicationDescriptor; | |
var ns:Namespace = descriptor.namespace(); | |
swf_file = descriptor.ns::initialWindow.ns::content; | |
} | |
return swf_file; | |
} | |
private function _parseSWFVersion():uint | |
{ | |
var air_swf:String = _getAIRSWF(); | |
var air_file:File = File.applicationDirectory; | |
air_file = air_file.resolvePath( air_swf ); | |
if( air_file.exists ) | |
{ | |
var fs:FileStream = new FileStream(); | |
fs.endian = Endian.LITTLE_ENDIAN; | |
fs.open( air_file, FileMode.READ ); | |
trace( "endian = " + fs.endian ); | |
trace( "bytesAvailable = " + fs.bytesAvailable ); | |
trace( "position = " + fs.position ); | |
// SWF magic number | |
var magic:Array = [ fs.readUnsignedByte(), fs.readUnsignedByte(), fs.readUnsignedByte() ]; | |
magic.reverse(); | |
magic.forEach( function(el:*, i:int, a:Array):void { a[i] = String.fromCharCode(el); } ); | |
var signature:String = magic.join(""); | |
if( (signature == "SWC") || (signature == "SWF") ) | |
{ | |
// valid signature found | |
var version:uint = fs.readUnsignedByte(); | |
fs.close(); | |
return version; | |
} | |
else | |
{ | |
trace( "This is not a valid SWF file (signature=" + signature + ")" ); | |
fs.close(); | |
return 0; | |
} | |
} | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment