/src/Avalonia.DesktopRuntime/AppBuilder.cs

https://gitlab.com/kush/Avalonia · C# · 112 lines · 86 code · 12 blank · 14 comment · 16 complexity · 36ccc235d8425ebe646f78787f8ae373 MD5 · raw file

  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using Avalonia.Controls;
  6. using Avalonia.Platform;
  7. using Avalonia.Shared.PlatformSupport;
  8. namespace Avalonia
  9. {
  10. /// <summary>
  11. /// Initializes platform-specific services for an <see cref="Application"/>.
  12. /// </summary>
  13. public sealed class AppBuilder : AppBuilderBase<AppBuilder>
  14. {
  15. /// <summary>
  16. /// Initializes a new instance of the <see cref="AppBuilder"/> class.
  17. /// </summary>
  18. public AppBuilder()
  19. : base(new StandardRuntimePlatform(),
  20. builder => StandardRuntimePlatformServices.Register(builder.Instance?.GetType()?.Assembly))
  21. {
  22. }
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="AppBuilder"/> class.
  25. /// </summary>
  26. /// <param name="app">The <see cref="Application"/> instance.</param>
  27. public AppBuilder(Application app) : this()
  28. {
  29. Instance = app;
  30. }
  31. bool CheckEnvironment(Type checkerType)
  32. {
  33. if (checkerType == null)
  34. return true;
  35. try
  36. {
  37. return ((IModuleEnvironmentChecker) Activator.CreateInstance(checkerType)).IsCompatible;
  38. }
  39. catch
  40. {
  41. return false;
  42. }
  43. }
  44. /// <summary>
  45. /// Instructs the <see cref="AppBuilder"/> to use the best settings for the platform.
  46. /// </summary>
  47. /// <returns>An <see cref="AppBuilder"/> instance.</returns>
  48. public AppBuilder UseSubsystemsFromStartupDirectory()
  49. {
  50. var os = RuntimePlatform.GetRuntimeInfo().OperatingSystem;
  51. LoadAssembliesInDirectory();
  52. var windowingSubsystemAttribute = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
  53. from attribute in assembly.GetCustomAttributes<ExportWindowingSubsystemAttribute>()
  54. where attribute.RequiredOS == os && CheckEnvironment(attribute.EnvironmentChecker)
  55. orderby attribute.Priority ascending
  56. select attribute).FirstOrDefault();
  57. if (windowingSubsystemAttribute == null)
  58. {
  59. throw new InvalidOperationException("No windowing subsystem found. Are you missing assembly references?");
  60. }
  61. var renderingSubsystemAttribute = (from assembly in AppDomain.CurrentDomain.GetAssemblies()
  62. from attribute in assembly.GetCustomAttributes<ExportRenderingSubsystemAttribute>()
  63. where attribute.RequiredOS == os && CheckEnvironment(attribute.EnvironmentChecker)
  64. where attribute.RequiresWindowingSubsystem == null
  65. || attribute.RequiresWindowingSubsystem == windowingSubsystemAttribute.Name
  66. orderby attribute.Priority ascending
  67. select attribute).FirstOrDefault();
  68. if (renderingSubsystemAttribute == null)
  69. {
  70. throw new InvalidOperationException("No rendering subsystem found. Are you missing assembly references?");
  71. }
  72. UseWindowingSubsystem(() => windowingSubsystemAttribute.InitializationType
  73. .GetRuntimeMethod(windowingSubsystemAttribute.InitializationMethod, Type.EmptyTypes).Invoke(null, null),
  74. windowingSubsystemAttribute.Name);
  75. UseRenderingSubsystem(() => renderingSubsystemAttribute.InitializationType
  76. .GetRuntimeMethod(renderingSubsystemAttribute.InitializationMethod, Type.EmptyTypes).Invoke(null, null),
  77. renderingSubsystemAttribute.Name);
  78. return this;
  79. }
  80. private void LoadAssembliesInDirectory()
  81. {
  82. var location = Assembly.GetEntryAssembly().Location;
  83. if (string.IsNullOrWhiteSpace(location))
  84. return;
  85. var dir = new FileInfo(location).Directory;
  86. if (dir == null)
  87. return;
  88. foreach (var file in dir.EnumerateFiles("*.dll"))
  89. {
  90. try
  91. {
  92. Assembly.LoadFile(file.FullName);
  93. }
  94. catch (Exception)
  95. {
  96. }
  97. }
  98. }
  99. }
  100. }