mirror of
https://github.com/Ryujinx/Ryujinx.git
synced 2025-06-28 13:40:47 -07:00
Source generated json serializers (#4582)
* Use source generated json serializers in order to improve code trimming * Use strongly typed github releases model to fetch updates instead of raw Newtonsoft.Json parsing * Use separate model for LogEventArgs serialization * Make dynamic object formatter static. Fix string builder pooling. * Do not inherit json version of LogEventArgs from EventArgs * Fix extra space in object formatting * Write log json directly to stream instead of using buffer writer * Rebase fixes * Rebase fixes * Rebase fixes * Enforce block-scoped namespaces in the solution. Convert style for existing code * Apply suggestions from code review Co-authored-by: TSRBerry <20988865+TSRBerry@users.noreply.github.com> * Rebase indent fix * Fix indent * Delete unnecessary json properties * Rebase fix * Remove overridden json property names as they are handled in the options * Apply suggestions from code review Co-authored-by: TSRBerry <20988865+TSRBerry@users.noreply.github.com> * Use default json options in github api calls * Indentation and spacing fixes * Fix json serialization * Fix missing JsonConverter for config enums * Add double \n\n after the whole string, not inside join --------- Co-authored-by: TSRBerry <20988865+TSRBerry@users.noreply.github.com>
This commit is contained in:
10
Ryujinx.Ui.Common/App/ApplicationJsonSerializerContext.cs
Normal file
10
Ryujinx.Ui.Common/App/ApplicationJsonSerializerContext.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Ryujinx.Ui.App.Common
|
||||
{
|
||||
[JsonSourceGenerationOptions(WriteIndented = true)]
|
||||
[JsonSerializable(typeof(ApplicationMetadata))]
|
||||
internal partial class ApplicationJsonSerializerContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
}
|
@ -10,6 +10,7 @@ using LibHac.Tools.FsSystem;
|
||||
using LibHac.Tools.FsSystem.NcaUtils;
|
||||
using Ryujinx.Common.Configuration;
|
||||
using Ryujinx.Common.Logging;
|
||||
using Ryujinx.Common.Utilities;
|
||||
using Ryujinx.HLE.FileSystem;
|
||||
using Ryujinx.HLE.HOS.SystemState;
|
||||
using Ryujinx.HLE.Loaders.Npdm;
|
||||
@ -22,7 +23,6 @@ using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using System.Threading;
|
||||
using JsonHelper = Ryujinx.Common.Utilities.JsonHelper;
|
||||
using Path = System.IO.Path;
|
||||
|
||||
namespace Ryujinx.Ui.App.Common
|
||||
@ -42,6 +42,9 @@ namespace Ryujinx.Ui.App.Common
|
||||
private Language _desiredTitleLanguage;
|
||||
private CancellationTokenSource _cancellationToken;
|
||||
|
||||
private static readonly ApplicationJsonSerializerContext SerializerContext = new(JsonHelper.GetDefaultSerializerOptions());
|
||||
private static readonly TitleUpdateMetadataJsonSerializerContext TitleSerializerContext = new(JsonHelper.GetDefaultSerializerOptions());
|
||||
|
||||
public ApplicationLibrary(VirtualFileSystem virtualFileSystem)
|
||||
{
|
||||
_virtualFileSystem = virtualFileSystem;
|
||||
@ -489,14 +492,12 @@ namespace Ryujinx.Ui.App.Common
|
||||
|
||||
appMetadata = new ApplicationMetadata();
|
||||
|
||||
using FileStream stream = File.Create(metadataFile, 4096, FileOptions.WriteThrough);
|
||||
|
||||
JsonHelper.Serialize(stream, appMetadata, true);
|
||||
JsonHelper.SerializeToFile(metadataFile, appMetadata, SerializerContext.ApplicationMetadata);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
appMetadata = JsonHelper.DeserializeFromFile<ApplicationMetadata>(metadataFile);
|
||||
appMetadata = JsonHelper.DeserializeFromFile(metadataFile, SerializerContext.ApplicationMetadata);
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
@ -509,9 +510,7 @@ namespace Ryujinx.Ui.App.Common
|
||||
{
|
||||
modifyFunction(appMetadata);
|
||||
|
||||
using FileStream stream = File.Create(metadataFile, 4096, FileOptions.WriteThrough);
|
||||
|
||||
JsonHelper.Serialize(stream, appMetadata, true);
|
||||
JsonHelper.SerializeToFile(metadataFile, appMetadata, SerializerContext.ApplicationMetadata);
|
||||
}
|
||||
|
||||
return appMetadata;
|
||||
@ -890,7 +889,7 @@ namespace Ryujinx.Ui.App.Common
|
||||
|
||||
if (File.Exists(titleUpdateMetadataPath))
|
||||
{
|
||||
updatePath = JsonHelper.DeserializeFromFile<TitleUpdateMetadata>(titleUpdateMetadataPath).Selected;
|
||||
updatePath = JsonHelper.DeserializeFromFile(titleUpdateMetadataPath, TitleSerializerContext.TitleUpdateMetadata).Selected;
|
||||
|
||||
if (File.Exists(updatePath))
|
||||
{
|
||||
|
@ -1,5 +1,9 @@
|
||||
namespace Ryujinx.Ui.Common.Configuration
|
||||
using Ryujinx.Common.Utilities;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Ryujinx.Ui.Common.Configuration
|
||||
{
|
||||
[JsonConverter(typeof(TypedStringEnumConverter<AudioBackend>))]
|
||||
public enum AudioBackend
|
||||
{
|
||||
Dummy,
|
||||
|
@ -5,7 +5,7 @@ using Ryujinx.Common.Utilities;
|
||||
using Ryujinx.Ui.Common.Configuration.System;
|
||||
using Ryujinx.Ui.Common.Configuration.Ui;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace Ryujinx.Ui.Common.Configuration
|
||||
{
|
||||
@ -321,14 +321,14 @@ namespace Ryujinx.Ui.Common.Configuration
|
||||
/// </summary>
|
||||
/// <remarks>Kept for file format compatibility (to avoid possible failure when parsing configuration on old versions)</remarks>
|
||||
/// TODO: Remove this when those older versions aren't in use anymore.
|
||||
public List<object> KeyboardConfig { get; set; }
|
||||
public List<JsonObject> KeyboardConfig { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Legacy controller control bindings
|
||||
/// </summary>
|
||||
/// <remarks>Kept for file format compatibility (to avoid possible failure when parsing configuration on old versions)</remarks>
|
||||
/// TODO: Remove this when those older versions aren't in use anymore.
|
||||
public List<object> ControllerConfig { get; set; }
|
||||
public List<JsonObject> ControllerConfig { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Input configurations
|
||||
@ -354,11 +354,12 @@ namespace Ryujinx.Ui.Common.Configuration
|
||||
/// Loads a configuration file from disk
|
||||
/// </summary>
|
||||
/// <param name="path">The path to the JSON configuration file</param>
|
||||
/// <param name="configurationFileFormat">Parsed configuration file</param>
|
||||
public static bool TryLoad(string path, out ConfigurationFileFormat configurationFileFormat)
|
||||
{
|
||||
try
|
||||
{
|
||||
configurationFileFormat = JsonHelper.DeserializeFromFile<ConfigurationFileFormat>(path);
|
||||
configurationFileFormat = JsonHelper.DeserializeFromFile(path, ConfigurationFileFormatSettings.SerializerContext.ConfigurationFileFormat);
|
||||
|
||||
return configurationFileFormat.Version != 0;
|
||||
}
|
||||
@ -376,8 +377,7 @@ namespace Ryujinx.Ui.Common.Configuration
|
||||
/// <param name="path">The path to the JSON configuration file</param>
|
||||
public void SaveConfig(string path)
|
||||
{
|
||||
using FileStream fileStream = File.Create(path, 4096, FileOptions.WriteThrough);
|
||||
JsonHelper.Serialize(fileStream, this, true);
|
||||
JsonHelper.SerializeToFile(path, this, ConfigurationFileFormatSettings.SerializerContext.ConfigurationFileFormat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,9 @@
|
||||
using Ryujinx.Common.Utilities;
|
||||
|
||||
namespace Ryujinx.Ui.Common.Configuration
|
||||
{
|
||||
internal static class ConfigurationFileFormatSettings
|
||||
{
|
||||
public static readonly ConfigurationJsonSerializerContext SerializerContext = new(JsonHelper.GetDefaultSerializerOptions());
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Ryujinx.Ui.Common.Configuration
|
||||
{
|
||||
[JsonSourceGenerationOptions(WriteIndented = true)]
|
||||
[JsonSerializable(typeof(ConfigurationFileFormat))]
|
||||
internal partial class ConfigurationJsonSerializerContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
}
|
@ -9,6 +9,7 @@ using Ryujinx.Ui.Common.Configuration.Ui;
|
||||
using Ryujinx.Ui.Common.Helper;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json.Nodes;
|
||||
|
||||
namespace Ryujinx.Ui.Common.Configuration
|
||||
{
|
||||
@ -631,8 +632,8 @@ namespace Ryujinx.Ui.Common.Configuration
|
||||
EnableKeyboard = Hid.EnableKeyboard,
|
||||
EnableMouse = Hid.EnableMouse,
|
||||
Hotkeys = Hid.Hotkeys,
|
||||
KeyboardConfig = new List<object>(),
|
||||
ControllerConfig = new List<object>(),
|
||||
KeyboardConfig = new List<JsonObject>(),
|
||||
ControllerConfig = new List<JsonObject>(),
|
||||
InputConfig = Hid.InputConfig,
|
||||
GraphicsBackend = Graphics.GraphicsBackend,
|
||||
PreferredGpu = Graphics.PreferredGpu
|
||||
|
@ -1,5 +1,9 @@
|
||||
namespace Ryujinx.Ui.Common.Configuration.System
|
||||
using Ryujinx.Common.Utilities;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Ryujinx.Ui.Common.Configuration.System
|
||||
{
|
||||
[JsonConverter(typeof(TypedStringEnumConverter<Language>))]
|
||||
public enum Language
|
||||
{
|
||||
Japanese,
|
||||
|
@ -1,5 +1,9 @@
|
||||
namespace Ryujinx.Ui.Common.Configuration.System
|
||||
using Ryujinx.Common.Utilities;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Ryujinx.Ui.Common.Configuration.System
|
||||
{
|
||||
[JsonConverter(typeof(TypedStringEnumConverter<Region>))]
|
||||
public enum Region
|
||||
{
|
||||
Japan,
|
||||
|
57
Ryujinx.Ui.Common/Models/Amiibo/AmiiboApi.cs
Normal file
57
Ryujinx.Ui.Common/Models/Amiibo/AmiiboApi.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Ryujinx.Ui.Common.Models.Amiibo
|
||||
{
|
||||
public struct AmiiboApi : IEquatable<AmiiboApi>
|
||||
{
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; }
|
||||
[JsonPropertyName("head")]
|
||||
public string Head { get; set; }
|
||||
[JsonPropertyName("tail")]
|
||||
public string Tail { get; set; }
|
||||
[JsonPropertyName("image")]
|
||||
public string Image { get; set; }
|
||||
[JsonPropertyName("amiiboSeries")]
|
||||
public string AmiiboSeries { get; set; }
|
||||
[JsonPropertyName("character")]
|
||||
public string Character { get; set; }
|
||||
[JsonPropertyName("gameSeries")]
|
||||
public string GameSeries { get; set; }
|
||||
[JsonPropertyName("type")]
|
||||
public string Type { get; set; }
|
||||
|
||||
[JsonPropertyName("release")]
|
||||
public Dictionary<string, string> Release { get; set; }
|
||||
|
||||
[JsonPropertyName("gamesSwitch")]
|
||||
public List<AmiiboApiGamesSwitch> GamesSwitch { get; set; }
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Name;
|
||||
}
|
||||
|
||||
public string GetId()
|
||||
{
|
||||
return Head + Tail;
|
||||
}
|
||||
|
||||
public bool Equals(AmiiboApi other)
|
||||
{
|
||||
return Head + Tail == other.Head + other.Tail;
|
||||
}
|
||||
|
||||
public override bool Equals(object obj)
|
||||
{
|
||||
return obj is AmiiboApi other && Equals(other);
|
||||
}
|
||||
|
||||
public override int GetHashCode()
|
||||
{
|
||||
return HashCode.Combine(Head, Tail);
|
||||
}
|
||||
}
|
||||
}
|
15
Ryujinx.Ui.Common/Models/Amiibo/AmiiboApiGamesSwitch.cs
Normal file
15
Ryujinx.Ui.Common/Models/Amiibo/AmiiboApiGamesSwitch.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Ryujinx.Ui.Common.Models.Amiibo
|
||||
{
|
||||
public class AmiiboApiGamesSwitch
|
||||
{
|
||||
[JsonPropertyName("amiiboUsage")]
|
||||
public List<AmiiboApiUsage> AmiiboUsage { get; set; }
|
||||
[JsonPropertyName("gameID")]
|
||||
public List<string> GameId { get; set; }
|
||||
[JsonPropertyName("gameName")]
|
||||
public string GameName { get; set; }
|
||||
}
|
||||
}
|
12
Ryujinx.Ui.Common/Models/Amiibo/AmiiboApiUsage.cs
Normal file
12
Ryujinx.Ui.Common/Models/Amiibo/AmiiboApiUsage.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Ryujinx.Ui.Common.Models.Amiibo
|
||||
{
|
||||
public class AmiiboApiUsage
|
||||
{
|
||||
[JsonPropertyName("Usage")]
|
||||
public string Usage { get; set; }
|
||||
[JsonPropertyName("write")]
|
||||
public bool Write { get; set; }
|
||||
}
|
||||
}
|
14
Ryujinx.Ui.Common/Models/Amiibo/AmiiboJson.cs
Normal file
14
Ryujinx.Ui.Common/Models/Amiibo/AmiiboJson.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Ryujinx.Ui.Common.Models.Amiibo
|
||||
{
|
||||
public struct AmiiboJson
|
||||
{
|
||||
[JsonPropertyName("amiibo")]
|
||||
public List<AmiiboApi> Amiibo { get; set; }
|
||||
[JsonPropertyName("lastUpdated")]
|
||||
public DateTime LastUpdated { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Ryujinx.Ui.Common.Models.Amiibo
|
||||
{
|
||||
[JsonSerializable(typeof(AmiiboJson))]
|
||||
public partial class AmiiboJsonSerializerContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
namespace Ryujinx.Ui.Common.Models.Github
|
||||
{
|
||||
public class GithubReleaseAssetJsonResponse
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public string State { get; set; }
|
||||
public string BrowserDownloadUrl { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Ryujinx.Ui.Common.Models.Github
|
||||
{
|
||||
public class GithubReleasesJsonResponse
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public List<GithubReleaseAssetJsonResponse> Assets { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Ryujinx.Ui.Common.Models.Github
|
||||
{
|
||||
[JsonSerializable(typeof(GithubReleasesJsonResponse), GenerationMode = JsonSourceGenerationMode.Metadata)]
|
||||
public partial class GithubReleasesJsonSerializerContext : JsonSerializerContext
|
||||
{
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user