Recently, I have supported the SWF zip. I rewrote the SWFLiteLibrary implementation, implemented the loading process myself, and then passed the zip list to the SWFLiteLibrary object, letting the SWFLiteLibrary object read the ZIP resource.
This can run normally on C and HTML5. The zip is generated by simply compressing the *.bundle folder into a zip.
package zygame.utils.load;
import lime.app.Future;
import lime.app.Promise;
import lime.graphics.Image;
import openfl._internal.symbols.BitmapSymbol;
import haxe.zip.Entry;
import openfl._internal.formats.swf.SWFLite;
import openfl.utils.Assets;
import zygame.utils.AssetsUtils;
import haxe.io.Bytes;
import lime._internal.format.Deflate;
#if android
import sys.io.File;
import sys.FileSystem;
#end
@:keep
class SWFLiteLibrary extends #if (openfl <= '8.3.0') openfl._internal.swf.SWFLiteLibrary #else openfl._internal.formats.swf.SWFLiteLibrary #end {
/**
* 弃用
*/
// public static var WEB_PATH:String;
/**
* SWF文件名
*/
public var name:String;
/**
* 已经解析好的ZIPList列表
*/
public var zipList:List<Entry>;
override public function load ():Future<lime.utils.AssetLibrary> {
if (id != null) {
preload.set (id, true);
}
var promise = new Promise<lime.utils.AssetLibrary> ();
preloading = true;
var onComplete = function (data) {
cachedText.set (id, data);
swf = SWFLite.unserialize (data);
swf.library = this;
var bitmapSymbol:BitmapSymbol;
for (symbol in swf.symbols) {
if (Std.is (symbol, BitmapSymbol)) {
bitmapSymbol = cast symbol;
if (bitmapSymbol.className != null) {
imageClassNames.set (bitmapSymbol.className, bitmapSymbol.path);
}
}
}
SWFLite.instances.set (instanceID, swf);
__load ().onProgress (promise.progress).onError (promise.error).onComplete (function (_) {
preloading = false;
promise.complete (this);
});
}
if (Assets.exists (id)) {
#if (js && html5)
for (id in paths.keys ()) {
preload.set (id, true);
}
#end
loadText (id).onError (promise.error).onComplete (onComplete);
} else {
for (id in paths.keys ()) {
preload.set (id, true);
}
var path = null;
if (paths.exists (id)) {
path = paths.get (id);
} else {
path = (rootPath != null && rootPath != "") ? rootPath + "/" + id : id;
}
var binPath:String = StringUtils.getName(path);
var entry:Entry = AssetsUtils.findZipData(zipList,binPath);
var bytes:Bytes = entry.compressed?Deflate.decompress(entry.data):entry.data;
onComplete(bytes.toString());
}
return promise.future;
}
override public function loadText (id:String):Future<String> {
var entry:Entry = AssetsUtils.findZipData(zipList,id);
var bytes:Bytes = entry.compressed?Deflate.decompress(entry.data):entry.data;
return Future.withValue(bytes.toString());
}
override public function loadImage (id:String):Future<Image> {
var entry:Entry = AssetsUtils.findZipData(zipList,id);
var bytes:Bytes = entry.compressed?Deflate.decompress(entry.data):entry.data;
return Image.loadFromBytes(bytes);
}
/**
* 载入逻辑重写
* @param id 图片ID
* @return Future<Image>
*/
override private function __loadImage (id:String):Future<Image> {
return super.loadImage(id);
}
/**
* 释放ZIP资源
*/
public function releaseZip():Void
{
if(zipList != null)
{
zipList.clear();
zipList = null;
}
}
/**
* 释放unload
*/
override public function unload():Void
{
super.unload();
var images:Map<String, Image> = cachedImages;
var iter:Iterator<String> = images.keys();
while(iter.hasNext())
{
var key:String = iter.next();
var img:Image = images.get(key);
images.remove(key);
}
cachedImages = null;
}
}