Skip to content

Commit

Permalink
Fix 2022.9 issues, add load failure warning
Browse files Browse the repository at this point in the history
  • Loading branch information
colinator27 committed Nov 21, 2022
1 parent 68057e1 commit 39d6cf0
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 42 deletions.
84 changes: 45 additions & 39 deletions UndertaleModLib/Models/UndertaleEmbeddedTexture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ public class UndertaleEmbeddedTexture : UndertaleNamedResource, IDisposable


/// <summary>
/// Helper variable for whether or not an external texture was loaded yet.
/// Helper variable for whether or not a texture was loaded yet.
/// </summary>
public bool TextureExternallyLoaded { get; set; } = false;
public bool TextureLoaded { get; set; } = false;

/// <summary>
/// Width of the texture. 2022.9+ only.
Expand Down Expand Up @@ -156,6 +156,7 @@ public void UnserializeBlob(UndertaleReader reader)
throw new IOException("Padding error!");

reader.ReadUndertaleObject(_textureData);
TextureLoaded = true;
}

/// <summary>
Expand All @@ -173,54 +174,59 @@ public static void FindAllTextureInfo(UndertaleData data)
}
}

private static TexData _placeholderTexture = null;
private static TexData CreatePlaceholderTexture()
// 1x1 black pixel in PNG format
private static TexData _placeholderTexture = new()
{
_placeholderTexture = new();

// Construct new PNG file that has placeholder image
// TODO: display a helpful message instead?
Bitmap image = new Bitmap(64, 64);
Graphics g = Graphics.FromImage(image);
g.Clear(Color.Black);
g.Dispose();

_placeholderTexture.TextureBlob = TextureWorker.GetImageBytes(image);
return _placeholderTexture;
}
TextureBlob = new byte[]
{
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x01,
0x00, 0x00, 0x00, 0x01, 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53, 0xDE, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47,
0x42, 0x00, 0xAE, 0xCE, 0x1C, 0xE9, 0x00, 0x00, 0x00, 0x04, 0x67, 0x41, 0x4D, 0x41, 0x00, 0x00, 0xB1, 0x8F, 0x0B, 0xFC,
0x61, 0x05, 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0E, 0xC3, 0x00, 0x00, 0x0E, 0xC3, 0x01, 0xC7,
0x6F, 0xA8, 0x64, 0x00, 0x00, 0x00, 0x0C, 0x49, 0x44, 0x41, 0x54, 0x18, 0x57, 0x63, 0x60, 0x60, 0x60, 0x00, 0x00, 0x00,
0x04, 0x00, 0x01, 0x5C, 0xCD, 0xFF, 0x69, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82
}
};
private static object _textureLoadLock = new();

/// <summary>
/// Attempts to load the corresponding external texture. Should only happen in 2022.9 and above.
/// </summary>
/// <returns></returns>
public TexData LoadExternalTexture()
{
TexData texData;
lock (_textureLoadLock)
{
if (TextureLoaded)
return _textureData;

if (_2022_9_GameDirectory == null)
return _placeholderTexture ?? CreatePlaceholderTexture();
TexData texData;

// Try to find file on disk
string path = Path.Combine(_2022_9_GameDirectory, TextureInfo.Directory.Content,
TextureInfo.Name.Content + "_" + IndexInGroup.ToString() + TextureInfo.Extension.Content);
if (!File.Exists(path))
return _placeholderTexture ?? CreatePlaceholderTexture();
if (_2022_9_GameDirectory == null)
return _placeholderTexture;

// Load file!
try
{
using FileStream fs = new(path, FileMode.Open);
using FileBinaryReader fbr = new(fs);
texData = new TexData();
texData.Unserialize(fbr, true);
TextureExternallyLoaded = true;
}
catch (IOException)
{
return _placeholderTexture ?? CreatePlaceholderTexture();
}
// Try to find file on disk
string path = Path.Combine(_2022_9_GameDirectory, TextureInfo.Directory.Content,
TextureInfo.Name.Content + "_" + IndexInGroup.ToString() + TextureInfo.Extension.Content);
if (!File.Exists(path))
return _placeholderTexture;

// Load file!
try
{
using FileStream fs = new(path, FileMode.Open);
using FileBinaryReader fbr = new(fs);
texData = new TexData();
texData.Unserialize(fbr, true);
TextureLoaded = true;
}
catch (IOException)
{
return _placeholderTexture;
}

return texData;
return texData;
}
}

/// <inheritdoc />
Expand All @@ -238,7 +244,7 @@ public void Dispose()
{
GC.SuppressFinalize(this);

_textureData.Dispose();
_textureData?.Dispose();
_textureData = null;
Name = null;
TextureInfo = null;
Expand Down
2 changes: 1 addition & 1 deletion UndertaleModLib/UndertaleChunks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,7 @@ internal override void SerializeChunk(UndertaleWriter writer)
bool anythingUsesQoi = false;
foreach (var tex in List)
{
if (tex.TextureExternal && !tex.TextureExternallyLoaded)
if (tex.TextureExternal && !tex.TextureLoaded)
continue; // don't accidentally load everything...
if (tex.TextureData.FormatQOI)
{
Expand Down
10 changes: 8 additions & 2 deletions UndertaleModTool/Editors/UndertaleEmbeddedTextureEditor.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
xmlns:local="clr-namespace:UndertaleModTool"
xmlns:undertale="clr-namespace:UndertaleModLib.Models;assembly=UndertaleModLib"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800" d:DataContext="{d:DesignInstance undertale:UndertaleEmbeddedTexture}">
d:DesignHeight="450" d:DesignWidth="800" d:DataContext="{d:DesignInstance undertale:UndertaleEmbeddedTexture}">
<UserControl.Resources>
<local:BooleanToVisibilityConverter x:Key="BoolFalseToVisConverter" local:trueValue="Collapsed" local:falseValue="Visible"/>
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
Expand All @@ -17,6 +20,7 @@
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<TextBlock Grid.Row="0" Grid.Column="0" Margin="3">Scaled</TextBlock>
Expand All @@ -34,8 +38,10 @@
<TextBox Grid.Column="0" Name="TexWidth" Margin="3" Text="{Binding TextureData.Width, Mode=OneWay}" IsReadOnly="True"/>
<TextBox Grid.Column="1" Name="TexHeight" Margin="3" Text="{Binding TextureData.Height, Mode=OneWay}" IsReadOnly="True"/>
</Grid>

<TextBlock Grid.Row="3" Grid.Column="4" Margin="3" Foreground="Red" Visibility="{Binding TextureLoaded, Mode=OneWay, Converter={StaticResource BoolFalseToVisConverter}}">Warning: Texture failed to load!</TextBlock>

<Grid Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" Margin="3">
<Grid Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" Margin="3">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
Expand Down

0 comments on commit 39d6cf0

Please sign in to comment.