From c66011c76341de767d9ac7c60c98ecdbf0e31225 Mon Sep 17 00:00:00 2001 From: Hakoyu Date: Sat, 9 Sep 2023 21:30:32 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20Utils?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- VPet.ModMaker/Models/Expansions.cs | 16 +++++++++++++++ VPet.ModMaker/Models/ModInfoModel.cs | 4 ++-- VPet.ModMaker/{ => Models}/Utils.cs | 30 ++++++---------------------- VPet.ModMaker/VPet.ModMaker.csproj | 2 +- 4 files changed, 25 insertions(+), 27 deletions(-) rename VPet.ModMaker/{ => Models}/Utils.cs (53%) diff --git a/VPet.ModMaker/Models/Expansions.cs b/VPet.ModMaker/Models/Expansions.cs index 41dbe8d..5ec1b7f 100644 --- a/VPet.ModMaker/Models/Expansions.cs +++ b/VPet.ModMaker/Models/Expansions.cs @@ -1,8 +1,11 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Windows.Media; +using System.Windows.Media.Imaging; namespace VPet.ModMaker.Models; @@ -12,4 +15,17 @@ public static class Extensions { return source.IndexOf(value, comparisonType) >= 0; } + + public static string GetSourceFile(this BitmapImage image) + { + return ((FileStream)image.StreamSource).Name; + } + + public static void CloseString(this ImageSource source) + { + if (source is BitmapImage image) + { + image.StreamSource?.Close(); + } + } } diff --git a/VPet.ModMaker/Models/ModInfoModel.cs b/VPet.ModMaker/Models/ModInfoModel.cs index daef52c..97fc4b9 100644 --- a/VPet.ModMaker/Models/ModInfoModel.cs +++ b/VPet.ModMaker/Models/ModInfoModel.cs @@ -169,7 +169,7 @@ public class ModInfoModel : I18nModel } ); } - var imagePath = Utils.GetImageSourceFile(Image.Value); + var imagePath = Image.Value.GetSourceFile(); var targetImagePath = Path.Combine(path, Path.GetFileName(imagePath)); if (imagePath != targetImagePath) File.Copy(imagePath, targetImagePath, true); @@ -311,7 +311,7 @@ public class ModInfoModel : I18nModel Directory.CreateDirectory(foodPath); foreach (var food in Foods) { - var foodImagePath = Utils.GetImageSourceFile(food.Image.Value); + var foodImagePath = food.Image.Value.GetSourceFile(); var targetImagePath = Path.Combine( foodPath, $"{food.Id.Value}{Path.GetExtension(foodImagePath)}" diff --git a/VPet.ModMaker/Utils.cs b/VPet.ModMaker/Models/Utils.cs similarity index 53% rename from VPet.ModMaker/Utils.cs rename to VPet.ModMaker/Models/Utils.cs index 762fed8..221ffcd 100644 --- a/VPet.ModMaker/Utils.cs +++ b/VPet.ModMaker/Models/Utils.cs @@ -7,15 +7,10 @@ using System.Threading.Tasks; using System.Windows; using System.Windows.Media.Imaging; -namespace VPet.ModMaker; +namespace VPet.ModMaker.Models; -internal static class Utils +public static class Utils { - public static string GetImageSourceFile(BitmapImage image) - { - return ((FileStream)image.StreamSource).Name; - } - public static BitmapImage LoadImageToStream(string imagePath) { BitmapImage bitmapImage = new(); @@ -25,9 +20,9 @@ internal static class Utils return bitmapImage; } - public static BitmapImage LoadImageToStream(BitmapImage image) + public static BitmapImage LoadImageToStream(this BitmapImage image) { - return LoadImageToStream(GetImageSourceFile(image)); + return LoadImageToStream(image.GetSourceFile()); } public static BitmapImage LoadImageToMemoryStream(string imagePath) @@ -42,21 +37,8 @@ internal static class Utils return bitmapImage; } - public static BitmapImage LoadImageToMemoryStream(BitmapImage image) + public static BitmapImage LoadImageToMemoryStream(this BitmapImage image) { - return LoadImageToMemoryStream(GetImageSourceFile(image)); - } - - public static void ShowDialogX(this Window window, Window owner) - { - owner.IsEnabled = false; - window.Owner = owner; - window.Closed += (s, e) => - { - owner.IsEnabled = true; - }; - window.ShowDialog(); + return LoadImageToMemoryStream(image.GetSourceFile()); } } - -public delegate void ShowDialogXHandler(Window window); diff --git a/VPet.ModMaker/VPet.ModMaker.csproj b/VPet.ModMaker/VPet.ModMaker.csproj index 3bb0b6b..b06c4be 100644 --- a/VPet.ModMaker/VPet.ModMaker.csproj +++ b/VPet.ModMaker/VPet.ModMaker.csproj @@ -160,7 +160,7 @@ - + FoodEditWindow.xaml