优化保存速度

测试模组: Code
30s -> 10s
This commit is contained in:
Hakoyu 2023-11-29 16:53:59 +08:00
parent 1be307cc93
commit ca1dc6d9dc
4 changed files with 46 additions and 16 deletions

View File

@ -84,23 +84,39 @@ public static class Extensions
/// </summary>
/// <param name="image">图片资源</param>
/// <param name="path">路径</param>
//public static void SaveToPng(this BitmapImage image, string path)
//{
// if (image is null)
// return;
// if (path.EndsWith(".png") is false)
// path += ".png";
// var encoder = new PngBitmapEncoder();
// var stream = image.StreamSource;
// // 保存位置
// var position = stream.Position;
// // 必须要重置位置, 否则EndInit将出错
// stream.Seek(0, SeekOrigin.Begin);
// encoder.Frames.Add(BitmapFrame.Create(image.StreamSource));
// // 恢复位置
// stream.Seek(position, SeekOrigin.Begin);
// using var fs = new FileStream(path, FileMode.Create);
// encoder.Save(fs);
//}
public static void SaveToPng(this BitmapImage image, string path)
{
if (image is null)
return;
if (path.EndsWith(".png") is false)
path += ".png";
var encoder = new PngBitmapEncoder();
var stream = image.StreamSource;
// 保存位置
var position = stream.Position;
// 必须要重置位置, 否则EndInit将出错
stream.Seek(0, SeekOrigin.Begin);
encoder.Frames.Add(BitmapFrame.Create(image.StreamSource));
using var fs = new FileStream(path, FileMode.Create);
stream.CopyTo(fs);
// 恢复位置
stream.Seek(position, SeekOrigin.Begin);
using var fs = new FileStream(path, FileMode.Create);
encoder.Save(fs);
}
/// <summary>

View File

@ -221,11 +221,11 @@ public class PetModel : I18nModel<I18nPetInfoModel>
/// <param name="path">路径</param>
public void Save(string path)
{
if (SourceId is not null)
Id.Value = SourceId;
if (IsSimplePetModel)
{
Id.Value = SourceId;
SaveSimplePetInfo(path);
Id.Value = SourceId + " (来自本体)".Translate();
return;
}
foreach (var cultureName in I18nHelper.Current.CultureNames)
@ -252,13 +252,8 @@ public class PetModel : I18nModel<I18nPetInfoModel>
SaveMoveInfo(lps);
File.WriteAllText(petFile, lps.ToString());
var petAnimePath = Path.Combine(path, Id.Value);
foreach (var anime in Animes)
anime.Save(petAnimePath);
foreach (var anime in FoodAnimes)
anime.Save(petAnimePath);
if (SourceId is not null)
Id.Value = SourceId + " (来自本体)".Translate();
// 保存图片
SaveAnime(path);
}
private void SaveSimplePetInfo(string path)
@ -270,9 +265,16 @@ public class PetModel : I18nModel<I18nPetInfoModel>
SaveWorksInfo(lps);
SaveMoveInfo(lps);
File.WriteAllText(petFile, lps.ToString());
SaveAnime(path);
}
private void SaveAnime(string path)
{
var petAnimePath = Path.Combine(path, Id.Value);
foreach (var animeType in Animes)
animeType.Save(petAnimePath);
foreach (var anime in Animes)
anime.Save(petAnimePath);
foreach (var anime in FoodAnimes)
anime.Save(petAnimePath);
}
/// <summary>

View File

@ -26,6 +26,7 @@
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Label Background="{x:Null}" Content="{ll:Str 打开最近的内容}" />
<Grid Grid.Row="1">
@ -90,7 +91,7 @@
<TextBlock
Grid.Row="1"
VerticalAlignment="Bottom"
d:Text="{ll:Str Mod描述}"
d:Text="{ll:Str 路径}"
Text="{Binding SourcePath}"
TextWrapping="Wrap" />
</Grid>
@ -101,6 +102,11 @@
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
<TextBlock Grid.Row="3" HorizontalAlignment="Center">
<Hyperlink Click="Hyperlink_Click">
<TextBlock Text="{ll:Str 模组制作教程}" />
</Hyperlink>
</TextBlock>
</Grid>
<Grid Grid.Column="1">
<Grid.RowDefinitions>

View File

@ -3,6 +3,7 @@ using LinePutScript.Localization.WPF;
using Panuon.WPF.UI;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
@ -69,4 +70,9 @@ public partial class ModMakerWindow : WindowX
}
ViewModel.LoadMod(history.SourcePath);
}
private void Hyperlink_Click(object sender, RoutedEventArgs e)
{
Process.Start(new ProcessStartInfo("https://github.com/LorisYounger/VPet.ModMaker/wiki"));
}
}