1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-22 17:18:39 +00:00

Introduce proper error handling for file streams

This commit is contained in:
sfan5 2024-05-08 20:37:10 +02:00
parent c38e0d05bf
commit 39fd9b93c3
23 changed files with 235 additions and 149 deletions

View file

@ -26,35 +26,17 @@ with this program; if not, write to the Free Software Foundation, Inc.,
ContentType getContentType(const std::string &path)
{
std::ifstream modpack_is((path + DIR_DELIM + "modpack.txt").c_str());
if (modpack_is.good()) {
modpack_is.close();
if (fs::IsFile(path + DIR_DELIM "modpack.txt") || fs::IsFile(path + DIR_DELIM "modpack.conf"))
return ContentType::MODPACK;
}
std::ifstream modpack2_is((path + DIR_DELIM + "modpack.conf").c_str());
if (modpack2_is.good()) {
modpack2_is.close();
return ContentType::MODPACK;
}
std::ifstream init_is((path + DIR_DELIM + "init.lua").c_str());
if (init_is.good()) {
init_is.close();
if (fs::IsFile(path + DIR_DELIM "init.lua"))
return ContentType::MOD;
}
std::ifstream game_is((path + DIR_DELIM + "game.conf").c_str());
if (game_is.good()) {
game_is.close();
if (fs::IsFile(path + DIR_DELIM "game.conf"))
return ContentType::GAME;
}
std::ifstream txp_is((path + DIR_DELIM + "texture_pack.conf").c_str());
if (txp_is.good()) {
txp_is.close();
if (fs::IsFile(path + DIR_DELIM "texture_pack.conf"))
return ContentType::TXP;
}
return ContentType::UNKNOWN;
}
@ -66,19 +48,19 @@ void parseContentInfo(ContentSpec &spec)
switch (getContentType(spec.path)) {
case ContentType::MOD:
spec.type = "mod";
conf_path = spec.path + DIR_DELIM + "mod.conf";
conf_path = spec.path + DIR_DELIM "mod.conf";
break;
case ContentType::MODPACK:
spec.type = "modpack";
conf_path = spec.path + DIR_DELIM + "modpack.conf";
conf_path = spec.path + DIR_DELIM "modpack.conf";
break;
case ContentType::GAME:
spec.type = "game";
conf_path = spec.path + DIR_DELIM + "game.conf";
conf_path = spec.path + DIR_DELIM "game.conf";
break;
case ContentType::TXP:
spec.type = "txp";
conf_path = spec.path + DIR_DELIM + "texture_pack.conf";
conf_path = spec.path + DIR_DELIM "texture_pack.conf";
break;
default:
spec.type = "unknown";
@ -124,8 +106,6 @@ void parseContentInfo(ContentSpec &spec)
spec.textdomain = spec.name;
if (spec.desc.empty()) {
std::ifstream is((spec.path + DIR_DELIM + "description.txt").c_str());
spec.desc = std::string((std::istreambuf_iterator<char>(is)),
std::istreambuf_iterator<char>());
fs::ReadFile(spec.path + DIR_DELIM + "description.txt", spec.desc);
}
}