Improved plugin version logging for Windows #4866

Conflicts:
	src/lib/arch/win32/ArchPluginWindows.cpp
This commit is contained in:
Nick Bolton 2015-07-23 18:02:05 -07:00 committed by Jerry (Xinyu Hou)
parent bfabd436d7
commit cb5f0f7b12
5 changed files with 53 additions and 27 deletions

View File

@ -57,33 +57,42 @@ ArchPluginWindows::load()
std::vector<String>::iterator it; std::vector<String>::iterator it;
for (it = plugins.begin(); it != plugins.end(); ++it) { for (it = plugins.begin(); it != plugins.end(); ++it) {
LOG((CLOG_DEBUG "loading plugin: %s", (*it).c_str())); String filename = *it;
String path = String(dir).append("\\").append(*it); String nameNoExt = synergy::string::removeFileExt(*it);
HINSTANCE library = LoadLibrary(path.c_str()); String path = synergy::string::sprintf(
"%s\\%s", dir.c_str(), filename.c_str());
if (library == NULL) { LOG((CLOG_DEBUG "loading plugin: %s", filename.c_str()));
HINSTANCE handle = LoadLibrary(path.c_str());
void* voidHandle = reinterpret_cast<void*>(handle);
if (handle == NULL) {
String error = XArchEvalWindows().eval(); String error = XArchEvalWindows().eval();
LOG((CLOG_ERR "failed to load plugin '%s', error: %s", (*it).c_str(), error.c_str())); LOG((CLOG_ERR "failed to load plugin '%s', error: %s",
filename.c_str(), error.c_str()));
continue; continue;
} }
void* lib = reinterpret_cast<void*>(library); String expectedVersion = getExpectedPluginVersion(nameNoExt.c_str());
String currentVersion = getCurrentVersion(nameNoExt.c_str(), voidHandle);
String pluginName = synergy::string::removeFileExt(*it); if (currentVersion.empty() || (expectedVersion != currentVersion)) {
char* version = (char*)invoke(pluginName.c_str(), "version", NULL, lib); LOG((CLOG_ERR
String expectedVersion(pluginVersion(pluginName.c_str())); "failed to load plugin '%s', "
"expected version %s but was %s",
filename.c_str(),
expectedVersion.c_str(),
currentVersion.empty() ? "unknown" : currentVersion.c_str()));
if (version != NULL && expectedVersion.compare(version) == 0) { FreeLibrary(handle);
LOG((CLOG_DEBUG "loaded plugin: %s (%s)", (*it).c_str(), version)); continue;
m_pluginTable.insert(std::make_pair(pluginName, lib));
}
else {
LOG((CLOG_ERR "plugin version doesn't match"));
LOG((CLOG_DEBUG "expected plugin version: %s actual plugin version: %s",
expectedVersion.c_str(), version));
LOG((CLOG_ERR "skip plugin: %s", (*it).c_str()));
FreeLibrary(library);
} }
LOG((CLOG_DEBUG "plugin loaded: %s (version %s)",
filename.c_str(),
currentVersion.c_str()));
m_pluginTable.insert(std::make_pair(nameNoExt, voidHandle));
} }
} }
@ -208,11 +217,24 @@ ArchPluginWindows::getFilenames(const String& pattern, std::vector<String>& file
FindClose(find); FindClose(find);
} }
String ArchPluginWindows::getPluginsDir() String
ArchPluginWindows::getPluginsDir()
{ {
return ARCH->getPluginDirectory(); return ARCH->getPluginDirectory();
} }
String
ArchPluginWindows::getCurrentVersion(const String& name, void* handle)
{
char* version = (char*)invoke(name.c_str(), "version", NULL, handle);
if (version == NULL) {
return "";
}
return version;
}
void void
sendEvent(const char* eventName, void* data) sendEvent(const char* eventName, void* data)
{ {

View File

@ -47,6 +47,7 @@ public:
private: private:
void getFilenames(const String& pattern, std::vector<String>& filenames); void getFilenames(const String& pattern, std::vector<String>& filenames);
String getPluginsDir(); String getPluginsDir();
String getCurrentVersion(const String& name, void* handle);
private: private:
PluginTable m_pluginTable; PluginTable m_pluginTable;

View File

@ -21,13 +21,13 @@
static const int kpluginCount = 1; static const int kpluginCount = 1;
static const char kUnknownVersion[] = "unknown"; static const char kUnknownVersion[] = "unknown";
static const char* s_pluginNames[] = {"ns"}; static const char* s_pluginNames[] = { "ns" };
static const char* s_pluginVersions[] = {"1.2"}; static const char* s_pluginVersions[] = { "1.2" };
const char* pluginVersion(const char* pluginName) const char* getExpectedPluginVersion(const char* name)
{ {
for (int i = 0; i < kpluginCount; i++) { for (int i = 0; i < kpluginCount; i++) {
if (strcmp(pluginName, s_pluginNames[i]) == 0) { if (strcmp(name, s_pluginNames[i]) == 0) {
return s_pluginVersions[i]; return s_pluginVersions[i];
break; break;
} }

View File

@ -17,5 +17,8 @@
#pragma once #pragma once
// return plugin version map //! Get expected plugin version
const char* pluginVersion(const char* pluginName); /*!
Returns the plugin version expected by the plugin loader.
*/
const char* getExpectedPluginVersion(const char* name);

View File

@ -107,7 +107,7 @@ invoke(const char* command, void** args)
} }
} }
else if (strcmp(command, "version") == 0) { else if (strcmp(command, "version") == 0) {
return (void*)pluginVersion(kPluginName); return (void*)getExpectedPluginVersion(kPluginName);
} }
return NULL; return NULL;