ProfilerConfiguration.cs 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using OpenTK.Input;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Utf8Json;
  8. using Utf8Json.Resolvers;
  9. namespace Ryujinx.Profiler
  10. {
  11. public class ProfilerConfiguration
  12. {
  13. public bool Enabled { get; private set; }
  14. public string DumpPath { get; private set; }
  15. public float UpdateRate { get; private set; }
  16. public int MaxLevel { get; private set; }
  17. public int MaxFlags { get; private set; }
  18. public float History { get; private set; }
  19. public ProfilerKeyboardHandler Controls { get; private set; }
  20. /// <summary>
  21. /// Loads a configuration file from disk
  22. /// </summary>
  23. /// <param name="path">The path to the JSON configuration file</param>
  24. public static ProfilerConfiguration Load(string path)
  25. {
  26. var resolver = CompositeResolver.Create(
  27. new[] { new ConfigurationEnumFormatter<Key>() },
  28. new[] { StandardResolver.AllowPrivateSnakeCase }
  29. );
  30. if (!File.Exists(path))
  31. {
  32. throw new FileNotFoundException($"Profiler configuration file {path} not found");
  33. }
  34. using (Stream stream = File.OpenRead(path))
  35. {
  36. return JsonSerializer.Deserialize<ProfilerConfiguration>(stream, resolver);
  37. }
  38. }
  39. private class ConfigurationEnumFormatter<T> : IJsonFormatter<T>
  40. where T : struct
  41. {
  42. public void Serialize(ref JsonWriter writer, T value, IJsonFormatterResolver formatterResolver)
  43. {
  44. formatterResolver.GetFormatterWithVerify<string>()
  45. .Serialize(ref writer, value.ToString(), formatterResolver);
  46. }
  47. public T Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver)
  48. {
  49. if (reader.ReadIsNull())
  50. {
  51. return default(T);
  52. }
  53. var enumName = formatterResolver.GetFormatterWithVerify<string>()
  54. .Deserialize(ref reader, formatterResolver);
  55. if (Enum.TryParse<T>(enumName, out T result))
  56. {
  57. return result;
  58. }
  59. return default(T);
  60. }
  61. }
  62. }
  63. }