SDKManager.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using PixiEditor.SDK.FileParsers;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Reflection;
  6. namespace PixiEditor.SDK
  7. {
  8. internal class SDKManager
  9. {
  10. public List<Extension> Extensions { get; } = new();
  11. public FileParserList Parsers { get; } = new();
  12. public void AddBaseExtension(Extension extension)
  13. {
  14. Extensions.Add(extension);
  15. }
  16. public ExtensionLoadingResult LoadExtensions(string extensionLocation)
  17. {
  18. List<ExtensionLoadingException> extensionExceptions = new();
  19. List<Assembly> loadedAssemblies = new();
  20. if (!Directory.Exists(extensionLocation))
  21. {
  22. Directory.CreateDirectory(extensionLocation);
  23. }
  24. foreach (string path in Directory.GetFiles(extensionLocation, "*.dll", SearchOption.AllDirectories))
  25. {
  26. try
  27. {
  28. loadedAssemblies.Add(Assembly.LoadFrom(path));
  29. }
  30. catch (Exception e)
  31. {
  32. extensionExceptions.Add(new ExtensionLoadingException(path, "Error while trying to load extension", e));
  33. }
  34. }
  35. foreach (Assembly assembly in loadedAssemblies)
  36. {
  37. try
  38. {
  39. Extension extension = LoadExtensionFromAssembly(assembly);
  40. extension.ExtensionPath = assembly.Location;
  41. Extensions.Add(extension);
  42. }
  43. catch (Exception e)
  44. {
  45. extensionExceptions.Add(new ExtensionLoadingException(assembly.Location, "Error while trying to initialize extension", e));
  46. }
  47. }
  48. return new ExtensionLoadingResult(extensionExceptions.ToArray());
  49. }
  50. public void SetupExtensions()
  51. {
  52. List<Extension> extensions = new List<Extension>(Extensions);
  53. foreach (Extension extension in extensions)
  54. {
  55. try
  56. {
  57. extension.Preferences = new Preferences(extension);
  58. ExtensionLoadingInformation information = new(extension);
  59. extension.Load(information);
  60. foreach (DocumentParserInfo fileParserInformation in information.DocumentParsers)
  61. {
  62. Parsers.AddDocumentParser(fileParserInformation);
  63. }
  64. foreach (ImageParserInfo fileParserInformation in information.ImageParsers)
  65. {
  66. Parsers.AddImageParser(fileParserInformation);
  67. }
  68. }
  69. catch
  70. {
  71. Extensions.Remove(extension);
  72. }
  73. }
  74. }
  75. private static Extension LoadExtensionFromAssembly(Assembly assembly)
  76. {
  77. PixiEditorExtensionAttribute attribute = assembly.GetCustomAttribute<PixiEditorExtensionAttribute>();
  78. ConstructorInfo info = attribute.ExtensionType.GetConstructor(Type.EmptyTypes);
  79. Extension extension = info.Invoke(null) as Extension;
  80. extension.ExtensionPath = assembly.Location;
  81. return extension;
  82. }
  83. }
  84. }