Skip to content

Commit

Permalink
Merge pull request #19 from HazelnutCheese/METIS-16
Browse files Browse the repository at this point in the history
Fix Loading Corrupt Image Crash and fix play logging
  • Loading branch information
HazelnutCheese authored Feb 26, 2023
2 parents fe131c6 + 48948b8 commit d471fb8
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 10 deletions.
4 changes: 2 additions & 2 deletions ModEngine2ConfigTool/ModEngine2ConfigTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<TargetFramework>net6.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
<AssemblyVersion>1.0.1</AssemblyVersion>
<AssemblyVersion>1.0.2</AssemblyVersion>
<FileAssemblyVersion>0.0.1</FileAssemblyVersion>
<Version>1.0.1</Version>
<Version>1.0.2</Version>
<EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
<ApplicationIcon>Resources\icon_07.ico</ApplicationIcon>
<StartupObject>ModEngine2ConfigTool.EntryPoint</StartupObject>
Expand Down
4 changes: 2 additions & 2 deletions ModEngine2ConfigTool/Services/PlayManagerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ await _modEngine2Service
catch (InvalidOperationException e)
{
MessageBox.Show(e.Message);
Log.Instance.Error(e.Message);
}

//// pop current profile saves
Expand All @@ -206,8 +207,7 @@ await _modEngine2Service
catch(Exception e)
{
MessageBox.Show(e.Message);
var logger = Logger.GetLogger(nameof(App));
logger.Error(e.Message);
Log.Instance.Error(e.Message);
}
}

Expand Down
48 changes: 46 additions & 2 deletions ModEngine2ConfigTool/Views/Converter/EmptyImagePathConverter.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
Expand All @@ -19,20 +20,63 @@ public object Convert(object value, Type targetType, object parameter, CultureIn
{
if(value is string imagePath && !string.IsNullOrWhiteSpace(imagePath) && File.Exists(imagePath))
{
return BitmapFrame.Create(new Uri(imagePath), BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.OnLoad);
return CreateBitmap(imagePath);
}

if(parameter is not string fallbackPath)
{
return DependencyProperty.UnsetValue;
}

return BitmapFrame.Create(new Uri(Path.GetFullPath($".\\Resources\\{fallbackPath}.png")), BitmapCreateOptions.IgnoreImageCache, BitmapCacheOption.OnLoad);
return CreateBitmap($".\\Resources\\{fallbackPath}.png");
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}

private BitmapFrame CreateBitmap(string path)
{
try
{
var encoder = new PngBitmapEncoder();
var image = new BitmapImage(new Uri(path, UriKind.Relative))
{
CreateOptions = BitmapCreateOptions.IgnoreImageCache,
CacheOption = BitmapCacheOption.OnLoad
};

encoder.Frames.Add(BitmapFrame.Create(image));

return encoder.Frames[0];
}
catch
{
return BitmapFrame.Create(CreateEmptyBitmap());
}
}

private BitmapSource CreateEmptyBitmap()
{
int width = 128;
int height = width;
int stride = width / 8;
byte[] pixels = new byte[height * stride];

// Try creating a new image with a custom palette.
List<System.Windows.Media.Color> colors = new List<System.Windows.Media.Color>();
colors.Add(System.Windows.Media.Colors.Black);
BitmapPalette myPalette = new BitmapPalette(colors);

// Creates a new empty image with the pre-defined palette
return BitmapSource.Create(
width, height,
96, 96,
PixelFormats.Indexed1,
myPalette,
pixels,
stride);
}
}
}
3 changes: 2 additions & 1 deletion ModEngine2ConfigTool/Views/Pages/DllsPageView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
mc:Ignorable="d">
<UserControl.Resources>
<converter:ProfileModTupleConverter x:Key="ProfileModTupleConverter" />
<converter:EmptyImagePathConverter x:Key="EmptyImagePathConverter" />
</UserControl.Resources>
<Grid>
<Image x:Name="BackImage"
VerticalAlignment="Top"
RenderOptions.BitmapScalingMode="HighQuality"
Source="{Binding BackgroundImage}"
Source="{Binding BackgroundImage, Converter={StaticResource EmptyImagePathConverter}, ConverterParameter='EldenRing'}"
Stretch="UniformToFill">
<Image.OpacityMask>
<LinearGradientBrush StartPoint="1,0" EndPoint="1,1">
Expand Down
5 changes: 4 additions & 1 deletion ModEngine2ConfigTool/Views/Pages/HomePageView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<UserControl.Resources>
<converter:EmptyImagePathConverter x:Key="EmptyImagePathConverter" />
</UserControl.Resources>
<Grid>
<Image x:Name="BackImage"
VerticalAlignment="Top"
RenderOptions.BitmapScalingMode="HighQuality"
Source="{Binding BackgroundImage}"
Source="{Binding BackgroundImage, Converter={StaticResource EmptyImagePathConverter}, ConverterParameter='EldenRing'}"
Stretch="UniformToFill">
<Image.OpacityMask>
<LinearGradientBrush StartPoint="1,0" EndPoint="1,1">
Expand Down
3 changes: 2 additions & 1 deletion ModEngine2ConfigTool/Views/Pages/ModsPageView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
mc:Ignorable="d">
<UserControl.Resources>
<converter:ProfileModTupleConverter x:Key="ProfileModTupleConverter" />
<converter:EmptyImagePathConverter x:Key="EmptyImagePathConverter" />
</UserControl.Resources>
<Grid>
<Image x:Name="BackImage"
VerticalAlignment="Top"
RenderOptions.BitmapScalingMode="HighQuality"
Source="{Binding BackgroundImage}"
Source="{Binding BackgroundImage, Converter={StaticResource EmptyImagePathConverter}, ConverterParameter='EldenRing'}"
Stretch="UniformToFill">
<Image.OpacityMask>
<LinearGradientBrush StartPoint="1,0" EndPoint="1,1">
Expand Down
5 changes: 4 additions & 1 deletion ModEngine2ConfigTool/Views/Pages/ProfilesPageView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<UserControl.Resources>
<converter:EmptyImagePathConverter x:Key="EmptyImagePathConverter" />
</UserControl.Resources>
<Grid>
<Image x:Name="BackImage"
VerticalAlignment="Top"
RenderOptions.BitmapScalingMode="HighQuality"
Source="{Binding BackgroundImage}"
Source="{Binding BackgroundImage, Converter={StaticResource EmptyImagePathConverter}, ConverterParameter='EldenRing'}"
Stretch="UniformToFill">
<Image.OpacityMask>
<LinearGradientBrush StartPoint="1,0" EndPoint="1,1">
Expand Down

0 comments on commit d471fb8

Please sign in to comment.