Skip to content

Commit

Permalink
Moved savegame name generation into World class and made it automatic…
Browse files Browse the repository at this point in the history
…, moved folder creation into World::save_state()
  • Loading branch information
Grumbel committed Aug 11, 2014
1 parent 8b24130 commit 4d8c468
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 49 deletions.
10 changes: 1 addition & 9 deletions src/supertux/game_manager.cpp
Expand Up @@ -43,7 +43,7 @@ GameManager::start_level(std::unique_ptr<World> world, int index)
{
m_world = std::move(world);

std::unique_ptr<Screen> screen(new GameSession(m_world->get_level_filename(index),
std::unique_ptr<Screen> screen(new GameSession(m_world->get_level_filename(index),
m_world->get_player_status()));
g_screen_manager->push_screen(std::move(screen));
}
Expand All @@ -55,16 +55,8 @@ GameManager::start_game(std::unique_ptr<World> world)

MenuManager::instance().clear_menu_stack();

std::string basename = m_world->get_basedir();
basename = basename.substr(0, basename.length()-1);
std::string worlddirname = FileSystem::basename(basename);
std::ostringstream stream;
stream << "profile" << g_config->profile << "/" << worlddirname << ".stsg";
std::string slotfile = stream.str();

try
{
m_world->set_savegame_filename(slotfile);
m_world->run();
}
catch(std::exception& e)
Expand Down
13 changes: 3 additions & 10 deletions src/supertux/menu/contrib_menu.cpp
Expand Up @@ -50,18 +50,11 @@ ContribMenu::ContribMenu() :
{
try
{
std::unique_ptr<World> world = World::load(*it + "/info");
std::unique_ptr<World> world = World::load(*it);

if (!world->hide_from_contribs())
{
{ // FIXME: yuck, this should be easier
std::ostringstream stream;
std::string worlddirname = FileSystem::basename(*it);
stream << "profile" << g_config->profile << "/" << worlddirname << ".stsg";
std::string slotfile = stream.str();
world->set_savegame_filename(stream.str());
world->load_state();
}
world->load_state();

std::ostringstream title;
title << world->get_title() << " (" << world->get_num_solved_levels() << "/" << world->get_num_levels() << ")";
Expand Down Expand Up @@ -97,7 +90,7 @@ ContribMenu::check_menu()
// so it might be ok
GameManager::current()->start_game(std::move(m_contrib_worlds[index]));
}
else
else
{
MenuManager::instance().push_menu(std::unique_ptr<Menu>(new ContribWorldMenu(std::move(m_contrib_worlds[index]))));
}
Expand Down
2 changes: 1 addition & 1 deletion src/supertux/menu/main_menu.cpp
Expand Up @@ -57,7 +57,7 @@ MainMenu::check_menu()
{
case MNID_STARTGAME:
{
std::unique_ptr<World> world = World::load("levels/world1/info");
std::unique_ptr<World> world = World::load("levels/world1");
GameManager::current()->start_game(std::move(world));
}
break;
Expand Down
61 changes: 35 additions & 26 deletions src/supertux/world.cpp
Expand Up @@ -21,21 +21,32 @@
#include "physfs/ifile_streambuf.hpp"
#include "scripting/serialize.hpp"
#include "scripting/squirrel_util.hpp"
#include "supertux/gameconfig.hpp"
#include "supertux/globals.hpp"
#include "supertux/player_status.hpp"
#include "supertux/screen_fade.hpp"
#include "supertux/screen_manager.hpp"
#include "supertux/player_status.hpp"
#include "supertux/world.hpp"
#include "util/file_system.hpp"
#include "util/reader.hpp"
#include "util/string_util.hpp"
#include "worldmap/worldmap.hpp"

std::unique_ptr<World>
World::load(const std::string& filename)
World::load(const std::string& directory)
{
std::unique_ptr<World> world(new World);
world->load_(filename);

world->load_(directory + "/info");

{ // generate savegame filename
std::string worlddirname = FileSystem::basename(directory);
std::ostringstream stream;
stream << "profile" << g_config->profile << "/" << worlddirname << ".stsg";
std::string slotfile = stream.str();
world->m_savegame_filename = stream.str();
}

return std::move(world);
}

Expand All @@ -59,29 +70,6 @@ World::~World()
sq_release(scripting::global_vm, &m_world_thread);
}

void
World::set_savegame_filename(const std::string& filename)
{
m_savegame_filename = filename;

// make sure the savegame directory exists
std::string dirname = FileSystem::dirname(filename);
if(!PHYSFS_exists(dirname.c_str())) {
if(!PHYSFS_mkdir(dirname.c_str())) {
std::ostringstream msg;
msg << "Couldn't create directory for savegames '"
<< dirname << "': " <<PHYSFS_getLastError();
throw std::runtime_error(msg.str());
}
}

if(!PHYSFS_isDirectory(dirname.c_str())) {
std::ostringstream msg;
msg << "Savegame path '" << dirname << "' is not a directory";
throw std::runtime_error(msg.str());
}
}

void
World::load_(const std::string& filename)
{
Expand Down Expand Up @@ -177,6 +165,27 @@ World::run()
void
World::save_state()
{
{ // make sure the savegame directory exists
std::string dirname = FileSystem::dirname(m_savegame_filename);
if(!PHYSFS_exists(dirname.c_str()))
{
if(!PHYSFS_mkdir(dirname.c_str()))
{
std::ostringstream msg;
msg << "Couldn't create directory for savegames '"
<< dirname << "': " <<PHYSFS_getLastError();
throw std::runtime_error(msg.str());
}
}

if(!PHYSFS_isDirectory(dirname.c_str()))
{
std::ostringstream msg;
msg << "Savegame path '" << dirname << "' is not a directory";
throw std::runtime_error(msg.str());
}
}

HSQUIRRELVM vm = scripting::global_vm;

lisp::Writer writer(m_savegame_filename);
Expand Down
9 changes: 6 additions & 3 deletions src/supertux/world.hpp
Expand Up @@ -34,13 +34,16 @@ class World : public Currenton<World>
void load_(const std::string& filename);

public:
static std::unique_ptr<World> load(const std::string& filename);
/**
Load a World
@param directory Directory containing the info file, e.g. "levels/world1"
*/
static std::unique_ptr<World> load(const std::string& directory);

public:
~World();

void set_savegame_filename(const std::string& filename);

void save_state();
void load_state();

Expand Down

0 comments on commit 4d8c468

Please sign in to comment.