initialize standalone OpenFL application structure with essential files and configurations

This commit is contained in:
2025-04-09 02:37:12 +02:00
parent d533ffd7a8
commit 8c36316409
12 changed files with 471 additions and 0 deletions

View File

@@ -0,0 +1,112 @@
package macros;
import haxe.macro.Context;
import haxe.macro.Expr;
import sys.FileSystem;
import sys.io.File;
import haxe.Json;
class AssetMacro {
public static macro function buildAssets():Array<Field> {
var fields = Context.getBuildFields();
// Define assets directory
var assetsDir = "assets";
// Check if directory exists
if (!FileSystem.exists(assetsDir) || !FileSystem.isDirectory(assetsDir)) {
Context.warning('Assets directory "$assetsDir" not found', Context.currentPos());
return fields;
}
// Generate manifest in the format OpenFL expects
var manifest = {
name: "default",
rootPath: "",
assets: []
};
// Scan assets
scanDirectory(assetsDir, manifest.assets);
// Create manifest file (for runtime use)
var manifestPath = "bin/manifest.json";
try {
// Make sure directory exists
var dir = manifestPath.split("/")[0];
if (!FileSystem.exists(dir)) {
FileSystem.createDirectory(dir);
}
File.saveContent(manifestPath, Json.stringify(manifest, null, " "));
} catch (e) {
Context.warning('Could not save manifest file: $e', Context.currentPos());
}
// Generate expression to register the assets
var registerExpr = macro {
// Create an AssetManifest from our generated file
var manifestPath = "bin/manifest.json";
if (sys.FileSystem.exists(manifestPath)) {
var manifest = lime.utils.AssetManifest.fromFile(manifestPath);
if (manifest != null) {
// Register the library with OpenFL
var library = openfl.utils.AssetLibrary.fromManifest(manifest);
if (library != null) {
openfl.Assets.registerLibrary("default", library);
trace("Asset library registered successfully");
} else {
trace("Failed to create library from manifest");
}
} else {
trace("Failed to parse manifest file");
}
} else {
trace("Manifest file not found at: " + manifestPath);
}
};
// Add initialization method
fields.push({
name: "initializeAssets",
access: [Access.APublic, Access.AStatic],
kind: FieldType.FFun({
args: [],
ret: macro:Void,
expr: registerExpr
}),
pos: Context.currentPos()
});
return fields;
}
private static function scanDirectory(dir:String, assets:Array<Dynamic>, ?prefix:String = ""):Void {
for (file in FileSystem.readDirectory(dir)) {
var path = dir + "/" + file;
var id = prefix + file;
if (FileSystem.isDirectory(path)) {
scanDirectory(path, assets, id + "/");
} else {
var type = getAssetType(file);
assets.push({
id: id,
path: path,
type: type
});
}
}
}
private static function getAssetType(file:String):String {
var ext = file.substr(file.lastIndexOf(".") + 1).toLowerCase();
return switch (ext) {
case "jpg", "jpeg", "png", "gif", "bmp": "image";
case "mp3", "ogg", "wav": "sound";
case "ttf", "otf": "font";
case "txt", "json", "xml", "csv": "text";
default: "binary";
}
}
}