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.

66 lines
2.0 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. public override bool Initialize(PluginLoader.PluginMetadata meta, string[] parameters)
  9. { // parameters should be (name, fully qualified type)
  10. if (parameters.Length != 2)
  11. {
  12. InvalidMessage = "Incorrect number of parameters";
  13. return false;
  14. }
  15. RequireLoaded(meta);
  16. Type type;
  17. try
  18. {
  19. type = meta.Assembly.GetType(parameters[1]);
  20. }
  21. catch (ArgumentException)
  22. {
  23. InvalidMessage = $"Invalid type name {parameters[1]}";
  24. return false;
  25. }
  26. catch (Exception e) when (e is FileNotFoundException || e is FileLoadException || e is BadImageFormatException)
  27. {
  28. var filename = "";
  29. switch (e)
  30. {
  31. case FileNotFoundException fn:
  32. filename = fn.FileName;
  33. break;
  34. case FileLoadException fl:
  35. filename = fl.FileName;
  36. break;
  37. case BadImageFormatException bi:
  38. filename = bi.FileName;
  39. break;
  40. }
  41. InvalidMessage = $"Could not find {filename} while loading type";
  42. return false;
  43. }
  44. try
  45. {
  46. if (RegisterFeature(parameters[0], type)) return NewFeature = true;
  47. InvalidMessage = $"Feature with name {parameters[0]} already exists";
  48. return false;
  49. }
  50. catch (ArgumentException)
  51. {
  52. InvalidMessage = $"{type.FullName} not a subclass of {nameof(Feature)}";
  53. return false;
  54. }
  55. }
  56. }
  57. }