Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #1898: Prevent NREs when adding new Texture Page Items or Embedded textures #1899

Merged
merged 3 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions UndertaleModLib/Models/UndertaleEmbeddedTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,21 +316,33 @@ public GMImage Image
/// <summary>
/// The width of the texture.
/// </summary>
colinator27 marked this conversation as resolved.
Show resolved Hide resolved
public int Width => _image.Width;
/// <remarks>
/// In the case of an invalid or missing image, this will always be <c>-1</c>.
/// </remarks>
public int Width => _image?.Width ?? -1;

/// <summary>
/// The height of the texture.
/// </summary>
public int Height => _image.Height;
/// <remarks>
/// In the case of an invalid or missing image, this will always be <c>-1</c>.
/// </remarks>
public int Height => _image?.Height ?? -1;

/// <summary>
/// Whether this texture uses the QOI format.
/// </summary>
/// <remarks>
/// In the case of an invalid or missing image, this will always be <see langword="false" />.
/// </remarks>
public bool FormatQOI => _image.Format is GMImage.ImageFormat.Qoi or GMImage.ImageFormat.Bz2Qoi;

/// <summary>
/// Whether this texture uses the BZ2 format. (Always used in combination with QOI.)
/// </summary>
/// <remarks>
/// In the case of an invalid or missing image, this will always be <see langword="false" />.
/// </remarks>
public bool FormatBZ2 => _image.Format is GMImage.ImageFormat.Bz2Qoi;

/// <summary>
Expand Down
13 changes: 12 additions & 1 deletion UndertaleModTool/Editors/UndertaleEmbeddedTextureEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ public UndertaleEmbeddedTextureEditor()

private void UpdateImage(UndertaleEmbeddedTexture texture)
{
if (texture.TextureData?.Image is null)
colinator27 marked this conversation as resolved.
Show resolved Hide resolved
{
TexturePageImage.Source = null;
return;
}

GMImage image = texture.TextureData.Image;
BitmapSource bitmap = mainWindow.GetBitmapSourceForImage(image);
TexturePageImage.Source = bitmap;
Expand All @@ -88,8 +94,13 @@ private void SwitchDataContext(object sender, DependencyPropertyChangedEventArgs
{
_textureDataContext.PropertyChanged -= ReloadTextureImage;
}

_textureDataContext = texture.TextureData;
_textureDataContext.PropertyChanged += ReloadTextureImage;

if (_textureDataContext is not null)
{
_textureDataContext.PropertyChanged += ReloadTextureImage;
}
}

private void ReloadTextureImage(object sender, PropertyChangedEventArgs e)
Expand Down
15 changes: 13 additions & 2 deletions UndertaleModTool/Editors/UndertaleTexturePageItemEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ public UndertaleTexturePageItemEditor()

private void UpdateImages(UndertaleTexturePageItem item)
{
if (item.TexturePage?.TextureData?.Image is null)
colinator27 marked this conversation as resolved.
Show resolved Hide resolved
{
ItemTextureBGImage.Source = null;
ItemTextureImage.Source = null;
return;
}

GMImage image = item.TexturePage.TextureData.Image;
BitmapSource bitmap = mainWindow.GetBitmapSourceForImage(image);
ItemTextureBGImage.Source = bitmap;
Expand Down Expand Up @@ -68,8 +75,12 @@ private void SwitchDataContext(object sender, DependencyPropertyChangedEventArgs
{
_textureDataContext.PropertyChanged -= ReloadTextureImage;
}
_textureDataContext = item.TexturePage.TextureData;
_textureDataContext.PropertyChanged += ReloadTextureImage;

if (item.TexturePage?.TextureData is not null)
{
_textureDataContext = item.TexturePage.TextureData;
_textureDataContext.PropertyChanged += ReloadTextureImage;
}
}

private void ReloadTexturePage(object sender, PropertyChangedEventArgs e)
Expand Down
Loading