wrapper of plugins to handle dependencies
This commit is contained in:
@@ -7,7 +7,7 @@ using FlaxEditor;
|
||||
|
||||
namespace Game
|
||||
{
|
||||
public class ConsolePlugin : GamePlugin
|
||||
public class ConsolePlugin : GGamePlugin
|
||||
{
|
||||
public static PluginDescription DescriptionInternal = new PluginDescription
|
||||
{
|
||||
@@ -19,34 +19,31 @@ namespace Game
|
||||
Category = "Game"
|
||||
};
|
||||
|
||||
public override void Initialize()
|
||||
public override void Init()
|
||||
{
|
||||
base.Initialize();
|
||||
FlaxEngine.Debug.Log("ConsolePlugin initialized");
|
||||
Console.Init();
|
||||
}
|
||||
|
||||
public override void Deinitialize()
|
||||
public override void Deinit()
|
||||
{
|
||||
base.Deinitialize();
|
||||
}
|
||||
}
|
||||
|
||||
#if FLAX_EDITOR
|
||||
public class ConsoleEditorPlugin : EditorPlugin
|
||||
public class ConsoleEditorPlugin : GEditorPlugin
|
||||
{
|
||||
public override PluginDescription Description => ConsolePlugin.DescriptionInternal;
|
||||
|
||||
public override Type GamePluginType => typeof(ConsolePlugin);
|
||||
|
||||
public override void Initialize()
|
||||
public override void Init()
|
||||
{
|
||||
base.Initialize();
|
||||
FlaxEngine.Debug.Log("ConsolePlugin initialized");
|
||||
Console.Init();
|
||||
}
|
||||
|
||||
public override void Deinitialize()
|
||||
public override void Deinit()
|
||||
{
|
||||
base.Deinitialize();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -8,8 +8,10 @@ using FlaxEditor;
|
||||
|
||||
namespace Game
|
||||
{
|
||||
public class NetworkManagerPlugin : GamePlugin
|
||||
public class NetworkManagerPlugin : GGamePlugin
|
||||
{
|
||||
public override Type[] PluginDependencies { get => new Type[] { typeof(ConsolePlugin) }; }
|
||||
|
||||
public static PluginDescription DescriptionInternal = new PluginDescription
|
||||
{
|
||||
Author = "Ari Vuollet",
|
||||
@@ -20,37 +22,27 @@ namespace Game
|
||||
Category = "Game"
|
||||
};
|
||||
|
||||
public override void Initialize()
|
||||
public override void Init()
|
||||
{
|
||||
base.Initialize();
|
||||
Console.Init();
|
||||
FlaxEngine.Debug.Log("NetworkManagerPlugin initialized");
|
||||
NetworkManager.Init();
|
||||
}
|
||||
|
||||
public override void Deinitialize()
|
||||
{
|
||||
base.Deinitialize();
|
||||
}
|
||||
}
|
||||
|
||||
#if FLAX_EDITOR
|
||||
public class NetworkManagerEditorPlugin : EditorPlugin
|
||||
public class NetworkManagerEditorPlugin : GEditorPlugin
|
||||
{
|
||||
public override PluginDescription Description => NetworkManagerPlugin.DescriptionInternal;
|
||||
public override Type[] PluginDependencies { get => new Type[] { typeof(ConsoleEditorPlugin) }; }
|
||||
|
||||
public override PluginDescription Description => NetworkManagerPlugin.DescriptionInternal;
|
||||
public override Type GamePluginType => typeof(NetworkManagerPlugin);
|
||||
|
||||
public override void Initialize()
|
||||
public override void Init()
|
||||
{
|
||||
base.Initialize();
|
||||
Console.Init();
|
||||
FlaxEngine.Debug.Log("NetworkManagerPlugin initialized");
|
||||
//Console.Init();
|
||||
//NetworkManager.Init();
|
||||
}
|
||||
|
||||
public override void Deinitialize()
|
||||
{
|
||||
base.Deinitialize();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
164
Source/Game/Utility/GGamePlugin.cs
Normal file
164
Source/Game/Utility/GGamePlugin.cs
Normal file
@@ -0,0 +1,164 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using FlaxEngine;
|
||||
using Debug = System.Diagnostics.Debug;
|
||||
#if FLAX_EDITOR
|
||||
using FlaxEditor;
|
||||
#endif
|
||||
|
||||
namespace Game
|
||||
{
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public abstract class GGamePlugin : GamePlugin
|
||||
{
|
||||
public virtual Type[] PluginDependencies { get => Array.Empty<Type>(); }
|
||||
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<GGamePlugin>().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
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
public abstract class GEditorPlugin : EditorPlugin
|
||||
{
|
||||
public virtual Type[] PluginDependencies { get => Array.Empty<Type>(); }
|
||||
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<GEditorPlugin>().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
|
||||
}
|
||||
Reference in New Issue
Block a user