Skip to content

Commit

Permalink
Add implementations for IDisplayObjectLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
jgranick committed Aug 7, 2024
1 parent 8f4875f commit 99cb8eb
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/swf/SWFLoader.hx
Original file line number Diff line number Diff line change
@@ -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<DisplayObject>
{
if (contentLoaderInfo.contentType != null && contentLoaderInfo.contentType == "application/x-shockwave-flash")
{
var promise = new Promise<DisplayObject>();

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<DisplayObject>
{
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);
}
}
32 changes: 32 additions & 0 deletions src/swf/exporters/animate/AnimateLoader.hx
Original file line number Diff line number Diff line change
@@ -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<DisplayObject>
{
// TODO
return null;
}

public function loadBytes(buffer:ByteArray, context:LoaderContext, contentLoaderInfo:LoaderInfo):Future<DisplayObject>
{
return null;
}
}
86 changes: 86 additions & 0 deletions src/swf/exporters/swflite/SWFLiteLoader.hx
Original file line number Diff line number Diff line change
@@ -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<DisplayObject>
{
if (contentLoaderInfo.contentType != null && contentLoaderInfo.contentType == "application/x-shockwave-flash")
{
var promise = new Promise<DisplayObject>();

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<DisplayObject>
{
// 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);
}
}

0 comments on commit 99cb8eb

Please sign in to comment.