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;
public:
NzDynLib(const NzString& libraryPath);
NzDynLib();
~NzDynLib();
NzString GetLastError() const;
NzDynLibFunc GetSymbol(const NzString& symbol) const;
bool Load();
bool IsLoaded() const;
bool Load(const NzString& libraryPath, bool appendExtension = true);
void Unload();
private:
void SetLastError(const NzString& error);
NazaraMutexAttrib(m_mutex, mutable)
NzString m_lastError;
NzString m_path;
mutable NzString m_lastError;
NzDynLibImpl* m_impl;
};

View File

@ -22,7 +22,7 @@ class NAZARA_API NzPluginManager
static bool Initialize();
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);

View File

@ -16,8 +16,7 @@
#include <Nazara/Core/Debug.hpp>
NzDynLib::NzDynLib(const NzString& libraryPath) :
m_path(libraryPath),
NzDynLib::NzDynLib() :
m_impl(nullptr)
{
}
@ -46,17 +45,22 @@ NzDynLibFunc NzDynLib::GetSymbol(const NzString& symbol) const
}
#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)
Unload();
m_impl = new NzDynLibImpl(this);
if (!m_impl->Load(m_path))
if (!m_impl->Load(libraryPath, appendExtension, &m_lastError))
{
delete m_impl;
m_impl = nullptr;
@ -78,10 +82,3 @@ void NzDynLib::Unload()
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]);
}
bool NzPluginManager::Mount(const NzString& pluginPath)
bool NzPluginManager::Mount(const NzString& pluginPath, bool appendExtension)
{
NzString path = pluginPath;
if (!path.EndsWith(NAZARA_DYNLIB_EXTENSION))
if (appendExtension && !path.EndsWith(NAZARA_DYNLIB_EXTENSION))
path += NAZARA_DYNLIB_EXTENSION;
bool exists = false;
@ -93,8 +93,8 @@ bool NzPluginManager::Mount(const NzString& pluginPath)
return false;
}
std::unique_ptr<NzDynLib> library(new NzDynLib(path));
if (!library->Load())
std::unique_ptr<NzDynLib> library(new NzDynLib);
if (!library->Load(path, false))
{
NazaraError("Failed to load plugin");
return false;

View File

@ -9,34 +9,34 @@
#include <memory>
#include <Nazara/Core/Debug.hpp>
NzDynLibImpl::NzDynLibImpl(NzDynLib* parent) :
m_parent(parent)
NzDynLibImpl::NzDynLibImpl(NzDynLib* 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()));
if (!sym)
m_parent->SetLastError(NzError::GetLastSystemError());
*errorMessage = NzError::GetLastSystemError();
return sym;
}
bool NzDynLibImpl::Load(const NzString& libraryPath)
bool NzDynLibImpl::Load(const NzString& libraryPath, bool appendExtension, NzString* errorMessage)
{
NzString path = libraryPath;
if (!path.EndsWith(".dll"))
if (appendExtension && !path.EndsWith(".dll"))
path += ".dll";
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)
return true;
else
{
m_parent->SetLastError(NzError::GetLastSystemError());
*errorMessage = NzError::GetLastSystemError();
return false;
}
}

View File

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