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.

74 lines
2.3 KiB

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