add AutoRegister property and typed Get<T> methods
This commit is contained in:
parent
b77bc4280c
commit
861622bac3
@ -19,6 +19,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace MBS.Framework
|
||||
{
|
||||
@ -44,6 +45,8 @@ namespace MBS.Framework
|
||||
// this method intentionally left blank
|
||||
}
|
||||
|
||||
protected virtual bool AutoRegister => true;
|
||||
|
||||
protected virtual bool IsSupportedInternal()
|
||||
{
|
||||
return true;
|
||||
@ -52,9 +55,111 @@ namespace MBS.Framework
|
||||
{
|
||||
return IsSupportedInternal();
|
||||
}
|
||||
private static Plugin[] _plugins = null;
|
||||
public static TPlugin[] Get<TPlugin>(bool resetCache = false) where TPlugin : Plugin
|
||||
{
|
||||
Plugin[] plugins = Get(resetCache);
|
||||
return plugins.OfType<TPlugin>().ToArray();
|
||||
}
|
||||
public static Plugin[] Get(bool resetCache = false)
|
||||
{
|
||||
if (resetCache)
|
||||
{
|
||||
_plugins = null; // should not be cached? // actually, yes it should...
|
||||
|
||||
public Plugin()
|
||||
// 2020-12-12 20:54 by beckermj
|
||||
// ACTUALLY, it depends on whether the configuration needs to be persisted across calls to Get()
|
||||
// [it does] and whether the list of plugins needs to be reloaded when CustomPlugins is modified
|
||||
// [it shouldn't, but it does] .
|
||||
//
|
||||
// The safest way we can handle this RIGHT NOW is to prevent any plugin from being loaded until after CustomPlugins
|
||||
// is initialized by UIApplication.
|
||||
//
|
||||
// We call Plugins.Get(new Feature[] { KnownFeatures.UWTPlatform }) to retrieve the available User Interface plugins
|
||||
// that supply the UWT Platform implementation (e.g. GTK, Windows Forms, etc.) - and this causes CustomPlugins to not
|
||||
// load properly since it loads AFTER this initial call to Plugins.Get().
|
||||
//
|
||||
// So I add ed a resetCache parameter that when specified TRUE will clear the cache and then return the appropriate
|
||||
// plugin. This way we can continue caching future calls to Get() without missing out on CustomPlugins.
|
||||
}
|
||||
|
||||
if (_plugins == null)
|
||||
{
|
||||
Type[] types = MBS.Framework.Reflection.GetAvailableTypes(new Type[] { typeof(Plugin) });
|
||||
System.Collections.Generic.List<Plugin> plugins = new System.Collections.Generic.List<Plugin>();
|
||||
for (int i = 0; i < types.Length; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (types[i].IsSubclassOf(typeof(ICustomPlugin)))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
Plugin plg = (Plugin)types[i].Assembly.CreateInstance(types[i].FullName);
|
||||
plugins.Add(plg);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
Plugin[] plugins2 = Application.Instance.GetAdditionalPlugins();
|
||||
for (int i = 0; i < plugins2.Length; i++)
|
||||
{
|
||||
plugins.Add(plugins2[i]);
|
||||
}
|
||||
_plugins = plugins.ToArray();
|
||||
|
||||
if (resetCache)
|
||||
{
|
||||
_plugins = null;
|
||||
return plugins.ToArray();
|
||||
}
|
||||
}
|
||||
return _plugins;
|
||||
}
|
||||
|
||||
public static TPlugin[] Get<TPlugin>(Feature[] providedFeatures, bool resetCache = false) where TPlugin : Plugin
|
||||
{
|
||||
Plugin[] plugins = Get(providedFeatures, resetCache);
|
||||
return plugins.OfType<TPlugin>().ToArray();
|
||||
}
|
||||
public static Plugin[] Get(Feature[] providedFeatures, bool resetCache = false)
|
||||
{
|
||||
System.Collections.Generic.List<Plugin> list = new System.Collections.Generic.List<Plugin>();
|
||||
Plugin[] plugins = Get(resetCache);
|
||||
for (int i = 0; i < plugins.Length; i++)
|
||||
{
|
||||
if (!plugins[i].AutoRegister)
|
||||
continue;
|
||||
|
||||
if (!plugins[i].IsSupported())
|
||||
continue;
|
||||
|
||||
for (int j = 0; j < providedFeatures.Length; j++)
|
||||
{
|
||||
if (plugins[i].ProvidedFeatures.Contains(providedFeatures[j]))
|
||||
list.Add(plugins[i]);
|
||||
}
|
||||
}
|
||||
return list.ToArray();
|
||||
}
|
||||
|
||||
public static TPlugin Get<TPlugin>(Guid id) where TPlugin : Plugin
|
||||
{
|
||||
Plugin plugin = Get(id);
|
||||
return (TPlugin)plugin;
|
||||
}
|
||||
public static Plugin Get(Guid id)
|
||||
{
|
||||
Plugin[] plugins = Get();
|
||||
for (int i = 0; i < plugins.Length; i++)
|
||||
{
|
||||
if (plugins[i].ID == id)
|
||||
return plugins[i];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user