ProfilerConfiguration.cs 2.2 KB

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