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.

65 lines
2.1 KiB

  1. using System;
  2. using System.IO;
  3. namespace IPA.Loader.Features
  4. {
  5. internal class ConfigProviderFeature : Feature
  6. {
  7. public override bool Initialize(PluginMetadata meta, string[] parameters)
  8. {// parameters should be (fully qualified name of provider type)
  9. if (parameters.Length != 1)
  10. {
  11. InvalidMessage = "Incorrect number of parameters";
  12. return false;
  13. }
  14. RequireLoaded(meta);
  15. Type getType;
  16. try
  17. {
  18. getType = meta.Assembly.GetType(parameters[0]);
  19. }
  20. catch (ArgumentException)
  21. {
  22. InvalidMessage = $"Invalid type name {parameters[0]}";
  23. return false;
  24. }
  25. catch (Exception e) when (e is FileNotFoundException || e is FileLoadException || e is BadImageFormatException)
  26. {
  27. string filename;
  28. switch (e)
  29. {
  30. case FileNotFoundException fn:
  31. filename = fn.FileName;
  32. goto hasFilename;
  33. case FileLoadException fl:
  34. filename = fl.FileName;
  35. goto hasFilename;
  36. case BadImageFormatException bi:
  37. filename = bi.FileName;
  38. hasFilename:
  39. InvalidMessage = $"Could not find {filename} while loading type";
  40. break;
  41. default:
  42. InvalidMessage = $"Error while loading type: {e}";
  43. break;
  44. }
  45. return false;
  46. }
  47. try
  48. {
  49. Config.Config.Register(getType);
  50. return true;
  51. }
  52. catch (Exception e)
  53. {
  54. InvalidMessage = $"Error while registering config provider: {e}";
  55. return false;
  56. }
  57. }
  58. }
  59. }