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

62 lines
2 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";
public static IList<UserProfile> ReadProfiles()
{
try
{
Debug.WriteLine("Reading user profile data file...");
2024-02-28 02:44:21 +00:00
using var reader = new StreamReader(Path.Combine(FileIo.GetUserDataFilePath(), USER_PROFILE_DATA_FILENAME));
var fileContent = reader.ReadToEnd();
2023-07-12 22:41:31 +00:00
return JsonConvert.DeserializeObject<UserProfileFile>(fileContent).Profiles;
}
catch
{
return new List<UserProfile>();
}
}
public static void WriteProfiles(IList<UserProfile> profiles)
{
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-02-28 02:44:21 +00:00
var dataFilePath = FileIo.GetUserDataFilePath();
2023-07-12 22:41:31 +00:00
2024-02-28 02:44:21 +00:00
if (string.IsNullOrEmpty(dataFilePath))
throw new Exception("Unable to get user profile dat file path.");
2023-07-12 22:41:31 +00:00
2024-02-28 02:44:21 +00:00
if (!Directory.Exists(dataFilePath))
Directory.CreateDirectory(dataFilePath);
2023-07-12 22:41:31 +00:00
2024-02-28 02:44:21 +00:00
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
}
catch
{
FileLogger.WriteLog($"Unable to write user data file: {USER_PROFILE_DATA_FILENAME}", StatusMessageType.Error);
}
}
}
}