Skip to content

Commit

Permalink
Merge pull request #954 from maiko3tattun/1127_FixPreference
Browse files Browse the repository at this point in the history
Improved stability of preferences
  • Loading branch information
stakira authored Nov 30, 2023
2 parents 7342b1b + 7bd415c commit 47ccf84
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
2 changes: 1 addition & 1 deletion OpenUtau.Core/Util/Onnx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ private static SessionOptions getOnnxSessionOptions(){
if (String.IsNullOrEmpty(runner)) {
runner = runnerOptions[0];
}
if (!(runnerOptions.Contains(runner))) {
if (!runnerOptions.Contains(runner)) {
runner = "cpu";
}
switch(runner){
Expand Down
20 changes: 20 additions & 0 deletions OpenUtau.Core/Util/Preferences.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using Newtonsoft.Json;
using OpenUtau.Core.Render;
using Serilog;

namespace OpenUtau.Core.Util {
Expand Down Expand Up @@ -87,6 +89,15 @@ private static void Load() {
if (File.Exists(PathManager.Inst.PrefsFilePath)) {
Default = JsonConvert.DeserializeObject<SerializablePreferences>(
File.ReadAllText(PathManager.Inst.PrefsFilePath, Encoding.UTF8));
if(Default == null) {
Reset();
return;
}

if (!ValidString(new Action(() => CultureInfo.GetCultureInfo(Default.Language)))) Default.Language = string.Empty;
if (!ValidString(new Action(() => CultureInfo.GetCultureInfo(Default.SortingOrder)))) Default.SortingOrder = string.Empty;
if (!Renderers.getRendererOptions().Contains(Default.DefaultRenderer)) Default.DefaultRenderer = string.Empty;
if (!Onnx.getRunnerOptions().Contains(Default.OnnxRunner)) Default.OnnxRunner = string.Empty;
} else {
Reset();
}
Expand All @@ -96,6 +107,15 @@ private static void Load() {
}
}

private static bool ValidString(Action action) {
try {
action();
return true;
} catch {
return false;
}
}

[Serializable]
public class SerializablePreferences {
public const int MidiWidth = 1024;
Expand Down
2 changes: 1 addition & 1 deletion OpenUtau/ViewModels/PreferencesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public AudioOutputDevice? AudioOutputDevice {
[Reactive] public int PlaybackAutoScroll { get; set; }
[Reactive] public double PlayPosMarkerMargin { get; set; }
[Reactive] public int LockStartTime { get; set; }
public string AdditionalSingersPath => PathManager.Inst.AdditionalSingersPath;
public string AdditionalSingersPath => !string.IsNullOrWhiteSpace(PathManager.Inst.AdditionalSingersPath)? PathManager.Inst.AdditionalSingersPath : "(None)";
[Reactive] public bool InstallToAdditionalSingersPath { get; set; }
[Reactive] public bool PreRender { get; set; }
public List<string> DefaultRendererOptions { get; set; }
Expand Down
11 changes: 10 additions & 1 deletion OpenUtau/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -539,8 +539,17 @@ async void OnMenuInstallSinger(object sender, RoutedEventArgs args) {
}

void OnMenuPreferences(object sender, RoutedEventArgs args) {
PreferencesViewModel dataContext;
try {
dataContext = new PreferencesViewModel();
} catch (Exception e) {
Log.Error(e, "Failed to load prefs. Initialize it.");
MessageBox.ShowError(this, e);
Preferences.Reset();
dataContext = new PreferencesViewModel();
}
var dialog = new PreferencesDialog() {
DataContext = new PreferencesViewModel(),
DataContext = dataContext
};
dialog.ShowDialog(this);
if (dialog.Position.Y < 0) {
Expand Down

0 comments on commit 47ccf84

Please sign in to comment.