Skip to content

Commit

Permalink
#5231: The core module is available as module itself when querying th…
Browse files Browse the repository at this point in the history
…e ModuleRegistry.

Plus some build order and events fixes.
  • Loading branch information
codereader committed Apr 30, 2020
1 parent 4f8e481 commit 26675d5
Show file tree
Hide file tree
Showing 22 changed files with 213 additions and 100 deletions.
29 changes: 24 additions & 5 deletions include/iradiant.h
Expand Up @@ -5,17 +5,25 @@
#include <sigc++/signal.h>
#include <functional>

const char* const MODULE_RADIANT("Radiant");
const char* const MODULE_RADIANT_APP("Radiant");
const char* const MODULE_RADIANT_CORE("RadiantCore");

namespace applog { class ILogWriter; }

namespace radiant
{

/**
* Main application host
* Main application host, offering access to the Module Registry
* and the logging infrastructure.
*
* As it implements the RegisterableModule interface, an instance
* can be acquired through the GlobalModuleRegistry at runtime.
* Module Registration doesn't need to be performed explicitly,
* the IRadiant module will register itself.
*/
class IRadiant
class IRadiant :
public RegisterableModule
{
public:
typedef std::shared_ptr<IRadiant> Ptr;
Expand All @@ -38,7 +46,7 @@ class IRadiant

/**
* \brief
* Interface to the core application.
* Interface to the radiant application.
*/
class IRadiantBase :
public RegisterableModule
Expand All @@ -52,12 +60,23 @@ class IRadiantBase :
virtual sigc::signal<void> signal_radiantShutdown() const = 0;
};

inline radiant::IRadiant& GlobalRadiantCore()
{
// Cache the reference locally
static radiant::IRadiant& _radiant(
*std::static_pointer_cast<radiant::IRadiant>(
module::GlobalModuleRegistry().getModule(MODULE_RADIANT_CORE)
)
);
return _radiant;
}

inline IRadiantBase& GlobalRadiant()
{
// Cache the reference locally
static IRadiantBase& _radiant(
*std::static_pointer_cast<IRadiantBase>(
module::GlobalModuleRegistry().getModule(MODULE_RADIANT)
module::GlobalModuleRegistry().getModule(MODULE_RADIANT_APP)
)
);
return _radiant;
Expand Down
2 changes: 1 addition & 1 deletion plugins/dm.editing/plugin.cpp
Expand Up @@ -38,7 +38,7 @@ class EditingModule :
_dependencies.insert(MODULE_SELECTIONSYSTEM);
_dependencies.insert(MODULE_COMMANDSYSTEM);
_dependencies.insert(MODULE_MAINFRAME);
_dependencies.insert(MODULE_RADIANT);
_dependencies.insert(MODULE_RADIANT_APP);
}

return _dependencies;
Expand Down
2 changes: 1 addition & 1 deletion plugins/dm.gui/plugin.cpp
Expand Up @@ -51,7 +51,7 @@ class GuiModule :
_dependencies.insert(MODULE_MAP);
_dependencies.insert(MODULE_OPENGL);
_dependencies.insert(MODULE_PREFERENCESYSTEM);
_dependencies.insert(MODULE_RADIANT);
_dependencies.insert(MODULE_RADIANT_APP);
_dependencies.insert(MODULE_RENDERSYSTEM);
_dependencies.insert(MODULE_SHADERSYSTEM);
_dependencies.insert(MODULE_UIMANAGER);
Expand Down
2 changes: 1 addition & 1 deletion plugins/script/ScriptingSystem.cpp
Expand Up @@ -404,7 +404,7 @@ const StringSet& ScriptingSystem::getDependencies() const

if (_dependencies.empty())
{
_dependencies.insert(MODULE_RADIANT);
_dependencies.insert(MODULE_RADIANT_APP);
_dependencies.insert(MODULE_COMMANDSYSTEM);
_dependencies.insert(MODULE_UIMANAGER);
_dependencies.insert(MODULE_EVENTMANAGER);
Expand Down
153 changes: 87 additions & 66 deletions radiant/Radiant.cpp
@@ -1,4 +1,4 @@
#include "iradiant.h"
#include "Radiant.h"

#include <iomanip>
#include "version.h"
Expand All @@ -21,98 +21,119 @@ namespace
const char* const TIME_FMT = "%Y-%m-%d %H:%M:%S";
}

class Radiant :
public IRadiant
Radiant::Radiant(ApplicationContext& context) :
_context(context)
{
private:
ApplicationContext& _context;
// Set the stream references for rMessage(), redirect std::cout, etc.
applog::LogStream::InitialiseStreams(getLogWriter());

std::unique_ptr<applog::LogFile> _logFile;
// Attach the logfile to the logwriter
createLogFile();

std::unique_ptr<module::ModuleRegistry> _moduleRegistry;

public:
Radiant(ApplicationContext& context) :
_context(context)
{
// Set the stream references for rMessage(), redirect std::cout, etc.
applog::LogStream::InitialiseStreams(getLogWriter());
_moduleRegistry.reset(new module::ModuleRegistry);
_moduleRegistry->setContext(_context);
}

// Attach the logfile to the logwriter
createLogFile();
Radiant::~Radiant()
{
_moduleRegistry.reset();

_moduleRegistry.reset(new module::ModuleRegistry);
_moduleRegistry->setContext(_context);
// Close the log file
if (_logFile)
{
_logFile->close();
getLogWriter().detach(_logFile.get());
_logFile.reset();
}

~Radiant()
{
_moduleRegistry.reset();
applog::LogStream::ShutdownStreams();
}

// Close the log file
if (_logFile)
{
_logFile->close();
getLogWriter().detach(_logFile.get());
_logFile.reset();
}
applog::ILogWriter& Radiant::getLogWriter()
{
return applog::LogWriter::Instance();
}

applog::LogStream::ShutdownStreams();
}
module::ModuleRegistry& Radiant::getModuleRegistry()
{
return *_moduleRegistry;
}

applog::ILogWriter& getLogWriter() override
{
return applog::LogWriter::Instance();
}
void Radiant::createLogFile()
{
_logFile.reset(new applog::LogFile(_context.getSettingsPath() + "darkradiant.log"));

IModuleRegistry& getModuleRegistry() override
if (_logFile->isOpen())
{
return *_moduleRegistry;
}
getLogWriter().attach(_logFile.get());

private:
void createLogFile()
{
_logFile.reset(new applog::LogFile(_context.getSettingsPath() + "darkradiant.log"));
rMessage() << "Started logging to " << _logFile->getFullPath() << std::endl;

if (_logFile->isOpen())
{
getLogWriter().attach(_logFile.get());
rMessage() << "This is " << RADIANT_APPNAME_FULL() << std::endl;

rMessage() << "Started logging to " << _logFile->getFullPath() << std::endl;
std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);

rMessage() << "This is " << RADIANT_APPNAME_FULL() << std::endl;
// Write timestamp and thread information
rMessage() << "Today is " << std::put_time(&tm, TIME_FMT) << std::endl;

std::time_t t = std::time(nullptr);
std::tm tm = *std::localtime(&t);
// Output the wxWidgets version to the logfile
std::string wxVersion = string::to_string(wxMAJOR_VERSION) + ".";
wxVersion += string::to_string(wxMINOR_VERSION) + ".";
wxVersion += string::to_string(wxRELEASE_NUMBER);

// Write timestamp and thread information
rMessage() << "Today is " << std::put_time(&tm, TIME_FMT) << std::endl;
rMessage() << "wxWidgets Version: " << wxVersion << std::endl;
}
else
{
rConsoleError() << "Failed to create log file '"
<< _logFile->getFullPath() << ", check write permissions in parent directory."
<< std::endl;
}
}

// Output the wxWidgets version to the logfile
std::string wxVersion = string::to_string(wxMAJOR_VERSION) + ".";
wxVersion += string::to_string(wxMINOR_VERSION) + ".";
wxVersion += string::to_string(wxRELEASE_NUMBER);
const std::string& Radiant::getName() const
{
static std::string _name(MODULE_RADIANT_CORE);
return _name;
}

rMessage() << "wxWidgets Version: " << wxVersion << std::endl;
}
else
{
rConsoleError() << "Failed to create log file '"
<< _logFile->getFullPath() << ", check write permissions in parent directory."
<< std::endl;
}
}
};
const StringSet& Radiant::getDependencies() const
{
static StringSet _dependencies;
return _dependencies;
}

void Radiant::initialiseModule(const ApplicationContext& ctx)
{}

std::shared_ptr<Radiant>& Radiant::InstancePtr()
{
static std::shared_ptr<Radiant> _instancePtr;
return _instancePtr;
}

}

extern "C" DARKRADIANT_DLLEXPORT radiant::IRadiant* SYMBOL_CREATE_RADIANT(ApplicationContext& context)
{
return new radiant::Radiant(context);
auto& instancePtr = radiant::Radiant::InstancePtr();

// Create a new instance, but ensure that this has only be called once
assert(!instancePtr);

instancePtr.reset(new radiant::Radiant(context));

// Add this module to the registry it's holding
instancePtr->getModuleRegistry().registerModule(instancePtr);
instancePtr->getModuleRegistry().initialiseCoreModule();

return instancePtr.get();
}

extern "C" DARKRADIANT_DLLEXPORT void SYMBOL_DESTROY_RADIANT(radiant::IRadiant* radiant)
{
delete radiant;
assert(radiant::Radiant::InstancePtr().get() == radiant);

radiant::Radiant::InstancePtr().reset();
}
39 changes: 39 additions & 0 deletions radiant/Radiant.h
@@ -0,0 +1,39 @@
#pragma once

#include "iradiant.h"
#include "modulesystem/ModuleRegistry.h"

namespace applog { class LogFile; }

namespace radiant
{

class Radiant :
public IRadiant
{
private:
ApplicationContext& _context;

std::unique_ptr<applog::LogFile> _logFile;

std::unique_ptr<module::ModuleRegistry> _moduleRegistry;

public:
Radiant(ApplicationContext& context);

~Radiant();

const std::string& getName() const override;
const StringSet& getDependencies() const override;
void initialiseModule(const ApplicationContext& ctx) override;

applog::ILogWriter& getLogWriter() override;
module::ModuleRegistry& getModuleRegistry() override;

static std::shared_ptr<Radiant>& InstancePtr();

private:
void createLogFile();
};

}
4 changes: 2 additions & 2 deletions radiant/RadiantModule.cpp
Expand Up @@ -54,7 +54,7 @@ void RadiantModule::broadcastStartupEvent()
// RegisterableModule implementation
const std::string& RadiantModule::getName() const
{
static std::string _name(MODULE_RADIANT);
static std::string _name(MODULE_RADIANT_APP);
return _name;
}

Expand Down Expand Up @@ -130,7 +130,7 @@ module::StaticModule<RadiantModule> radiantCoreModule;
RadiantModulePtr getGlobalRadiant()
{
return std::static_pointer_cast<RadiantModule>(
module::GlobalModuleRegistry().getModule(MODULE_RADIANT)
module::GlobalModuleRegistry().getModule(MODULE_RADIANT_APP)
);
}

Expand Down
2 changes: 1 addition & 1 deletion radiant/eventmanager/MouseToolManager.cpp
Expand Up @@ -29,7 +29,7 @@ const StringSet& MouseToolManager::getDependencies() const

if (_dependencies.empty())
{
_dependencies.insert(MODULE_RADIANT);
_dependencies.insert(MODULE_RADIANT_APP);
_dependencies.insert(MODULE_UIMANAGER);
}

Expand Down
2 changes: 1 addition & 1 deletion radiant/map/AutoSaver.cpp
Expand Up @@ -379,7 +379,7 @@ const StringSet& AutoMapSaver::getDependencies() const
_dependencies.insert(MODULE_PREFERENCESYSTEM);
_dependencies.insert(MODULE_XMLREGISTRY);
_dependencies.insert(MODULE_MAINFRAME);
_dependencies.insert(MODULE_RADIANT);
_dependencies.insert(MODULE_RADIANT_APP);
}

return _dependencies;
Expand Down
2 changes: 1 addition & 1 deletion radiant/map/Map.cpp
Expand Up @@ -796,7 +796,7 @@ const StringSet& Map::getDependencies() const

if (_dependencies.empty())
{
_dependencies.insert(MODULE_RADIANT);
_dependencies.insert(MODULE_RADIANT_APP);
_dependencies.insert(MODULE_GAMEMANAGER);
_dependencies.insert(MODULE_SCENEGRAPH);
_dependencies.insert(MODULE_MAPINFOFILEMANAGER);
Expand Down

0 comments on commit 26675d5

Please sign in to comment.