Skip to content

Commit

Permalink
Better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kuroni committed Oct 5, 2018
1 parent 32565e0 commit 24996b6
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,25 @@ void create_config()

void error_msg(std::string error, std::string title)
{
MessageBoxA(NULL, error.c_str(), title.c_str(), MB_ICONERROR | MB_OK);
exit(0);
if (MessageBoxA(NULL, error.c_str(), title.c_str(), MB_ICONERROR | MB_RETRYCANCEL) == 2) // CANCEL or CLOSE
exit(0);
}

bool init()
{
create_config();
std::ifstream cfg_file("config.json", std::ifstream::binary);
std::string cfg_string((std::istreambuf_iterator<char>(cfg_file)), std::istreambuf_iterator<char>()), error;
Json::CharReaderBuilder cfg_builder;
Json::CharReader *cfg_reader = cfg_builder.newCharReader();
if (!cfg_reader->parse(cfg_string.c_str(), cfg_string.c_str() + cfg_string.size(), &cfg, &error))
error_msg(error, "Error reading config.json");

delete cfg_reader;
cfg_file.close();
while (true)
{
std::ifstream cfg_file("config.json", std::ifstream::binary);
std::string cfg_string((std::istreambuf_iterator<char>(cfg_file)), std::istreambuf_iterator<char>()), error;
Json::CharReaderBuilder cfg_builder;
Json::CharReader *cfg_reader = cfg_builder.newCharReader();
if (!cfg_reader->parse(cfg_string.c_str(), cfg_string.c_str() + cfg_string.size(), &cfg, &error))
error_msg(error, "Error reading config.json");
else
break;
}

img_holder.clear();

int mode = data::cfg["mode"].asInt();
Expand All @@ -88,7 +91,7 @@ bool init()
sf::Texture &load_texture(std::string path)
{
if (img_holder.find(path) == img_holder.end())
if (!img_holder[path].loadFromFile(path))
while (!img_holder[path].loadFromFile(path))
error_msg("Cannot find file " + path, "Error importing images");
return img_holder[path];
}
Expand Down Expand Up @@ -125,7 +128,7 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLi
TCHAR w_title[256];
GetWindowText(handle, w_title, GetWindowTextLength(handle));
std::string title = w_title;
is_bongo = (title.find("Bongo Cat for osu!") == 0);
is_bongo = (title.find("Bongo Cat for osu") == 0);
}

// reloading config device
Expand Down

0 comments on commit 24996b6

Please sign in to comment.