ProfilerConfiguration.cs 2.3 KB

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