120 lines
3.9 KiB
C#
120 lines
3.9 KiB
C#
using System.Configuration;
|
|
using System.IO;
|
|
using System.Windows;
|
|
using System.Windows.Threading;
|
|
using Livia.Properties;
|
|
using Livia.Views.Utility;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Livia.Models;
|
|
|
|
public class AppHelper(ILogger logger, IWarningSystem warningSystem)
|
|
{
|
|
public void UpdateSettings()
|
|
{
|
|
try
|
|
{
|
|
string? configPathStr = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;
|
|
if (configPathStr == null)
|
|
return;
|
|
|
|
string[] pathSplit = configPathStr.Split('\\');
|
|
string currentDirectory = string.Join("\\", pathSplit.Take(pathSplit.Length - 2));
|
|
string subFolderNamePrefix = string.Join('_', pathSplit[^3].Split('_').Take(2));
|
|
|
|
logger.LogInformation("Settings directory is {path}", currentDirectory);
|
|
if (Directory.Exists(currentDirectory))
|
|
{
|
|
logger.LogInformation("Settings directory already exists, move on");
|
|
}
|
|
else
|
|
{
|
|
logger.LogInformation("Settings directory does not exist. Finding another one");
|
|
string searchDirectory = string.Join("\\", pathSplit.Take(pathSplit.Length - 3));
|
|
if (Directory.Exists(searchDirectory))
|
|
{
|
|
string[] possibleDirectories = Directory.GetDirectories(searchDirectory, "*", SearchOption.TopDirectoryOnly);
|
|
|
|
foreach (string possibleDirectory in possibleDirectories)
|
|
{
|
|
string directoryName = Path.GetRelativePath(searchDirectory, possibleDirectory);
|
|
if (!directoryName.StartsWith(subFolderNamePrefix))
|
|
continue;
|
|
logger.LogInformation("Found {directory}", directoryName);
|
|
Directory.Move(possibleDirectory, currentDirectory);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logger.LogError(e, "Error updating settings");
|
|
throw;
|
|
}
|
|
|
|
if (!Settings.Default.UpgradeRequired)
|
|
return;
|
|
|
|
logger.LogInformation("Upgrading Settings");
|
|
|
|
Settings.Default.Upgrade();
|
|
ViewSettings.Default.Upgrade();
|
|
|
|
Settings.Default.UpgradeRequired = false;
|
|
|
|
Settings.Default.Save();
|
|
ViewSettings.Default.Save();
|
|
}
|
|
|
|
public void LoadStrings()
|
|
{
|
|
//load language files
|
|
string language = Settings.Default.Language;
|
|
if (language == "zh-CN")
|
|
{
|
|
logger.LogInformation("Using zh language, no additional strings needs loading");
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
language = language.Split('-')[0];
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logger.LogInformation(e, "Cannot parse language {language}", language);
|
|
}
|
|
|
|
logger.LogInformation($"Loading strings for {language}");
|
|
|
|
//livia dict
|
|
LoadXamlFile($"/livia;component/Resources/Strings/{language}.xaml");
|
|
|
|
//dict of each project
|
|
LoadXamlFile($"../Resources/Strings/{language}.xaml");
|
|
}
|
|
}
|
|
|
|
private void LoadXamlFile(string path)
|
|
{
|
|
try
|
|
{
|
|
//livia dict
|
|
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
|
|
{
|
|
Source = new Uri(path, UriKind.RelativeOrAbsolute)
|
|
});
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logger.LogError(e, "Error load language files for {path}", path);
|
|
}
|
|
}
|
|
|
|
public void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
|
|
{
|
|
logger.LogError(e.Exception, "System crashing");
|
|
warningSystem.ShowDialog(WarningWindowKind.Error, true, "SystemCrashing");
|
|
}
|
|
} |