Runtime assets common solution?

I know that by default there is no way to use assets that wasn’t described in project file.
But maybe there is any class-library-backend that can simplify this? Without BitmapData.fromBytes() and etc?

Have you checked out Assets.loadLibrary? This should enable you to plug your own assets into the asset system.

Also, if this is about static builds, you can try setting embed="true" to your <asset /> tag on native to try and push into the executable

So there is nothing like custom AssetLibrary that reads assets in runtime, is there? I was asking about this, maybe I’ve missed something…

An AssetLibrary says “hey, yeah, I have a file by that name” and when requested, returns an instance of it.

Someone could make a RuntimeAssetLibrary that checks the file system when the exists method is called, then loads it from disk when a get method is called. Similarly, someone could even do one that’s a RemoteAssetLibrary which returns files from a web server.

Maybe I should have called it “asset provider” :smile:

maybe I was expecting to much from openfl community…

Something like this might work:

package;


import lime.audio.AudioBuffer;
import lime.graphics.Font;
import lime.graphics.Image;
import lime.utils.ByteArray;
import lime.Assets;

#if (sys || nodejs)
import sys.FileSystem;
#end


class RuntimeAssetLibrary extends AssetLibrary {
	
	
	public function new () {
		
		super ();
		
	}
	
	
	public override function exists (id:String, type:String):Bool {
		
		return Filesystem.exists (id);
		
	}
	
	
	public override function getAudioBuffer (id:String):AudioBuffer {
		
		return AudioBuffer.fromFile (id);
		
	}
	
	
	public override function getBytes (id:String):ByteArray {
		
		return ByteArray.readFile (id);
		
	}
	
	
	public override function getFont (id:String):Dynamic /*Font*/ {
		
		// TODO: Complete Lime Font API
		
		#if openfl
		return new openfl.text.Font (id);
		#end
		
		return null;
		
	}
	
	
	public override function getImage (id:String):Image {
		
		return Image.fromFile (id);
		
	}
	
	
	public override function getPath (id:String):String {
		
		return id;
		
	}
	
	
	public override function getText (id:String):String {
		
		var bytes = getBytes (id);
		
		if (bytes == null) {
			
			return null;
			
		} else {
			
			return bytes.readUTFBytes (bytes.length);
			
		}
		
	}
	
	
	public override function isLocal (id:String, type:String):Bool {
		
		return true;
		
	}
	
	
	public override function list (type:String):Array<String> {
		
		return [];
		
	}
	
	
	public override function loadText (id:String, handler:String -> Void):Void {
		
		var callback = function (bytes:ByteArray):Void {
			
			if (bytes == null) {
				
				handler (null);
				
			} else {
				
				handler (bytes.readUTFBytes (bytes.length));
				
			}
			
		}
		
		loadBytes (id, callback);
		
	}
	
	
}

This would only work on a target with a local filesystem, but theoretically you could load it in with Assets.loadLibrary ("runtime", new RuntimeAssetLibrary ()); then use Assets.getBitmapData ("runtime:MyImage.jpg"); etc

This is on Lime or OpenFL “next”, this would be a little different on OpenFL _v2 (which is default on native right now), for that it would look more like this file:

https://github.com/openfl/openfl/blob/master/templates/v2/haxe/DefaultAssetLibrary.hx (the concept is similar, but the way to load each asset type from disk is different, and it extends an openfl.AssetLibrary (imported from openfl.Assets) rather than a Lime one, so the functions are a little different (getBitmapData vs. getImage, etc)

Of course, you can just skip the asset library, and load the files yourself, the contents of either of these examples should show you how you can do that

I thought that there is library-class for this and I’ve missed it. I certainly can implement something like this by myself.
So there is no common lib for that?

I’m building a convention in the newer code to use from___ to create an instance of an object, fromFile, fromBitmapData, fromCanvas, etc.

This would be nice to bring back to the older code, for convenience. In the meantime, it will look a little different depending on the type you are trying to construct. Of course, you can use the Loader or URLLoader class, like Flash, but these are a bit easier when you know you have local access to the file

Thanks for your time Joshua
I’ll try to implement something common for desktop targets and maybe pull request results, who knows :smile: