For this, implemented Folders::file_exists to check for the existence
of a given file, so that we can exit gracefully before raising a FileNotFound
exception which then closes the game.
The console and stderr both display an error message.
Part of #23.
void ch_map(const char *args)
{
void ch_map(const char *args)
{
+ if (!LoadLevel(args)) {
+ // FIXME: Reduce code duplication with GameTick (should come from a Console class)
+ for (int k = 14; k >= 1; k--) {
+ consoletext[k] = consoletext[k - 1];
+ }
+ consoletext[0] = std::string("Could not load the requested level '") + args + "', aborting.";
+ consoleselected = 0;
+ }
whichlevel = -2;
campaign = 0;
}
whichlevel = -2;
campaign = 0;
}
int DrawGLScene(StereoSide side);
void playdialoguescenesound();
int findClosestPlayer();
int DrawGLScene(StereoSide side);
void playdialoguescenesound();
int findClosestPlayer();
-void Loadlevel(int which);
-void Loadlevel(const std::string& name, bool tutorial = false);
+bool LoadLevel(int which);
+bool LoadLevel(const std::string& name, bool tutorial = false);
void Tick();
void TickOnce();
void TickOnceAfter();
void Tick();
void TickOnce();
void TickOnceAfter();
texdetail = temptexdetail;
}
texdetail = temptexdetail;
}
-void Game::Loadlevel(int which)
+bool Game::LoadLevel(int which)
{
stealthloading = 0;
whichlevel = which;
if (which == -1) {
{
stealthloading = 0;
whichlevel = which;
if (which == -1) {
- Loadlevel("tutorial", true);
+ return LoadLevel("tutorial", true);
} else if (which >= 0 && which <= 15) {
char buf[32];
snprintf(buf, 32, "map%d", which + 1); // challenges
} else if (which >= 0 && which <= 15) {
char buf[32];
snprintf(buf, 32, "map%d", which + 1); // challenges
+ return LoadLevel("mapsave");
-void Game::Loadlevel(const std::string& name, bool tutorial)
+bool Game::LoadLevel(const std::string& name, bool tutorial)
+ const std::string level_path = Folders::getResourcePath("Maps/" + name);
+ if (!Folders::file_exists(level_path)) {
+ perror(std::string("LoadLevel: Could not open file '" + level_path).c_str());
+ return false;
+ }
+
int indemo; // FIXME this should be removed
int templength;
float lamefloat;
int indemo; // FIXME this should be removed
int templength;
float lamefloat;
int mapvers;
FILE *tfile;
errno = 0;
int mapvers;
FILE *tfile;
errno = 0;
- tfile = Folders::openMandatoryFile( Folders::getResourcePath("Maps/"+name), "rb" );
+ tfile = Folders::openMandatoryFile(level_path, "rb");
pause_sound(stream_firesound);
scoreadded = 0;
pause_sound(stream_firesound);
scoreadded = 0;
leveltime = 0;
wonleveltime = 0;
visibleloading = false;
leveltime = 0;
wonleveltime = 0;
visibleloading = false;
if (!Person::players[0]->dead && targetlevel != whichlevel)
startbonustotal = bonustotal;
if (!Person::players[0]->dead && targetlevel != whichlevel)
startbonustotal = bonustotal;
- if (Person::players[0]->dead)
- Loadlevel(whichlevel);
- else
- Loadlevel(targetlevel);
+ LoadLevel(targetlevel);
fireSound();
loading = 3;
fireSound();
loading = 3;
fireSound(firestartsound);
fireSound(firestartsound);
- Loadlevel(campaignlevels[Account::active().getCampaignChoicesMade()].mapname.c_str());
+ LoadLevel(campaignlevels[Account::active().getCampaignChoicesMade()].mapname.c_str());
actuallevel = campaignlevels[actuallevel].nextlevel.front();
visibleloading = true;
stillloading = 1;
actuallevel = campaignlevels[actuallevel].nextlevel.front();
visibleloading = true;
stillloading = 1;
- Loadlevel(campaignlevels[actuallevel].mapname.c_str());
+ LoadLevel(campaignlevels[actuallevel].mapname.c_str());
campaign = 1;
mainmenu = 0;
gameon = 1;
campaign = 1;
mainmenu = 0;
gameon = 1;
actuallevel = (Account::active().getCampaignChoicesMade() > 0 ? campaignlevels[Account::active().getCampaignChoicesMade() - 1].nextlevel[whichchoice] : 0);
visibleloading = true;
stillloading = 1;
actuallevel = (Account::active().getCampaignChoicesMade() > 0 ? campaignlevels[Account::active().getCampaignChoicesMade() - 1].nextlevel[whichchoice] : 0);
visibleloading = true;
stillloading = 1;
- Loadlevel(campaignlevels[actuallevel].mapname.c_str());
+ LoadLevel(campaignlevels[actuallevel].mapname.c_str());
campaign = 1;
mainmenu = 0;
gameon = 1;
campaign = 1;
mainmenu = 0;
gameon = 1;
mainmenu = 0;
gameon = 1;
mainmenu = 0;
gameon = 1;
campaign = 0;
mainmenu = 0;
campaign = 0;
mainmenu = 0;
+
+bool Folders::file_exists(const std::string& filepath)
+{
+ FILE* file;
+ file = fopen(filepath.c_str(), "rb");
+ if (file == NULL) {
+ return false;
+ } else {
+ fclose(file);
+ return true;
+ }
+}
static FILE* openMandatoryFile(const std::string& filename, const char* mode);
static FILE* openMandatoryFile(const std::string& filename, const char* mode);
+ static bool file_exists(const std::string& filepath);
+
/* Returns full path for a game resource */
static inline std::string getResourcePath(const std::string& filepath)
{ return dataDir + '/' + filepath; }
/* Returns full path for a game resource */
static inline std::string getResourcePath(const std::string& filepath)
{ return dataDir + '/' + filepath; }