Skip to content

Commit

Permalink
引擎初始化提前及报错弹窗
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuYunPlayer committed May 29, 2024
1 parent 62e69d2 commit f699f8b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
18 changes: 18 additions & 0 deletions TuneLab/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System;
using Tmds.DBus.Protocol;
using TuneLab.GUI;
using TuneLab.Extensions.Voices;

namespace TuneLab;

Expand Down Expand Up @@ -58,6 +59,23 @@ public override void OnFrameworkInitializationCompleted()
dialog.AddButton("Quit", Dialog.ButtonType.Primary).Clicked += () => { Process.GetCurrentProcess().Kill(); };
dialog.Show();
}

// 暂时改为提前初始化,使Set Voice右键菜单能更快弹出
foreach (var engine in VoicesManager.GetAllVoiceEngines())
{
try
{
VoicesManager.InitEngine(engine);
}
catch (Exception ex)
{
var dialog = new Dialog();
dialog.SetTitle("Error");
dialog.SetMessage(string.Format("Voice engine [{0}] failed to init:\n{1}", engine, ex.Message));
dialog.AddButton("OK", Dialog.ButtonType.Primary);
dialog.Show();
}
}
}

base.OnFrameworkInitializationCompleted();
Expand Down
2 changes: 1 addition & 1 deletion TuneLab/Extensions/ExtensionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public static void LoadExtensions()
{
PathManager.MakeSure(PathManager.ExtensionsFolder);
FormatsManager.LoadBuiltIn();
VoicesManager.LoadBuiltIn();
foreach (var dir in Directory.GetDirectories(PathManager.ExtensionsFolder))
{
Load(dir);
}
VoicesManager.LoadBuiltIn();
}

public static void Destroy()
Expand Down
25 changes: 16 additions & 9 deletions TuneLab/Extensions/Voices/VoicesManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ public static void Load(string path)
}
catch { }
}

if (GetInitedEngine(string.Empty) == null)
throw new Exception("Default engine init failed!");
}

public static void Destroy()
Expand All @@ -76,9 +73,6 @@ static void LoadFromTypes(Type[] types, string path)
{
if (typeof(IVoiceEngine).IsAssignableFrom(type))
{
if (mVoiceEngines.ContainsKey(attribute.Type))
continue;

var constructor = type.GetConstructor(Type.EmptyTypes);
if (constructor != null)
mVoiceEngines.Add(attribute.Type, new VoiceEngineStatus((IVoiceEngine)constructor.Invoke(null), path));
Expand Down Expand Up @@ -115,6 +109,16 @@ public static IVoiceSource Create(string type, string id)
return Create(string.Empty, string.Empty);
}

public static void InitEngine(string type)
{
var engine = mVoiceEngines[type];
if (engine.IsInited)
return;

if (!engine.Init(out var error))
throw new Exception(error);
}

static IVoiceEngine? GetInitedEngine(string type)
{
if (!mVoiceEngines.ContainsKey(type))
Expand All @@ -126,10 +130,13 @@ public static IVoiceSource Create(string type, string id)

if (!engine.IsInited)
{
bool success = engine.Init(out string? error);
if (!success)
try
{
InitEngine(type);
}
catch (Exception ex)
{
Log.Error(string.Format("Engine {0} init failed: {1}", type, error));
Log.Error(string.Format("Engine {0} init failed: {1}", type, ex));
return null;
}
}
Expand Down

0 comments on commit f699f8b

Please sign in to comment.