You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.3 KiB

  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.IO;
  5. namespace IPA.Loader.Features
  6. {
  7. internal class ConfigProviderFeature : Feature
  8. {
  9. private class DataModel
  10. {
  11. [JsonProperty("type", Required = Required.Always)]
  12. public string TypeName = "";
  13. }
  14. protected override bool Initialize(PluginMetadata meta, JObject featureData)
  15. {
  16. DataModel data;
  17. try
  18. {
  19. data = featureData.ToObject<DataModel>();
  20. }
  21. catch (Exception e)
  22. {
  23. InvalidMessage = $"Invalid data: {e}";
  24. return false;
  25. }
  26. Type getType;
  27. try
  28. {
  29. getType = meta.Assembly.GetType(data.TypeName);
  30. }
  31. catch (ArgumentException)
  32. {
  33. InvalidMessage = $"Invalid type name {data.TypeName}";
  34. return false;
  35. }
  36. catch (Exception e) when (e is FileNotFoundException || e is FileLoadException || e is BadImageFormatException)
  37. {
  38. string filename;
  39. switch (e)
  40. {
  41. case FileNotFoundException fn:
  42. filename = fn.FileName;
  43. goto hasFilename;
  44. case FileLoadException fl:
  45. filename = fl.FileName;
  46. goto hasFilename;
  47. case BadImageFormatException bi:
  48. filename = bi.FileName;
  49. hasFilename:
  50. InvalidMessage = $"Could not find {filename} while loading type";
  51. break;
  52. default:
  53. InvalidMessage = $"Error while loading type: {e}";
  54. break;
  55. }
  56. return false;
  57. }
  58. try
  59. {
  60. Config.Config.Register(getType);
  61. return true;
  62. }
  63. catch (Exception e)
  64. {
  65. InvalidMessage = $"Error while registering config provider: {e}";
  66. return false;
  67. }
  68. }
  69. }
  70. }