From e3d338b6ec2a9ad9f01cfb8344571d2630f21a05 Mon Sep 17 00:00:00 2001 From: Mark Johnson Date: Fri, 12 Jan 2007 22:04:46 +0000 Subject: [PATCH] Use the EmbededClassLoader to get full access to an embeded swf file, including all classes, functions and namespaces defined in it. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4502 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../com/threerings/util/EmbededClassLoader.as | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/as/com/threerings/util/EmbededClassLoader.as diff --git a/src/as/com/threerings/util/EmbededClassLoader.as b/src/as/com/threerings/util/EmbededClassLoader.as new file mode 100644 index 000000000..b18fef5ff --- /dev/null +++ b/src/as/com/threerings/util/EmbededClassLoader.as @@ -0,0 +1,83 @@ +package com.threerings.util { + +import flash.display.Loader; +import flash.errors.IllegalOperationError; +import flash.events.Event; +import flash.events.EventDispatcher; +import flash.events.IOErrorEvent; +import flash.utils.ByteArray; +import flash.system.ApplicationDomain; +import flash.system.LoaderContext; + +/** + * Allows you to load an embeded SWF stored as a Byte Array then access any stored classes + * within the SWF. + */ +public class EmbededClassLoader extends EventDispatcher +{ + public static var SWF_LOADED :String = "swfLoaded"; + public static var LOAD_ERROR :String = "loadError"; + + public function EmbededClassLoader () + { + _loader = new Loader(); + _loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler); + _loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); + } + + /** + * Load the SWF from a Byte Array. A CLASS_LOADED event will be dispatched on successful + * completion of the load. If any errors occur, a LOAD_ERROR event will be dispatched. + */ + public function load (byteArray :ByteArray) :void + { + var context :LoaderContext = new LoaderContext(); + context.applicationDomain = ApplicationDomain.currentDomain; + _loader.loadBytes(byteArray, context); + } + + /** + * Retrieves a class definition from the loaded swf. + * + * @throws IllegalOperationError when the swf has not completed loading or the class does + * not exist + */ + public function getClass (className :String) :Class + { + if (_loader.contentLoaderInfo.bytesLoaded != _loader.contentLoaderInfo.bytesTotal) { + throw new IllegalOperationError("SWF has not completed loading"); + } + try { + return _loader.contentLoaderInfo.applicationDomain.getDefinition(className) as Class; + } catch (e: Error) { + throw new IllegalOperationError(className + " definition not found"); + } + return null; + } + + /** + * Checks if a class exists in the library. + * + * @throws IllegalOperationError when the swf has not completed loading. + */ + public function isClass (className :String) :Boolean + { + if (_loader.contentLoaderInfo.bytesLoaded != _loader.contentLoaderInfo.bytesTotal) { + throw new IllegalOperationError("SWF has not completed loading"); + } + return _loader.contentLoaderInfo.applicationDomain.hasDefinition(className); + } + + protected function completeHandler (e :Event) :void + { + dispatchEvent(new Event(SWF_LOADED)); + } + + protected function ioErrorHandler (E :Event) :void + { + dispatchEvent(new Event(LOAD_ERROR)); + } + + protected var _loader :Loader; +} +}