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/MigrateData.cs

200 lines
9.1 KiB
C#
Raw Normal View History

2023-07-12 22:41:31 +00:00
using MSFSPopoutPanelManager.DomainModel.Legacy;
using MSFSPopoutPanelManager.DomainModel.Profile;
using MSFSPopoutPanelManager.DomainModel.Setting;
using MSFSPopoutPanelManager.Shared;
using Newtonsoft.Json;
2022-07-23 19:23:32 +00:00
using System;
2023-07-12 22:41:31 +00:00
using System.Collections.Generic;
2022-07-23 19:23:32 +00:00
using System.IO;
2023-07-12 22:41:31 +00:00
using System.Linq;
2022-07-23 19:23:32 +00:00
namespace MSFSPopoutPanelManager.Orchestration
{
internal class MigrateData
{
2023-07-12 22:41:31 +00:00
private const string USER_PROFILE_DATA_FILENAME = "userprofiledata.json";
private const string APP_SETTING_DATA_FILENAME = "appsettingdata.json";
private const string ERROR_LOG_FILENAME = "error.log";
private const string INFO_LOG_FILENAME = "info.log";
private const string DEBUG_LOG_FILENAME = "debug.log";
public static ApplicationSetting MigrateAppSettingFile(string content)
2022-07-23 19:23:32 +00:00
{
try
{
2023-07-12 22:41:31 +00:00
BackupAppSettingFile();
2022-07-23 20:04:06 +00:00
2023-07-12 22:41:31 +00:00
var appSetting = new ApplicationSetting();
var legacyAppSetting = JsonConvert.DeserializeObject<LegacyAppSetting>(content);
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
// General settings
appSetting.GeneralSetting.AlwaysOnTop = legacyAppSetting.AlwaysOnTop;
appSetting.GeneralSetting.AutoClose = legacyAppSetting.AutoClose;
appSetting.GeneralSetting.MinimizeToTray = legacyAppSetting.MinimizeToTray;
appSetting.GeneralSetting.StartMinimized = legacyAppSetting.StartMinimized;
2022-07-23 20:04:06 +00:00
2023-07-12 22:41:31 +00:00
// Auto pop out setting
appSetting.AutoPopOutSetting.IsEnabled = legacyAppSetting.AutoPopOutPanels;
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
// Pop out setting
appSetting.PopOutSetting.UseLeftRightControlToPopOut = legacyAppSetting.UseLeftRightControlToPopOut;
appSetting.PopOutSetting.MinimizeAfterPopOut = legacyAppSetting.MinimizeAfterPopOut;
appSetting.PopOutSetting.AutoPanning.IsEnabled = legacyAppSetting.UseAutoPanning;
appSetting.PopOutSetting.AutoPanning.KeyBinding = legacyAppSetting.AutoPanningKeyBinding;
appSetting.PopOutSetting.AfterPopOutCameraView.IsEnabled = legacyAppSetting.AfterPopOutCameraView.EnableReturnToCameraView;
appSetting.PopOutSetting.AfterPopOutCameraView.CameraView = legacyAppSetting.AfterPopOutCameraView.CameraView;
appSetting.PopOutSetting.AfterPopOutCameraView.KeyBinding = legacyAppSetting.AfterPopOutCameraView.CustomCameraKeyBinding;
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
// Refocus setting
appSetting.RefocusSetting.RefocusGameWindow.IsEnabled = legacyAppSetting.TouchScreenSettings.RefocusGameWindow;
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
var delay = Math.Round(legacyAppSetting.TouchScreenSettings.RefocusGameWindowDelay / 1000.0, 1);
appSetting.RefocusSetting.RefocusGameWindow.Delay = delay < 1.0 ? 1.0 : delay;
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
// Touch setting
appSetting.TouchSetting.TouchDownUpDelay = 0;
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
// Track IR setting
appSetting.TrackIRSetting.AutoDisableTrackIR = legacyAppSetting.AutoDisableTrackIR;
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
// Windowed mode setting
appSetting.WindowedModeSetting.AutoResizeMsfsGameWindow = legacyAppSetting.AutoResizeMsfsGameWindow;
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
return appSetting;
}
catch (Exception ex)
{
var msg = "An unknown application setting data migration error has occured. Application will close";
FileLogger.WriteException(msg, ex);
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
Environment.Exit(0);
}
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
return null;
}
public static IList<UserProfile> MigrateUserProfileFile(string content)
{
try
{
BackupUserProfileFile();
var profiles = new List<UserProfile>();
var legacyProfiles = JsonConvert.DeserializeObject<List<LegacyProfile>>(content);
legacyProfiles = legacyProfiles.OrderBy(p => p.ProfileName.ToLower()).ToList();
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
foreach (var legacyProfile in legacyProfiles)
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
var profile = new UserProfile();
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
profile.Name = legacyProfile.ProfileName;
profile.IsLocked = legacyProfile.IsLocked;
profile.AircraftBindings = legacyProfile.BindingAircrafts;
profile.ProfileSetting.PowerOnRequiredForColdStart = legacyProfile.PowerOnRequiredForColdStart;
profile.ProfileSetting.IncludeInGamePanels = legacyProfile.IncludeInGamePanels;
if (legacyProfile.MsfsGameWindowConfig != null)
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
profile.MsfsGameWindowConfig.Top = legacyProfile.MsfsGameWindowConfig.Top;
profile.MsfsGameWindowConfig.Left = legacyProfile.MsfsGameWindowConfig.Left;
profile.MsfsGameWindowConfig.Width = legacyProfile.MsfsGameWindowConfig.Width;
profile.MsfsGameWindowConfig.Height = legacyProfile.MsfsGameWindowConfig.Height;
2022-07-23 19:23:32 +00:00
}
2023-07-12 22:41:31 +00:00
foreach (var legacyPanelConfig in legacyProfile.PanelConfigs)
{
var panelConfig = new PanelConfig();
panelConfig.PanelName = legacyPanelConfig.PanelName;
panelConfig.PanelType = legacyPanelConfig.PanelType;
panelConfig.Top = legacyPanelConfig.Top;
panelConfig.Left = legacyPanelConfig.Left + 9;
panelConfig.Width = legacyPanelConfig.Width - 18;
panelConfig.Height = legacyPanelConfig.Height - 9;
if (panelConfig.PanelType == PanelType.CustomPopout)
{
var legacyPanelSource = legacyProfile.PanelSourceCoordinates.FirstOrDefault(x => x.PanelIndex == legacyPanelConfig.PanelIndex);
if (legacyPanelSource != null)
{
panelConfig.PanelSource.X = legacyPanelSource.X;
panelConfig.PanelSource.Y = legacyPanelSource.Y;
panelConfig.PanelSource.Color = PanelConfigColors.GetNextAvailableColor(profile.PanelConfigs.ToList());
}
}
panelConfig.AlwaysOnTop = legacyPanelConfig.AlwaysOnTop;
panelConfig.HideTitlebar = legacyPanelConfig.HideTitlebar;
panelConfig.FullScreen = legacyPanelConfig.FullScreen;
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
if (legacyProfile.RealSimGearGTN750Gen1Override)
panelConfig.TouchEnabled = false;
else
panelConfig.TouchEnabled = legacyPanelConfig.TouchEnabled;
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
if (legacyPanelConfig.DisableGameRefocus || panelConfig.PanelType == PanelType.BuiltInPopout)
panelConfig.AutoGameRefocus = false;
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
profile.PanelConfigs.Add(panelConfig);
}
profiles.Add(profile);
}
return profiles;
2022-07-23 19:23:32 +00:00
}
catch (Exception ex)
{
var msg = "An unknown user data migration error has occured. Application will close";
FileLogger.WriteException(msg, ex);
Environment.Exit(0);
}
2023-07-12 22:41:31 +00:00
return null;
2022-07-23 19:23:32 +00:00
}
2023-07-12 22:41:31 +00:00
private static void BackupAppSettingFile()
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
var srcPath = Path.Combine(FileIo.GetUserDataFilePath(), APP_SETTING_DATA_FILENAME);
var backupPath = Path.Combine(FileIo.GetUserDataFilePath(), "Backup-previous-version", APP_SETTING_DATA_FILENAME);
if (File.Exists(srcPath))
2022-07-23 19:23:32 +00:00
{
2023-07-12 22:41:31 +00:00
Directory.CreateDirectory(Path.Combine(FileIo.GetUserDataFilePath(), "Backup-previous-version"));
File.Copy(srcPath, backupPath, true);
2022-07-23 19:23:32 +00:00
}
2023-07-12 22:41:31 +00:00
// Delete existing error log
var logFilePath = Path.Combine(FileIo.GetUserDataFilePath(), "LogFiles", ERROR_LOG_FILENAME);
if (File.Exists(logFilePath))
File.Delete(logFilePath);
2022-07-23 19:23:32 +00:00
2023-07-12 22:41:31 +00:00
logFilePath = Path.Combine(FileIo.GetUserDataFilePath(), "LogFiles", INFO_LOG_FILENAME);
if (File.Exists(logFilePath))
File.Delete(logFilePath);
logFilePath = Path.Combine(FileIo.GetUserDataFilePath(), "LogFiles", DEBUG_LOG_FILENAME);
if (File.Exists(logFilePath))
File.Delete(logFilePath);
FileLogger.WriteLog("File initialized...", StatusMessageType.Error);
2022-07-23 19:23:32 +00:00
}
2022-07-23 20:04:06 +00:00
2023-07-12 22:41:31 +00:00
private static void BackupUserProfileFile()
2022-07-23 20:04:06 +00:00
{
2023-07-12 22:41:31 +00:00
var srcPath = Path.Combine(FileIo.GetUserDataFilePath(), USER_PROFILE_DATA_FILENAME);
var backupPath = Path.Combine(FileIo.GetUserDataFilePath(), "Backup-previous-version", USER_PROFILE_DATA_FILENAME);
2022-07-23 20:04:06 +00:00
2023-07-12 22:41:31 +00:00
if (File.Exists(srcPath))
2022-07-23 20:04:06 +00:00
{
2023-07-12 22:41:31 +00:00
Directory.CreateDirectory(Path.Combine(FileIo.GetUserDataFilePath(), "Backup-previous-version"));
File.Copy(srcPath, backupPath, true);
2022-07-23 20:04:06 +00:00
}
}
2022-07-23 19:23:32 +00:00
}
}