Skip to content

Commit

Permalink
Merge pull request #1899 from Garethp/#1898-Texture-NRE-Fix
Browse files Browse the repository at this point in the history
Issue #1898: Prevent NREs when adding new Texture Page Items or Embedded textures
  • Loading branch information
Miepee authored Aug 31, 2024
2 parents dc1e209 + 64a6b98 commit 69095cd
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
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>
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)
{
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)
{
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

0 comments on commit 69095cd

Please sign in to comment.