Преглед изворни кода

Robust interface to add Converts. (#476)

Pavel Alexandrov пре 6 година
родитељ
комит
755d98bc0f
2 измењених фајлова са 40 додато и 0 уклоњено
  1. 32 0
      hxd/fs/Convert.hx
  2. 8 0
      hxd/fs/LocalFileSystem.hx

+ 32 - 0
hxd/fs/Convert.hx

@@ -2,6 +2,38 @@ package hxd.fs;
 
 class Convert {
 
+	/**
+		Custom list of converts that should be used in FileSystem.
+		Should be added in hxml `--macro` to ensure proper function.
+		Example of adding a Convert:
+		```haxe
+		static function initCustomConverts() {
+			hxd.fs.Convert.converts.push("path.to.my.Convert");
+			hxd.fs.Convert.converts.push("path.to.my.OtherConvert");
+		}
+		```
+		Adn in hxml:
+		`--macro path.to.my.MacroScript.initCustomConverts()`
+	**/
+	public static var converts:Array<String> = new Array();
+
+	static macro function getConverts() : haxe.macro.Expr {
+		var exprs:Array<haxe.macro.Expr> = new Array();
+		var pos = haxe.macro.Context.currentPos();
+		
+		for ( p in converts ) {
+			var t = haxe.macro.Context.getType(p);
+			var ct = haxe.macro.TypeTools.toComplexType(t);
+			switch (ct) {
+				case TPath(t):
+					var newExpr : haxe.macro.Expr = { expr: ENew(t, []), pos: pos };
+					exprs.push(macro addConvert($newExpr));
+				default:
+			}
+		}
+		return macro $b{exprs}
+	}
+
 	public var sourceExt(default,null) : String;
 	public var destExt(default,null) : String;
 

+ 8 - 0
hxd/fs/LocalFileSystem.hx

@@ -301,6 +301,14 @@ class LocalFileSystem implements FileSystem {
 		addConvert(new Convert.ConvertFBX2HMD());
 		addConvert(new Convert.ConvertTGA2PNG());
 		addConvert(new Convert.ConvertFNT2BFNT());
+		#if macro
+		// In macro context just resolve classes.
+		for ( p in Convert.converts ) {
+			addConvert(Type.createInstance(Type.resolveClass(p), []));
+		}
+		#else
+		@:privateAccess Convert.getConverts();
+		#end
 		#if flash
 		var froot = new flash.filesystem.File(flash.filesystem.File.applicationDirectory.nativePath + "/" + baseDir);
 		if( !froot.exists ) throw "Could not find dir " + dir;