Animations 新增 IsFail 参数, 防止加载失败的图片卡住进程

This commit is contained in:
ZouJin 2024-05-10 17:12:20 +08:00
parent 6d523d464d
commit ff011f6055
6 changed files with 99 additions and 49 deletions

View File

@ -1,6 +1,7 @@
using LinePutScript.Localization.WPF;
using Panuon.WPF.UI;
using System;
using System.Collections.Generic;
using System.Media;
using System.Threading;
using System.Threading.Tasks;
@ -72,6 +73,7 @@ namespace VPet_Simulator.Core
MoveTimer.Elapsed += MoveTimer_Elapsed;
SmartMoveTimer.Elapsed += SmartMoveTimer_Elapsed;
}
public List<string> ErrorMessage = new List<string>();
public async Task Load_2_WaitGraph()
{
//新功能:等待所有图像加载完成再跑
@ -79,11 +81,19 @@ namespace VPet_Simulator.Core
{
foreach (var ig2 in igs.Values)
{
foreach (var ig3 in ig2)
for (int i = 0; i < ig2.Count; i++)
{
IGraph ig3 = ig2[i];
while (!ig3.IsReady)
{
await Task.Delay(100);
if (ig3.IsFail)
{
ErrorMessage.Add(ig3.FailMessage);
ig2.Remove(ig3);
break;
}
else
await Task.Delay(100);
}
}
}

View File

@ -43,6 +43,7 @@ namespace VPet_Simulator.Core
Animations.Add(new Animation(this, sub));
sub = animations.Find("a" + ++i);
}
IsReady = true;
}
public static void LoadGraph(GraphCore graph, FileSystemInfo path, ILine info)
@ -63,12 +64,12 @@ namespace VPet_Simulator.Core
/// 所有动画帧
/// </summary>
public List<Animation> Animations;
/// <summary>
/// 是否循环播放
/// </summary>
public bool IsLoop { get; set; }
/// <summary>
/// 动画信息
/// </summary>
@ -76,10 +77,12 @@ namespace VPet_Simulator.Core
/// <summary>
/// 是否准备完成
/// </summary>
public bool IsReady => true;
public bool IsReady { get; set; } = false;
public bool IsFail => false;
public string FailMessage => "";
public TaskControl Control { get; set; }
int nowid;
/// <summary>
/// 图片资源

View File

@ -272,5 +272,9 @@ namespace VPet_Simulator.Core
///// 其他附带的储存信息
///// </summary>
//public ILine Info { get; set; }
public override string ToString()
{
return $"[{Name}]{Type}_{ModeType.ToString()[0]}{Animat.ToString()[0]}]";
}
}
}

View File

@ -25,6 +25,15 @@ namespace VPet_Simulator.Core
/// 是否准备完成
/// </summary>
bool IsReady { get; }
/// <summary>
/// 是否读取失败
/// </summary>
bool IsFail { get; }
/// <summary>
/// 失败报错信息
/// </summary>
string FailMessage { get; }
/// <summary>
/// 该动画信息
/// </summary>

View File

@ -50,6 +50,10 @@ namespace VPet_Simulator.Core
/// 反正一次性生成太多导致闪退
/// </summary>
public static int NowLoading = 0;
public bool IsFail { get; set; } = false;
public string FailMessage { get; set; } = "";
/// <summary>
/// 新建 PNG 动画
/// </summary>
@ -100,56 +104,67 @@ namespace VPet_Simulator.Core
Thread.Sleep(100);
}
Interlocked.Increment(ref NowLoading);
//新方法:加载大图片
//生成大文件加载非常慢,先看看有没有缓存能用
Path = System.IO.Path.Combine(GraphCore.CachePath, $"{GraphCore.Resolution}_{Math.Abs(Sub.GetHashCode(path))}_{paths.Length}.png");
Width = 500 * (paths.Length + 1);
if (!File.Exists(Path) && !((List<string>)GraphCore.CommConfig["Cache"]).Contains(path))
try
{
((List<string>)GraphCore.CommConfig["Cache"]).Add(path);
int w = 0;
int h = 0;
FileInfo firstImage = paths[0];
var img = System.Drawing.Image.FromFile(firstImage.FullName);
w = img.Width;
h = img.Height;
if (w > GraphCore.Resolution)
//新方法:加载大图片
//生成大文件加载非常慢,先看看有没有缓存能用
Path = System.IO.Path.Combine(GraphCore.CachePath, $"{GraphCore.Resolution}_{Math.Abs(Sub.GetHashCode(path))}_{paths.Length}.png");
Width = 500 * (paths.Length + 1);
if (!File.Exists(Path) && !((List<string>)GraphCore.CommConfig["Cache"]).Contains(path))
{
w = GraphCore.Resolution;
h = (int)(h * (GraphCore.Resolution / (double)img.Width));
}
if (paths.Length * w >= 60000)
{//修复大长动画导致过长分辨率导致可能的报错
w = 60000 / paths.Length;
h = (int)(img.Height * (w / (double)img.Width));
}
using (Bitmap joinedBitmap = new Bitmap(w * paths.Length, h))
using (Graphics graph = Graphics.FromImage(joinedBitmap))
{
using (img)
graph.DrawImage(img, 0, 0, w, h);
Parallel.For(1, paths.Length, i =>
((List<string>)GraphCore.CommConfig["Cache"]).Add(path);
int w = 0;
int h = 0;
FileInfo firstImage = paths[0];
var img = System.Drawing.Image.FromFile(firstImage.FullName);
w = img.Width;
h = img.Height;
if (w > GraphCore.Resolution)
{
using (var img = System.Drawing.Image.FromFile(paths[i].FullName))
w = GraphCore.Resolution;
h = (int)(h * (GraphCore.Resolution / (double)img.Width));
}
if (paths.Length * w >= 60000)
{//修复大长动画导致过长分辨率导致可能的报错
w = 60000 / paths.Length;
h = (int)(img.Height * (w / (double)img.Width));
}
using (Bitmap joinedBitmap = new Bitmap(w * paths.Length, h))
using (Graphics graph = Graphics.FromImage(joinedBitmap))
{
using (img)
graph.DrawImage(img, 0, 0, w, h);
Parallel.For(1, paths.Length, i =>
{
lock (graph)
graph.DrawImage(img, w * i, 0, w, h);
}
});
if (!File.Exists(Path))
joinedBitmap.Save(Path);
using (var img = System.Drawing.Image.FromFile(paths[i].FullName))
{
lock (graph)
graph.DrawImage(img, w * i, 0, w, h);
}
});
if (!File.Exists(Path))
joinedBitmap.Save(Path);
}
}
for (int i = 0; i < paths.Length; i++)
{
var noExtFileName = System.IO.Path.GetFileNameWithoutExtension(paths[i].Name);
int time = int.Parse(noExtFileName.Substring(noExtFileName.LastIndexOf('_') + 1));
Animations.Add(new Animation(this, time, -500 * i));
}
//stream = new MemoryStream(File.ReadAllBytes(cp));
IsReady = true;
}
for (int i = 0; i < paths.Length; i++)
catch (Exception e)
{
var noExtFileName = System.IO.Path.GetFileNameWithoutExtension(paths[i].Name);
int time = int.Parse(noExtFileName.Substring(noExtFileName.LastIndexOf('_') + 1));
Animations.Add(new Animation(this, time, -500 * i));
IsFail = true;
FailMessage =$"--PNGAnimation--{GraphInfo}--\nPath: {path}\n{e.Message}";
}
finally
{
Interlocked.Decrement(ref NowLoading);
}
//stream = new MemoryStream(File.ReadAllBytes(cp));
IsReady = true;
Interlocked.Decrement(ref NowLoading);
}
/// <summary>
@ -316,5 +331,6 @@ namespace VPet_Simulator.Core
});
}
}
}

View File

@ -36,6 +36,7 @@ namespace VPet_Simulator.Core
GraphCore.CommUIElements["Image2.Picture"] = new Image() { Width = 500, Height = 500 };
GraphCore.CommUIElements["Image3.Picture"] = new Image() { Width = 500, Height = 500 };
}
IsReady = true;
}
public static void LoadGraph(GraphCore graph, FileSystemInfo path, ILine info)
{
@ -71,10 +72,14 @@ namespace VPet_Simulator.Core
/// </summary>
public GraphInfo GraphInfo { get; private set; }
public bool IsReady => true;
public bool IsReady { get; set; } = false;
public TaskControl Control { get; set; }
public bool IsFail => false;
public string FailMessage => "";
public void Run(Decorator parant, Action EndAction = null)
{
if (Control?.PlayState == true)
@ -176,6 +181,9 @@ namespace VPet_Simulator.Core
return new Task(() => Run(Control));
});
}
/// <summary>
/// 可以通过图片模块运行该动画
/// </summary>
public interface IImageRun : IGraph
{
/// <summary>