Esempio n. 1
0
        public void Initialize(SkyContext skyContext, ICollection <BaseRenderer> renderers)
        {
            mapContext = new MapContext(this, skyContext);

            Projection            = new ArcProjection(mapContext);
            Projection.IsInverted = settings.Get("IsInverted");
            Projection.IsMirrored = settings.Get("IsMirrored");

            this.renderers.AddRange(renderers);
            this.renderers.ForEach(r => r.Initialize());

            Schema = settings.Get <ColorSchema>("Schema");

            // get saved rendering orders
            RenderingOrder renderingOrder = settings.Get <RenderingOrder>("RenderingOrder");

            // sort renderers according saved orders
            this.renderers.Sort(renderingOrder.Select(r => r.RendererTypeName));

            // build rendering order based on existing renderers
            renderingOrder = new RenderingOrder(this.renderers.Select(r => new RenderingOrderItem(r)));

            // save actual rendering order
            settings.Set("RenderingOrder", renderingOrder);

            settings.SettingValueChanged += (name, value) =>
            {
                // redraw if rendering order changed
                if (name == "RenderingOrder")
                {
                    this.renderers.Sort(settings.Get <RenderingOrder>("RenderingOrder").Select(r => r.RendererTypeName));
                    Invalidate();
                }

                if (name == "Schema")
                {
                    Schema = settings.Get <ColorSchema>("Schema");
                    Invalidate();
                }

                if (name == "IsMirrored")
                {
                    Projection.IsMirrored = settings.Get("IsMirrored");
                    Invalidate();
                }

                if (name == "IsInverted")
                {
                    Projection.IsInverted = settings.Get("IsInverted");
                    Invalidate();
                }
            };
        }
Esempio n. 2
0
        private void ConfigureContainer(IProgress <string> progress)
        {
            kernel.Bind <ICommandLineArgs>().ToConstant(commandLineArgs).InSingletonScope();
            kernel.Bind <ISettings, Settings>().To <Settings>().InSingletonScope();
            kernel.Bind <ISky, Sky>().To <Sky>().InSingletonScope();
            kernel.Bind <ISkyMap, SkyMap>().To <SkyMap>().InSingletonScope();
            kernel.Bind <IGeoLocationsManager, GeoLocationsManager>().To <GeoLocationsManager>().InSingletonScope();
            kernel.Bind <IMainWindow, MainVM>().To <MainVM>().InSingletonScope();
            kernel.Bind <UIElementsIntegration>().ToSelf().InSingletonScope();
            UIElementsIntegration uiIntegration = kernel.Get <UIElementsIntegration>();

            ICollection <AbstractPlugin> plugins = new List <AbstractPlugin>();

            string homeFolder = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            IEnumerable <string> pluginPaths = Directory.EnumerateFiles(homeFolder, "Astrarium.Plugins.*.dll", SearchOption.AllDirectories);

            progress.Report("Loading plugins");

            foreach (string path in pluginPaths)
            {
                try
                {
                    var plugin = Assembly.UnsafeLoadFrom(path);

                    // get singletons defined in plugin
                    var singletons = plugin.GetExportedTypes().Where(t => t.IsDefined(typeof(SingletonAttribute), false)).ToArray();
                    foreach (var singletonImpl in singletons)
                    {
                        var singletonAttr = singletonImpl.GetCustomAttribute <SingletonAttribute>();
                        if (singletonAttr.InterfaceType != null)
                        {
                            if (!singletonAttr.InterfaceType.IsAssignableFrom(singletonImpl))
                            {
                                throw new Exception($"Interface type {singletonAttr.InterfaceType} is not assignable from {singletonImpl}");
                            }
                            kernel.Bind(singletonAttr.InterfaceType).To(singletonImpl).InSingletonScope();
                        }
                        else
                        {
                            kernel.Bind(singletonImpl).ToSelf().InSingletonScope();
                        }
                    }

                    Log.Info($"Loaded plugin {plugin.FullName}");
                }
                catch (Exception ex)
                {
                    Log.Error($"Unable to load plugin assembly with path {path}. {ex})");
                }
            }

            // collect all plugins implementations
            Type[] pluginTypes = AppDomain.CurrentDomain.GetAssemblies()
                                 .SelectMany(a => a.GetTypes())
                                 .Where(t => typeof(AbstractPlugin).IsAssignableFrom(t) && !t.IsAbstract)
                                 .ToArray();

            // collect all calculators types
            Type[] calcTypes = pluginTypes
                               .SelectMany(p => GetCalculators(p))
                               .ToArray();

            foreach (Type calcType in calcTypes)
            {
                var types = new[] { calcType }.Concat(calcType.GetInterfaces()).ToArray();
                kernel.Bind(types).To(calcType).InSingletonScope();
            }

            // collect all renderers types
            Type[] rendererTypes = pluginTypes
                                   .SelectMany(p => GetRenderers(p))
                                   .ToArray();

            foreach (Type rendererType in rendererTypes)
            {
                var types = new[] { rendererType }.Concat(rendererType.GetInterfaces()).ToArray();
                kernel.Bind(types).To(rendererType).InSingletonScope();
            }

            // collect all event provider implementations
            Type[] eventProviderTypes = pluginTypes
                                        .SelectMany(p => GetAstroEventProviders(p))
                                        .ToArray();

            foreach (Type eventProviderType in eventProviderTypes)
            {
                var types = new[] { eventProviderType }.Concat(eventProviderType.GetInterfaces()).ToArray();
                kernel.Bind(types).To(eventProviderType).InSingletonScope();
            }

            foreach (Type pluginType in pluginTypes)
            {
                progress.Report($"Creating plugin {pluginType}");

                try
                {
                    // plugin is a singleton
                    kernel.Bind(pluginType).ToSelf().InSingletonScope();
                    var plugin = kernel.Get(pluginType) as AbstractPlugin;

                    // add settings definitions
                    uiIntegration.SettingDefinitions.AddRange(plugin.SettingDefinitions);

                    // add settings sections
                    uiIntegration.SettingSections.AddRange(plugin.SettingSections);

                    // add configured toolbar buttons
                    uiIntegration.ToolbarButtons.AddRange(plugin.ToolbarItems);

                    // add menu items
                    uiIntegration.MenuItems.AddRange(plugin.MenuItems);

                    plugins.Add(plugin);
                }
                catch (Exception ex)
                {
                    Log.Error($"Unable to create plugin of type {pluginType.Name}: {ex}");
                    kernel.Unbind(pluginType);
                }
            }

            progress.Report($"Creating renderers");

            var renderers             = rendererTypes.Select(r => kernel.Get(r)).Cast <BaseRenderer>().ToArray();
            var defaultRenderingOrder = new RenderingOrder(renderers.OrderBy(r => r.Order).Select(r => new RenderingOrderItem(r)));

            uiIntegration.SettingDefinitions.Add(new SettingDefinition("RenderingOrder", defaultRenderingOrder, false));

            var settings = kernel.Get <ISettings>();

            settings.Define(uiIntegration.SettingDefinitions);
            settings.Save("Defaults");

            progress.Report($"Loading settings");

            settings.Load();

            SetLanguage(settings.Get <string>("Language"));
            SetColorSchema(settings.Get <ColorSchema>("Schema"), settings.Get("AppTheme", "DeepBlue"));

            SkyContext context = new SkyContext(
                new Date(DateTime.Now).ToJulianEphemerisDay(),
                new CrdsGeographical(settings.Get <CrdsGeographical>("ObserverLocation")));

            progress.Report($"Creating calculators");

            var calculators = calcTypes
                              .Select(c => kernel.Get(c))
                              .Cast <BaseCalc>()
                              .ToArray();

            progress.Report($"Creating event providers");

            var eventProviders = eventProviderTypes
                                 .Select(c => kernel.Get(c))
                                 .Cast <BaseAstroEventsProvider>()
                                 .ToArray();

            progress.Report($"Initializing sky");
            kernel.Get <Sky>().Initialize(context, calculators, eventProviders);

            progress.Report($"Initializing sky map");
            kernel.Get <SkyMap>().Initialize(context, renderers);

            Log.Debug("Application container has been configured.");

            progress.Report($"Initializing shell");

            settings.SettingValueChanged += (settingName, value) =>
            {
                if (settingName == "Schema" || settingName == "AppTheme")
                {
                    SetColorSchema(settings.Get("Schema", ColorSchema.Night), settings.Get("AppTheme", "DeepBlue"));
                }
                else if (settingName == "Language")
                {
                    SetLanguage((string)value);
                }
            };

            foreach (var plugin in plugins)
            {
                plugin.Initialize();
            }
        }