successfully implemented asset handling
This commit is contained in:
parent
3c35a616cc
commit
31d458f8b7
@ -24,6 +24,6 @@ class Assets {
|
|||||||
public static var data:String;
|
public static var data:String;
|
||||||
|
|
||||||
// You can also define individual files if needed
|
// You can also define individual files if needed
|
||||||
@:Assets("assets/images/logo.png", "images", "logo.png")
|
// @:Assets("assets/images/logo.png", "images", "logo.png")
|
||||||
public static var logo:String;
|
// public static var logo:String;
|
||||||
}
|
}
|
||||||
@ -16,15 +16,29 @@ class Main extends Sprite {
|
|||||||
graphics.drawRect(0, 0, 800, 600);
|
graphics.drawRect(0, 0, 800, 600);
|
||||||
graphics.endFill();
|
graphics.endFill();
|
||||||
|
|
||||||
var testAsset = openfl.Assets.getText("img/placeholder.txt");
|
|
||||||
trace("Successfully loaded asset: " + testAsset);
|
|
||||||
|
|
||||||
// var testLogo = Assets.logo;
|
// Get data asset library
|
||||||
// trace("Successfully loaded logo asset: " + testLogo);
|
var dataLibrary = openfl.utils.Assets.getLibrary("data");
|
||||||
// var testImage = Assets.images + "/logo.png";
|
var imageLibrary = openfl.utils.Assets.getLibrary("images");
|
||||||
var testImage = openfl.Assets.getPath("logo.png");
|
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);
|
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));
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,9 +2,9 @@ package;
|
|||||||
|
|
||||||
#if sys
|
#if sys
|
||||||
import haxe.io.Path;
|
import haxe.io.Path;
|
||||||
import lime.utils.AssetLibrary;
|
import openfl.utils.AssetLibrary;
|
||||||
import lime.utils.AssetManifest;
|
import openfl.utils.AssetManifest;
|
||||||
import lime.utils.Assets as LimeAssets;
|
import openfl.utils.Assets as LimeAssets;
|
||||||
import sys.FileSystem;
|
import sys.FileSystem;
|
||||||
|
|
||||||
@:access(lime.utils.Assets)
|
@:access(lime.utils.Assets)
|
||||||
@ -22,7 +22,7 @@ import sys.FileSystem;
|
|||||||
trace(manifest);
|
trace(manifest);
|
||||||
if (StringTools.endsWith(manifest, ".json")) {
|
if (StringTools.endsWith(manifest, ".json")) {
|
||||||
var manifestPath = Path.join([manifestDirPath, manifest]);
|
var manifestPath = Path.join([manifestDirPath, manifest]);
|
||||||
loadManifest(manifestPath, manifest);
|
loadManifest(manifestPath, manifest.split(".json")[0]);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
throw("we eating shit tonight");
|
throw("we eating shit tonight");
|
||||||
@ -36,15 +36,17 @@ import sys.FileSystem;
|
|||||||
trace("Manifest file not found: " + manifestPath);
|
trace("Manifest file not found: " + manifestPath);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
var manifest = AssetManifest.fromFile(manifestPath);
|
var manifest = AssetManifest.fromFile(manifestPath);
|
||||||
if (manifest != null) {
|
if (manifest != null) {
|
||||||
var library = AssetLibrary.fromManifest(manifest);
|
var library = AssetLibrary.fromManifest(manifest);
|
||||||
if (library != null) {
|
if (library != null) {
|
||||||
|
trace("Library: `"+library+"`");
|
||||||
|
trace("manifestName: `"+manifestName+"`");
|
||||||
preloadLibraries.push(library);
|
preloadLibraries.push(library);
|
||||||
preloadLibraryNames.push(manifestName);
|
preloadLibraryNames.push(manifestName);
|
||||||
LimeAssets.registerLibrary(manifestName, library);
|
// LimeAssets.registerLibrary(manifestName, library);
|
||||||
|
openfl.utils.Assets.registerLibrary(manifestName, library);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e:Dynamic) {
|
} catch (e:Dynamic) {
|
||||||
|
|||||||
@ -164,6 +164,10 @@ class AssetMacro {
|
|||||||
* Build manifest JSON files for each library
|
* Build manifest JSON files for each library
|
||||||
*/
|
*/
|
||||||
private static function buildManifests(manifestDir:String) {
|
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()) {
|
for (library in assetMap.keys()) {
|
||||||
var assets = assetMap.get(library);
|
var assets = assetMap.get(library);
|
||||||
var manifest = {
|
var manifest = {
|
||||||
@ -172,6 +176,16 @@ class AssetMacro {
|
|||||||
rootPath: "../assets/" + library + "/"
|
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";
|
var manifestPath = manifestDir + "/" + library + ".json";
|
||||||
try {
|
try {
|
||||||
var content = Json.stringify(manifest, null, " ");
|
var content = Json.stringify(manifest, null, " ");
|
||||||
@ -181,6 +195,23 @@ class AssetMacro {
|
|||||||
Context.warning('Failed to write manifest: ${manifestPath}. Error: ${e}', Context.currentPos());
|
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
|
// Helper functions
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user