Skip to content

Commit

Permalink
Fix metadata.json JsonParsingException crash (#1062)
Browse files Browse the repository at this point in the history
* Load defualt metadata if the metadata.json gets corrupt

* Write to disk immediately to decrease the chances of corruption
  • Loading branch information
Xpl0itR authored Apr 23, 2020
1 parent cdbb689 commit 72b560d
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions Ryujinx/Ui/ApplicationLibrary.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using JsonPrettyPrinterPlus;
using LibHac;
using LibHac.Common;
using LibHac.Fs;
Expand All @@ -17,7 +16,6 @@
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using Utf8Json;
Expand Down Expand Up @@ -511,7 +509,7 @@ internal static ApplicationMetadata LoadAndSaveMetaData(string titleId, Action<A
string metadataFolder = Path.Combine(_virtualFileSystem.GetBasePath(), "games", titleId, "gui");
string metadataFile = Path.Combine(metadataFolder, "metadata.json");

IJsonFormatterResolver resolver = CompositeResolver.Create(new[] { StandardResolver.AllowPrivateSnakeCase });
IJsonFormatterResolver resolver = CompositeResolver.Create(StandardResolver.AllowPrivateSnakeCase);

ApplicationMetadata appMetadata;

Expand All @@ -526,21 +524,39 @@ internal static ApplicationMetadata LoadAndSaveMetaData(string titleId, Action<A
LastPlayed = "Never"
};

byte[] data = JsonSerializer.Serialize(appMetadata, resolver);
File.WriteAllText(metadataFile, Encoding.UTF8.GetString(data, 0, data.Length).PrettyPrintJson());
using (FileStream stream = File.Create(metadataFile, 4096, FileOptions.WriteThrough))
{
JsonSerializer.Serialize(stream, appMetadata, resolver);
}
}

using (Stream stream = File.OpenRead(metadataFile))
{
appMetadata = JsonSerializer.Deserialize<ApplicationMetadata>(stream, resolver);
try
{
appMetadata = JsonSerializer.Deserialize<ApplicationMetadata>(stream, resolver);
}
catch (JsonParsingException)
{
Logger.PrintWarning(LogClass.Application, $"Failed to parse metadata json for {titleId}. Loading defaults.");

appMetadata = new ApplicationMetadata
{
Favorite = false,
TimePlayed = 0,
LastPlayed = "Never"
};
}
}

if (modifyFunction != null)
{
modifyFunction(appMetadata);

byte[] saveData = JsonSerializer.Serialize(appMetadata, resolver);
File.WriteAllText(metadataFile, Encoding.UTF8.GetString(saveData, 0, saveData.Length).PrettyPrintJson());
using (FileStream stream = File.Create(metadataFile, 4096, FileOptions.WriteThrough))
{
JsonSerializer.Serialize(stream, appMetadata, resolver);
}
}

return appMetadata;
Expand Down

0 comments on commit 72b560d

Please sign in to comment.