common: change FError to throw an exception

This commit is contained in:
Eric Wasylishen 2023-06-03 22:06:51 -06:00
parent f2ecb9fe1c
commit 9c6f6c2589
7 changed files with 78 additions and 38 deletions

View File

@ -85,40 +85,44 @@ settings::common_settings bspinfo_options;
int main(int argc, char **argv)
{
logging::preinitialize();
try {
logging::preinitialize();
fmt::print("---- bspinfo / ericw-tools {} ----\n", ERICWTOOLS_VERSION);
if (argc == 1) {
printf("usage: bspinfo bspfile [bspfiles]\n");
exit(1);
fmt::print("---- bspinfo / ericw-tools {} ----\n", ERICWTOOLS_VERSION);
if (argc == 1) {
printf("usage: bspinfo bspfile [bspfiles]\n");
exit(1);
}
for (int32_t i = 1; i < argc; i++) {
printf("---------------------\n");
fs::path source = DefaultExtension(argv[i], ".bsp");
fmt::print("{}\n", source);
bspdata_t bsp;
LoadBSPFile(source, &bsp);
bsp.version->game->init_filesystem(source, bspinfo_options);
PrintBSPFileSizes(&bsp);
// WriteBSPFile(fs::path(source).replace_extension("bsp.rewrite"), &bsp);
ConvertBSPFormat(&bsp, &bspver_generic);
serialize_bsp(bsp, std::get<mbsp_t>(bsp.bsp), fs::path(source).replace_extension("bsp.json"));
PrintBSPTextureUsage(std::get<mbsp_t>(bsp.bsp));
FindInfiniteChains(std::get<mbsp_t>(bsp.bsp));
printf("---------------------\n");
fs::clear();
}
return 0;
} catch (const std::exception &e) {
exit_on_exception(e);
}
for (int32_t i = 1; i < argc; i++) {
printf("---------------------\n");
fs::path source = DefaultExtension(argv[i], ".bsp");
fmt::print("{}\n", source);
bspdata_t bsp;
LoadBSPFile(source, &bsp);
bsp.version->game->init_filesystem(source, bspinfo_options);
PrintBSPFileSizes(&bsp);
// WriteBSPFile(fs::path(source).replace_extension("bsp.rewrite"), &bsp);
ConvertBSPFormat(&bsp, &bspver_generic);
serialize_bsp(bsp, std::get<mbsp_t>(bsp.bsp), fs::path(source).replace_extension("bsp.json"));
PrintBSPTextureUsage(std::get<mbsp_t>(bsp.bsp));
FindInfiniteChains(std::get<mbsp_t>(bsp.bsp));
printf("---------------------\n");
fs::clear();
}
return 0;
}

View File

@ -18,8 +18,13 @@
*/
#include <bsputil/bsputil.hh>
#include <common/log.hh>
int main(int argc, char **argv)
{
return bsputil_main(argc, argv);
try {
return bsputil_main(argc, argv);
} catch (const std::exception &e) {
exit_on_exception(e);
}
}

View File

@ -327,6 +327,18 @@ stat_tracker_t::~stat_tracker_t()
}
}; // namespace logging
ericwtools_error::ericwtools_error(const char *what)
: std::runtime_error(what)
{
}
[[noreturn]] void exit_on_exception(const std::exception &e)
{
logging::print("************ ERROR ************\n{}\n", e.what());
logging::close();
exit(1);
}
/*
* =================
* Error
@ -335,10 +347,8 @@ stat_tracker_t::~stat_tracker_t()
*/
[[noreturn]] void Error(const char *error)
{
logging::print("************ ERROR ************\n{}\n", error);
logging::close();
#ifdef _DEBUG
__debugbreak();
#endif
exit(1);
throw ericwtools_error(error);
}

View File

@ -30,6 +30,7 @@
#include <cstdarg>
#include <list>
#include <cmath> // for log10
#include <stdexcept> // for std::runtime_error
#include <fmt/core.h>
#include <common/bitflags.hh>
#include <common/fs.hh>
@ -180,6 +181,20 @@ struct stat_tracker_t
};
}; // namespace logging
class ericwtools_error : public std::runtime_error
{
public:
ericwtools_error(const char *what);
};
[[noreturn]] void exit_on_exception(const std::exception &e);
/**
* Throws a ericwtools_error
*
* The command-line tools should exit with a nonzero exit status.
* lightpreview can catch this to avoid crashing the whole UI.
*/
[[noreturn]] void Error(const char *error);
template<typename... Args>

View File

@ -29,5 +29,7 @@ int main(int argc, const char **argv)
return light_main(argc, argv);
} catch (const settings::quit_after_help_exception &) {
return 0;
} catch (const std::exception &e) {
exit_on_exception(e);
}
}

View File

@ -29,5 +29,7 @@ int main(int argc, const char **argv)
return qbsp_main(argc, argv);
} catch (const settings::quit_after_help_exception &) {
return 0;
} catch (const std::exception &e) {
exit_on_exception(e);
}
}

View File

@ -29,5 +29,7 @@ int main(int argc, const char **argv)
return vis_main(argc, argv);
} catch (const settings::quit_after_help_exception &) {
return 0;
} catch (const std::exception &e) {
exit_on_exception(e);
}
}