Can I get a zip swflite file?

#1

I used the openfl process command to generate a bundle, but I found that when I request it via http, it is still a multiple request, not a one-time load. In addition, if you add compression such as zip, the size can be smaller, but it seems that this can not be loaded properly?

Can I get a one-time join to load the SWF?

Please help me thank you.

0 Likes

#2

Theoretically, we could join the SWF support with the PAK style libraries, but currently these two library types cannot be mixed. I’m thinking either we should consider changes to the library system to allow multiple “processors” (sort of like Webpack and other systems) so you can mix-and-match, or just direct improvements to SWF. It’s slower to load images if it’s done that way on HTML5, but I understand that that isn’t always the only concern

0 Likes

#3

I believe that decompression will take less time than multiple requests from the network. Thank you for your answer, I will see if there is any way to optimize this problem. :grinning:

0 Likes

#4

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;
    }
}
0 Likes