1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#include "CoreModule.h"
#include "DynamicLibrary.h"
#include "string/join.h"
// In Linux the CORE_MODULE_LIBRARY symbol is defined in config.h
#ifdef HAVE_CONFIG_H
#include <config.h>
#else
#define CORE_MODULE_LIBRARY "DarkRadiantCore"
#endif
namespace module
{
CoreModule::CoreModule(IApplicationContext& context) :
_instance(nullptr)
{
auto coreModulePath = findCoreModule(context);
_coreModuleLibrary.reset(new DynamicLibrary(coreModulePath));
if (_coreModuleLibrary->failed())
{
throw FailureException("Cannot load the main module " + _coreModuleLibrary->getName());
}
auto symbol = _coreModuleLibrary->findSymbol(QUOTE(SYMBOL_CREATE_RADIANT));
if (symbol == nullptr)
{
throw FailureException("Main module " + _coreModuleLibrary->getName() +
" doesn't expose the symbol " + QUOTE(SYMBOL_CREATE_RADIANT));
}
auto createFunc = reinterpret_cast<CreateRadiantFunc>(symbol);
_instance = createFunc(context);
}
CoreModule::~CoreModule()
{
destroy();
}
std::string CoreModule::Filename()
{
return std::string(CORE_MODULE_LIBRARY) + MODULE_FILE_EXTENSION;
}
std::string CoreModule::findCoreModule(IApplicationContext& context)
{
auto libraryPaths = context.getLibraryPaths();
for (auto libPath : libraryPaths)
{
fs::path coreModulePath = libPath;
coreModulePath /= Filename();
if (fs::exists(coreModulePath))
{
return coreModulePath.string();
}
}
throw FailureException("Cannot find the main module in any of the paths: " +
string::join(libraryPaths, "; "));
}
radiant::IRadiant* CoreModule::get()
{
return _instance;
}
void CoreModule::destroy()
{
if (_instance)
{
assert(_coreModuleLibrary);
auto symbol = _coreModuleLibrary->findSymbol(QUOTE(SYMBOL_DESTROY_RADIANT));
if (symbol == nullptr)
{
throw FailureException("Main module " + _coreModuleLibrary->getName() +
" doesn't expose the symbol " + QUOTE(SYMBOL_DESTROY_RADIANT));
}
auto destroyFunc = reinterpret_cast<DestroyRadiantFunc>(symbol);
destroyFunc(_instance);
_instance = nullptr;
}
}
}
|