Skip to content

Instantly share code, notes, and snippets.

@zwetan
Last active June 23, 2018 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zwetan/1f91e0271018f745b01a43eb54845b1a to your computer and use it in GitHub Desktop.
Save zwetan/1f91e0271018f745b01a43eb54845b1a to your computer and use it in GitHub Desktop.
Detect AIR SWF version
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