1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-22 05:40:11 +00:00
msfs-popout-panel-manager/Orchestration/AppSettingDataManager.cs

97 lines
3.4 KiB
C#
Raw Permalink Normal View History

2024-07-28 00:12:07 +00:00
using MSFSPopoutPanelManager.DomainModel.DataFile;
2023-07-12 22:41:31 +00:00
using MSFSPopoutPanelManager.DomainModel.Setting;
using MSFSPopoutPanelManager.Shared;
using Newtonsoft.Json;
2024-07-28 00:12:07 +00:00
using System;
2023-07-12 22:41:31 +00:00
using System.Diagnostics;
using System.IO;
namespace MSFSPopoutPanelManager.Orchestration
{
public class AppSettingDataManager
{
private const string APP_SETTING_DATA_FILENAME = "appsettingdata.json";
public static ApplicationSetting ReadAppSetting()
{
try
{
Debug.WriteLine("Reading application settings data file...");
2024-07-28 00:12:07 +00:00
// Try to read folder path from both locations
var documentsFolderPath = Path.Combine(FileIo.GetUserDataFilePath(false), APP_SETTING_DATA_FILENAME);
var applicationDataFolderPath = Path.Combine(FileIo.GetUserDataFilePath(true), APP_SETTING_DATA_FILENAME);
string folderPath;
if (File.Exists(applicationDataFolderPath))
{
folderPath = applicationDataFolderPath;
FileLogger.UseApplicationDataPath = true;
}
else
{
folderPath = documentsFolderPath;
FileLogger.UseApplicationDataPath = false;
}
using var reader = new StreamReader(folderPath);
2024-02-28 02:44:21 +00:00
var fileContent = reader.ReadToEnd();
2023-07-12 22:41:31 +00:00
2024-07-28 00:12:07 +00:00
var appSetting = JsonConvert.DeserializeObject<AppSettingFile>(fileContent).ApplicationSetting;
appSetting.GeneralSetting.UseApplicationDataPath = folderPath.IndexOf("Roaming", StringComparison.Ordinal) > -1;
return appSetting;
2023-07-12 22:41:31 +00:00
}
catch
{
return null;
}
}
public static void WriteAppSetting(ApplicationSetting appSetting)
{
try
{
2024-07-28 00:12:07 +00:00
CreateAppSettings(appSetting);
}
catch
{
FileLogger.WriteLog($"Unable to write app setting data file: {APP_SETTING_DATA_FILENAME}", StatusMessageType.Error);
}
}
2023-07-12 22:41:31 +00:00
2024-07-28 00:12:07 +00:00
public static void MoveAppSettings(ApplicationSetting appSetting)
{
try
{
CreateAppSettings(appSetting);
2023-07-12 22:41:31 +00:00
2024-07-28 00:12:07 +00:00
// Remove file in old path
var oldPath = FileIo.GetUserDataFilePath(!appSetting.GeneralSetting.UseApplicationDataPath);
if (File.Exists(oldPath))
File.Delete(oldPath);
2023-07-12 22:41:31 +00:00
}
catch
{
2024-07-28 00:12:07 +00:00
FileLogger.WriteLog($"Unable to remove old app setting data folder:", StatusMessageType.Error);
2023-07-12 22:41:31 +00:00
}
}
2024-07-28 00:12:07 +00:00
private static void CreateAppSettings(ApplicationSetting appSetting)
{
var dataFilePath = FileIo.GetUserDataFilePath(appSetting.GeneralSetting.UseApplicationDataPath);
if (string.IsNullOrEmpty(dataFilePath))
throw new Exception("Unable to get app setting data file path.");
if (!Directory.Exists(dataFilePath))
Directory.CreateDirectory(dataFilePath);
using var file = File.CreateText(Path.Combine(dataFilePath, APP_SETTING_DATA_FILENAME));
var serializer = new JsonSerializer();
serializer.Serialize(file, new AppSettingFile { ApplicationSetting = appSetting });
}
2023-07-12 22:41:31 +00:00
}
}