using System; using System.Linq; using FlaxEngine; using Debug = System.Diagnostics.Debug; #if FLAX_EDITOR using FlaxEditor; #endif namespace Game { /// /// Wrapper for GamePlugin with support of initializing plugins in correct order. /// The initialization is deferred to later time after the last dependant plugin is initialized. /// public abstract class GGamePlugin : GamePlugin { public virtual Type[] PluginDependencies { get => Array.Empty(); } public bool Initialized { get; private set; } public bool Deinitialized { get; private set; } public override void Initialize() { //FlaxEngine.Debug.Log($"Plugin {this.GetType()}::Initialize"); base.Initialize(); if (!Initialized) { // Check if dependencies have been initialized, defer to later if not bool dependenciesInitialized = true; var plugins = PluginManager.GamePlugins.OfType().Where(x => x.GetType() != this.GetType()); foreach (var pluginType in PluginDependencies) { //FlaxEngine.Debug.Log($"Plugintype {pluginType}"); var plugin = plugins.FirstOrDefault(x => x.GetType() == pluginType); if (plugin != null) { //FlaxEngine.Debug.Log($"Plugin {plugin.GetType()} is Initialized?"); if (!plugin.Initialized) { dependenciesInitialized = false; break; } } else { dependenciesInitialized = false; break; } } if (dependenciesInitialized) { Init(); Initialized = true; // Initialize any previously deferred plugins in case their dependencies were initialized foreach (var p in plugins) { //FlaxEngine.Debug.Log($"try init Plugin {p.GetType()}"); if (!p.Initialized /* && p.PluginDependencies.Contains(this.GetType())*/) { //FlaxEngine.Debug.Log($"Plugin run init {p.GetType()}"); p.Initialize(); } } } } } public override void Deinitialize() { base.Deinitialize(); if (!Deinitialized) { Deinit(); Deinitialized = true; } } public virtual void Init() { } public virtual void Deinit() { } } #if FLAX_EDITOR /// /// Wrapper for EditorPlugin with support of initializing plugins in correct order. /// The initialization is deferred to later time after the last dependant plugin is initialized. /// public abstract class GEditorPlugin : EditorPlugin { public virtual Type[] PluginDependencies { get => Array.Empty(); } public bool Initialized { get; private set; } public bool Deinitialized { get; private set; } public override void Initialize() { //FlaxEngine.Debug.Log($"Plugin {this.GetType()}::Initialize"); base.Initialize(); if (!Initialized) { // Check if dependencies have been initialized, defer to later if not bool dependenciesInitialized = true; var plugins = PluginManager.EditorPlugins.OfType().Where(x => x.GetType() != this.GetType()); foreach (var pluginType in PluginDependencies) { //FlaxEngine.Debug.Log($"Plugintype {pluginType}"); var plugin = plugins.FirstOrDefault(x => x.GetType() == pluginType); if (plugin != null) { //FlaxEngine.Debug.Log($"Plugin {plugin.GetType()} is Initialized?"); if (!plugin.Initialized) { dependenciesInitialized = false; break; } } else { dependenciesInitialized = false; break; } } if (dependenciesInitialized) { Init(); Initialized = true; // Initialize any previously deferred plugins in case their dependencies were initialized foreach (var p in plugins) { //FlaxEngine.Debug.Log($"try init Plugin {p.GetType()}"); if (!p.Initialized /* && p.PluginDependencies.Contains(this.GetType())*/) { //FlaxEngine.Debug.Log($"Plugin run init {p.GetType()}"); p.Initialize(); } } } } } public override void Deinitialize() { base.Deinitialize(); if (!Deinitialized) { Deinit(); Deinitialized = true; } } public virtual void Init() { } public virtual void Deinit() { } } #endif }