Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 11.14 KB | None | 0 0
  1. ''' <summary>
  2. ''' Manages all the plugins of the system.
  3. ''' </summary>
  4. ''' <remarks></remarks>
  5. Public Class PluginManager
  6.     Inherits Collection(Of IToolPlugin)
  7.  
  8.     ''' <summary>
  9.     ''' Stores the plugin information, including the file path.
  10.     ''' </summary>
  11.     ''' <remarks></remarks>
  12.     Private Structure PluginData
  13.  
  14.         Public Property Plugin As IToolPlugin
  15.         Public Property Path As String
  16.  
  17.     End Structure
  18.  
  19.     'Variables
  20.     Private _errors As New List(Of LoadErrors)
  21.     Private _pluginDirectory As String = IO.Path.Combine(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "tsharp"), "plugins")
  22.     Private _inactiveDirectory As String = IO.Path.Combine(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "tsharp"), "unused")
  23.     Private _plugins As New List(Of PluginData)
  24.  
  25.     '' Load and Close Plugins
  26.  
  27.     ''' <summary>
  28.     ''' Loads plugins into the program.
  29.     ''' </summary>
  30.     ''' <remarks></remarks>
  31.     Public Sub LoadPlugins()
  32.  
  33.         'First, get all DLL's from the plugin folder.
  34.         For Each plugin As String In IO.Directory.GetFiles(_pluginDirectory, "*.dll")
  35.  
  36.             Try
  37.  
  38.                 'Create a new plugin interface
  39.                 Dim _pluginData As IToolPlugin = Nothing
  40.  
  41.                 'Load the plugin.
  42.                 Dim _plugin As Reflection.Assembly = Reflection.Assembly.LoadFile(plugin)
  43.  
  44.                 'See if we can find the type.
  45.                 For Each PluginType As Type In _plugin.GetTypes
  46.  
  47.                     'Assign the Interface.
  48.                     If PluginType.GetInterface("ToolSharp.IToolPlugin") IsNot Nothing Then
  49.  
  50.                         'Create the instance.
  51.                         _pluginData = DirectCast(_plugin.CreateInstance(PluginType.FullName), IToolPlugin)
  52.  
  53.                         'Add it to my built in list.
  54.                         Me.Add(_pluginData)
  55.  
  56.                         'Add it to the managing list
  57.                         _plugins.Add(New PluginData With {.Plugin = _pluginData, .Path = plugin})
  58.  
  59.                     End If
  60.  
  61.                 Next
  62.  
  63.             Catch ex As Reflection.ReflectionTypeLoadException
  64.  
  65.                 'Invalid plugin or assembly.
  66.                 Debug.WriteLine(ex.ToString)
  67.  
  68.                 'Create a new load error.
  69.                 Dim _error As New LoadErrors
  70.                 _error.ExceptionStrings = New List(Of String)
  71.  
  72.                 'Get the load exceptions
  73.                 For Each _ex As Exception In ex.LoaderExceptions
  74.                     _error.ExceptionStrings.Add(_ex.ToString)
  75.                 Next
  76.  
  77.                 'Set some more variables.
  78.                 _error.ExceptionData = ex
  79.                 _error.ModuleName = plugin
  80.  
  81.                 'Add it to the error list.
  82.                 _errors.Add(_error)
  83.  
  84.             Catch ex As Exception
  85.  
  86.                 'Invalid plugin or assembly.
  87.                 Debug.WriteLine(ex.ToString)
  88.  
  89.                 'Create a new load error.
  90.                 Dim _error As New LoadErrors
  91.                 _error.ExceptionData = ex
  92.                 _error.ModuleName = plugin
  93.  
  94.                 'Add it to the error list.
  95.                 _errors.Add(_error)
  96.  
  97.             End Try
  98.  
  99.         Next
  100.  
  101.     End Sub
  102.  
  103.     ''' <summary>
  104.     ''' Closes all the plugins.
  105.     ''' </summary>
  106.     ''' <remarks></remarks>
  107.     Public Sub ClosePlugins()
  108.  
  109.         'Setup a loop
  110.         For Each _plug As IToolPlugin In Me.Items
  111.             Try
  112.                 _plug.Remove()
  113.             Catch ex As Exception
  114.  
  115.             End Try
  116.         Next
  117.  
  118.     End Sub
  119.  
  120.     ''' <summary>
  121.     ''' Unloads all plugins.
  122.     ''' </summary>
  123.     ''' <remarks></remarks>
  124.     Public Sub Unload()
  125.  
  126.         'Clears all the items out.
  127.         Me.Items.Clear()
  128.  
  129.     End Sub
  130.  
  131.  
  132.     ' Install and uninstall
  133.  
  134.     ''' <summary>
  135.     ''' Installs a plugin into the system.
  136.     ''' </summary>
  137.     ''' <param name="path">The path to the plugin.</param>
  138.     ''' <remarks></remarks>
  139.     Public Sub InstallPlugin(ByVal path As String)
  140.  
  141.         'Create a small switch
  142.         Dim _valid As Boolean = False
  143.  
  144.         'Check to see if it exists
  145.         If IO.File.Exists(path) Then
  146.  
  147.             'Create a new plugin interface
  148.             Dim _pluginData As IToolPlugin = Nothing
  149.  
  150.             'Load the plugin.
  151.             Dim _plugin As Reflection.Assembly = Reflection.Assembly.LoadFile(path)
  152.  
  153.             'See if we can find the type.
  154.             For Each PluginType As Type In _plugin.GetTypes
  155.  
  156.                 'Assign the Interface.
  157.                 If PluginType.GetInterface("ToolSharp.IToolPlugin") IsNot Nothing Then
  158.  
  159.                     'We can install this plugin.
  160.                     If Not New IO.FileInfo(path).Directory.ToString.TrimEnd("\"c).Equals(_pluginDirectory) AndAlso
  161.                         Not IO.File.Exists(IO.Path.Combine(_pluginDirectory, IO.Path.GetFileName(path))) Then IO.File.Move(path,
  162.                             IO.Path.Combine(_pluginDirectory, IO.Path.GetFileName(path))) : _plugin = Nothing ': IO.File.Delete(path)
  163.  
  164.                 End If
  165.  
  166.             Next
  167.  
  168.             'Reload plugins
  169.             Me.Items.Clear()
  170.             Me.LoadPlugins()
  171.  
  172.         End If
  173.  
  174.     End Sub
  175.  
  176.     ''' <summary>
  177.     ''' Uninstalls a plugin from the system.
  178.     ''' </summary>
  179.     ''' <param name="path">The plugin to remove.</param>
  180.     ''' <remarks></remarks>
  181.     Public Sub UninstallPlugin(ByVal path As String)
  182.  
  183.         'First, we need to remove it from the system. Figure out what type it is and all subsequent types it might contain.
  184.  
  185.         'Create a new plugin interface
  186.         Dim _pluginData As IToolPlugin = Nothing
  187.  
  188.         'Load the plugin.
  189.         Dim _plugin As Reflection.Assembly = Reflection.Assembly.LoadFile(path)
  190.  
  191.         'See if we can find the type.
  192.         For Each PluginType As Type In _plugin.GetTypes
  193.  
  194.             'Assign the Interface.
  195.             If PluginType.GetInterface("ToolSharp.IToolPlugin") IsNot Nothing Then
  196.  
  197.                 'Create the instance.
  198.                 _pluginData = DirectCast(_plugin.CreateInstance(PluginType.FullName), IToolPlugin)
  199.  
  200.                 'REMOVE IT
  201.                 Me.Remove(_pluginData)
  202.  
  203.             End If
  204.  
  205.         Next
  206.  
  207.         _plugin = Nothing
  208.  
  209.         'Now that the nasty deed is done, move it.
  210.         IO.File.Move(path, IO.Path.Combine(_inactiveDirectory, IO.Path.GetFileName(path)))
  211.  
  212.     End Sub
  213.  
  214.     ''' <summary>
  215.     ''' Uninstalls a plugin from the system.
  216.     ''' </summary>
  217.     ''' <param name="plugin">The plugin to uninstall.</param>
  218.     ''' <remarks></remarks>
  219.     Public Sub UninstallPlugin(ByVal plugin As IToolPlugin)
  220.  
  221.         'Grab the path
  222.         For i As Integer = _plugins.Count - 1 To 0 Step -1
  223.  
  224.             If _plugins(i).Plugin.Equals(plugin) Then
  225.  
  226.                 'Grab the path
  227.                 Dim _path As String = _plugins(i).Path
  228.  
  229.                 'Remove it from reference
  230.                 Me.Remove(plugin)
  231.  
  232.                 'Move it.
  233.                 IO.File.Move(_path, IO.Path.Combine(_inactiveDirectory, IO.Path.GetFileName(_path)))
  234.  
  235.                 'Remove it
  236.                 _plugins.RemoveAt(i)
  237.  
  238.             End If
  239.  
  240.         Next
  241.  
  242.     End Sub
  243.  
  244.     ' Ultra Loader
  245.  
  246.     ''' <summary>
  247.     ''' Initializes the Plugins.
  248.     ''' </summary>
  249.     ''' <remarks></remarks>
  250.     Public Sub InitializePlugins(ByRef mainForm As gurella_mdi)
  251.  
  252.         'Setup the loop
  253.         For Each plugin As IToolPlugin In Me.Items
  254.             plugin.Initialize(mainForm)
  255.         Next
  256.  
  257.     End Sub
  258.  
  259.     ' Sorters (Authors & Categories).
  260.  
  261.     ''' <summary>
  262.     ''' Returns a List of Plugin Authors, sorted alphabetically.
  263.     ''' </summary>
  264.     ''' <value></value>
  265.     ''' <returns></returns>
  266.     ''' <remarks></remarks>
  267.     Public ReadOnly Property Authors As List(Of String)
  268.         Get
  269.  
  270.             'Create the return list
  271.             Dim _list As New List(Of String)
  272.  
  273.             'Setup a for loop
  274.             For Each _plug As IToolPlugin In Me.Items
  275.  
  276.                 'See if we already have the author name.
  277.                 If Not _list.Contains(_plug.PluginAuthor) Then _list.Add(_plug.PluginAuthor)
  278.  
  279.             Next
  280.  
  281.             'Sort the list
  282.             _list.Sort()
  283.  
  284.             'Returh the list
  285.             Return _list
  286.  
  287.         End Get
  288.     End Property
  289.  
  290.     ''' <summary>
  291.     ''' Gets a list of Categories in the manager.
  292.     ''' </summary>
  293.     ''' <value></value>
  294.     ''' <returns></returns>
  295.     ''' <remarks></remarks>
  296.     Public ReadOnly Property Categories As List(Of String)
  297.         Get
  298.  
  299.             'Create the return list
  300.             Dim _list As New List(Of String)
  301.  
  302.             'Setup a for loop
  303.             For Each _plug As IToolPlugin In Me.Items
  304.  
  305.                 'See if we already have the author name.
  306.                 If Not _list.Contains(_plug.Category) Then _list.Add(_plug.Category)
  307.  
  308.             Next
  309.  
  310.             'Sort the list
  311.             _list.Sort()
  312.  
  313.             'Returh the list
  314.             Return _list
  315.  
  316.         End Get
  317.     End Property
  318.  
  319.     ' Getters.
  320.  
  321.     ''' <summary>
  322.     ''' Get the plugins via author name.
  323.     ''' </summary>
  324.     ''' <param name="_author">The author name to use.</param>
  325.     ''' <returns></returns>
  326.     ''' <remarks></remarks>
  327.     Public Function GetPluginsByAuthor(ByVal _author As String) As List(Of IToolPlugin)
  328.  
  329.         'Setup the list.
  330.         Dim _returnList As New List(Of IToolPlugin)
  331.  
  332.         'Setup a loop
  333.         For Each plugin As IToolPlugin In Me.Items
  334.  
  335.             'Check the name.
  336.             If plugin.PluginAuthor.ToLower.Equals(_author.ToLower) Then _returnList.Add(plugin)
  337.  
  338.         Next
  339.  
  340.         'Return it
  341.         Return _returnList
  342.  
  343.     End Function
  344.  
  345.     ''' <summary>
  346.     ''' Gets the plugins via the category name.
  347.     ''' </summary>
  348.     ''' <param name="_category">The name of the category to use.</param>
  349.     ''' <returns></returns>
  350.     ''' <remarks></remarks>
  351.     Public Function GetPluginsByCategory(ByVal _category As String) As List(Of IToolPlugin)
  352.  
  353.         'Setup the list.
  354.         Dim _returnList As New List(Of IToolPlugin)
  355.  
  356.         'Setup a loop
  357.         For Each plugin As IToolPlugin In Me.Items
  358.  
  359.             'Check the name.
  360.             If plugin.Category.ToLower.Equals(_category.ToLower) Then _returnList.Add(plugin)
  361.  
  362.         Next
  363.  
  364.         'Return it
  365.         Return _returnList
  366.  
  367.     End Function
  368.  
  369.     ''' <summary>
  370.     ''' Gets a plugin via the name of it.
  371.     ''' </summary>
  372.     ''' <param name="name">The name of the plugin to use.</param>
  373.     ''' <returns></returns>
  374.     ''' <remarks></remarks>
  375.     Public Function GetPluginByName(ByVal name As String) As IToolPlugin
  376.  
  377.         'Loop
  378.         For Each _plugin As IToolPlugin In Me.Items
  379.  
  380.             'Check the name
  381.             If _plugin.PluginName.Equals(name) Then Return _plugin
  382.  
  383.         Next
  384.  
  385.         'Not found
  386.         Return Nothing
  387.  
  388.     End Function
  389.  
  390.     ' More Properties
  391.  
  392.     ''' <summary>
  393.     ''' Returns the number of plugins in the list.
  394.     ''' </summary>
  395.     ''' <value></value>
  396.     ''' <returns></returns>
  397.     ''' <remarks></remarks>
  398.     Public Shadows ReadOnly Property Count As Integer
  399.         Get
  400.             Return Me.Items.Count
  401.         End Get
  402.     End Property
  403.  
  404.     ''' <summary>
  405.     ''' Returns a List(Of LoadErrors)
  406.     ''' </summary>
  407.     ''' <value></value>
  408.     ''' <returns></returns>
  409.     ''' <remarks></remarks>
  410.     Public ReadOnly Property Errors As List(Of LoadErrors)
  411.         Get
  412.             Return _errors
  413.         End Get
  414.     End Property
  415.  
  416. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement