From e1e1e8db8cab6802a1658a8367e4dfa9eead2f1c Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 9 Nov 2024 18:01:52 +0100 Subject: [PATCH] Fixed UiParser ImageExceptionHandler being ignored when an exception occurs during texture construction --- CHANGELOG.md | 1 + MLEM.Ui/Parsers/UiParser.cs | 32 +++++++++++++++++++++----------- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 133752d..fad1207 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ Fixes - Fixed tooltips not being bounded correctly for viewports that don't start at the origin - Fixed a stack overflow exception when a panel's children have just enough height to cause a scroll bar to appear - Fixed AddedToUi and RemovedFromUi being called for freshly added or removed children whose parents are not in a ui system +- Fixed UiParser ImageExceptionHandler being ignored when an exception occurs during texture construction ### MLEM.Data Improvements diff --git a/MLEM.Ui/Parsers/UiParser.cs b/MLEM.Ui/Parsers/UiParser.cs index ccd3262..8774199 100644 --- a/MLEM.Ui/Parsers/UiParser.cs +++ b/MLEM.Ui/Parsers/UiParser.cs @@ -154,16 +154,22 @@ namespace MLEM.Ui.Parsers { lock (bytesLock) bytesNull = bytes == null; if (!bytesNull) { - Texture2D tex; + Texture2D tex = null; lock (bytesLock) { - using (var stream = new MemoryStream(bytes)) { - using (var read = Texture2D.FromStream(this.GraphicsDevice, stream)) - tex = read.PremultipliedCopy(); + try { + using (var stream = new MemoryStream(bytes)) { + using (var read = Texture2D.FromStream(this.GraphicsDevice, stream)) + tex = read.PremultipliedCopy(); + } + } catch (Exception e) { + CatchOrRethrow(e); } bytes = null; } - image = new TextureRegion(tex); - onImageFetched?.Invoke(image); + if (tex != null) { + image = new TextureRegion(tex); + onImageFetched?.Invoke(image); + } } } return image; @@ -211,11 +217,15 @@ namespace MLEM.Ui.Parsers { lock (bytesLock) bytes = src; } catch (Exception e) { - if (this.ImageExceptionHandler != null) { - this.ImageExceptionHandler.Invoke(path, e); - } else { - throw new NullReferenceException($"Couldn't parse image {path}, and no ImageExceptionHandler was set", e); - } + CatchOrRethrow(e); + } + } + + void CatchOrRethrow(Exception e) { + if (this.ImageExceptionHandler != null) { + this.ImageExceptionHandler.Invoke(path, e); + } else { + throw new NullReferenceException($"Couldn't parse image {path}, and no ImageExceptionHandler was set", e); } } }