diff --git a/src/swf/SWFLoader.hx b/src/swf/SWFLoader.hx new file mode 100644 index 0000000..cad0104 --- /dev/null +++ b/src/swf/SWFLoader.hx @@ -0,0 +1,60 @@ +package swf; + +import openfl.display.DisplayObject; +import openfl.display.IDisplayObjectLoader; +import openfl.display.LoaderInfo; +import openfl.events.Event; +import openfl.events.IOErrorEvent; +import openfl.events.ProgressEvent; +import openfl.net.URLLoader; +import openfl.net.URLLoaderDataFormat; +import openfl.net.URLRequest; +import openfl.system.LoaderContext; +import openfl.utils.ByteArray; +import openfl.utils.Future; +import openfl.utils.Promise; + +class SWFLoader implements IDisplayObjectLoader +{ + public function new() {} + + public function load(request:URLRequest, context:LoaderContext, contentLoaderInfo:LoaderInfo):Future + { + if (contentLoaderInfo.contentType != null && contentLoaderInfo.contentType == "application/x-shockwave-flash") + { + var promise = new Promise(); + + var loader = new URLLoader(); + loader.dataFormat = URLLoaderDataFormat.BINARY; + loader.addEventListener(Event.COMPLETE, function(event) + { + promise.completeWith(loadBytes(loader.data, context, contentLoaderInfo)); + }); + loader.addEventListener(IOErrorEvent.IO_ERROR, function(event) + { + promise.error(event); + }); + loader.addEventListener(ProgressEvent.PROGRESS, function(event) + { + promise.progress(Std.int(event.bytesLoaded), Std.int(event.bytesTotal)); + }); + loader.load(request); + + return promise.future; + } + else + { + return null; + } + } + + public function loadBytes(buffer:ByteArray, context:LoaderContext, contentLoaderInfo:LoaderInfo):Future + { + var swf = new SWF(buffer); + var content:DisplayObject = new swf.runtime.MovieClip(swf.data); + @:privateAccess contentLoaderInfo.width = swf.width; + @:privateAccess contentLoaderInfo.height = swf.height; + @:privateAccess contentLoaderInfo.frameRate = swf.frameRate; + return Future.withValue(content); + } +} diff --git a/src/swf/exporters/animate/AnimateLoader.hx b/src/swf/exporters/animate/AnimateLoader.hx new file mode 100644 index 0000000..55d360a --- /dev/null +++ b/src/swf/exporters/animate/AnimateLoader.hx @@ -0,0 +1,32 @@ +package swf.exporters.animate; + +import openfl.utils.Promise; +import openfl.display.DisplayObject; +import openfl.display.IDisplayObjectLoader; +import openfl.display.LoaderInfo; +import openfl.events.Event; +import openfl.events.IOErrorEvent; +import openfl.events.ProgressEvent; +import openfl.net.URLLoader; +import openfl.net.URLLoaderDataFormat; +import openfl.net.URLRequest; +import openfl.net.URLRequestMethod; +import openfl.system.LoaderContext; +import openfl.utils.ByteArray; +import openfl.utils.Future; + +class AnimateLoader implements IDisplayObjectLoader +{ + public function new() {} + + public function load(request:URLRequest, context:LoaderContext, contentLoaderInfo:LoaderInfo):Future + { + // TODO + return null; + } + + public function loadBytes(buffer:ByteArray, context:LoaderContext, contentLoaderInfo:LoaderInfo):Future + { + return null; + } +} diff --git a/src/swf/exporters/swflite/SWFLiteLoader.hx b/src/swf/exporters/swflite/SWFLiteLoader.hx new file mode 100644 index 0000000..ff1ad48 --- /dev/null +++ b/src/swf/exporters/swflite/SWFLiteLoader.hx @@ -0,0 +1,86 @@ +package swf.exporters.swflite; + +import lime.graphics.Image; +import openfl.display.DisplayObject; +import openfl.display.IDisplayObjectLoader; +import openfl.display.LoaderInfo; +import openfl.events.Event; +import openfl.events.IOErrorEvent; +import openfl.events.ProgressEvent; +import openfl.net.URLLoader; +import openfl.net.URLLoaderDataFormat; +import openfl.net.URLRequest; +import openfl.system.LoaderContext; +import openfl.utils.ByteArray; +import openfl.utils.Future; +import openfl.utils.Promise; +import swf.exporters.SWFLiteExporter; + +@:access(swf.exporters.swflite.SWFLiteLibrary) +class SWFLiteLoader implements IDisplayObjectLoader +{ + public function new() {} + + public function load(request:URLRequest, context:LoaderContext, contentLoaderInfo:LoaderInfo):Future + { + if (contentLoaderInfo.contentType != null && contentLoaderInfo.contentType == "application/x-shockwave-flash") + { + var promise = new Promise(); + + var loader = new URLLoader(); + loader.dataFormat = URLLoaderDataFormat.BINARY; + loader.addEventListener(Event.COMPLETE, function(event) + { + promise.completeWith(loadBytes(loader.data, context, contentLoaderInfo)); + }); + loader.addEventListener(IOErrorEvent.IO_ERROR, function(event) + { + promise.error(event); + }); + loader.addEventListener(ProgressEvent.PROGRESS, function(event) + { + promise.progress(Std.int(event.bytesLoaded), Std.int(event.bytesTotal)); + }); + loader.load(request); + + return promise.future; + } + else + { + return null; + } + } + + public function loadBytes(buffer:ByteArray, context:LoaderContext, contentLoaderInfo:LoaderInfo):Future + { + // TODO: No intermediate format + var swf = new SWF(buffer); + var exporter = new SWFLiteExporter(swf.data); + var swfLite = exporter.swfLite; + var library = new SWFLiteLibrary("test"); + swfLite.library = library; + library.swf = swfLite; + + for (id in exporter.bitmaps.keys()) + { + var type = exporter.bitmapTypes.get(id) == BitmapType.PNG ? "png" : "jpg"; + var symbol:BitmapSymbol = cast swfLite.symbols.get(id); + symbol.path = id + "." + type; + swfLite.symbols.set(id, symbol); + library.cachedImages.set(symbol.path, Image.fromBytes(exporter.bitmaps.get(id))); + + if (exporter.bitmapTypes.get(id) == BitmapType.JPEG_ALPHA) + { + symbol.alpha = id + "a.png"; + library.cachedImages.set(symbol.alpha, Image.fromBytes(exporter.bitmapAlpha.get(id))); + } + } + + var content:DisplayObject = exporter.swfLite.createMovieClip(""); + @:privateAccess contentLoaderInfo.width = swf.width; + @:privateAccess contentLoaderInfo.height = swf.height; + @:privateAccess contentLoaderInfo.frameRate = swf.frameRate; + @:privateAccess contentLoaderInfo.assetLibrary = library; + return Future.withValue(content); + } +}