| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <memory> |
| 4 |
#include <functional> |
| 5 |
#include <cassert> |
| 6 |
#include <sigc++/trackable.h> |
| 7 |
#include <sigc++/signal.h> |
| 8 |
#include <mutex> |
| 9 |
#include <stdexcept> |
| 10 |
|
| 11 |
#include <string> |
| 12 |
#include <set> |
| 13 |
#include <vector> |
| 14 |
|
| 15 |
#include "itextstream.h" |
| 16 |
#include "ilogwriter.h" |
| 17 |
|
| 18 |
/** |
| 19 |
* \defgroup module Module system |
| 20 |
*/ |
| 21 |
|
| 22 |
/** |
| 23 |
* greebo: Compatibility level: a number inlined into all the modules and returned |
| 24 |
* by their RegisterableModule::getCompatibilityLevel() method. |
| 25 |
* |
| 26 |
* This number should be changed each time the set of module/plugin files (.so/.dll/.dylib) |
| 27 |
* is modified, especially when files are going to be removed from a DarkRadiant release. |
| 28 |
* The number will be checked by the ModuleRegistry against the internally stored one |
| 29 |
* to detect outdated binaries and reject their registration. |
| 30 |
* |
| 31 |
* As long as no external module/plugin files are removed this number is safe to stay |
| 32 |
* as it is. Keep this number compatible to std::size_t, i.e. unsigned. |
| 33 |
*/ |
| 34 |
#define MODULE_COMPATIBILITY_LEVEL 20211014 |
| 35 |
|
| 36 |
// A function taking an error title and an error message string, invoked in debug builds |
| 37 |
// for things like ASSERT_MESSAGE and ERROR_MESSAGE |
| 38 |
typedef std::function<void (const std::string&, const std::string&)> ErrorHandlingFunction; |
| 39 |
|
| 40 |
// This method holds a function pointer which can do some error display (like popups) |
| 41 |
// Each module binary has its own copy of this, it's initialised in performDefaultInitialisation() |
| 42 |
inline ErrorHandlingFunction& GlobalErrorHandler() |
| 43 |
{ |
| 44 |
static ErrorHandlingFunction _func; |
| 45 |
return _func; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Provider for various information that may be required by modules during |
| 50 |
* initialisation. |
| 51 |
* |
| 52 |
* \ingroup module |
| 53 |
*/ |
| 54 |
class IApplicationContext |
| 55 |
{ |
| 56 |
public: |
| 57 |
|
| 58 |
/** |
| 59 |
* \brief |
| 60 |
* Argument list type. |
| 61 |
*/ |
| 62 |
typedef std::vector<std::string> ArgumentList; |
| 63 |
|
| 64 |
/** |
| 65 |
* Destructor |
| 66 |
*/ |
| 67 |
virtual ~IApplicationContext() {} |
| 68 |
|
| 69 |
/// Return the path to the installed application |
| 70 |
virtual std::string getApplicationPath() const = 0; |
| 71 |
|
| 72 |
/** |
| 73 |
* Return the application library paths, each of these is searched |
| 74 |
* for any libraries containing modules and/or plugins. |
| 75 |
* |
| 76 |
* On Windows this is most likely the same as the application path. On |
| 77 |
* Linux it might be a hard-coded path like /usr/lib/darkradiant, or a |
| 78 |
* relocatable relative path like ../lib |
| 79 |
*/ |
| 80 |
virtual std::vector<std::string> getLibraryPaths() const = 0; |
| 81 |
|
| 82 |
/** |
| 83 |
* Return the toplevel path contaning runtime data files, such as the GL |
| 84 |
* programs or UI descriptor files. On Windows this is the same as the |
| 85 |
* application path, on Linux it is determined at compile-time but probably |
| 86 |
* something like /usr/share/darkradiant. |
| 87 |
*/ |
| 88 |
virtual std::string getRuntimeDataPath() const = 0; |
| 89 |
|
| 90 |
/// Return the directory containing user settings (user.xml and friends) |
| 91 |
virtual std::string getSettingsPath() const = 0; |
| 92 |
|
| 93 |
/// Return the directory for temporary output files, such as logs |
| 94 |
virtual std::string getCacheDataPath() const = 0; |
| 95 |
|
| 96 |
/// Return the directory containing UI bitmaps |
| 97 |
virtual std::string getBitmapsPath() const = 0; |
| 98 |
|
| 99 |
/// Return the path to HTML documentation files |
| 100 |
virtual std::string getHTMLPath() const = 0; |
| 101 |
|
| 102 |
/** |
| 103 |
* \brief |
| 104 |
* Return the list of command line arguments. |
| 105 |
*/ |
| 106 |
virtual const ArgumentList& getCmdLineArgs() const = 0; |
| 107 |
|
| 108 |
/** |
| 109 |
* Retrieve a function pointer which can handle assertions and runtime errors |
| 110 |
*/ |
| 111 |
virtual const ErrorHandlingFunction& getErrorHandlingFunction() const = 0; |
| 112 |
}; |
| 113 |
|
| 114 |
/** |
| 115 |
* Set of strings for module dependencies. |
| 116 |
*/ |
| 117 |
typedef std::set<std::string> StringSet; |
| 118 |
|
| 119 |
/** |
| 120 |
* Interface class for modules. Each RegisterableModule can return its name and |
| 121 |
* its set of dependencies. |
| 122 |
* |
| 123 |
* Note that this interface does NOT concern itself with the type (interface) |
| 124 |
* of each individual module; it is up to the GlobalBlah() accessor function |
| 125 |
* associated with each module to perform the required downcast to the known |
| 126 |
* type. |
| 127 |
* |
| 128 |
* All RegisterableModules implement sigc::trackable, since they will often want |
| 129 |
* to connect themselves to another module's signal(s). |
| 130 |
* |
| 131 |
* \ingroup module |
| 132 |
*/ |
| 133 |
class RegisterableModule: public sigc::trackable |
| 134 |
{ |
| 135 |
private: |
| 136 |
const std::size_t _compatibilityLevel; |
| 137 |
|
| 138 |
public: |
| 139 |
RegisterableModule() : |
| 140 |
_compatibilityLevel(MODULE_COMPATIBILITY_LEVEL) |
| 141 |
{} |
| 142 |
|
| 143 |
// Modules are not copyable |
| 144 |
RegisterableModule(const RegisterableModule& other) = delete; |
| 145 |
RegisterableModule& operator=(const RegisterableModule& other) = delete; |
| 146 |
|
| 147 |
/** |
| 148 |
* Destructor |
| 149 |
*/ |
| 150 |
virtual ~RegisterableModule() {} |
| 151 |
|
| 152 |
/** |
| 153 |
* Return the name of this module. This must be globally unique across all |
| 154 |
* modules; the modulesystem will throw a logic_error if two modules attempt |
| 155 |
* to register themselves with the same name. |
| 156 |
*/ |
| 157 |
virtual const std::string& getName() const = 0; |
| 158 |
|
| 159 |
/** |
| 160 |
* Return the set of dependencies for this module. The return value is a |
| 161 |
* set of strings, each containing the unique name (as returned by |
| 162 |
* getName()) of a module which must be initialised before this one. |
| 163 |
*/ |
| 164 |
virtual const StringSet& getDependencies() const = 0; |
| 165 |
|
| 166 |
/** |
| 167 |
* Instruct this module to initialise itself. A RegisterableModule must NOT |
| 168 |
* invoke any calls to other modules in its constructor, since at the point |
| 169 |
* of construction the other modules will not have been initialised. Once |
| 170 |
* all of the dependencies are processed by the ModuleRegistry, each module |
| 171 |
* will have its initialiseModule() method called at the appropriate point. |
| 172 |
* |
| 173 |
* The ModuleRegistry guarantees that any modules named in the set of |
| 174 |
* dependencies returned by getDependencies() will be initialised and ready |
| 175 |
* for use at the time this method is invoked. Attempting to access a module |
| 176 |
* which was NOT listed as a dependency is undefined. |
| 177 |
* |
| 178 |
* @param ctx |
| 179 |
* The ApplicationContext of the running Radiant application. |
| 180 |
*/ |
| 181 |
virtual void initialiseModule(const IApplicationContext& ctx) = 0; |
| 182 |
|
| 183 |
/** |
| 184 |
* Optional shutdown routine. Allows the module to de-register itself, |
| 185 |
* shutdown windows, save stuff into the Registry and so on. |
| 186 |
* |
| 187 |
* All the modules are getting called one by one, all other modules |
| 188 |
* are available until the last shutdownModule() call was invoked. |
| 189 |
*/ |
| 190 |
virtual void shutdownModule() { |
| 191 |
// Empty default implementation |
| 192 |
} |
| 193 |
|
| 194 |
// Internally queried by the ModuleRegistry. To protect against leftover |
| 195 |
// binaries containing outdated moudles from being loaded and registered |
| 196 |
// the compatibility level is compared with the one in the ModuleRegistry. |
| 197 |
// Old modules with mismatching numbers will be rejected. |
| 198 |
// Function is intentionally non-virtual and inlined. |
| 199 |
std::size_t getCompatibilityLevel() const |
| 200 |
{ |
| 201 |
return _compatibilityLevel; |
| 202 |
} |
| 203 |
}; |
| 204 |
|
| 205 |
/** |
| 206 |
* Shared pointer typdef. |
| 207 |
*/ |
| 208 |
typedef std::shared_ptr<RegisterableModule> RegisterableModulePtr; |
| 209 |
|
| 210 |
/** |
| 211 |
* Interface for the module registry. This is the owner and manager of all |
| 212 |
* RegisterableModules defined in DLLs and the main binary. |
| 213 |
* |
| 214 |
* For obvious reasons, the ModuleRegistry itself is not a module, but a static |
| 215 |
* object owned by the core binary and returned through a globally-accessible |
| 216 |
* method. |
| 217 |
* |
| 218 |
* \ingroup module |
| 219 |
*/ |
| 220 |
class IModuleRegistry |
| 221 |
{ |
| 222 |
public: |
| 223 |
virtual ~IModuleRegistry() {} |
| 224 |
|
| 225 |
/** |
| 226 |
* Register a RegisterableModule. The name and dependencies are retrieved |
| 227 |
* through the appropriate RegisterableModule interface methods. |
| 228 |
* |
| 229 |
* This method does not cause the RegisterableModule to be initialised. |
| 230 |
*/ |
| 231 |
virtual void registerModule(const RegisterableModulePtr& module) = 0; |
| 232 |
|
| 233 |
/** |
| 234 |
* Initialise all of the modules previously registered with |
| 235 |
* registerModule() in the order required by their dependencies. This method |
| 236 |
* is invoked once, at application startup, with any subsequent attempts |
| 237 |
* to invoke this method throwing a logic_error. |
| 238 |
*/ |
| 239 |
virtual void loadAndInitialiseModules() = 0; |
| 240 |
|
| 241 |
/** |
| 242 |
* All the RegisterableModule::shutdownModule() routines are getting |
| 243 |
* called one by one. No modules are actually destroyed during the |
| 244 |
* iteration is ongoing, so each module is guaranteed to exist. |
| 245 |
* |
| 246 |
* After the last shutdownModule() call has been invoked, the modules |
| 247 |
* can be safely unloaded/destroyed. |
| 248 |
*/ |
| 249 |
virtual void shutdownModules() = 0; |
| 250 |
|
| 251 |
/** |
| 252 |
* Retrieve the module associated with the provided unique name. If the |
| 253 |
* named module is not found, an empty RegisterableModulePtr is returned |
| 254 |
* (this allows modules to be optional). |
| 255 |
* |
| 256 |
* Note that the return value of this function is RegisterableModulePtr, |
| 257 |
* which in itself is useless to application code. It is up to the accessor |
| 258 |
* functions defined in each module interface (e.g. GlobalEntityCreator()) |
| 259 |
* to downcast the pointer to the appropriate type. |
| 260 |
*/ |
| 261 |
virtual RegisterableModulePtr getModule(const std::string& name) const = 0; |
| 262 |
|
| 263 |
/** |
| 264 |
* Returns TRUE if the named module exists in the records. |
| 265 |
*/ |
| 266 |
virtual bool moduleExists(const std::string& name) const = 0; |
| 267 |
|
| 268 |
/** |
| 269 |
* This retrieves a reference to the information structure ApplicationContext, |
| 270 |
* which holds the AppPath, SettingsPath and references to the application |
| 271 |
* streams. The latter can be used by modules to initialise their |
| 272 |
* rMessage/globalErrorStreams(). |
| 273 |
*/ |
| 274 |
virtual const IApplicationContext& getApplicationContext() const = 0; |
| 275 |
|
| 276 |
/** |
| 277 |
* Callable during the module registration phase, this method attempts |
| 278 |
* to locate the default application log writer instance. It is usually |
| 279 |
* hosted in the IRadiant implementation, which is where this method |
| 280 |
* will attempt to look for. If the core module happens to be unavailable |
| 281 |
* at the time this method is called, a std::runtime_error will be thrown. |
| 282 |
*/ |
| 283 |
virtual applog::ILogWriter& getApplicationLogWriter() = 0; |
| 284 |
|
| 285 |
/** |
| 286 |
* Invoked when all modules have been initialised. |
| 287 |
*/ |
| 288 |
virtual sigc::signal<void>& signal_allModulesInitialised() = 0; |
| 289 |
|
| 290 |
/** |
| 291 |
* Progress function called during module loading and intialisation. |
| 292 |
* The string value will carry a message about what is currently in progress. |
| 293 |
* The float value passed to the signal indicates the overall progress and |
| 294 |
* will be in the range [0.0f..1.0f]. |
| 295 |
*/ |
| 296 |
typedef sigc::signal<void, const std::string&, float> ProgressSignal; |
| 297 |
virtual ProgressSignal& signal_moduleInitialisationProgress() = 0; |
| 298 |
|
| 299 |
/** |
| 300 |
* Invoked right before all modules are going to uninitialised, at the |
| 301 |
* beginning of the radiant shutdown phase. |
| 302 |
* This is a fire-once signal which removes all subscribers after firing. |
| 303 |
*/ |
| 304 |
virtual sigc::signal<void>& signal_modulesUninitialising() = 0; |
| 305 |
|
| 306 |
/** |
| 307 |
* Invoked when all modules have been shut down (i.e. after shutdownModule()). |
| 308 |
* This is a fire-once signal which removes all subscribers after firing. |
| 309 |
*/ |
| 310 |
virtual sigc::signal<void>& signal_allModulesUninitialised() = 0; |
| 311 |
|
| 312 |
/** |
| 313 |
* Invoked right before the module binaries will be unloaded, which will |
| 314 |
* trigger the destruction of any static instances in them. |
| 315 |
*/ |
| 316 |
virtual sigc::signal<void>& signal_modulesUnloading() = 0; |
| 317 |
|
| 318 |
// The compatibility level this Registry instance was compiled against. |
| 319 |
// Old module registrations will be rejected by the registry anyway, |
| 320 |
// on top of that they can actively query this number from the registry |
| 321 |
// to check whether they are being loaded into an incompatible binary. |
| 322 |
virtual std::size_t getCompatibilityLevel() const = 0; |
| 323 |
}; |
| 324 |
|
| 325 |
namespace module |
| 326 |
{ |
| 327 |
/** |
| 328 |
* \namespace module |
| 329 |
* Types and functions implementing the module registry system. |
| 330 |
* |
| 331 |
* \ingroup module |
| 332 |
*/ |
| 333 |
|
| 334 |
/** |
| 335 |
* greebo: A module-wide accessible container holding a registry reference. |
| 336 |
* The reference it holds has to be initialised in the RegisterModule() |
| 337 |
* routine of each .so/.dll binary. |
| 338 |
* Use GlobalModuleRegistry() to access the instance. |
| 339 |
*/ |
| 340 |
class RegistryReference { |
| 341 |
IModuleRegistry* _registry; |
| 342 |
public: |
| 343 |
RegistryReference() : |
| 344 |
_registry(nullptr) |
| 345 |
{} |
| 346 |
|
| 347 |
inline void setRegistry(IModuleRegistry& registry) |
| 348 |
{ |
| 349 |
_registry = ®istry; |
| 350 |
} |
| 351 |
|
| 352 |
inline IModuleRegistry& getRegistry() const |
| 353 |
{ |
| 354 |
assert(_registry); // must not be empty |
| 355 |
return *_registry; |
| 356 |
} |
| 357 |
|
| 358 |
inline bool isEmpty() const |
| 359 |
{ |
| 360 |
return _registry == nullptr; |
| 361 |
} |
| 362 |
|
| 363 |
static RegistryReference& Instance() |
| 364 |
{ |
| 365 |
static RegistryReference _registryRef; |
| 366 |
return _registryRef; |
| 367 |
} |
| 368 |
}; |
| 369 |
|
| 370 |
/** |
| 371 |
* Global accessor method for the ModuleRegistry. |
| 372 |
*/ |
| 373 |
inline IModuleRegistry& GlobalModuleRegistry() |
| 374 |
{ |
| 375 |
return RegistryReference::Instance().getRegistry(); |
| 376 |
} |
| 377 |
|
| 378 |
// Returns true if we have a registry instance known to this binary |
| 379 |
inline bool IsGlobalModuleRegistryAvailable() |
| 380 |
{ |
| 381 |
return !RegistryReference::Instance().isEmpty(); |
| 382 |
} |
| 383 |
|
| 384 |
// Reference container to hold the cached module references. |
| 385 |
// It automatically invalidates its reference as soon as the IModuleRegistry |
| 386 |
// un-initialises the registered modules. |
| 387 |
template<typename ModuleType> |
| 388 |
class InstanceReference |
| 389 |
{ |
| 390 |
private: |
| 391 |
const char* const _moduleName; |
| 392 |
ModuleType* _instancePtr; |
| 393 |
|
| 394 |
public: |
| 395 |
InstanceReference(const char* const moduleName) : |
| 396 |
_moduleName(moduleName), |
| 397 |
_instancePtr(nullptr) |
| 398 |
{ |
| 399 |
acquireReference(); |
| 400 |
} |
| 401 |
|
| 402 |
// Cast-operator used to access the module reference |
| 403 |
inline operator ModuleType&() |
| 404 |
{ |
| 405 |
// Check if we have an instance or if it is outdated |
| 406 |
if (_instancePtr == nullptr) |
| 407 |
{ |
| 408 |
acquireReference(); |
| 409 |
} |
| 410 |
|
| 411 |
return *_instancePtr; |
| 412 |
} |
| 413 |
|
| 414 |
private: |
| 415 |
void acquireReference() |
| 416 |
{ |
| 417 |
auto& registry = GlobalModuleRegistry(); |
| 418 |
|
| 419 |
_instancePtr = dynamic_cast<ModuleType*>(registry.getModule(_moduleName).get()); |
| 420 |
|
| 421 |
registry.signal_allModulesUninitialised().connect([this] |
| 422 |
{ |
| 423 |
_instancePtr = nullptr; |
| 424 |
}); |
| 425 |
} |
| 426 |
}; |
| 427 |
|
| 428 |
// Exception thrown if the module being loaded is incompatible with the main binary |
| 429 |
class ModuleCompatibilityException : |
| 430 |
public std::runtime_error |
| 431 |
{ |
| 432 |
public: |
| 433 |
ModuleCompatibilityException(const std::string& msg) : |
| 434 |
std::runtime_error(msg) |
| 435 |
{} |
| 436 |
}; |
| 437 |
|
| 438 |
// greebo: This should be called once by each module at load time to initialise |
| 439 |
// the OutputStreamHolders |
| 440 |
inline void initialiseStreams(applog::ILogWriter& logWriter) |
| 441 |
{ |
| 442 |
GlobalOutputStream().setStream(logWriter.getLogStream(applog::LogLevel::Standard)); |
| 443 |
GlobalWarningStream().setStream(logWriter.getLogStream(applog::LogLevel::Warning)); |
| 444 |
GlobalErrorStream().setStream(logWriter.getLogStream(applog::LogLevel::Error)); |
| 445 |
|
| 446 |
#ifndef NDEBUG |
| 447 |
GlobalDebugStream().setStream(logWriter.getLogStream(applog::LogLevel::Verbose)); |
| 448 |
#endif |
| 449 |
|
| 450 |
// Set up the mutex for thread-safe logging |
| 451 |
GlobalOutputStream().setLock(logWriter.getStreamLock()); |
| 452 |
GlobalWarningStream().setLock(logWriter.getStreamLock()); |
| 453 |
GlobalErrorStream().setLock(logWriter.getStreamLock()); |
| 454 |
GlobalDebugStream().setLock(logWriter.getStreamLock()); |
| 455 |
} |
| 456 |
|
| 457 |
// Should be called when shutting down the application to be sure that no dangling |
| 458 |
// references remain in the output stream holders |
| 459 |
inline void shutdownStreams() |
| 460 |
{ |
| 461 |
GlobalOutputStream().reset(); |
| 462 |
GlobalWarningStream().reset(); |
| 463 |
GlobalErrorStream().reset(); |
| 464 |
#ifndef NDEBUG |
| 465 |
GlobalDebugStream().reset(); |
| 466 |
#endif |
| 467 |
} |
| 468 |
|
| 469 |
// Helper method initialising a few references and checking a module's |
| 470 |
// compatibility level with the one reported by the ModuleRegistry |
| 471 |
inline void performDefaultInitialisation(IModuleRegistry& registry) |
| 472 |
{ |
| 473 |
if (registry.getCompatibilityLevel() != MODULE_COMPATIBILITY_LEVEL) |
| 474 |
{ |
| 475 |
throw ModuleCompatibilityException("Compatibility level mismatch"); |
| 476 |
} |
| 477 |
|
| 478 |
// Initialise the streams using the central application log writer instance |
| 479 |
initialiseStreams(registry.getApplicationLogWriter()); |
| 480 |
|
| 481 |
// Remember the reference to the ModuleRegistry |
| 482 |
RegistryReference::Instance().setRegistry(registry); |
| 483 |
|
| 484 |
// Set up the assertion handler |
| 485 |
GlobalErrorHandler() = registry.getApplicationContext().getErrorHandlingFunction(); |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
// Platform-specific definition which needs to be defined both |
| 490 |
// in the plugins and the main binary. |
| 491 |
#if defined(WIN32) |
| 492 |
#if defined(_MSC_VER) |
| 493 |
// In VC++ we use this to export symbols instead of using .def files |
| 494 |
// Note: don't use __stdcall since this is adding stack bytes to the function name |
| 495 |
#define DARKRADIANT_DLLEXPORT __declspec(dllexport) |
| 496 |
#else |
| 497 |
#define DARKRADIANT_DLLEXPORT |
| 498 |
#endif |
| 499 |
#elif defined(__APPLE__) |
| 500 |
#define DARKRADIANT_DLLEXPORT __attribute__((visibility("default"))) |
| 501 |
#else |
| 502 |
#define DARKRADIANT_DLLEXPORT __attribute__((visibility("default"))) |
| 503 |
#endif |