diff --git a/Mirivoice/Mirivoice.Core/Format/Mrp.cs b/Mirivoice/Mirivoice.Core/Format/Mrp.cs index b3be984..a5cac62 100644 --- a/Mirivoice/Mirivoice.Core/Format/Mrp.cs +++ b/Mirivoice/Mirivoice.Core/Format/Mrp.cs @@ -103,15 +103,29 @@ await Dispatcher.UIThread.InvokeAsync(async () => var mLineTasks =this.mLines .Select(async (l, index) => { - - int voicerIndex = MainManager.Instance.VoicerM.FindVoicerIndex( - MainManager.Instance.VoicerM.FindVoicerWithNameAndLangCodeAndUUID(l.voicerName, l.langCode, l.voicerUuid) - ); + int voicerIndex; + Voicer voicer = MainManager.Instance.VoicerM.FindVoicerWithNameAndLangCodeAndUUID(l.voicerName, l.langCode, l.voicerUuid); + if (voicer != null) + { + voicerIndex = MainManager.Instance.VoicerM.FindVoicerIndex( + voicer + ); + } + else + { + Log.Information($"[Mrp load] voicer not exists, use first voicer instead."); + voicerIndex = 0; + MainManager.Instance.cmd.ChangedToDefVoicer = true; + } int spkid = l.voicerStyleId; int metaIndex = 0; - foreach (VoicerMeta v in v.voicerSelector.Voicers[voicerIndex].VoicerMetaCollection) + + v.voicerSelector.UpdateVoicerCollection(); + Log.Debug($"voicerIndex = {voicerIndex}, Voicers = {v.voicerSelector.Voicers}"); + foreach (VoicerMeta v_ in v.voicerSelector.Voicers[voicerIndex].VoicerMetaCollection) { - if (v.SpeakerId == spkid) + + if (v_.SpeakerId == spkid) { break; } @@ -136,6 +150,7 @@ await Dispatcher.UIThread.InvokeAsync(async () => if (defVoicer == null) { Log.Warning($"Non-Existing Voicer: {this.DefaultVoicerName}. Using Current Default Voicer instead."); + MainManager.Instance.cmd.ChangedToDefVoicer = true; } else { @@ -145,8 +160,6 @@ await Dispatcher.UIThread.InvokeAsync(async () => } - - MainManager.Instance.cmd.ProjectOpened(); }, DispatcherPriority.Send); await Dispatcher.UIThread.InvokeAsync(() => { diff --git a/Mirivoice/Mirivoice.Core/Managers/CommandManager.cs b/Mirivoice/Mirivoice.Core/Managers/CommandManager.cs index 56df35e..41f88b6 100644 --- a/Mirivoice/Mirivoice.Core/Managers/CommandManager.cs +++ b/Mirivoice/Mirivoice.Core/Managers/CommandManager.cs @@ -12,6 +12,7 @@ public class CommandManager private readonly Stack _undoStack = new Stack(); private readonly Stack _redoStack = new Stack(); private MainViewModel v; + public bool ChangedToDefVoicer = false; public bool IsNeedSave = true; @@ -27,7 +28,16 @@ public void SetMainViewModel(MainViewModel v) public void ProjectOpened() { ClearStacks(); - IsNeedSave = false; + if (ChangedToDefVoicer) + { + IsNeedSave = true; + ChangedToDefVoicer = false; + } + else + { + IsNeedSave = false; + } + v.OnPropertyChanged(nameof(v.Title)); } diff --git a/Mirivoice/Mirivoice.Core/Managers/VoicerManager.cs b/Mirivoice/Mirivoice.Core/Managers/VoicerManager.cs index 14993d1..a739186 100644 --- a/Mirivoice/Mirivoice.Core/Managers/VoicerManager.cs +++ b/Mirivoice/Mirivoice.Core/Managers/VoicerManager.cs @@ -12,7 +12,7 @@ namespace Mirivoice.Mirivoice.Core.Managers public class VoicerManager { string[] VoicerDirs; - List ValidVoicerDirs; + public List ValidVoicerDirs; public VoicerManager() { @@ -37,11 +37,26 @@ public Voicer FindVoicerWithName(string name) return null; } + public Voicer FindLastInstalledVoicer() + { + VoicerInfo voicerInfo; + string voicerDir = ValidVoicerDirs.Last(); + string voicerYamlPath = Path.Combine(voicerDir, "voicer.yaml"); + var yamlUtf8Bytes = System.Text.Encoding.UTF8.GetBytes(ReadTxtFile(voicerYamlPath)); + voicerInfo = YamlSerializer.Deserialize(yamlUtf8Bytes); + Voicer voicer = new Voicer(voicerInfo); + voicer.SetRootPath(voicerDir); + return voicer; + } + public int FindVoicerIndex(Voicer voicer) { + Log.Information($"Valid dirs: {ValidVoicerDirs}"); + return ValidVoicerDirs.IndexOf(voicer.RootPath); } - public Voicer FindVoicerWithNameAndLangCodeAndUUID(string name, string langCode, string uuid) + + public Voicer? FindVoicerWithNameAndLangCodeAndUUID(string name, string langCode, string uuid) { // priority: langCode > name > uuid Voicer FindFirstMatchBy(Func predicate, List voicers) @@ -120,7 +135,11 @@ public List LoadVoicers (PathManager pathM) voicers.Add(voicer); - ValidVoicerDirs.Add(voicerDir); + if (! ValidVoicerDirs.Contains(voicerDir)) + { + ValidVoicerDirs.Add(voicerDir); + } + } else { diff --git a/Mirivoice/Mirivoice.Core/Utils/VoicerInstaller.cs b/Mirivoice/Mirivoice.Core/Utils/VoicerInstaller.cs index 35ffe59..3b7d792 100644 --- a/Mirivoice/Mirivoice.Core/Utils/VoicerInstaller.cs +++ b/Mirivoice/Mirivoice.Core/Utils/VoicerInstaller.cs @@ -7,6 +7,8 @@ using System.Threading.Tasks; using System.Xml.Linq; using Avalonia.Controls; +using Avalonia.Controls.Shapes; +using DynamicData; using Mirivoice; using Mirivoice.Mirivoice.Core.Format; using Mirivoice.ViewModels; @@ -59,11 +61,22 @@ public async void InstallVoicers(string[] paths) { Log.Information($"Installing voicer from {p}"); // Currently it puts suffix to the directory name if it already exists, but if the bug in VoicerSelector.cs is fixed, it should be changed to overwrite the existing directory(to update Voicer). - var result = await v.ShowTaskWindow("menu.tools.voicerinstall", "menu.tools.voicerinstall", Install(MakeDirectoryUnique(p)), "menu.tools.voicerinstall.process", "menu.tools.voicerinstall.success", "menu.tools.voicerinstall.failed"); + string finalPath = MakeDirectoryUnique(p); + var result = await v.ShowTaskWindow("menu.tools.voicerinstall", "menu.tools.voicerinstall", Install(finalPath), "menu.tools.voicerinstall.process", "menu.tools.voicerinstall.success", "menu.tools.voicerinstall.failed"); + + string dirName = System.IO.Path.GetFileNameWithoutExtension(finalPath); + string extPath = System.IO.Path.Combine(basePath, dirName); + if (! MainManager.Instance.VoicerM.ValidVoicerDirs.Contains(extPath)) + { + MainManager.Instance.VoicerM.ValidVoicerDirs.Add(extPath); + v.voicerSelector.Voicers.Add(MainManager.Instance.VoicerM.FindLastInstalledVoicer()); + } + Log.Information($"Voicer installed."); } + Log.Information($"Refresh Voicers"); - v.voicerSelector.UpdateVoicerCollection(); + foreach (LineBoxView l in v.LineBoxCollection) { l.viewModel.voicerSelector.UpdateVoicerCollection(); @@ -85,12 +98,13 @@ public async Task Install(string path) { Overwrite = true, }; + string extPath; using (var archive = ArchiveFactory.Open(path, readerOptions)) { var touches = new List(); int total = archive.Entries.Count(); int count = 0; - bool hasVoicerYaml = archive.Entries.Any(e => Path.GetFileName(e.Key) == kVoicerYaml); + bool hasVoicerYaml = archive.Entries.Any(e => System.IO.Path.GetFileName(e.Key) == kVoicerYaml); if (!hasVoicerYaml) { await v.ShowConfirmWindow("menu.tools.voicerinstall.error"); @@ -98,8 +112,8 @@ public async Task Install(string path) return false; } - string dirName = Path.GetFileNameWithoutExtension(path); - string extPath = Path.Combine(basePath, dirName); + string dirName = System.IO.Path.GetFileNameWithoutExtension(path); + extPath = System.IO.Path.Combine(basePath, dirName); Directory.CreateDirectory(extPath); foreach (var entry in archive.Entries) { @@ -108,15 +122,20 @@ public async Task Install(string path) // Prevent zipSlip attack continue; } - var filePath = Path.Combine(extPath, entry.Key); - Directory.CreateDirectory(Path.GetDirectoryName(filePath)); + var filePath = System.IO.Path.Combine(extPath, entry.Key); + Directory.CreateDirectory(System.IO.Path.GetDirectoryName(filePath)); if (!entry.IsDirectory) { - entry.WriteToFile(Path.Combine(extPath, entry.Key), extractionOptions); + entry.WriteToFile(System.IO.Path.Combine(extPath, entry.Key), extractionOptions); } } } + + + + + return true; }