1
0
Fork 0
mirror of https://github.com/hawkeye-stan/msfs-popout-panel-manager.git synced 2024-11-25 07:10:10 +00:00
msfs-popout-panel-manager/Orchestration/ProfileDataManager.cs

84 lines
2.8 KiB
C#
Raw Normal View History

2023-07-12 22:41:31 +00:00
using MSFSPopoutPanelManager.DomainModel.DataFile;
using MSFSPopoutPanelManager.DomainModel.Profile;
using MSFSPopoutPanelManager.Shared;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace MSFSPopoutPanelManager.Orchestration
{
public class ProfileDataManager
{
private const string USER_PROFILE_DATA_FILENAME = "userprofiledata.json";
2024-07-28 00:12:07 +00:00
public static IList<UserProfile> ReadProfiles(bool isRoamingPath)
2023-07-12 22:41:31 +00:00
{
try
{
Debug.WriteLine("Reading user profile data file...");
2024-07-28 00:12:07 +00:00
using var reader = new StreamReader(Path.Combine(FileIo.GetUserDataFilePath(isRoamingPath), USER_PROFILE_DATA_FILENAME));
2024-02-28 02:44:21 +00:00
var fileContent = reader.ReadToEnd();
2023-07-12 22:41:31 +00:00
return JsonConvert.DeserializeObject<UserProfileFile>(fileContent).Profiles;
}
catch
{
return new List<UserProfile>();
}
}
2024-07-28 00:12:07 +00:00
public static void WriteProfiles(IList<UserProfile> profiles, bool isRoamingPath)
2023-07-12 22:41:31 +00:00
{
if (profiles == null)
{
2024-02-28 02:44:21 +00:00
FileLogger.WriteLog("User Profiles is null.", StatusMessageType.Error);
2023-07-12 22:41:31 +00:00
throw new Exception("User Profiles is null.");
}
try
{
2024-07-28 00:12:07 +00:00
CreateProfiles(profiles, isRoamingPath);
}
catch
{
FileLogger.WriteLog($"Unable to write user data file: {USER_PROFILE_DATA_FILENAME}", StatusMessageType.Error);
}
}
2023-07-12 22:41:31 +00:00
2024-07-28 00:12:07 +00:00
public static void MoveProfiles(IList<UserProfile> profiles, bool isRoamingPath)
{
try
{
CreateProfiles(profiles, isRoamingPath);
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(!isRoamingPath);
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 move user data file: {USER_PROFILE_DATA_FILENAME}", StatusMessageType.Error);
2023-07-12 22:41:31 +00:00
}
}
2024-07-28 00:12:07 +00:00
private static void CreateProfiles(IList<UserProfile> profiles, bool isRoamingPath)
{
var dataFilePath = FileIo.GetUserDataFilePath(isRoamingPath);
if (string.IsNullOrEmpty(dataFilePath))
throw new Exception("Unable to get user profile data file path.");
if (!Directory.Exists(dataFilePath))
Directory.CreateDirectory(dataFilePath);
using var file = File.CreateText(Path.Combine(dataFilePath, USER_PROFILE_DATA_FILENAME));
var serializer = new JsonSerializer();
serializer.Serialize(file, new UserProfileFile { Profiles = profiles });
}
2023-07-12 22:41:31 +00:00
}
}