JsonMotionConfigControllerConverter.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Ryujinx.Common.Utilities;
  2. using System;
  3. using System.Text.Json;
  4. using System.Text.Json.Serialization;
  5. namespace Ryujinx.Common.Configuration.Hid.Controller.Motion
  6. {
  7. class JsonMotionConfigControllerConverter : JsonConverter<MotionConfigController>
  8. {
  9. private static readonly MotionConfigJsonSerializerContext SerializerContext = new(JsonHelper.GetDefaultSerializerOptions());
  10. private static MotionInputBackendType GetMotionInputBackendType(ref Utf8JsonReader reader)
  11. {
  12. // Temporary reader to get the backend type
  13. Utf8JsonReader tempReader = reader;
  14. MotionInputBackendType result = MotionInputBackendType.Invalid;
  15. while (tempReader.Read())
  16. {
  17. // NOTE: We scan all properties ignoring the depth entirely on purpose.
  18. // The reason behind this is that we cannot track in a reliable way the depth of the object because Utf8JsonReader never emit the first TokenType == StartObject if the json start with an object.
  19. // As such, this code will try to parse very field named "motion_backend" to the correct enum.
  20. if (tempReader.TokenType == JsonTokenType.PropertyName)
  21. {
  22. string propertyName = tempReader.GetString();
  23. if (propertyName.Equals("motion_backend"))
  24. {
  25. tempReader.Read();
  26. if (tempReader.TokenType == JsonTokenType.String)
  27. {
  28. string backendTypeRaw = tempReader.GetString();
  29. if (!Enum.TryParse(backendTypeRaw, out result))
  30. {
  31. result = MotionInputBackendType.Invalid;
  32. }
  33. else
  34. {
  35. break;
  36. }
  37. }
  38. }
  39. }
  40. }
  41. return result;
  42. }
  43. public override MotionConfigController Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
  44. {
  45. MotionInputBackendType motionBackendType = GetMotionInputBackendType(ref reader);
  46. return motionBackendType switch
  47. {
  48. MotionInputBackendType.GamepadDriver => JsonSerializer.Deserialize(ref reader, SerializerContext.StandardMotionConfigController),
  49. MotionInputBackendType.CemuHook => JsonSerializer.Deserialize(ref reader, SerializerContext.CemuHookMotionConfigController),
  50. _ => throw new InvalidOperationException($"Unknown backend type {motionBackendType}"),
  51. };
  52. }
  53. public override void Write(Utf8JsonWriter writer, MotionConfigController value, JsonSerializerOptions options)
  54. {
  55. switch (value.MotionBackend)
  56. {
  57. case MotionInputBackendType.GamepadDriver:
  58. JsonSerializer.Serialize(writer, value as StandardMotionConfigController, SerializerContext.StandardMotionConfigController);
  59. break;
  60. case MotionInputBackendType.CemuHook:
  61. JsonSerializer.Serialize(writer, value as CemuHookMotionConfigController, SerializerContext.CemuHookMotionConfigController);
  62. break;
  63. default:
  64. throw new ArgumentException($"Unknown motion backend type {value.MotionBackend}");
  65. }
  66. }
  67. }
  68. }