/Source Code/PowerSong/Plugins.vb
Visual Basic | 115 lines | 70 code | 35 blank | 10 comment | 0 complexity | 92796aa5351c0396d3260c8e886a6be4 MD5 | raw file
- Imports PluginSupport
- Imports PowerSong.SongDatabase
- Imports System.IO.Path
- Imports System.Reflection
-
- Public Class Plugins
-
- #Region " Enforce the Singleton Design Pattern "
-
- Private Sub New()
-
- End Sub
-
- Private Shared FInstance As Plugins = Nothing
-
- Public Shared ReadOnly Property Instance() As Plugins
- Get
- If FInstance Is Nothing Then FInstance = New Plugins
- Return FInstance
- End Get
- End Property
-
- #End Region
-
- Private FItems As New Dictionary(Of String, IPlugin)
-
- Public ReadOnly Property Items() As Dictionary(Of String, IPlugin)
- Get
- Return FItems
- End Get
- End Property
-
- Public Sub LoadAllPlugins(ByVal database As Database)
-
- For Each Filename As String In database.PluginFiles
- LoadPlugin(database, Filename)
- Next
-
- End Sub
-
- Public Sub LoadPlugin(ByVal database As Database, ByVal fileName As String)
-
- Try
-
- ' Instantiate the plugin
- Dim Plugin As IPlugin = database.InstantiatePlugin(fileName, PowerSongApi.Instance)
-
- ' Add the plugin to the list and call its Start method
- FItems.Add(fileName, Plugin)
- Plugin.Start()
-
- Catch ex As Exception
- If MsgBox("Could not start plugin '" + fileName + "':" + Environment.NewLine + _
- ex.Message + Environment.NewLine + Environment.NewLine + _
- "Do you wish to uninstall it?", MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
- database.RemovePlugin(fileName)
- End If
- End Try
-
- End Sub
-
- Public Sub UnloadAllPlugins()
-
- ' Build the list of plugins to unload
- Dim PluginsToUnload As New List(Of IPlugin)
- For Each Plugin As IPlugin In FItems.Values
- PluginsToUnload.Add(Plugin)
- Next
-
- ' Unload all of the plugins
- For Each Plugin As IPlugin In PluginsToUnload
- UnloadPlugin(Plugin)
- Next
- FItems.Clear()
-
- End Sub
-
- Public Sub UnloadPlugin(ByVal plugin As IPlugin)
-
- ' Remove the plugin from the list and call its Stop method
- plugin.Stop()
- Dim PluginFileToRemove As String = ""
- For Each PluginInfo As KeyValuePair(Of String, IPlugin) In FItems
- If PluginInfo.Value Is plugin Then PluginFileToRemove = PluginInfo.Key
- Next
- If PluginFileToRemove = "" Then Throw New ApplicationException("Could not unload plugin completely.")
- FItems.Remove(PluginFileToRemove)
-
- End Sub
-
- Public Function InstallPlugin(ByVal database As Database, ByVal sourceFileName As String) As String
-
- ' Add the plugin to the active database
- Dim ActualFileName As String = database.AddExistingPlugin(sourceFileName)
-
- ' Instantiate the plugin and call its Install method
- Dim Plugin As IPlugin = database.InstantiatePlugin(GetFileName(ActualFileName), PowerSongApi.Instance)
- Plugin.Install()
-
- ' Return the actual file name
- Return ActualFileName
-
- End Function
-
- Public Sub UninstallPlugin(ByVal database As Database, ByVal fileName As String, ByVal plugin As IPlugin)
-
- ' Call the Uninstall method on the plugin
- plugin.Uninstall()
-
- ' Remove the plugin from the active database
- database.RemovePlugin(fileName)
-
- End Sub
-
- End Class