-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #239 from anno-mods/devel/icon_embeds
Assign an Icon for a tweakerfile
- Loading branch information
Showing
11 changed files
with
246 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using Imya.Models; | ||
using Imya.Services.Interfaces; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Drawing; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Pfim; | ||
using AnnoRDA; | ||
using System.Windows.Media.Imaging; | ||
using System.Windows.Media; | ||
using AnnoRDA.Builder; | ||
|
||
namespace Imya.Services | ||
{ | ||
public class GameFileService : IGameFilesService | ||
{ | ||
private IGameSetupService _gameSetupService; | ||
|
||
private FileSystem _fileSystem; | ||
|
||
private bool isLoading = false; | ||
|
||
public GameFileService(IGameSetupService gameSetupService) | ||
{ | ||
_gameSetupService = gameSetupService; | ||
_gameSetupService.GameRootPathChanged += async (newpath) => await LoadAsync(); | ||
} | ||
|
||
public async Task LoadAsync() | ||
{ | ||
if (!_gameSetupService.IsMaindataValid) | ||
return; | ||
isLoading = true; | ||
await Task.Run(() => | ||
{ | ||
var fs = FileSystemBuilder.Create() | ||
.FromPath(_gameSetupService.MaindataPath) | ||
.WithDefaultSorting() | ||
.OnlyArchivesMatchingWildcard(@"data*.rda") | ||
.AddWhitelisted("*.png", "*_0.dds") | ||
.Build(); | ||
_fileSystem = fs; | ||
isLoading = false; | ||
}); | ||
} | ||
|
||
public Stream? OpenFile(string path) | ||
{ | ||
if (isLoading) | ||
return null; | ||
|
||
return _fileSystem.OpenRead(path); | ||
} | ||
|
||
/// <summary> | ||
/// Like OpenFile, but redirects *.png to *_0.dds | ||
/// </summary> | ||
/// <param name="path"></param> | ||
/// <returns></returns> | ||
public Stream? OpenIcon(string path) | ||
{ | ||
if (path.EndsWith(".png")) | ||
path = path[0..^4] + "_0.dds"; | ||
return OpenFile(path); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using Imya.Models; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Drawing; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Windows.Media; | ||
|
||
namespace Imya.Services.Interfaces | ||
{ | ||
public interface IGameFilesService | ||
{ | ||
Task LoadAsync(); | ||
Stream? OpenFile(String filepath); | ||
Stream? OpenIcon(String iconPath); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
using Imya.Services.Interfaces; | ||
using Pfim; | ||
using System; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Runtime.InteropServices; | ||
using System.Text.RegularExpressions; | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using System.Windows.Data; | ||
using System.Windows.Media; | ||
using System.Windows.Media.Imaging; | ||
|
||
namespace Imya.UI.ValueConverters | ||
{ | ||
[ValueConversion(typeof(String), typeof(ImageSource))] | ||
internal class FilepathToImageConverter : IValueConverter | ||
{ | ||
private IGameFilesService _gameFilesService; | ||
static string parameterregex = @"\b[0-9]+x[0-9]+\b"; | ||
|
||
public FilepathToImageConverter(IGameFilesService gameFilesService) | ||
{ | ||
_gameFilesService = gameFilesService; | ||
} | ||
|
||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) | ||
{ | ||
if (string.IsNullOrWhiteSpace(value as String)) | ||
return new Image(); | ||
|
||
IImage image = Pfimage.FromStream(_gameFilesService.OpenIcon((String)value)); | ||
|
||
if (image is null) | ||
return new Image(); | ||
|
||
Point size; | ||
bool UseMipmaps = false; | ||
if (parameter is string parameter_str && Regex.IsMatch(parameter_str, parameterregex)) | ||
{ | ||
var desired_size = parameter_str.Split("x"); | ||
if (long.TryParse(desired_size[0], out var x) && long.TryParse(desired_size[1], out var y)) | ||
{ | ||
size = new Point(x, y); | ||
UseMipmaps = true; | ||
} | ||
} | ||
var wpfimg = UseMipmaps ? WpfImageMipmapped(image, size) : WpfImage(image); | ||
return wpfimg; | ||
} | ||
|
||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
private static ImageSource WpfImage(IImage image) | ||
{ | ||
var pinnedArray = GCHandle.Alloc(image.Data, GCHandleType.Pinned); | ||
var addr = pinnedArray.AddrOfPinnedObject(); | ||
var bsource = BitmapSource.Create(image.Width, image.Height, 96.0, 96.0, | ||
PixelFormat(image), null, addr, image.DataLen, image.Stride); | ||
|
||
return bsource; | ||
} | ||
|
||
private static ImageSource WpfImageMipmapped(IImage image, Point size) | ||
{ | ||
var pinnedArray = GCHandle.Alloc(image.Data, GCHandleType.Pinned); | ||
var addr = pinnedArray.AddrOfPinnedObject(); | ||
|
||
var mip = image.MipMaps.Where(x => x.Height >= size.X && x.Width >= size.Y).LastOrDefault(); | ||
if (mip is null) | ||
return WpfImage(image); | ||
|
||
var mipAddr = addr + mip.DataOffset; | ||
var mipSource = BitmapSource.Create(mip.Width, mip.Height, 96.0, 96.0, | ||
PixelFormat(image), null, mipAddr, mip.DataLen, mip.Stride); | ||
|
||
return mipSource; | ||
} | ||
|
||
private static PixelFormat PixelFormat(IImage image) | ||
{ | ||
switch (image.Format) | ||
{ | ||
case ImageFormat.Rgb24: | ||
return PixelFormats.Bgr24; | ||
case ImageFormat.Rgba32: | ||
return PixelFormats.Bgra32; | ||
case ImageFormat.Rgb8: | ||
return PixelFormats.Gray8; | ||
case ImageFormat.R5g5b5a1: | ||
case ImageFormat.R5g5b5: | ||
return PixelFormats.Bgr555; | ||
case ImageFormat.R5g6b5: | ||
return PixelFormats.Bgr565; | ||
default: | ||
throw new Exception($"Unable to convert {image.Format} to WPF PixelFormat"); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters