From 8d08bc45bde8c01e8d5dfd4a9b5a8a39eb61767d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20Br=C3=BCckner?= Date: Mon, 12 Dec 2022 00:47:12 +0100 Subject: [PATCH] [Krom/HL] Implement asset loading failed callbacks for most cases --- Backends/Kinc-HL/kha/Image.hx | 3 ++ Backends/Kinc-HL/kha/LoaderImpl.hx | 26 +++++++++++++++-- Backends/Krom/kha/LoaderImpl.hx | 45 ++++++++++++++++++++++++++---- 3 files changed, 66 insertions(+), 8 deletions(-) diff --git a/Backends/Kinc-HL/kha/Image.hx b/Backends/Kinc-HL/kha/Image.hx index 0aed80d89..fed326731 100644 --- a/Backends/Kinc-HL/kha/Image.hx +++ b/Backends/Kinc-HL/kha/Image.hx @@ -182,6 +182,9 @@ class Image implements Canvas implements Resource { var isFloat = StringTools.endsWith(filename, ".hdr"); image.myFormat = isFloat ? TextureFormat.RGBA128 : TextureFormat.RGBA32; image.initFromFile(filename); + if (image._texture == null) { + return null; + } return image; } diff --git a/Backends/Kinc-HL/kha/LoaderImpl.hx b/Backends/Kinc-HL/kha/LoaderImpl.hx index 84ae72347..7f887a3fa 100644 --- a/Backends/Kinc-HL/kha/LoaderImpl.hx +++ b/Backends/Kinc-HL/kha/LoaderImpl.hx @@ -16,7 +16,16 @@ class LoaderImpl { public static function loadImageFromDescription(desc: Dynamic, done: kha.Image->Void, failed: AssetError->Void) { var readable = Reflect.hasField(desc, "readable") ? desc.readable : false; - done(kha.Image.fromFile(desc.files[0], readable)); + var image = kha.Image.fromFile(desc.files[0], readable); + if (image == null) { + failed({ + url: desc.files.join(","), + error: "Could not load image(s)", + }); + } + else { + done(image); + } } public static function getImageFormats(): Array { @@ -27,13 +36,24 @@ class LoaderImpl { // done(new Blob(File.getBytes(desc.files[0]))); var size = 0; var bytes = kinc_file_contents(StringHelper.convert(desc.files[0]), size); - done(new Blob(@:privateAccess new haxe.io.Bytes(bytes, size))); + if (bytes == null) { + failed({ + url: desc.files.join(","), + error: "Could not load blob(s)", + }); + } + else { + done(new Blob(@:privateAccess new haxe.io.Bytes(bytes, size))); + } } public static function loadFontFromDescription(desc: Dynamic, done: Font->Void, failed: AssetError->Void): Void { loadBlobFromDescription(desc, function(blob: Blob) { done(new Kravur(blob)); - }, failed); + }, (a: AssetError) -> { + a.error = "Could not load font(s)"; + failed(a); + }); } public static function loadVideoFromDescription(desc: Dynamic, done: Video->Void, failed: AssetError->Void) { diff --git a/Backends/Krom/kha/LoaderImpl.hx b/Backends/Krom/kha/LoaderImpl.hx index 4fc4ca2cd..18e60ebf9 100644 --- a/Backends/Krom/kha/LoaderImpl.hx +++ b/Backends/Krom/kha/LoaderImpl.hx @@ -13,7 +13,16 @@ class LoaderImpl { public static function loadImageFromDescription(desc: Dynamic, done: kha.Image->Void, failed: AssetError->Void) { var readable = Reflect.hasField(desc, "readable") ? desc.readable : false; - done(Image._fromTexture(Krom.loadImage(desc.files[0], readable))); + var texture = Krom.loadImage(desc.files[0], readable); + if (texture == null) { + failed({ + url: desc.files.join(","), + error: "Could not load image(s)", + }); + } + else { + done(Image._fromTexture(texture)); + } } public static function getSoundFormats(): Array { @@ -21,22 +30,48 @@ class LoaderImpl { } public static function loadSoundFromDescription(desc: Dynamic, done: kha.Sound->Void, failed: AssetError->Void) { - done(new kha.krom.Sound(Bytes.ofData(Krom.loadSound(desc.files[0])))); + var sound = Krom.loadSound(desc.files[0]); + if (sound == null) { + failed({ + url: desc.files.join(","), + error: "Could not load sound(s)", + }); + } + else { + done(new kha.krom.Sound(Bytes.ofData(sound))); + } } public static function getVideoFormats(): Array { return ["webm"]; } - public static function loadVideoFromDescription(desc: Dynamic, done: kha.Video->Void, failed: AssetError->Void): Void {} + public static function loadVideoFromDescription(desc: Dynamic, done: kha.Video->Void, failed: AssetError->Void): Void { + failed({ + url: desc.files.join(","), + error: "Could not load video(s), Krom currently does not support loading videos", + }); + } public static function loadBlobFromDescription(desc: Dynamic, done: Blob->Void, failed: AssetError->Void) { - done(new Blob(Bytes.ofData(Krom.loadBlob(desc.files[0])))); + var blob = Krom.loadBlob(desc.files[0]); + if (blob == null) { + failed({ + url: desc.files.join(","), + error: "Could not load blob(s)", + }); + } + else { + done(new Blob(Bytes.ofData(blob))); + } } public static function loadFontFromDescription(desc: Dynamic, done: Font->Void, failed: AssetError->Void): Void { loadBlobFromDescription(desc, function(blob: Blob) { done(new Kravur(blob)); - }, failed); + }, (a: AssetError) -> { + a.error = "Could not load font(s)"; + failed(a); + }); } }