PageRenderTime 46ms CodeModel.GetById 22ms 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
  1. Imports PluginSupport
  2. Imports PowerSong.SongDatabase
  3. Imports System.IO.Path
  4. Imports System.Reflection
  5. Public Class Plugins
  6. #Region " Enforce the Singleton Design Pattern "
  7. Private Sub New()
  8. End Sub
  9. Private Shared FInstance As Plugins = Nothing
  10. Public Shared ReadOnly Property Instance() As Plugins
  11. Get
  12. If FInstance Is Nothing Then FInstance = New Plugins
  13. Return FInstance
  14. End Get
  15. End Property
  16. #End Region
  17. Private FItems As New Dictionary(Of String, IPlugin)
  18. Public ReadOnly Property Items() As Dictionary(Of String, IPlugin)
  19. Get
  20. Return FItems
  21. End Get
  22. End Property
  23. Public Sub LoadAllPlugins(ByVal database As Database)
  24. For Each Filename As String In database.PluginFiles
  25. LoadPlugin(database, Filename)
  26. Next
  27. End Sub
  28. Public Sub LoadPlugin(ByVal database As Database, ByVal fileName As String)
  29. Try
  30. ' Instantiate the plugin
  31. Dim Plugin As IPlugin = database.InstantiatePlugin(fileName, PowerSongApi.Instance)
  32. ' Add the plugin to the list and call its Start method
  33. FItems.Add(fileName, Plugin)
  34. Plugin.Start()
  35. Catch ex As Exception
  36. If MsgBox("Could not start plugin '" + fileName + "':" + Environment.NewLine + _
  37. ex.Message + Environment.NewLine + Environment.NewLine + _
  38. "Do you wish to uninstall it?", MsgBoxStyle.Question + MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
  39. database.RemovePlugin(fileName)
  40. End If
  41. End Try
  42. End Sub
  43. Public Sub UnloadAllPlugins()
  44. ' Build the list of plugins to unload
  45. Dim PluginsToUnload As New List(Of IPlugin)
  46. For Each Plugin As IPlugin In FItems.Values
  47. PluginsToUnload.Add(Plugin)
  48. Next
  49. ' Unload all of the plugins
  50. For Each Plugin As IPlugin In PluginsToUnload
  51. UnloadPlugin(Plugin)
  52. Next
  53. FItems.Clear()
  54. End Sub
  55. Public Sub UnloadPlugin(ByVal plugin As IPlugin)
  56. ' Remove the plugin from the list and call its Stop method
  57. plugin.Stop()
  58. Dim PluginFileToRemove As String = ""
  59. For Each PluginInfo As KeyValuePair(Of String, IPlugin) In FItems
  60. If PluginInfo.Value Is plugin Then PluginFileToRemove = PluginInfo.Key
  61. Next
  62. If PluginFileToRemove = "" Then Throw New ApplicationException("Could not unload plugin completely.")
  63. FItems.Remove(PluginFileToRemove)
  64. End Sub
  65. Public Function InstallPlugin(ByVal database As Database, ByVal sourceFileName As String) As String
  66. ' Add the plugin to the active database
  67. Dim ActualFileName As String = database.AddExistingPlugin(sourceFileName)
  68. ' Instantiate the plugin and call its Install method
  69. Dim Plugin As IPlugin = database.InstantiatePlugin(GetFileName(ActualFileName), PowerSongApi.Instance)
  70. Plugin.Install()
  71. ' Return the actual file name
  72. Return ActualFileName
  73. End Function
  74. Public Sub UninstallPlugin(ByVal database As Database, ByVal fileName As String, ByVal plugin As IPlugin)
  75. ' Call the Uninstall method on the plugin
  76. plugin.Uninstall()
  77. ' Remove the plugin from the active database
  78. database.RemovePlugin(fileName)
  79. End Sub
  80. End Class