Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#5231: Move DynamicLibrary to a static modulelib to be able to use it…
… in more than one binary. Add preliminary code to instantiate the core module from the main binary at startup.
- Loading branch information
1 parent
04681d3
commit 6e2305a
Showing
18 changed files
with
405 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#pragma once | ||
|
||
#include "imodule.h" | ||
#include "iradiant.h" | ||
#include "os/fs.h" | ||
|
||
#include "DynamicLibrary.h" | ||
|
||
#define SYMBOL_CREATE_RADIANT CreateRadiant | ||
#define SYMBOL_DESTROY_RADIANT DestroyRadiant | ||
#define Q(x) #x | ||
#define QUOTE(x) Q(x) | ||
|
||
namespace module | ||
{ | ||
|
||
class CoreModule | ||
{ | ||
private: | ||
typedef radiant::IRadiant* (*CreateRadiantFunc)(ApplicationContext& context); | ||
typedef void (*DestroyRadiantFunc)(radiant::IRadiant* radiant); | ||
|
||
radiant::IRadiant* _instance; | ||
|
||
DynamicLibraryPtr _coreModuleLibrary; | ||
|
||
public: | ||
class FailureException : | ||
public std::runtime_error | ||
{ | ||
public: | ||
FailureException(const std::string& msg) : | ||
std::runtime_error(msg) | ||
{} | ||
}; | ||
|
||
CoreModule(ApplicationContext& context) : | ||
_instance(nullptr) | ||
{ | ||
std::string coreModuleFile = std::string("DarkRadiantCore") + MODULE_FILE_EXTENSION; | ||
|
||
fs::path coreModulePath = context.getApplicationPath(); | ||
coreModulePath /= coreModuleFile; | ||
|
||
if (!fs::exists(coreModulePath)) | ||
{ | ||
throw FailureException("Cannot find the main module " + coreModuleFile); | ||
} | ||
|
||
_coreModuleLibrary = std::make_shared<DynamicLibrary>(coreModulePath.string()); | ||
|
||
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() | ||
{ | ||
destroy(); | ||
} | ||
|
||
radiant::IRadiant* get() | ||
{ | ||
return _instance; | ||
} | ||
|
||
private: | ||
void 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; | ||
} | ||
} | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include "iradiant.h" | ||
#include "module/CoreModule.h" | ||
|
||
namespace radiant | ||
{ | ||
|
||
class Radiant : | ||
public IRadiant | ||
{ | ||
private: | ||
ApplicationContext& _context; | ||
|
||
public: | ||
Radiant(ApplicationContext& context) : | ||
_context(context) | ||
{} | ||
}; | ||
|
||
} | ||
|
||
extern "C" DARKRADIANT_DLLEXPORT radiant::IRadiant* SYMBOL_CREATE_RADIANT(ApplicationContext& context) | ||
{ | ||
return new radiant::Radiant(context); | ||
} | ||
|
||
extern "C" DARKRADIANT_DLLEXPORT void SYMBOL_DESTROY_RADIANT(radiant::IRadiant* radiant) | ||
{ | ||
delete radiant; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.