Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add recovery project function #1166

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions OpenUtau.Core/DocManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,13 @@ public void SearchAllPlugins() {
UCommandGroup? undoGroup = null;
UCommandGroup? savedPoint = null;
UCommandGroup? autosavedPoint = null;
public bool Recovered { get; set; } = false; // Flag to not overwrite backup file

public bool ChangesSaved {
get {
return (Project.Saved || (Project.tracks.Count <= 1 && Project.parts.Count == 0)) &&
(undoQueue.Count > 0 && savedPoint == undoQueue.Last() || undoQueue.Count == 0 && savedPoint == null);
(undoQueue.Count > 0 && savedPoint == undoQueue.Last() || undoQueue.Count == 0 && savedPoint == null) &&
!Recovered;
}
}

Expand All @@ -142,7 +144,7 @@ private void CrashSave() {
: Path.GetFileNameWithoutExtension(Project.FilePath);
string backup = Path.Join(dir, filename + "-backup.ustx");
Log.Information($"Saving backup {backup}.");
Format.Ustx.Save(backup, Project);
Format.Ustx.AutoSave(backup, Project);
Log.Information($"Saved backup {backup}.");
} catch (Exception e) {
Log.Error(e, "Save backup failed.");
Expand Down
19 changes: 19 additions & 0 deletions OpenUtau.Core/Format/Formats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,25 @@ public static UProject[] ReadProjects(string[] files){
.ToArray();
}

/// <summary>
/// Load project from backup file.
/// </summary>
/// <param name="files">Names of the files to be loaded</param>
public static void RecoveryProject(string[] files) {
UProject project = ReadProject(files);
if (project != null) {
string originalPath = project.FilePath.Replace("-autosave.ustx", ".ustx").Replace("-backup.ustx", ".ustx");
if (File.Exists(originalPath)) {
project.FilePath = originalPath;
} else {
project.FilePath = string.Empty;
project.Saved = false;
}

DocManager.Inst.ExecuteCmd(new LoadProjectNotification(project));
}
}

/// <summary>
/// Import tracks from files to the current existing editing project.
/// </summary>
Expand Down
6 changes: 6 additions & 0 deletions OpenUtau.Core/Format/USTx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;
using OpenUtau.Classic;
using OpenUtau.Core.Ustx;
using OpenUtau.Core.Util;
using Serilog;

namespace OpenUtau.Core.Format {
Expand Down Expand Up @@ -101,6 +102,9 @@ public static void Save(string filePath, UProject project) {
File.WriteAllText(filePath, Yaml.DefaultSerializer.Serialize(project), Encoding.UTF8);
project.Saved = true;
project.AfterSave();
Preferences.Default.RecoveryPath = string.Empty;
Preferences.Save();
DocManager.Inst.Recovered = false;
} catch (Exception ex) {
var e = new MessageCustomizableException("Failed to save ustx: {filePath}", $"<translate:errors.failed.save>: {filePath}", ex);
DocManager.Inst.ExecuteCmd(new ErrorMessageNotification(e));
Expand All @@ -113,6 +117,8 @@ public static void AutoSave(string filePath, UProject project) {
project.BeforeSave();
File.WriteAllText(filePath, Yaml.DefaultSerializer.Serialize(project), Encoding.UTF8);
project.AfterSave();
Preferences.Default.RecoveryPath = filePath;
Preferences.Save();
} catch (Exception ex) {
Log.Error(ex, $"Failed to autosave: {filePath}");
}
Expand Down
2 changes: 1 addition & 1 deletion OpenUtau.Core/Util/Preferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,9 @@ public class SerializablePreferences {
public bool LockUnselectedNotesPitch = true;
public bool LockUnselectedNotesVibrato = true;
public bool LockUnselectedNotesExpressions = true;

public bool VoicebankPublishUseIgnore = true;
public string VoicebankPublishIgnores = "#Adobe Audition\n*.pkf\n\n#UTAU Engines\n*.ctspec\n*.d4c\n*.dio\n*.frc\n*.frt\n#*.frq\n*.harvest\n*.lessaudio\n*.llsm\n*.mrq\n*.pitchtier\n*.pkf\n*.platinum\n*.pmk\n*.star\n*.uspec\n*.vs4ufrq\n\n#UTAU related tools\n$read\n*.setParam-Scache\n*.lbp\n*.lbp.caches/*\n\n#OpenUtau\nerrors.txt\n*.sc.npz";
public string RecoveryPath = string.Empty;
}
}
}
2 changes: 2 additions & 0 deletions OpenUtau/Strings/Strings.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
<system:String x:Key="dialogs.messagebox.yes">Yes</system:String>
<system:String x:Key="dialogs.noresampler.caption">No resampler</system:String>
<system:String x:Key="dialogs.noresampler.message">No resampler! Put your favourite resampler exe or dll in the Resamplers folder and choose it in Preferences!</system:String>
<system:String x:Key="dialogs.recovery">Previously, OpenUtau terminated abnormally. Do you want to recover?</system:String>
<system:String x:Key="dialogs.recovery.caption">Recovery project</system:String>
<system:String x:Key="dialogs.remaptimeaxis.message">This tool will change the tempo of the project without changing the actual positions and durations (in seconds) of notes.
New BPM:</system:String>
<system:String x:Key="dialogs.timesig.caption">Time Signature</system:String>
Expand Down
2 changes: 2 additions & 0 deletions OpenUtau/Strings/Strings.ja-JP.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
<system:String x:Key="dialogs.messagebox.yes">はい</system:String>
<system:String x:Key="dialogs.noresampler.caption">エンジンがありません</system:String>
<system:String x:Key="dialogs.noresampler.message">エンジンが設定されていません!お好みのエンジンのEXEやDLLファイルをResamplerフォルダに入れて、環境設定でエンジンを選択してください!</system:String>
<system:String x:Key="dialogs.recovery">前回異常終了しました。復旧しますか?</system:String>
<system:String x:Key="dialogs.recovery.caption">プロジェクトの復旧</system:String>
<system:String x:Key="dialogs.remaptimeaxis.message">このツールでは、音符の実際の位置と長さ(秒単位)を変えずに、プロジェクトのテンポを変更します。
新しいBPM:</system:String>
<system:String x:Key="dialogs.timesig.caption">拍子</system:String>
Expand Down
30 changes: 26 additions & 4 deletions OpenUtau/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using OpenUtau.App.Views;
using OpenUtau.Core;
using OpenUtau.Core.Ustx;
using OpenUtau.Core.Util;
using ReactiveUI;
using ReactiveUI.Fody.Helpers;

Expand Down Expand Up @@ -90,19 +91,38 @@ public void Redo() {
DocManager.Inst.Redo();
}

public void InitProject() {
public async void InitProject(MainWindow window) {
var recPath = Preferences.Default.RecoveryPath;
if (!string.IsNullOrWhiteSpace(recPath) && File.Exists(recPath)) {
var result = await MessageBox.Show(
window,
$"{ThemeManager.GetString("dialogs.recovery")}\n{recPath}",
ThemeManager.GetString("dialogs.recovery.caption"),
MessageBox.MessageBoxButtons.YesNo);
if (result == MessageBox.MessageBoxResult.Yes) {
DocManager.Inst.ExecuteCmd(new LoadingNotification(typeof(MainWindow), true, "project"));
try {
Core.Format.Formats.RecoveryProject(new string[] { recPath });
DocManager.Inst.ExecuteCmd(new VoiceColorRemappingNotification(-1, true));
DocManager.Inst.Recovered = true;
this.RaisePropertyChanged(nameof(Title));
} finally {
DocManager.Inst.ExecuteCmd(new LoadingNotification(typeof(MainWindow), false, "project"));
}
return;
}
}

var args = Environment.GetCommandLineArgs();
if (args.Length == 2 && File.Exists(args[1])) {
try {
Core.Format.Formats.LoadProject(new string[] { args[1] });
DocManager.Inst.ExecuteCmd(new VoiceColorRemappingNotification(-1, true));
return;
} catch (Exception e) {
var customEx = new MessageCustomizableException($"Failed to open file {args[1]}", $"<translate:errors.failed.openfile>: {args[1]}", e);
DocManager.Inst.ExecuteCmd(new ErrorMessageNotification(customEx));
}
}
NewProject();
}

public void NewProject() {
Expand All @@ -119,6 +139,7 @@ public void NewProject() {
}
}
DocManager.Inst.ExecuteCmd(new LoadProjectNotification(Core.Format.Ustx.Create()));
DocManager.Inst.Recovered = false;
}

public void OpenProject(string[] files) {
Expand All @@ -133,6 +154,7 @@ public void OpenProject(string[] files) {
} finally {
DocManager.Inst.ExecuteCmd(new LoadingNotification(typeof(MainWindow), false, "project"));
}
DocManager.Inst.Recovered = false;
}

public void SaveProject(string file = "") {
Expand All @@ -142,7 +164,7 @@ public void SaveProject(string file = "") {
DocManager.Inst.ExecuteCmd(new SaveProjectNotification(file));
this.RaisePropertyChanged(nameof(Title));
}

public void ImportTracks(UProject[] loadedProjects, bool importTempo){
if (loadedProjects == null || loadedProjects.Length < 1) {
return;
Expand Down
11 changes: 8 additions & 3 deletions OpenUtau/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.IO;
using System.Linq;
using System.Reactive;
using System.Threading;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
Expand Down Expand Up @@ -53,7 +52,7 @@ public MainWindow() {
Log.Information("Initialized main window component.");
DataContext = viewModel = new MainWindowViewModel();

viewModel.InitProject();
viewModel.NewProject();
viewModel.AddTempoChangeCmd = ReactiveCommand.Create<int>(tick => AddTempoChange(tick));
viewModel.DelTempoChangeCmd = ReactiveCommand.Create<int>(tick => DelTempoChange(tick));
viewModel.AddTimeSigChangeCmd = ReactiveCommand.Create<int>(bar => AddTimeSigChange(bar));
Expand Down Expand Up @@ -88,6 +87,10 @@ public MainWindow() {
Log.Information("Created main window.");
}

public void InitProject() {
viewModel.InitProject(this);
}

void OnEditTimeSignature(object sender, PointerPressedEventArgs args) {
var project = DocManager.Inst.Project;
var timeSig = project.timeSignatures[0];
Expand Down Expand Up @@ -1298,6 +1301,8 @@ public async void WindowClosing(object? sender, WindowClosingEventArgs e) {
PathManager.Inst.ClearCache();
Log.Information("Cache cleared.");
}
Preferences.Default.RecoveryPath = string.Empty;
Preferences.Save();
return;
}
e.Cancel = true;
Expand Down Expand Up @@ -1361,4 +1366,4 @@ public void OnNext(UCommand cmd, bool isUndo) {
}
}
}
}
}
1 change: 1 addition & 0 deletions OpenUtau/Views/SplashWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ private void Start() {
var mainWindow = new MainWindow();
mainWindow.Show();
desktop.MainWindow = mainWindow;
mainWindow.InitProject();
Close();
}
}, CancellationToken.None, TaskContinuationOptions.None, mainScheduler);
Expand Down
Loading