Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkeye116477 committed Feb 27, 2024
1 parent b9f3d1a commit e3ca7f1
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 39 deletions.
3 changes: 2 additions & 1 deletion src/LegendaryDownloadManager.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ public DownloadManagerData.Rootobject LoadSavedData()
bool correctJson = false;
if (File.Exists(dataFile))
{
if (Serialization.TryFromJson(FileSystem.ReadFileAsStringSafe(dataFile), out downloadManagerData))
var content = FileSystem.ReadFileAsStringSafe(dataFile);
if (!content.IsNullOrWhiteSpace() && Serialization.TryFromJson(content, out downloadManagerData))
{
if (downloadManagerData != null && downloadManagerData.downloads != null)
{
Expand Down
44 changes: 26 additions & 18 deletions src/LegendaryGameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,27 +200,35 @@ public void OnGameClosed(double sessionLength)
httpClient.DefaultRequestHeaders.Clear();
if (File.Exists(LegendaryLauncher.TokensPath))
{
var userData = Serialization.FromJson<OauthResponse>(FileSystem.ReadFileAsStringSafe(LegendaryLauncher.TokensPath));
httpClient.DefaultRequestHeaders.Add("Authorization", userData.token_type + " " + userData.access_token);
var uri = $"https://library-service.live.use1a.on.epicgames.com/library/api/public/playtime/account/{userData.account_id}";
PlaytimePayload playtimePayload = new PlaytimePayload
var tokensContent = FileSystem.ReadFileAsStringSafe(LegendaryLauncher.TokensPath);
if (!tokensContent.IsNullOrWhiteSpace() && Serialization.TryFromJson(tokensContent, out OauthResponse userData))
{
artifactId = Game.GameId,
machineId = LegendaryLibrary.GetSettings().SyncPlaytimeMachineId
};
DateTime now = DateTime.UtcNow;
playtimePayload.endTime = now;
var totalSeconds = sessionLength;
var startTime = now.AddSeconds(-(double)totalSeconds);
playtimePayload.startTime = now.AddSeconds(-(double)totalSeconds);
var playtimeJson = Serialization.ToJson(playtimePayload);
var content = new StringContent(playtimeJson, Encoding.UTF8, "application/json");
a.CurrentProgressValue = 1;
var result = await httpClient.PutAsync(uri, content);
if (!result.IsSuccessStatusCode)
httpClient.DefaultRequestHeaders.Add("Authorization", userData.token_type + " " + userData.access_token);
var uri = $"https://library-service.live.use1a.on.epicgames.com/library/api/public/playtime/account/{userData.account_id}";
PlaytimePayload playtimePayload = new PlaytimePayload
{
artifactId = Game.GameId,
machineId = LegendaryLibrary.GetSettings().SyncPlaytimeMachineId
};
DateTime now = DateTime.UtcNow;
playtimePayload.endTime = now;
var totalSeconds = sessionLength;
var startTime = now.AddSeconds(-(double)totalSeconds);
playtimePayload.startTime = now.AddSeconds(-(double)totalSeconds);
var playtimeJson = Serialization.ToJson(playtimePayload);
var content = new StringContent(playtimeJson, Encoding.UTF8, "application/json");
a.CurrentProgressValue = 1;
var result = await httpClient.PutAsync(uri, content);
if (!result.IsSuccessStatusCode)
{
playniteAPI.Dialogs.ShowErrorMessage(playniteAPI.Resources.GetString(LOC.LegendarySyncError).Format(Game.Name));
logger.Error($"An error occured during uploading playtime to the cloud. Status code: {result.StatusCode}.");
}
}
else
{
logger.Error("An error occured during reading tokens file.");
playniteAPI.Dialogs.ShowErrorMessage(playniteAPI.Resources.GetString(LOC.LegendarySyncError).Format(Game.Name));
logger.Error($"An error occured during uploading playtime to the cloud. Status code: {result.StatusCode}.");
}
}
else
Expand Down
16 changes: 9 additions & 7 deletions src/LegendaryGameInstaller.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ private void InstallBtn_Click(object sender, RoutedEventArgs e)
if (File.Exists(cacheDlcInfoFile))
{
LegendaryGameInfo.Rootobject dlcManifest = new LegendaryGameInfo.Rootobject();
if (Serialization.TryFromJson(FileSystem.ReadFileAsStringSafe(cacheDlcInfoFile), out dlcManifest))
var cacheDlcContent = FileSystem.ReadFileAsStringSafe(cacheDlcInfoFile);
if (!cacheDlcContent.IsNullOrWhiteSpace() && Serialization.TryFromJson(cacheDlcContent, out dlcManifest))
{
if (dlcManifest != null && dlcManifest.Manifest != null)
{
Expand Down Expand Up @@ -309,12 +310,8 @@ private async void LegendaryGameInstallerUC_Loaded(object sender, RoutedEventArg
{
content = FileSystem.ReadFileAsStringSafe(cacheSDLFile);
}
if (content.IsNullOrEmpty())
{
logger.Error("An error occurred while downloading SDL data.");
}
bool correctSdlJson = false;
if (Serialization.TryFromJson(content, out extraContentInfo))
if (!content.IsNullOrWhiteSpace() && Serialization.TryFromJson(content, out extraContentInfo))
{
correctSdlJson = true;
if (extraContentInfo.ContainsKey("__required"))
Expand Down Expand Up @@ -360,6 +357,10 @@ private async void LegendaryGameInstallerUC_Loaded(object sender, RoutedEventArg
InstallData.downloadSize = Helpers.FormatSize(downloadSizeNumber);
InstallData.installSize = Helpers.FormatSize(installSizeNumber);
}
else
{
logger.Error("An error occurred while reading SDL data.");
}
if (!correctSdlJson)
{
extraContentInfo = new Dictionary<string, LegendarySDLInfo>();
Expand Down Expand Up @@ -505,7 +506,8 @@ private void ExtraContentLB_SelectionChanged(object sender, SelectionChangedEven
var cacheDlcInfoFile = Path.Combine(cacheInfoPath, selectedOption.Key + ".json");
if (File.Exists(cacheDlcInfoFile))
{
if (Serialization.TryFromJson(FileSystem.ReadFileAsStringSafe(cacheDlcInfoFile), out dlcManifest))
var cacheDlcContent = FileSystem.ReadFileAsStringSafe(cacheDlcInfoFile);
if (!cacheDlcContent.IsNullOrWhiteSpace() && Serialization.TryFromJson(cacheDlcContent, out dlcManifest))
{
if (dlcManifest != null && dlcManifest.Manifest != null)
{
Expand Down
24 changes: 12 additions & 12 deletions src/LegendaryLauncher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,12 +193,15 @@ internal static string GetExecutablePath(string rootPath)
public static Dictionary<string, Installed> GetInstalledAppList()
{
var installListPath = Path.Combine(ConfigPath, "installed.json");
if (!File.Exists(installListPath))
var list = new Dictionary<string, Installed>();
if (File.Exists(installListPath))
{
return new Dictionary<string, Installed>();
var content = FileSystem.ReadFileAsStringSafe(installListPath);
if (!content.IsNullOrWhiteSpace() && Serialization.TryFromJson(content, out Dictionary<string, Installed> nonEmptyList))
{
list = nonEmptyList;
}
}

var list = Serialization.FromJson<Dictionary<string, Installed>>(FileSystem.ReadFileAsStringSafe(installListPath));
return list;
}

Expand Down Expand Up @@ -253,14 +256,11 @@ public static Dictionary<string, string> DefaultEnvironmentVariables
if (File.Exists(cacheInfoFile))
{
var content = FileSystem.ReadFileAsStringSafe(cacheInfoFile);
if(!content.IsNullOrEmpty())
if (!content.IsNullOrWhiteSpace() && Serialization.TryFromJson(content, out manifest))
{
if (Serialization.TryFromJson(content, out manifest))
if (manifest != null && manifest.Manifest != null && manifest.Game != null)
{
if (manifest != null && manifest.Manifest != null && manifest.Game != null)
{
correctJson = true;
}
correctJson = true;
}
}
}
Expand Down Expand Up @@ -393,11 +393,11 @@ public static async Task<string> GetLauncherVersion()
{
content = FileSystem.ReadFileAsStringSafe(cacheVersionFile);
}
if (content.IsNullOrEmpty())
if (content.IsNullOrWhiteSpace())
{
logger.Error("An error occurred while downloading Legendary's version info.");
}
if (Serialization.TryFromJson(content, out LauncherVersion.Rootobject versionInfoContent))
else if (Serialization.TryFromJson(content, out LauncherVersion.Rootobject versionInfoContent))
{
newVersionInfoContent = versionInfoContent;
}
Expand Down
4 changes: 3 additions & 1 deletion src/LegendaryMessagesSettings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Playnite.Common;
using Playnite.SDK.Data;
using System;
using System.IO;

namespace LegendaryLibraryNS
Expand All @@ -19,7 +20,8 @@ public static LegendaryMessagesSettingsModel LoadSettings()
bool correctJson = false;
if (File.Exists(dataFile))
{
if (Serialization.TryFromJson(FileSystem.ReadFileAsStringSafe(dataFile), out messagesSettings))
var content = FileSystem.ReadFileAsStringSafe(dataFile);
if (!content.IsNullOrWhiteSpace() && Serialization.TryFromJson(content, out messagesSettings))
{
correctJson = true;
}
Expand Down

0 comments on commit e3ca7f1

Please sign in to comment.