//================================================================================= bool PluginManager::LoadPlugin( const Cornucopia::Path& libraryPath ) { bool success = false; Plugin* plugin = 0; do { // Make sure that the plugin is not already loaded. if( FindPlugin( libraryPath ) ) { wxMessageBox( wxString::Format( wxT( "The plugin \"%s\" is already loaded." ), ( const char* )libraryPath ), wxT( "Error" ), wxOK | wxCENTRE ); break; } // Create a new plugin object to represent the plugin in our list. plugin = new ( std::nothrow ) Plugin(); if( !plugin ) break; // Attempt to load the plugin library. if( !plugin->Load( libraryPath ) ) break; // Attempt to initialize the plugin. CornucopiaEditor::Plugin* eventHandler = plugin->GetEventHandler(); if( !eventHandler->Initialize( &assistant ) ) { wxMessageBox( wxString::Format( wxT( "The plugin \"%s\" failed to initialize." ), ( const char* )libraryPath ), wxT( "Error" ), wxOK | wxCENTRE ); break; } // Send the first event to the plugin. PluginEvent event; event.SetEventObject(0); event.SetEventType( CORNUCOPIA_PLUGIN_INITIALIZE ); ( void )CallPlugin( plugin, event ); // We made it threw the gauntlet! success = true; } while( false ); // Discard or keep the plugin. if( !success ) delete plugin; else pluginList.push_back( plugin ); return success; }
void PluginManager::Initialize() { eventNames_.Add(_T("iconMenuOpening")); eventNames_.Add(_T("click")); luaApplication = new azApplication(); luaOptionPanel = new azOptionPanel(); luaDialogs = new azDialogs(); luaSystem = new azSystem(); TiXmlDocument doc(FilePaths::GetPluginSettingsFile().mb_str()); doc.LoadFile(TIXML_ENCODING_UTF8); TiXmlElement* pluginSettingsXml = doc.FirstChildElement("Plugins"); if (!pluginSettingsXml) WLOG(_T("PluginManager::Initialize: Could not load XML. No Plugins element found.")); wxString pluginPath = FilePaths::GetPluginsDirectory(); wxDir pluginFolder; if (wxFileName::DirExists(pluginPath) && pluginFolder.Open(pluginPath)) { wxString folderName; bool success = pluginFolder.GetFirst(&folderName, wxALL_FILES_PATTERN, wxDIR_DIRS); while (success) { Plugin* p = new Plugin(); plugins_.push_back(p); wxString folderPath = pluginPath + wxFileName::GetPathSeparator() + folderName; p->LoadMetadata(folderPath); bool pluginIsEnabled = true; if (pluginSettingsXml) { for (TiXmlElement* element = pluginSettingsXml->FirstChildElement(); element; element = element->NextSiblingElement()) { wxString elementName = wxString::FromUTF8(element->Value()); TiXmlHandle handle(element); if (elementName == _T("Plugin")) { wxString uuid = XmlUtil::ReadElementText(handle, "UUID"); if (uuid == p->GetUUID()) { pluginIsEnabled = XmlUtil::ReadElementTextAsBool(handle, "Enabled", true); break; } } else { WLOG(wxString::Format(_T("PluginManager::Initialize: Unknown element: %s"), elementName)); } } // for } // if p->Enable(pluginIsEnabled); p->SetInitiallyEnabled(pluginIsEnabled); if (pluginIsEnabled) { ILOG(_T("Loading plugin: ") + folderName); p->Load(folderPath); } else { ILOG(_T("Skipping disabled plugin: ") + folderName); } success = pluginFolder.GetNext(&folderName); } } }