From 9c6f6c25890cd250b2a884f5d7af6bf44045641b Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sat, 3 Jun 2023 22:06:51 -0600 Subject: [PATCH] common: change FError to throw an exception --- bspinfo/main.cc | 72 +++++++++++++++++++++++-------------------- bsputil/main.cc | 7 ++++- common/log.cc | 16 ++++++++-- include/common/log.hh | 15 +++++++++ light/main.cc | 2 ++ qbsp/main.cc | 2 ++ vis/main.cc | 2 ++ 7 files changed, 78 insertions(+), 38 deletions(-) diff --git a/bspinfo/main.cc b/bspinfo/main.cc index cb0ff012..4fe11a8a 100644 --- a/bspinfo/main.cc +++ b/bspinfo/main.cc @@ -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(bsp.bsp), fs::path(source).replace_extension("bsp.json")); + + PrintBSPTextureUsage(std::get(bsp.bsp)); + + FindInfiniteChains(std::get(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(bsp.bsp), fs::path(source).replace_extension("bsp.json")); - - PrintBSPTextureUsage(std::get(bsp.bsp)); - - FindInfiniteChains(std::get(bsp.bsp)); - - printf("---------------------\n"); - - fs::clear(); - } - - return 0; } diff --git a/bsputil/main.cc b/bsputil/main.cc index fb2c6b1b..5d53ca20 100644 --- a/bsputil/main.cc +++ b/bsputil/main.cc @@ -18,8 +18,13 @@ */ #include +#include 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); + } } diff --git a/common/log.cc b/common/log.cc index 64608523..d7cb3a26 100644 --- a/common/log.cc +++ b/common/log.cc @@ -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); } diff --git a/include/common/log.hh b/include/common/log.hh index 31b31989..3ad8197c 100644 --- a/include/common/log.hh +++ b/include/common/log.hh @@ -30,6 +30,7 @@ #include #include #include // for log10 +#include // for std::runtime_error #include #include #include @@ -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 diff --git a/light/main.cc b/light/main.cc index 330cb7a1..063509f1 100644 --- a/light/main.cc +++ b/light/main.cc @@ -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); } } diff --git a/qbsp/main.cc b/qbsp/main.cc index bccd048f..b967cd46 100644 --- a/qbsp/main.cc +++ b/qbsp/main.cc @@ -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); } } diff --git a/vis/main.cc b/vis/main.cc index de10a70e..004c395e 100644 --- a/vis/main.cc +++ b/vis/main.cc @@ -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); } }