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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
#include "ModuleRegistry.h"
#include "i18n.h"
#include "iradiant.h"
#include "itextstream.h"
#include <stdexcept>
#include <iostream>
#include "ModuleLoader.h"
#include <fmt/format.h>
namespace module
{
ModuleRegistry::ModuleRegistry(const IApplicationContext& ctx) :
_context(ctx),
_modulesInitialised(false),
_modulesShutdown(false),
_loader(new ModuleLoader(*this))
{
rMessage() << "ModuleRegistry instantiated." << std::endl;
// Initialise the Reference in the GlobalModuleRegistry() accessor.
RegistryReference::Instance().setRegistry(*this);
}
ModuleRegistry::~ModuleRegistry()
{
// The modules map might be non-empty if the app is failing during very
// early startup stages, and unloadModules() might not have been called yet.
// Some modules might need to call this instance during their own destruction,
// so it's better not to rely on the shared_ptr to destruct them.
unloadModules();
}
void ModuleRegistry::unloadModules()
{
_uninitialisedModules.clear();
// greebo: It's entirely possible that the clear() method will clear the
// last shared_ptr of a module. Module might still call this class' moduleExists()
// method which in turn refers to a semi-destructed ModulesMap instance.
// So, copy the contents to a temporary map before clearing it out.
ModulesMap tempMap;
tempMap.swap(_initialisedModules);
tempMap.clear();
// Send out the signal that the DLLs/SOs will be unloaded
signal_modulesUnloading().emit();
signal_modulesUnloading().clear();
_loader->unloadModules();
}
void ModuleRegistry::registerModule(const RegisterableModulePtr& module)
{
assert(module); // don't take NULL module pointers
if (_modulesInitialised)
{
// The train has left, this module is registered too late
throw std::logic_error(
"ModuleRegistry: module " + module->getName() +
" registered after initialisation."
);
}
// Check the compatibility level of this module against our internal one
if (module->getCompatibilityLevel() != getCompatibilityLevel())
{
rError() << "ModuleRegistry: Incompatible module rejected: " << module->getName() <<
" (module level: " << module->getCompatibilityLevel() << ", registry level: " <<
getCompatibilityLevel() << ")" << std::endl;
return;
}
// Add this module to the list of uninitialised ones
std::pair<ModulesMap::iterator, bool> result = _uninitialisedModules.insert(
ModulesMap::value_type(module->getName(), module)
);
// Don't allow modules with the same name being added twice
if (!result.second)
{
throw std::logic_error(
"ModuleRegistry: multiple modules named " + module->getName()
);
}
rMessage() << "Module registered: " << module->getName() << std::endl;
}
// Initialise the module (including dependencies, if necessary)
void ModuleRegistry::initialiseModuleRecursive(const std::string& name)
{
// Check if the module is already initialised
if (_initialisedModules.find(name) != _initialisedModules.end())
{
return;
}
// Check if the module exists at all
if (_uninitialisedModules.find(name) == _uninitialisedModules.end())
{
throw std::logic_error("ModuleRegistry: Module doesn't exist: " + name);
}
// Tag this module as "ready" by moving it into the initialised list.
RegisterableModulePtr module = _initialisedModules.emplace(name, _uninitialisedModules[name]).first->second;
const StringSet& dependencies = module->getDependencies();
// Debug builds should ensure that the dependencies don't reference the
// module itself directly
assert(dependencies.find(module->getName()) == dependencies.end());
// Initialise the dependencies first
for (const std::string& namedDependency : dependencies)
{
initialiseModuleRecursive(namedDependency);
}
_progress = 0.1f + (static_cast<float>(_initialisedModules.size())/_uninitialisedModules.size())*0.9f;
_sigModuleInitialisationProgress.emit(
fmt::format(_("Initialising Module: {0}"), module->getName()),
_progress);
// Initialise the module itself, now that the dependencies are ready
module->initialiseModule(_context);
}
void ModuleRegistry::initialiseCoreModule()
{
std::string coreModuleName = MODULE_RADIANT_CORE;
auto moduleIter = _uninitialisedModules.find(coreModuleName);
assert(moduleIter != _uninitialisedModules.end());
assert(_initialisedModules.find(coreModuleName) == _initialisedModules.end());
// Tag this module as "ready" by inserting it into the initialised list.
moduleIter = _initialisedModules.emplace(moduleIter->second->getName(), moduleIter->second).first;
// We assume that the core module doesn't have any dependencies
assert(moduleIter->second->getDependencies().empty());
moduleIter->second->initialiseModule(_context);
_uninitialisedModules.erase(coreModuleName);
}
void ModuleRegistry::loadAndInitialiseModules()
{
if (_modulesInitialised)
{
throw std::runtime_error("ModuleRegistry::initialiseModule called twice.");
}
_sigModuleInitialisationProgress.emit(_("Searching for Modules"), 0.0f);
rMessage() << "ModuleRegistry Compatibility Level is " << getCompatibilityLevel() << std::endl;
// Invoke the ModuleLoad routine to load the DLLs from modules/ and plugins/
auto libraryPaths = _context.getLibraryPaths();
for (auto path : libraryPaths)
{
_loader->loadModulesFromPath(path);
}
_progress = 0.1f;
_sigModuleInitialisationProgress.emit(_("Initialising Modules"), _progress);
for (ModulesMap::iterator i = _uninitialisedModules.begin();
i != _uninitialisedModules.end(); ++i)
{
// greebo: Dive into the recursion
// (this will return immediately if the module is already initialised).
initialiseModuleRecursive(i->first);
}
_uninitialisedModules.clear();
// Make sure this isn't called again
_modulesInitialised = true;
_progress = 1.0f;
_sigModuleInitialisationProgress.emit(_("Modules initialised"), _progress);
// Fire the signal now, this will destroy the Splash dialog as well
// This event only happens once, release the listeners afterwards
_sigAllModulesInitialised.emit();
_sigAllModulesInitialised.clear();
}
void ModuleRegistry::shutdownModules()
{
if (_modulesShutdown)
{
throw std::logic_error("ModuleRegistry: shutdownModules called twice.");
}
_sigModulesUninitialising.emit();
_sigModulesUninitialising.clear();
for (ModulesMap::value_type& pair : _initialisedModules)
{
pair.second->shutdownModule();
}
// Fire the signal before unloading the modules, clear the listeners afterwards
_sigAllModulesUninitialised.emit();
_sigAllModulesUninitialised.clear();
// Free all the shared ptrs
unloadModules();
_modulesShutdown = true;
}
bool ModuleRegistry::moduleExists(const std::string& name) const
{
// Try to find the initialised module, uninitialised don't count as existing
return _initialisedModules.find(name) != _initialisedModules.end();
}
// Get the module
RegisterableModulePtr ModuleRegistry::getModule(const std::string& name) const {
// The return value (NULL) by default
RegisterableModulePtr returnValue;
// Try to find the module
ModulesMap::const_iterator found = _initialisedModules.find(name);
if (found != _initialisedModules.end())
{
returnValue = found->second;
}
if (!returnValue)
{
rConsoleError() << "ModuleRegistry: Warning! Module with name "
<< name << " requested but not found!" << std::endl;
}
return returnValue;
}
const IApplicationContext& ModuleRegistry::getApplicationContext() const
{
return _context;
}
applog::ILogWriter& ModuleRegistry::getApplicationLogWriter()
{
auto moduleIter = _initialisedModules.find(MODULE_RADIANT_CORE);
if (moduleIter == _initialisedModules.end())
{
throw std::runtime_error("Core module not available.");
}
auto coreModule = std::dynamic_pointer_cast<radiant::IRadiant>(moduleIter->second);
assert(coreModule);
return coreModule->getLogWriter();
}
sigc::signal<void>& ModuleRegistry::signal_allModulesInitialised()
{
return _sigAllModulesInitialised;
}
ModuleRegistry::ProgressSignal& ModuleRegistry::signal_moduleInitialisationProgress()
{
return _sigModuleInitialisationProgress;
}
sigc::signal<void>& ModuleRegistry::signal_modulesUninitialising()
{
return _sigModulesUninitialising;
}
sigc::signal<void>& ModuleRegistry::signal_allModulesUninitialised()
{
return _sigAllModulesUninitialised;
}
sigc::signal<void>& ModuleRegistry::signal_modulesUnloading()
{
return _sigModulesUnloading;
}
std::size_t ModuleRegistry::getCompatibilityLevel() const
{
return MODULE_COMPATIBILITY_LEVEL;
}
std::string ModuleRegistry::getModuleList(const std::string& separator)
{
std::string returnValue;
for (ModulesMap::value_type& pair : _initialisedModules)
{
returnValue += (returnValue.empty()) ? "" : separator;
returnValue += pair.first;
}
return returnValue;
}
} // namespace module
|