Skip to content

Commit

Permalink
Fix #4471: DarkRadiant crashes when "Load last map" option is active
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Jan 29, 2017
1 parent 4f855fa commit 4d06e9d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
51 changes: 36 additions & 15 deletions radiant/map/StartupMapLoader.cpp
@@ -1,6 +1,8 @@
#include "StartupMapLoader.h"

#include "imodule.h"
#include "igl.h"
#include "irender.h"
#include "iregistry.h"
#include "Map.h"
#include "ui/mru/MRU.h"
Expand All @@ -10,7 +12,8 @@
#include "os/file.h"
#include <boost/filesystem.hpp>

namespace map {
namespace map
{

void StartupMapLoader::onRadiantStartup()
{
Expand All @@ -20,13 +23,8 @@ void StartupMapLoader::onRadiantStartup()
module::ModuleRegistry::Instance().getApplicationContext().getCmdLineArgs()
);

for (ApplicationContext::ArgumentList::const_iterator i = args.begin();
i != args.end();
++i)
for (const std::string& candidate : args)
{
// Investigate the i-th argument
std::string candidate = *i;

if (os::getExtension(candidate) != "map") continue;

// We have a map file, check if it exists (and where)
Expand All @@ -35,34 +33,57 @@ void StartupMapLoader::onRadiantStartup()
boost::filesystem::path fullMapPath = mapsPath / candidate;

// First, look in the regular maps path
if (boost::filesystem::exists(fullMapPath)) {
if (boost::filesystem::exists(fullMapPath))
{
mapToLoad = fullMapPath.string();
break;
}

// Second, check for mod-relative paths
fullMapPath = mapsPath.remove_leaf().remove_leaf() / candidate;

if (boost::filesystem::exists(fullMapPath)) {
if (boost::filesystem::exists(fullMapPath))
{
mapToLoad = fullMapPath.string();
break;
}
}

if (!mapToLoad.empty()) {
GlobalMap().load(mapToLoad);
if (!mapToLoad.empty())
{
loadMapSafe(mapToLoad);
}
else {
else
{
std::string lastMap = GlobalMRU().getLastMapName();
if (GlobalMRU().loadLastMap() && !lastMap.empty() && os::fileOrDirExists(lastMap)) {
GlobalMap().load(lastMap);

if (GlobalMRU().loadLastMap() && !lastMap.empty() && os::fileOrDirExists(lastMap))
{
loadMapSafe(lastMap);
}
else {
else
{
GlobalMap().createNew();
}
}
}

void StartupMapLoader::loadMapSafe(const std::string& mapToLoad)
{
// Check if we have a valid openGL context, otherwise postpone the load
if (GlobalOpenGL().wxContextValid())
{
GlobalMap().load(mapToLoad);
return;
}

// No valid context, subscribe to the extensionsInitialised signal
GlobalRenderSystem().signal_extensionsInitialised().connect([mapToLoad]()
{
GlobalMap().load(mapToLoad);
});
}

void StartupMapLoader::onRadiantShutdown()
{
GlobalMRU().saveRecentFiles();
Expand Down
3 changes: 3 additions & 0 deletions radiant/map/StartupMapLoader.h
Expand Up @@ -13,6 +13,9 @@ class StartupMapLoader: public sigc::trackable

// Called when the mainframe shuts down
void onRadiantShutdown();

private:
void loadMapSafe(const std::string& map);
};
typedef std::shared_ptr<StartupMapLoader> StartupMapLoaderPtr;

Expand Down

0 comments on commit 4d06e9d

Please sign in to comment.