delete trash code
This commit is contained in:
@@ -1,264 +1,9 @@
|
||||
package macros;
|
||||
|
||||
import haxe.macro.Context;
|
||||
import haxe.macro.Expr;
|
||||
import sys.FileSystem;
|
||||
import sys.io.File;
|
||||
import haxe.io.Path;
|
||||
#if macro
|
||||
import haxe.Json;
|
||||
import haxe.crypto.Base64;
|
||||
import haxe.format.JsonPrinter;
|
||||
#end
|
||||
class AssetMacro{
|
||||
|
||||
public static function buildAssets(){
|
||||
|
||||
class AssetMacro {
|
||||
public static macro function buildAssets():Array<Field> {
|
||||
var fields = Context.getBuildFields();
|
||||
|
||||
#if !display
|
||||
try {
|
||||
// Define output directories
|
||||
var targetBuildDir = "bin/cpp";
|
||||
var manifestDir = targetBuildDir + "/manifest";
|
||||
var assetsOutputDir = targetBuildDir + "/assets";
|
||||
|
||||
// Create directories if they don't exist
|
||||
ensureDirectory(manifestDir);
|
||||
ensureDirectory(assetsOutputDir);
|
||||
ensureDirectory("manifest");
|
||||
|
||||
// Add a test placeholder if no assets exist
|
||||
if (!FileSystem.exists("assets")) {
|
||||
ensureDirectory("assets/data/img");
|
||||
File.saveContent("assets/data/img/placeholder.txt", "This is a placeholder file.");
|
||||
}
|
||||
|
||||
// Scan for assets and build asset list
|
||||
var assets = [];
|
||||
var assetDir = "assets";
|
||||
var embeddedAssets = new Map<String, {data: String, type: String}>();
|
||||
|
||||
if (FileSystem.exists(assetDir)) {
|
||||
scanAssets(assetDir, assets, "", assetsOutputDir, embeddedAssets);
|
||||
trace('Found ${assets.length} assets in ${assetDir}');
|
||||
} else {
|
||||
trace('WARNING: Assets directory not found: ${assetDir}');
|
||||
}
|
||||
|
||||
// Create a default asset if none found
|
||||
if (assets.length == 0) {
|
||||
var placeholder = {
|
||||
path: "assets/data/img/placeholder.txt",
|
||||
type: "TEXT",
|
||||
id: "data/img/placeholder.txt"
|
||||
};
|
||||
assets.push(placeholder);
|
||||
var placeholderContent = "This is a placeholder file.";
|
||||
embeddedAssets.set(placeholder.id, {
|
||||
data: Base64.encode(haxe.io.Bytes.ofString(placeholderContent)),
|
||||
type: "TEXT"
|
||||
});
|
||||
}
|
||||
|
||||
// Create manifest in OpenFL format
|
||||
var manifest = {
|
||||
name: "default",
|
||||
assets: assets.map(function(asset) {
|
||||
return {
|
||||
id: asset.id,
|
||||
path: asset.path,
|
||||
type: asset.type.toLowerCase()
|
||||
};
|
||||
}),
|
||||
rootPath: ".",
|
||||
version: 2,
|
||||
libraryType: null,
|
||||
libraryArgs: []
|
||||
};
|
||||
var manifestJson = haxe.format.JsonPrinter.print(manifest, null, " ");
|
||||
File.saveContent(manifestDir + "/default.json", manifestJson);
|
||||
trace('Asset manifest saved to: ' + manifestDir + "/default.json");
|
||||
File.saveContent("manifest/default.json", manifestJson);
|
||||
trace('Asset manifest also saved to: manifest/default.json');
|
||||
|
||||
// Generate the embedded assets code
|
||||
if (embeddedAssets.keys().hasNext()) {
|
||||
var initExprs = [];
|
||||
initExprs.push(macro var assetLibrary = new CustomAssetLibrary());
|
||||
for (id in embeddedAssets.keys()) {
|
||||
var asset = embeddedAssets.get(id);
|
||||
if (asset == null) continue;
|
||||
switch (asset.type) {
|
||||
case "TEXT":
|
||||
var dataExpr = macro haxe.crypto.Base64.decode($v{asset.data}).getString(0, haxe.crypto.Base64.decode($v{asset.data}).length);
|
||||
initExprs.push(macro assetLibrary.registerAsset($v{id}, "text", $dataExpr));
|
||||
case "IMAGE":
|
||||
initExprs.push(macro {
|
||||
var bytes = haxe.crypto.Base64.decode($v{asset.data});
|
||||
assetLibrary.registerAsset($v{id}, "image", bytes);
|
||||
});
|
||||
case "SOUND":
|
||||
initExprs.push(macro {
|
||||
var bytes = haxe.crypto.Base64.decode($v{asset.data});
|
||||
assetLibrary.registerAsset($v{id}, "sound", bytes);
|
||||
});
|
||||
default:
|
||||
var dataExpr = macro haxe.crypto.Base64.decode($v{asset.data});
|
||||
initExprs.push(macro assetLibrary.registerAsset($v{id}, "binary", $dataExpr));
|
||||
}
|
||||
}
|
||||
initExprs.push(macro openfl.Assets.registerLibrary("default", assetLibrary));
|
||||
fields.push({
|
||||
name: "initEmbeddedAssets",
|
||||
doc: "Initializes embedded assets for the application",
|
||||
meta: [],
|
||||
access: [Access.APublic, Access.AStatic],
|
||||
kind: FieldType.FFun({
|
||||
args: [],
|
||||
expr: macro $b{initExprs},
|
||||
params: [],
|
||||
ret: macro :Void
|
||||
}),
|
||||
pos: Context.currentPos()
|
||||
});
|
||||
}
|
||||
} catch (e:Dynamic) {
|
||||
trace('ERROR in asset macro: ${e}');
|
||||
#if debug
|
||||
trace(haxe.CallStack.toString(haxe.CallStack.exceptionStack()));
|
||||
#end
|
||||
}
|
||||
#end
|
||||
|
||||
return fields;
|
||||
}
|
||||
|
||||
#if macro
|
||||
private static function ensureDirectory(dir:String):Void {
|
||||
if (!FileSystem.exists(dir)) {
|
||||
try {
|
||||
var pathParts = dir.split("/");
|
||||
var currentPath = "";
|
||||
|
||||
for (part in pathParts) {
|
||||
if (part == "") continue;
|
||||
currentPath += part + "/";
|
||||
if (!FileSystem.exists(currentPath)) {
|
||||
FileSystem.createDirectory(currentPath);
|
||||
}
|
||||
}
|
||||
} catch (e:Dynamic) {
|
||||
trace('ERROR creating directory ${dir}: ${e}');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static function scanAssets(dir:String, assets:Array<Dynamic>, relativePath:String = "",
|
||||
outputDir:String = null, embeddedAssets:Map<String, {data:String, type:String}>):Void {
|
||||
if (!FileSystem.exists(dir)) return;
|
||||
|
||||
try {
|
||||
var files = FileSystem.readDirectory(dir);
|
||||
|
||||
for (file in files) {
|
||||
try {
|
||||
var path = dir + "/" + file;
|
||||
var relPath = relativePath == "" ? file : relativePath + "/" + file;
|
||||
|
||||
if (FileSystem.isDirectory(path)) {
|
||||
// Create the corresponding directory in output
|
||||
if (outputDir != null) {
|
||||
ensureDirectory(outputDir + "/" + relPath);
|
||||
}
|
||||
|
||||
// Recursive scan subdirectories
|
||||
scanAssets(path, assets, relPath, outputDir, embeddedAssets);
|
||||
} else {
|
||||
// Determine asset type based on extension
|
||||
var extension = Path.extension(file).toLowerCase();
|
||||
var type = getAssetType(extension);
|
||||
|
||||
// Skip hidden files and non-asset files
|
||||
if (file.charAt(0) == "." || type == "UNKNOWN") continue;
|
||||
|
||||
// Copy the file to output directory
|
||||
if (outputDir != null) {
|
||||
var outputPath = outputDir + "/" + relPath;
|
||||
try {
|
||||
File.copy(path, outputPath);
|
||||
trace('Copied asset: ${path} to ${outputPath}');
|
||||
} catch (e:Dynamic) {
|
||||
trace('Error copying ${path} to ${outputPath}: ${e}');
|
||||
}
|
||||
}
|
||||
|
||||
// Add to asset list with proper path format
|
||||
var assetId = relPath.split("\\").join("/"); // Normalize path separators
|
||||
var assetPath = "assets/" + assetId;
|
||||
|
||||
assets.push({
|
||||
path: assetPath,
|
||||
type: type,
|
||||
id: assetId
|
||||
});
|
||||
|
||||
// Embed the asset content for runtime access
|
||||
try {
|
||||
var content = File.getBytes(path);
|
||||
embeddedAssets.set(assetId, {
|
||||
data: Base64.encode(content),
|
||||
type: type
|
||||
});
|
||||
} catch (e:Dynamic) {
|
||||
trace('Warning: Could not embed asset ${assetId}: ${e}');
|
||||
}
|
||||
|
||||
trace('Added asset: ${assetId}');
|
||||
}
|
||||
} catch (innerE:Dynamic) {
|
||||
trace('Error processing asset ${file}: ${innerE}');
|
||||
}
|
||||
}
|
||||
} catch (e:Dynamic) {
|
||||
trace('Error reading directory ${dir}: ${e}');
|
||||
}
|
||||
}
|
||||
|
||||
private static function getAssetType(extension:String):String {
|
||||
return switch(extension) {
|
||||
case "jpg", "jpeg", "png", "gif", "bmp":
|
||||
"IMAGE";
|
||||
case "mp3", "ogg", "wav":
|
||||
"SOUND";
|
||||
case "ttf", "otf":
|
||||
"FONT";
|
||||
case "txt", "json", "xml", "csv", "tsv", "frag", "vert", "properties":
|
||||
"TEXT";
|
||||
case "bytes", "bin":
|
||||
"BINARY";
|
||||
default:
|
||||
"TEXT"; // Default to TEXT for unknown types
|
||||
}
|
||||
}
|
||||
|
||||
private static function generateEmbeddedAssetExprs(embeddedAssets:Map<String, {data:String, type:String}>):Array<Expr> {
|
||||
var exprs:Array<Expr> = [];
|
||||
for (id in embeddedAssets.keys()) {
|
||||
var asset = embeddedAssets.get(id);
|
||||
if (asset == null) continue;
|
||||
var dataExpr = macro haxe.crypto.Base64.decode($v{asset.data});
|
||||
switch (asset.type) {
|
||||
case "TEXT":
|
||||
exprs.push(macro texts.set($v{id}, dataExpr.getString(0, dataExpr.length)));
|
||||
case "IMAGE":
|
||||
exprs.push(macro byteData.set($v{id}, dataExpr));
|
||||
case "SOUND":
|
||||
exprs.push(macro byteData.set($v{id}, dataExpr));
|
||||
default:
|
||||
exprs.push(macro byteData.set($v{id}, dataExpr));
|
||||
}
|
||||
}
|
||||
return exprs;
|
||||
}
|
||||
#end
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user