Skip to content

Commit

Permalink
Fixed voicers not properly updated and leads to Mrp load fail when us…
Browse files Browse the repository at this point in the history
…ing voicer installer
  • Loading branch information
EX3exp committed Oct 10, 2024
1 parent 17b2f31 commit 24384cf
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 20 deletions.
29 changes: 21 additions & 8 deletions Mirivoice/Mirivoice.Core/Format/Mrp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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
{
Expand All @@ -145,8 +160,6 @@ await Dispatcher.UIThread.InvokeAsync(async () =>
}



MainManager.Instance.cmd.ProjectOpened();
}, DispatcherPriority.Send);
await Dispatcher.UIThread.InvokeAsync(() =>
{
Expand Down
12 changes: 11 additions & 1 deletion Mirivoice/Mirivoice.Core/Managers/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public class CommandManager
private readonly Stack<ICommand> _undoStack = new Stack<ICommand>();
private readonly Stack<ICommand> _redoStack = new Stack<ICommand>();
private MainViewModel v;
public bool ChangedToDefVoicer = false;
public bool IsNeedSave = true;


Expand All @@ -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));
}

Expand Down
25 changes: 22 additions & 3 deletions Mirivoice/Mirivoice.Core/Managers/VoicerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Mirivoice.Mirivoice.Core.Managers
public class VoicerManager
{
string[] VoicerDirs;
List<string> ValidVoicerDirs;
public List<string> ValidVoicerDirs;

public VoicerManager()
{
Expand All @@ -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<VoicerInfo>(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<Voicer, bool> predicate, List<Voicer> voicers)
Expand Down Expand Up @@ -120,7 +135,11 @@ public List<Voicer> LoadVoicers (PathManager pathM)


voicers.Add(voicer);
ValidVoicerDirs.Add(voicerDir);
if (! ValidVoicerDirs.Contains(voicerDir))
{
ValidVoicerDirs.Add(voicerDir);
}

}
else
{
Expand Down
35 changes: 27 additions & 8 deletions Mirivoice/Mirivoice.Core/Utils/VoicerInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -85,21 +98,22 @@ public async Task<bool> Install(string path)
{
Overwrite = true,
};
string extPath;
using (var archive = ArchiveFactory.Open(path, readerOptions))
{
var touches = new List<string>();
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");
Log.Error("Voicer archive does not contain voicer.yaml.");
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)
{
Expand All @@ -108,15 +122,20 @@ public async Task<bool> 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;
}

Expand Down

0 comments on commit 24384cf

Please sign in to comment.