PageRenderTime 48ms CodeModel.GetById 25ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Source Code/PowerSong/Plugins.vb

#
Visual Basic | 115 lines | 70 code | 35 blank | 10 comment | 0 complexity | 92796aa5351c0396d3260c8e886a6be4 MD5 | raw file
  1Imports PluginSupport
  2Imports PowerSong.SongDatabase
  3Imports System.IO.Path
  4Imports System.Reflection
  5
  6Public Class Plugins
  7
  8#Region " Enforce the Singleton Design Pattern "
  9
 10    Private Sub New()
 11
 12    End Sub
 13
 14    Private Shared FInstance As Plugins = Nothing
 15
 16    Public Shared ReadOnly Property Instance() As Plugins
 17        Get
 18            If FInstance Is Nothing Then FInstance = New Plugins
 19            Return FInstance
 20        End Get
 21    End Property
 22
 23#End Region
 24
 25    Private FItems As New Dictionary(Of String, IPlugin)
 26
 27    Public ReadOnly Property Items() As Dictionary(Of String, IPlugin)
 28        Get
 29            Return FItems
 30        End Get
 31    End Property
 32
 33    Public Sub LoadAllPlugins(ByVal database As Database)
 34
 35        For Each Filename As String In database.PluginFiles
 36            LoadPlugin(database, Filename)
 37        Next
 38
 39    End Sub
 40
 41    Public Sub LoadPlugin(ByVal database As Database, ByVal fileName As String)
 42
 43        Try
 44
 45            ' Instantiate the plugin
 46            Dim Plugin As IPlugin = database.InstantiatePlugin(fileName, PowerSongApi.Instance)
 47
 48            ' Add the plugin to the list and call its Start method
 49            FItems.Add(fileName, Plugin)
 50            Plugin.Start()
 51
 52        Catch ex As Exception
 53            If MsgBox("Could not start plugin '" + fileName + "':" + Environment.NewLine + _
 54                      ex.Message + Environment.NewLine + Environment.NewLine + _
 55                      "Do you wish to uninstall it?", MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
 56                database.RemovePlugin(fileName)
 57            End If
 58        End Try
 59
 60    End Sub
 61
 62    Public Sub UnloadAllPlugins()
 63
 64        ' Build the list of plugins to unload
 65        Dim PluginsToUnload As New List(Of IPlugin)
 66        For Each Plugin As IPlugin In FItems.Values
 67            PluginsToUnload.Add(Plugin)
 68        Next
 69
 70        ' Unload all of the plugins
 71        For Each Plugin As IPlugin In PluginsToUnload
 72            UnloadPlugin(Plugin)
 73        Next
 74        FItems.Clear()
 75
 76    End Sub
 77
 78    Public Sub UnloadPlugin(ByVal plugin As IPlugin)
 79
 80        ' Remove the plugin from the list and call its Stop method
 81        plugin.Stop()
 82        Dim PluginFileToRemove As String = ""
 83        For Each PluginInfo As KeyValuePair(Of String, IPlugin) In FItems
 84            If PluginInfo.Value Is plugin Then PluginFileToRemove = PluginInfo.Key
 85        Next
 86        If PluginFileToRemove = "" Then Throw New ApplicationException("Could not unload plugin completely.")
 87        FItems.Remove(PluginFileToRemove)
 88
 89    End Sub
 90
 91    Public Function InstallPlugin(ByVal database As Database, ByVal sourceFileName As String) As String
 92
 93        ' Add the plugin to the active database
 94        Dim ActualFileName As String = database.AddExistingPlugin(sourceFileName)
 95
 96        ' Instantiate the plugin and call its Install method
 97        Dim Plugin As IPlugin = database.InstantiatePlugin(GetFileName(ActualFileName), PowerSongApi.Instance)
 98        Plugin.Install()
 99
100        ' Return the actual file name
101        Return ActualFileName
102
103    End Function
104
105    Public Sub UninstallPlugin(ByVal database As Database, ByVal fileName As String, ByVal plugin As IPlugin)
106
107        ' Call the Uninstall method on the plugin
108        plugin.Uninstall()
109
110        ' Remove the plugin from the active database
111        database.RemovePlugin(fileName)
112
113    End Sub
114
115End Class