successfully implemented asset handling

This commit is contained in:
Andreas Schaafsma 2025-04-12 01:00:16 +02:00
parent 3c35a616cc
commit 31d458f8b7
4 changed files with 62 additions and 15 deletions

View File

@ -24,6 +24,6 @@ class Assets {
public static var data:String;
// You can also define individual files if needed
@:Assets("assets/images/logo.png", "images", "logo.png")
public static var logo:String;
// @:Assets("assets/images/logo.png", "images", "logo.png")
// public static var logo:String;
}

View File

@ -16,15 +16,29 @@ class Main extends Sprite {
graphics.drawRect(0, 0, 800, 600);
graphics.endFill();
var testAsset = openfl.Assets.getText("img/placeholder.txt");
trace("Successfully loaded asset: " + testAsset);
// var testLogo = Assets.logo;
// trace("Successfully loaded logo asset: " + testLogo);
// var testImage = Assets.images + "/logo.png";
var testImage = openfl.Assets.getPath("logo.png");
// Get data asset library
var dataLibrary = openfl.utils.Assets.getLibrary("data");
var imageLibrary = openfl.utils.Assets.getLibrary("images");
if (dataLibrary == null) {
trace("Data library not found");
return;
}
if (imageLibrary == null) {
trace("Image library not found");
return;
}
// test a data asset
var testAsset = dataLibrary.getText("img/placeholder.txt");
trace("Successfully loaded asset: " + testAsset);
// test an image asset
var testImage = imageLibrary.getImage("logo.png");
trace("Successfully loaded image asset: " + testImage);
this.addChild(new openfl.display.Bitmap(openfl.Assets.getBitmapData(testImage)));
var bitmapData = openfl.display.BitmapData.fromImage(testImage);
this.addChild(new openfl.display.Bitmap(bitmapData));
}
}

View File

@ -2,9 +2,9 @@ package;
#if sys
import haxe.io.Path;
import lime.utils.AssetLibrary;
import lime.utils.AssetManifest;
import lime.utils.Assets as LimeAssets;
import openfl.utils.AssetLibrary;
import openfl.utils.AssetManifest;
import openfl.utils.Assets as LimeAssets;
import sys.FileSystem;
@:access(lime.utils.Assets)
@ -22,7 +22,7 @@ import sys.FileSystem;
trace(manifest);
if (StringTools.endsWith(manifest, ".json")) {
var manifestPath = Path.join([manifestDirPath, manifest]);
loadManifest(manifestPath, manifest);
loadManifest(manifestPath, manifest.split(".json")[0]);
}
else{
throw("we eating shit tonight");
@ -36,15 +36,17 @@ import sys.FileSystem;
trace("Manifest file not found: " + manifestPath);
return;
}
try {
var manifest = AssetManifest.fromFile(manifestPath);
if (manifest != null) {
var library = AssetLibrary.fromManifest(manifest);
if (library != null) {
trace("Library: `"+library+"`");
trace("manifestName: `"+manifestName+"`");
preloadLibraries.push(library);
preloadLibraryNames.push(manifestName);
LimeAssets.registerLibrary(manifestName, library);
// LimeAssets.registerLibrary(manifestName, library);
openfl.utils.Assets.registerLibrary(manifestName, library);
}
}
} catch (e:Dynamic) {

View File

@ -164,6 +164,10 @@ class AssetMacro {
* Build manifest JSON files for each library
*/
private static function buildManifests(manifestDir:String) {
// Create an array to hold all assets for the default manifest
var allAssets = [];
// First, process individual library manifests
for (library in assetMap.keys()) {
var assets = assetMap.get(library);
var manifest = {
@ -172,6 +176,16 @@ class AssetMacro {
rootPath: "../assets/" + library + "/"
};
// Add assets to combined collection with library prefix
for (asset in assets) {
allAssets.push({
id: library + "/" + asset.id,
path: library + "/" + asset.path,
size: asset.size,
type: asset.type
});
}
var manifestPath = manifestDir + "/" + library + ".json";
try {
var content = Json.stringify(manifest, null, " ");
@ -181,6 +195,23 @@ class AssetMacro {
Context.warning('Failed to write manifest: ${manifestPath}. Error: ${e}', Context.currentPos());
}
}
// Create the default manifest with all assets
var defaultManifest = {
name: "default",
assets: allAssets,
rootPath: "../assets/"
};
// Save the default manifest
var defaultManifestPath = manifestDir + "/default.json";
try {
var content = Json.stringify(defaultManifest, null, " ");
File.saveContent(defaultManifestPath, content);
Context.info('Built default manifest: ${defaultManifestPath} with ${allAssets.length} assets', Context.currentPos());
} catch (e) {
Context.warning('Failed to write default manifest: ${defaultManifestPath}. Error: ${e}', Context.currentPos());
}
}
// Helper functions