forked from LeenkxTeam/LNXSDK
Update
This commit is contained in:
@ -6,13 +6,17 @@ import haxe.macro.Context;
|
||||
import haxe.macro.Expr;
|
||||
#if macro
|
||||
import sys.io.File;
|
||||
import sys.FileSystem;
|
||||
#end
|
||||
|
||||
using StringTools;
|
||||
|
||||
class AssetsBuilder {
|
||||
#if macro
|
||||
public static var files: Array<Dynamic>;
|
||||
@:persistent static var cache: Map<String, {time: Float, fields: Array<Field>}> = [];
|
||||
@:persistent static var files: Array<Dynamic>;
|
||||
@:persistent static var filesTime: Float = 0.0;
|
||||
static var debug = false;
|
||||
#end
|
||||
|
||||
public static function findResources(): String {
|
||||
@ -51,29 +55,48 @@ class AssetsBuilder {
|
||||
}
|
||||
|
||||
macro static public function build(type: String): Array<Field> {
|
||||
var fields = Context.getBuildFields();
|
||||
if (files == null) {
|
||||
var content = Json.parse(File.getContent(findResources() + "files.json"));
|
||||
files = content.files;
|
||||
final fields = Context.getBuildFields();
|
||||
final resPath = findResources() + "files.json";
|
||||
Context.registerModuleDependency(Context.getLocalModule(), resPath);
|
||||
|
||||
final time = FileSystem.stat(resPath).mtime.getTime();
|
||||
if (cache[type] != null && cache[type].time == time) {
|
||||
return cache[type].fields;
|
||||
}
|
||||
|
||||
var names = new Array<Expr>();
|
||||
if (files == null || filesTime != time) {
|
||||
final content = Json.parse(File.getContent(resPath));
|
||||
files = content.files;
|
||||
filesTime = time;
|
||||
if (debug)
|
||||
trace("reparse files.json (Assets)");
|
||||
}
|
||||
if (debug)
|
||||
trace('invalidate Assets.$type');
|
||||
|
||||
final names = new Array<Expr>();
|
||||
|
||||
for (file in files) {
|
||||
var name = file.name;
|
||||
final name = file.name;
|
||||
final pos = Context.currentPos();
|
||||
var filesize: Int = file.file_sizes[0];
|
||||
final filesize: Int = file.file_sizes[0];
|
||||
|
||||
if (file.type == type) {
|
||||
names.push(macro $v{name});
|
||||
|
||||
switch (type) {
|
||||
case "image":
|
||||
var output = Compiler.getOutput();
|
||||
output = output.replace("\\", "/");
|
||||
output = output.substring(0, output.lastIndexOf("/"));
|
||||
final path = '$output/${file.files[0]}';
|
||||
final url = makeFileUrl(path);
|
||||
fields.push({
|
||||
name: name,
|
||||
meta: [{pos: pos, name: ":keep"}],
|
||||
access: [APublic],
|
||||
kind: FVar(macro : kha.Image, macro null),
|
||||
doc: '[]($url)',
|
||||
pos: pos
|
||||
});
|
||||
case "sound":
|
||||
@ -129,7 +152,7 @@ class AssetsBuilder {
|
||||
fields.push({
|
||||
name: name + "Size",
|
||||
doc: null,
|
||||
meta: [],
|
||||
meta: [{pos: pos, name: ":keep"}],
|
||||
access: [APublic],
|
||||
kind: FVar(macro : Dynamic, macro $v{filesize}),
|
||||
pos: Context.currentPos()
|
||||
@ -210,7 +233,20 @@ class AssetsBuilder {
|
||||
kind: FVar(macro : Array<String>, macro $a{names}),
|
||||
pos: Context.currentPos()
|
||||
});
|
||||
cache[type] = {
|
||||
fields: fields,
|
||||
time: time,
|
||||
};
|
||||
|
||||
return fields;
|
||||
}
|
||||
|
||||
#if macro
|
||||
/** Converts a filesystem path into a `file://` URL safe for IDE hover previews. **/
|
||||
static function makeFileUrl(path:String):String {
|
||||
final absPath = FileSystem.absolutePath(path).replace("\\", "/");
|
||||
final url = absPath.urlEncode().replace("%2F", "/").replace("%3A", ":");
|
||||
return "file://" + (url.startsWith("/") ? "" : "/") + url;
|
||||
}
|
||||
#end
|
||||
}
|
||||
|
||||
@ -6,25 +6,32 @@ import haxe.macro.Expr.Field;
|
||||
import haxe.Serializer;
|
||||
#if macro
|
||||
import sys.io.File;
|
||||
import sys.FileSystem;
|
||||
#end
|
||||
|
||||
using StringTools;
|
||||
|
||||
class ShadersBuilder {
|
||||
#if macro
|
||||
public static var files: Array<Dynamic>;
|
||||
@:persistent static var cache: {time: Float, fields: Array<Field>} = null;
|
||||
static var debug = false;
|
||||
#end
|
||||
|
||||
macro static public function build(): Array<Field> {
|
||||
var fields = Context.getBuildFields();
|
||||
|
||||
var manifestPath = AssetsBuilder.findResources() + "files.json";
|
||||
var content = Json.parse(File.getContent(manifestPath));
|
||||
|
||||
final fields = Context.getBuildFields();
|
||||
final manifestPath = AssetsBuilder.findResources() + "files.json";
|
||||
// rebuild Shaders module whenever manifest file is changed
|
||||
Context.registerModuleDependency(Context.getLocalModule(), manifestPath);
|
||||
|
||||
files = content.files;
|
||||
final time = FileSystem.stat(manifestPath).mtime.getTime();
|
||||
if (cache != null && time == cache.time) {
|
||||
return cache.fields;
|
||||
}
|
||||
|
||||
final content = Json.parse(File.getContent(manifestPath));
|
||||
final files: Array<Dynamic> = content.files;
|
||||
if (debug)
|
||||
trace("invalidate Shaders");
|
||||
|
||||
var init = macro {};
|
||||
|
||||
@ -56,7 +63,7 @@ class ShadersBuilder {
|
||||
doc: null,
|
||||
meta: [],
|
||||
access: [APublic, AStatic],
|
||||
kind: FVar(macro : kha.compute.Shader, macro null),
|
||||
kind: FVar(macro : kha.graphics4.ComputeShader, macro null),
|
||||
pos: Context.currentPos()
|
||||
});
|
||||
|
||||
@ -69,7 +76,7 @@ class ShadersBuilder {
|
||||
var bytes: haxe.io.Bytes = haxe.Unserializer.run(data);
|
||||
blobs.push(kha.Blob.fromBytes(bytes));
|
||||
}
|
||||
$i{fixedName} = new kha.compute.Shader(blobs, $v{filenames});
|
||||
$i{fixedName} = new kha.graphics4.ComputeShader(blobs, $v{filenames});
|
||||
}
|
||||
};
|
||||
}
|
||||
@ -204,6 +211,10 @@ class ShadersBuilder {
|
||||
}),
|
||||
pos: Context.currentPos()
|
||||
});
|
||||
cache = {
|
||||
fields: fields,
|
||||
time: time,
|
||||
};
|
||||
|
||||
return fields;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user