Modified DynLib/PluginManager interface

Former-commit-id: 2dae56cad852a46c9bac828cc7775a4da42c96cf
This commit is contained in:
Lynix 2014-01-05 15:30:27 +01:00
parent 3625713914
commit 3dd8b8fce1
6 changed files with 30 additions and 34 deletions

View File

@ -36,21 +36,21 @@ class NzDynLib : NzNonCopyable
friend NzDynLibImpl; friend NzDynLibImpl;
public: public:
NzDynLib(const NzString& libraryPath); NzDynLib();
~NzDynLib(); ~NzDynLib();
NzString GetLastError() const; NzString GetLastError() const;
NzDynLibFunc GetSymbol(const NzString& symbol) const; NzDynLibFunc GetSymbol(const NzString& symbol) const;
bool Load();
bool IsLoaded() const;
bool Load(const NzString& libraryPath, bool appendExtension = true);
void Unload(); void Unload();
private: private:
void SetLastError(const NzString& error);
NazaraMutexAttrib(m_mutex, mutable) NazaraMutexAttrib(m_mutex, mutable)
NzString m_lastError; mutable NzString m_lastError;
NzString m_path;
NzDynLibImpl* m_impl; NzDynLibImpl* m_impl;
}; };

View File

@ -22,7 +22,7 @@ class NAZARA_API NzPluginManager
static bool Initialize(); static bool Initialize();
static bool Mount(nzPlugin plugin); static bool Mount(nzPlugin plugin);
static bool Mount(const NzString& pluginPath); static bool Mount(const NzString& pluginPath, bool appendExtension = true);
static void RemoveDirectory(const NzString& directoryPath); static void RemoveDirectory(const NzString& directoryPath);

View File

@ -16,8 +16,7 @@
#include <Nazara/Core/Debug.hpp> #include <Nazara/Core/Debug.hpp>
NzDynLib::NzDynLib(const NzString& libraryPath) : NzDynLib::NzDynLib() :
m_path(libraryPath),
m_impl(nullptr) m_impl(nullptr)
{ {
} }
@ -46,17 +45,22 @@ NzDynLibFunc NzDynLib::GetSymbol(const NzString& symbol) const
} }
#endif #endif
return m_impl->GetSymbol(symbol); return m_impl->GetSymbol(symbol, &m_lastError);
} }
bool NzDynLib::Load() bool NzDynLib::IsLoaded() const
{
return m_impl != nullptr;
}
bool NzDynLib::Load(const NzString& libraryPath, bool appendExtension)
{ {
NazaraLock(m_mutex) NazaraLock(m_mutex)
Unload(); Unload();
m_impl = new NzDynLibImpl(this); m_impl = new NzDynLibImpl(this);
if (!m_impl->Load(m_path)) if (!m_impl->Load(libraryPath, appendExtension, &m_lastError))
{ {
delete m_impl; delete m_impl;
m_impl = nullptr; m_impl = nullptr;
@ -78,10 +82,3 @@ void NzDynLib::Unload()
m_impl = nullptr; m_impl = nullptr;
} }
} }
void NzDynLib::SetLastError(const NzString& error)
{
NazaraLock(m_mutex)
m_lastError = error;
}

View File

@ -58,10 +58,10 @@ bool NzPluginManager::Mount(nzPlugin plugin)
return Mount(s_pluginFiles[plugin]); return Mount(s_pluginFiles[plugin]);
} }
bool NzPluginManager::Mount(const NzString& pluginPath) bool NzPluginManager::Mount(const NzString& pluginPath, bool appendExtension)
{ {
NzString path = pluginPath; NzString path = pluginPath;
if (!path.EndsWith(NAZARA_DYNLIB_EXTENSION)) if (appendExtension && !path.EndsWith(NAZARA_DYNLIB_EXTENSION))
path += NAZARA_DYNLIB_EXTENSION; path += NAZARA_DYNLIB_EXTENSION;
bool exists = false; bool exists = false;
@ -93,8 +93,8 @@ bool NzPluginManager::Mount(const NzString& pluginPath)
return false; return false;
} }
std::unique_ptr<NzDynLib> library(new NzDynLib(path)); std::unique_ptr<NzDynLib> library(new NzDynLib);
if (!library->Load()) if (!library->Load(path, false))
{ {
NazaraError("Failed to load plugin"); NazaraError("Failed to load plugin");
return false; return false;

View File

@ -9,34 +9,34 @@
#include <memory> #include <memory>
#include <Nazara/Core/Debug.hpp> #include <Nazara/Core/Debug.hpp>
NzDynLibImpl::NzDynLibImpl(NzDynLib* parent) : NzDynLibImpl::NzDynLibImpl(NzDynLib* parent)
m_parent(parent)
{ {
NazaraUnused(parent);
} }
NzDynLibFunc NzDynLibImpl::GetSymbol(const NzString& symbol) const NzDynLibFunc NzDynLibImpl::GetSymbol(const NzString& symbol, NzString* errorMessage) const
{ {
NzDynLibFunc sym = reinterpret_cast<NzDynLibFunc>(GetProcAddress(m_handle, symbol.GetConstBuffer())); NzDynLibFunc sym = reinterpret_cast<NzDynLibFunc>(GetProcAddress(m_handle, symbol.GetConstBuffer()));
if (!sym) if (!sym)
m_parent->SetLastError(NzError::GetLastSystemError()); *errorMessage = NzError::GetLastSystemError();
return sym; return sym;
} }
bool NzDynLibImpl::Load(const NzString& libraryPath) bool NzDynLibImpl::Load(const NzString& libraryPath, bool appendExtension, NzString* errorMessage)
{ {
NzString path = libraryPath; NzString path = libraryPath;
if (!path.EndsWith(".dll")) if (appendExtension && !path.EndsWith(".dll"))
path += ".dll"; path += ".dll";
std::unique_ptr<wchar_t[]> wPath(path.GetWideBuffer()); std::unique_ptr<wchar_t[]> wPath(path.GetWideBuffer());
m_handle = LoadLibraryExW(wPath.get(), nullptr, LOAD_WITH_ALTERED_SEARCH_PATH); m_handle = LoadLibraryW(wPath.get());
if (m_handle) if (m_handle)
return true; return true;
else else
{ {
m_parent->SetLastError(NzError::GetLastSystemError()); *errorMessage = NzError::GetLastSystemError();
return false; return false;
} }
} }

View File

@ -19,13 +19,12 @@ class NzDynLibImpl : NzNonCopyable
NzDynLibImpl(NzDynLib* m_parent); NzDynLibImpl(NzDynLib* m_parent);
~NzDynLibImpl() = default; ~NzDynLibImpl() = default;
NzDynLibFunc GetSymbol(const NzString& symbol) const; NzDynLibFunc GetSymbol(const NzString& symbol, NzString* errorMessage) const;
bool Load(const NzString& libraryPath); bool Load(const NzString& libraryPath, bool appendExtension, NzString* errorMessage);
void Unload(); void Unload();
private: private:
HMODULE m_handle; HMODULE m_handle;
NzDynLib* m_parent;
}; };
#endif // NAZARA_DYNLIBIMPL_HPP #endif // NAZARA_DYNLIBIMPL_HPP