0
0
Fork 0

Create method to LoadAndSave ApplicationMetaData and get rid of code duplication (#872)

This commit is contained in:
Jonathan Goyvaerts 2020-01-12 04:01:04 +01:00 committed by Ac_K
parent 29e8576b0d
commit 78f6b1d396
3 changed files with 39 additions and 101 deletions

View file

@ -34,9 +34,8 @@ namespace Ryujinx.Ui
private static readonly byte[] _nroIcon = GetResourceBytes("Ryujinx.Ui.assets.NROIcon.png"); private static readonly byte[] _nroIcon = GetResourceBytes("Ryujinx.Ui.assets.NROIcon.png");
private static readonly byte[] _nsoIcon = GetResourceBytes("Ryujinx.Ui.assets.NSOIcon.png"); private static readonly byte[] _nsoIcon = GetResourceBytes("Ryujinx.Ui.assets.NSOIcon.png");
private static Keyset _keySet; private static Keyset _keySet;
private static TitleLanguage _desiredTitleLanguage; private static TitleLanguage _desiredTitleLanguage;
private static ApplicationMetadata _appMetadata;
public static void LoadApplications(List<string> appDirs, Keyset keySet, TitleLanguage desiredTitleLanguage, FileSystemClient fsClient = null, VirtualFileSystem vfs = null) public static void LoadApplications(List<string> appDirs, Keyset keySet, TitleLanguage desiredTitleLanguage, FileSystemClient fsClient = null, VirtualFileSystem vfs = null)
{ {
@ -339,7 +338,7 @@ namespace Ryujinx.Ui
} }
} }
(bool favorite, string timePlayed, string lastPlayed) = GetMetadata(titleId); ApplicationMetadata appMetadata = LoadAndSaveMetaData(titleId);
if (ulong.TryParse(titleId, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out ulong titleIdNum)) if (ulong.TryParse(titleId, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out ulong titleIdNum))
{ {
@ -357,14 +356,14 @@ namespace Ryujinx.Ui
ApplicationData data = new ApplicationData() ApplicationData data = new ApplicationData()
{ {
Favorite = favorite, Favorite = appMetadata.Favorite,
Icon = applicationIcon, Icon = applicationIcon,
TitleName = titleName, TitleName = titleName,
TitleId = titleId, TitleId = titleId,
Developer = developer, Developer = developer,
Version = version, Version = version,
TimePlayed = timePlayed, TimePlayed = ConvertSecondsToReadableString(appMetadata.TimePlayed),
LastPlayed = lastPlayed, LastPlayed = appMetadata.LastPlayed,
FileExtension = Path.GetExtension(applicationPath).ToUpper().Remove(0 ,1), FileExtension = Path.GetExtension(applicationPath).ToUpper().Remove(0 ,1),
FileSize = (fileSize < 1) ? (fileSize * 1024).ToString("0.##") + "MB" : fileSize.ToString("0.##") + "GB", FileSize = (fileSize < 1) ? (fileSize * 1024).ToString("0.##") + "MB" : fileSize.ToString("0.##") + "GB",
Path = applicationPath, Path = applicationPath,
@ -431,34 +430,44 @@ namespace Ryujinx.Ui
return controlNca?.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.None); return controlNca?.OpenFileSystem(NcaSectionType.Data, IntegrityCheckLevel.None);
} }
private static (bool favorite, string timePlayed, string lastPlayed) GetMetadata(string titleId) internal static ApplicationMetadata LoadAndSaveMetaData(string titleId, Action<ApplicationMetadata> modifyFunction = null)
{ {
string metadataFolder = Path.Combine(new VirtualFileSystem().GetBasePath(), "games", titleId, "gui"); string metadataFolder = Path.Combine(new VirtualFileSystem().GetBasePath(), "games", titleId, "gui");
string metadataFile = Path.Combine(metadataFolder, "metadata.json"); string metadataFile = Path.Combine(metadataFolder, "metadata.json");
IJsonFormatterResolver resolver = CompositeResolver.Create(StandardResolver.AllowPrivateSnakeCase); IJsonFormatterResolver resolver = CompositeResolver.Create(new[] { StandardResolver.AllowPrivateSnakeCase });
ApplicationMetadata appMetadata;
if (!File.Exists(metadataFile)) if (!File.Exists(metadataFile))
{ {
Directory.CreateDirectory(metadataFolder); Directory.CreateDirectory(metadataFolder);
_appMetadata = new ApplicationMetadata appMetadata = new ApplicationMetadata
{ {
Favorite = false, Favorite = false,
TimePlayed = 0, TimePlayed = 0,
LastPlayed = "Never" LastPlayed = "Never"
}; };
byte[] saveData = JsonSerializer.Serialize(_appMetadata, resolver); byte[] data = JsonSerializer.Serialize(appMetadata, resolver);
File.WriteAllText(metadataFile, Encoding.UTF8.GetString(saveData, 0, saveData.Length).PrettyPrintJson()); File.WriteAllText(metadataFile, Encoding.UTF8.GetString(data, 0, data.Length).PrettyPrintJson());
} }
using (Stream stream = File.OpenRead(metadataFile)) using (Stream stream = File.OpenRead(metadataFile))
{ {
_appMetadata = JsonSerializer.Deserialize<ApplicationMetadata>(stream, resolver); appMetadata = JsonSerializer.Deserialize<ApplicationMetadata>(stream, resolver);
} }
return (_appMetadata.Favorite, ConvertSecondsToReadableString(_appMetadata.TimePlayed), _appMetadata.LastPlayed); if (modifyFunction != null)
{
modifyFunction(appMetadata);
byte[] saveData = JsonSerializer.Serialize(appMetadata, resolver);
File.WriteAllText(metadataFile, Encoding.UTF8.GetString(saveData, 0, saveData.Length).PrettyPrintJson());
}
return appMetadata;
} }
private static string ConvertSecondsToReadableString(double seconds) private static string ConvertSecondsToReadableString(double seconds)

View file

@ -1,6 +1,6 @@
namespace Ryujinx.Ui namespace Ryujinx.Ui
{ {
internal struct ApplicationMetadata internal class ApplicationMetadata
{ {
public bool Favorite { get; set; } public bool Favorite { get; set; }
public double TimePlayed { get; set; } public double TimePlayed { get; set; }

View file

@ -307,37 +307,10 @@ namespace Ryujinx.Ui
DiscordIntegrationModule.SwitchToPlayingState(_device.System.TitleId, _device.System.TitleName); DiscordIntegrationModule.SwitchToPlayingState(_device.System.TitleId, _device.System.TitleName);
string metadataFolder = System.IO.Path.Combine(new VirtualFileSystem().GetBasePath(), "games", _device.System.TitleId, "gui"); ApplicationLibrary.LoadAndSaveMetaData(_device.System.TitleId, appMetadata =>
string metadataFile = System.IO.Path.Combine(metadataFolder, "metadata.json");
IJsonFormatterResolver resolver = CompositeResolver.Create(new[] { StandardResolver.AllowPrivateSnakeCase });
ApplicationMetadata appMetadata;
if (!File.Exists(metadataFile))
{ {
Directory.CreateDirectory(metadataFolder); appMetadata.LastPlayed = DateTime.UtcNow.ToString();
});
appMetadata = new ApplicationMetadata
{
Favorite = false,
TimePlayed = 0,
LastPlayed = "Never"
};
byte[] data = JsonSerializer.Serialize(appMetadata, resolver);
File.WriteAllText(metadataFile, Encoding.UTF8.GetString(data, 0, data.Length).PrettyPrintJson());
}
using (Stream stream = File.OpenRead(metadataFile))
{
appMetadata = JsonSerializer.Deserialize<ApplicationMetadata>(stream, resolver);
}
appMetadata.LastPlayed = DateTime.UtcNow.ToString();
byte[] saveData = JsonSerializer.Serialize(appMetadata, resolver);
File.WriteAllText(metadataFile, Encoding.UTF8.GetString(saveData, 0, saveData.Length).PrettyPrintJson());
} }
} }
@ -364,40 +337,13 @@ namespace Ryujinx.Ui
if (_gameLoaded) if (_gameLoaded)
{ {
string metadataFolder = System.IO.Path.Combine(new VirtualFileSystem().GetBasePath(), "games", _device.System.TitleId, "gui"); ApplicationLibrary.LoadAndSaveMetaData(_device.System.TitleId, appMetadata =>
string metadataFile = System.IO.Path.Combine(metadataFolder, "metadata.json");
IJsonFormatterResolver resolver = CompositeResolver.Create(new[] { StandardResolver.AllowPrivateSnakeCase });
ApplicationMetadata appMetadata;
if (!File.Exists(metadataFile))
{ {
Directory.CreateDirectory(metadataFolder); DateTime lastPlayedDateTime = DateTime.Parse(appMetadata.LastPlayed);
double sessionTimePlayed = DateTime.UtcNow.Subtract(lastPlayedDateTime).TotalSeconds;
appMetadata = new ApplicationMetadata appMetadata.TimePlayed += Math.Round(sessionTimePlayed, MidpointRounding.AwayFromZero);
{ });
Favorite = false,
TimePlayed = 0,
LastPlayed = "Never"
};
byte[] data = JsonSerializer.Serialize(appMetadata, resolver);
File.WriteAllText(metadataFile, Encoding.UTF8.GetString(data, 0, data.Length).PrettyPrintJson());
}
using (Stream stream = File.OpenRead(metadataFile))
{
appMetadata = JsonSerializer.Deserialize<ApplicationMetadata>(stream, resolver);
}
DateTime lastPlayedDateTime = DateTime.Parse(appMetadata.LastPlayed);
double sessionTimePlayed = DateTime.UtcNow.Subtract(lastPlayedDateTime).TotalSeconds;
appMetadata.TimePlayed += Math.Round(sessionTimePlayed, MidpointRounding.AwayFromZero);
byte[] saveData = JsonSerializer.Serialize(appMetadata, resolver);
File.WriteAllText(metadataFile, Encoding.UTF8.GetString(saveData, 0, saveData.Length).PrettyPrintJson());
} }
Profile.FinishProfiling(); Profile.FinishProfiling();
@ -453,33 +399,16 @@ namespace Ryujinx.Ui
{ {
_tableStore.GetIter(out TreeIter treeIter, new TreePath(args.Path)); _tableStore.GetIter(out TreeIter treeIter, new TreePath(args.Path));
string titleId = _tableStore.GetValue(treeIter, 2).ToString().Split("\n")[1].ToLower(); string titleId = _tableStore.GetValue(treeIter, 2).ToString().Split("\n")[1].ToLower();
string metadataPath = System.IO.Path.Combine(new VirtualFileSystem().GetBasePath(), "games", titleId, "gui", "metadata.json");
IJsonFormatterResolver resolver = CompositeResolver.Create(new[] { StandardResolver.AllowPrivateSnakeCase }); bool newToggleValue = !(bool)_tableStore.GetValue(treeIter, 0);
ApplicationMetadata appMetadata; _tableStore.SetValue(treeIter, 0, newToggleValue);
using (Stream stream = File.OpenRead(metadataPath)) ApplicationLibrary.LoadAndSaveMetaData(titleId, appMetadata =>
{ {
appMetadata = JsonSerializer.Deserialize<ApplicationMetadata>(stream, resolver); appMetadata.Favorite = newToggleValue;
} });
if ((bool)_tableStore.GetValue(treeIter, 0))
{
_tableStore.SetValue(treeIter, 0, false);
appMetadata.Favorite = false;
}
else
{
_tableStore.SetValue(treeIter, 0, true);
appMetadata.Favorite = true;
}
byte[] saveData = JsonSerializer.Serialize(appMetadata, resolver);
File.WriteAllText(metadataPath, Encoding.UTF8.GetString(saveData, 0, saveData.Length).PrettyPrintJson());
} }
private void Row_Activated(object sender, RowActivatedArgs args) private void Row_Activated(object sender, RowActivatedArgs args)