4 Commits

4 changed files with 28 additions and 4 deletions

View File

@@ -25,4 +25,5 @@
--macro keep("Main")
--macro keep("Assets")
--macro keep("AssetMacro")
--macro macros.AssetMacro.includeAssets()
# --macro macros.MacroLoader.run()

View File

@@ -25,7 +25,7 @@ class Main extends Sprite {
}
// test a data asset
var testAsset = dataLibrary.getText("img/placeholder.txt");
var testAsset = dataLibrary.getText("txt/placeholder.txt");
trace("Successfully loaded asset: " + testAsset);
// test an image asset
var testImage = imageLibrary.getImage("logo.png");

View File

@@ -1,8 +1,8 @@
package macros;
#if macro
import haxe.macro.Context;
import haxe.macro.Expr;
import haxe.macro.Type;
import sys.FileSystem;
import sys.io.File;
import haxe.Json;
@@ -15,6 +15,11 @@ using StringTools;
class AssetMacro {
// Track all assets processed
private static var assetMap:Map<String, Array<{id:String, path:String, size:Int, type:String}>> = new Map();
/** Force Assets to be typed, which invokes its @:build macro. */
public static function includeAssets():Void {
Context.getType("Assets");
}
/**
* Entry point for the macro - called during compilation
@@ -41,7 +46,10 @@ class AssetMacro {
var manifestDir = outputDir + "/manifest";
var assetsOutputDir = outputDir + "/assets";
// Create directories if they don't exist
// These directories are owned by this macro. Recreate them so removed or
// renamed source assets cannot survive as stale build output.
removeDirectory(manifestDir);
removeDirectory(assetsOutputDir);
createDirectory(manifestDir);
createDirectory(assetsOutputDir);
copyLimeNativeLibrary(outputDir);
@@ -241,6 +249,20 @@ class AssetMacro {
}
}
private static function removeDirectory(path:String):Void {
if (!FileSystem.exists(path)) return;
for (entry in FileSystem.readDirectory(path)) {
var entryPath = path + "/" + entry;
if (FileSystem.isDirectory(entryPath)) {
removeDirectory(entryPath);
} else {
FileSystem.deleteFile(entryPath);
}
}
FileSystem.deleteDirectory(path);
}
/** Stage the Lime CFFI module that matches the selected haxelib version. */
private static function copyLimeNativeLibrary(outputDir:String):Void {
var platformDirectory = if (Context.defined("linux")) {
@@ -312,4 +334,5 @@ class AssetMacro {
default: "binary";
}).toUpperCase();
}
}
}
#end