Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ''' <summary>
- ''' Manages all the plugins of the system.
- ''' </summary>
- ''' <remarks></remarks>
- Public Class PluginManager
- Inherits Collection(Of IToolPlugin)
- ''' <summary>
- ''' Stores the plugin information, including the file path.
- ''' </summary>
- ''' <remarks></remarks>
- Private Structure PluginData
- Public Property Plugin As IToolPlugin
- Public Property Path As String
- End Structure
- 'Variables
- Private _errors As New List(Of LoadErrors)
- Private _pluginDirectory As String = IO.Path.Combine(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "tsharp"), "plugins")
- Private _inactiveDirectory As String = IO.Path.Combine(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "tsharp"), "unused")
- Private _plugins As New List(Of PluginData)
- '' Load and Close Plugins
- ''' <summary>
- ''' Loads plugins into the program.
- ''' </summary>
- ''' <remarks></remarks>
- Public Sub LoadPlugins()
- 'First, get all DLL's from the plugin folder.
- For Each plugin As String In IO.Directory.GetFiles(_pluginDirectory, "*.dll")
- Try
- 'Create a new plugin interface
- Dim _pluginData As IToolPlugin = Nothing
- 'Load the plugin.
- Dim _plugin As Reflection.Assembly = Reflection.Assembly.LoadFile(plugin)
- 'See if we can find the type.
- For Each PluginType As Type In _plugin.GetTypes
- 'Assign the Interface.
- If PluginType.GetInterface("ToolSharp.IToolPlugin") IsNot Nothing Then
- 'Create the instance.
- _pluginData = DirectCast(_plugin.CreateInstance(PluginType.FullName), IToolPlugin)
- 'Add it to my built in list.
- Me.Add(_pluginData)
- 'Add it to the managing list
- _plugins.Add(New PluginData With {.Plugin = _pluginData, .Path = plugin})
- End If
- Next
- Catch ex As Reflection.ReflectionTypeLoadException
- 'Invalid plugin or assembly.
- Debug.WriteLine(ex.ToString)
- 'Create a new load error.
- Dim _error As New LoadErrors
- _error.ExceptionStrings = New List(Of String)
- 'Get the load exceptions
- For Each _ex As Exception In ex.LoaderExceptions
- _error.ExceptionStrings.Add(_ex.ToString)
- Next
- 'Set some more variables.
- _error.ExceptionData = ex
- _error.ModuleName = plugin
- 'Add it to the error list.
- _errors.Add(_error)
- Catch ex As Exception
- 'Invalid plugin or assembly.
- Debug.WriteLine(ex.ToString)
- 'Create a new load error.
- Dim _error As New LoadErrors
- _error.ExceptionData = ex
- _error.ModuleName = plugin
- 'Add it to the error list.
- _errors.Add(_error)
- End Try
- Next
- End Sub
- ''' <summary>
- ''' Closes all the plugins.
- ''' </summary>
- ''' <remarks></remarks>
- Public Sub ClosePlugins()
- 'Setup a loop
- For Each _plug As IToolPlugin In Me.Items
- Try
- _plug.Remove()
- Catch ex As Exception
- End Try
- Next
- End Sub
- ''' <summary>
- ''' Unloads all plugins.
- ''' </summary>
- ''' <remarks></remarks>
- Public Sub Unload()
- 'Clears all the items out.
- Me.Items.Clear()
- End Sub
- ' Install and uninstall
- ''' <summary>
- ''' Installs a plugin into the system.
- ''' </summary>
- ''' <param name="path">The path to the plugin.</param>
- ''' <remarks></remarks>
- Public Sub InstallPlugin(ByVal path As String)
- 'Create a small switch
- Dim _valid As Boolean = False
- 'Check to see if it exists
- If IO.File.Exists(path) Then
- 'Create a new plugin interface
- Dim _pluginData As IToolPlugin = Nothing
- 'Load the plugin.
- Dim _plugin As Reflection.Assembly = Reflection.Assembly.LoadFile(path)
- 'See if we can find the type.
- For Each PluginType As Type In _plugin.GetTypes
- 'Assign the Interface.
- If PluginType.GetInterface("ToolSharp.IToolPlugin") IsNot Nothing Then
- 'We can install this plugin.
- If Not New IO.FileInfo(path).Directory.ToString.TrimEnd("\"c).Equals(_pluginDirectory) AndAlso
- Not IO.File.Exists(IO.Path.Combine(_pluginDirectory, IO.Path.GetFileName(path))) Then IO.File.Move(path,
- IO.Path.Combine(_pluginDirectory, IO.Path.GetFileName(path))) : _plugin = Nothing ': IO.File.Delete(path)
- End If
- Next
- 'Reload plugins
- Me.Items.Clear()
- Me.LoadPlugins()
- End If
- End Sub
- ''' <summary>
- ''' Uninstalls a plugin from the system.
- ''' </summary>
- ''' <param name="path">The plugin to remove.</param>
- ''' <remarks></remarks>
- Public Sub UninstallPlugin(ByVal path As String)
- 'First, we need to remove it from the system. Figure out what type it is and all subsequent types it might contain.
- 'Create a new plugin interface
- Dim _pluginData As IToolPlugin = Nothing
- 'Load the plugin.
- Dim _plugin As Reflection.Assembly = Reflection.Assembly.LoadFile(path)
- 'See if we can find the type.
- For Each PluginType As Type In _plugin.GetTypes
- 'Assign the Interface.
- If PluginType.GetInterface("ToolSharp.IToolPlugin") IsNot Nothing Then
- 'Create the instance.
- _pluginData = DirectCast(_plugin.CreateInstance(PluginType.FullName), IToolPlugin)
- 'REMOVE IT
- Me.Remove(_pluginData)
- End If
- Next
- _plugin = Nothing
- 'Now that the nasty deed is done, move it.
- IO.File.Move(path, IO.Path.Combine(_inactiveDirectory, IO.Path.GetFileName(path)))
- End Sub
- ''' <summary>
- ''' Uninstalls a plugin from the system.
- ''' </summary>
- ''' <param name="plugin">The plugin to uninstall.</param>
- ''' <remarks></remarks>
- Public Sub UninstallPlugin(ByVal plugin As IToolPlugin)
- 'Grab the path
- For i As Integer = _plugins.Count - 1 To 0 Step -1
- If _plugins(i).Plugin.Equals(plugin) Then
- 'Grab the path
- Dim _path As String = _plugins(i).Path
- 'Remove it from reference
- Me.Remove(plugin)
- 'Move it.
- IO.File.Move(_path, IO.Path.Combine(_inactiveDirectory, IO.Path.GetFileName(_path)))
- 'Remove it
- _plugins.RemoveAt(i)
- End If
- Next
- End Sub
- ' Ultra Loader
- ''' <summary>
- ''' Initializes the Plugins.
- ''' </summary>
- ''' <remarks></remarks>
- Public Sub InitializePlugins(ByRef mainForm As gurella_mdi)
- 'Setup the loop
- For Each plugin As IToolPlugin In Me.Items
- plugin.Initialize(mainForm)
- Next
- End Sub
- ' Sorters (Authors & Categories).
- ''' <summary>
- ''' Returns a List of Plugin Authors, sorted alphabetically.
- ''' </summary>
- ''' <value></value>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public ReadOnly Property Authors As List(Of String)
- Get
- 'Create the return list
- Dim _list As New List(Of String)
- 'Setup a for loop
- For Each _plug As IToolPlugin In Me.Items
- 'See if we already have the author name.
- If Not _list.Contains(_plug.PluginAuthor) Then _list.Add(_plug.PluginAuthor)
- Next
- 'Sort the list
- _list.Sort()
- 'Returh the list
- Return _list
- End Get
- End Property
- ''' <summary>
- ''' Gets a list of Categories in the manager.
- ''' </summary>
- ''' <value></value>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public ReadOnly Property Categories As List(Of String)
- Get
- 'Create the return list
- Dim _list As New List(Of String)
- 'Setup a for loop
- For Each _plug As IToolPlugin In Me.Items
- 'See if we already have the author name.
- If Not _list.Contains(_plug.Category) Then _list.Add(_plug.Category)
- Next
- 'Sort the list
- _list.Sort()
- 'Returh the list
- Return _list
- End Get
- End Property
- ' Getters.
- ''' <summary>
- ''' Get the plugins via author name.
- ''' </summary>
- ''' <param name="_author">The author name to use.</param>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public Function GetPluginsByAuthor(ByVal _author As String) As List(Of IToolPlugin)
- 'Setup the list.
- Dim _returnList As New List(Of IToolPlugin)
- 'Setup a loop
- For Each plugin As IToolPlugin In Me.Items
- 'Check the name.
- If plugin.PluginAuthor.ToLower.Equals(_author.ToLower) Then _returnList.Add(plugin)
- Next
- 'Return it
- Return _returnList
- End Function
- ''' <summary>
- ''' Gets the plugins via the category name.
- ''' </summary>
- ''' <param name="_category">The name of the category to use.</param>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public Function GetPluginsByCategory(ByVal _category As String) As List(Of IToolPlugin)
- 'Setup the list.
- Dim _returnList As New List(Of IToolPlugin)
- 'Setup a loop
- For Each plugin As IToolPlugin In Me.Items
- 'Check the name.
- If plugin.Category.ToLower.Equals(_category.ToLower) Then _returnList.Add(plugin)
- Next
- 'Return it
- Return _returnList
- End Function
- ''' <summary>
- ''' Gets a plugin via the name of it.
- ''' </summary>
- ''' <param name="name">The name of the plugin to use.</param>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public Function GetPluginByName(ByVal name As String) As IToolPlugin
- 'Loop
- For Each _plugin As IToolPlugin In Me.Items
- 'Check the name
- If _plugin.PluginName.Equals(name) Then Return _plugin
- Next
- 'Not found
- Return Nothing
- End Function
- ' More Properties
- ''' <summary>
- ''' Returns the number of plugins in the list.
- ''' </summary>
- ''' <value></value>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public Shadows ReadOnly Property Count As Integer
- Get
- Return Me.Items.Count
- End Get
- End Property
- ''' <summary>
- ''' Returns a List(Of LoadErrors)
- ''' </summary>
- ''' <value></value>
- ''' <returns></returns>
- ''' <remarks></remarks>
- Public ReadOnly Property Errors As List(Of LoadErrors)
- Get
- Return _errors
- End Get
- End Property
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement