Skip to content

Commit

Permalink
fix(exit): main cli app properly returns sane exit codes
Browse files Browse the repository at this point in the history
  • Loading branch information
ineed bots committed Dec 26, 2023
1 parent cbfcce1 commit d24fc65
Showing 1 changed file with 56 additions and 26 deletions.
82 changes: 56 additions & 26 deletions src/tool/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ namespace gsc
{

std::map<game, std::map<mach, std::unique_ptr<context>>> contexts;
std::map<mode, std::function<void(game game, mach mach, fs::path file, fs::path rel)>> funcs;
std::map<mode, std::function<int(game game, mach mach, fs::path file, fs::path rel)>> funcs;
bool zonetool = false;

auto assemble_file(game game, mach mach, fs::path file, fs::path rel) -> void
auto assemble_file(game game, mach mach, fs::path file, fs::path rel) -> int
{
try
{
Expand Down Expand Up @@ -192,14 +192,17 @@ auto assemble_file(game game, mach mach, fs::path file, fs::path rel) -> void
std::cout << fmt::format("assembled {}\n", rel.generic_string());
}
}

return 0;
}
catch (std::exception const& e)
{
std::cerr << fmt::format("{} at {}\n", e.what(), file.generic_string());
return 1;
}
}

auto disassemble_file(game game, mach mach, fs::path file, fs::path rel) -> void
auto disassemble_file(game game, mach mach, fs::path file, fs::path rel) -> int
{
try
{
Expand Down Expand Up @@ -238,14 +241,16 @@ auto disassemble_file(game game, mach mach, fs::path file, fs::path rel) -> void

utils::file::save(fs::path{ "disassembled" } / rel, outsrc);
std::cout << fmt::format("disassembled {}\n", rel.generic_string());
return 0;
}
catch (std::exception const& e)
{
std::cerr << fmt::format("{} at {}\n", e.what(), file.generic_string());
return 1;
}
}

auto compile_file(game game, mach mach, fs::path file, fs::path rel) -> void
auto compile_file(game game, mach mach, fs::path file, fs::path rel) -> int
{
try
{
Expand Down Expand Up @@ -288,14 +293,17 @@ auto compile_file(game game, mach mach, fs::path file, fs::path rel) -> void
std::cout << fmt::format("compiled {}\n", rel.generic_string());
}
}

return 0;
}
catch (std::exception const& e)
{
std::cerr << fmt::format("{} at {}\n", e.what(), file.generic_string());
return 1;
}
}

auto decompile_file(game game, mach mach, fs::path file, fs::path rel) -> void
auto decompile_file(game game, mach mach, fs::path file, fs::path rel) -> int
{
try
{
Expand Down Expand Up @@ -335,14 +343,16 @@ auto decompile_file(game game, mach mach, fs::path file, fs::path rel) -> void

utils::file::save(fs::path{ "decompiled" } / rel, outsrc);
std::cout << fmt::format("decompiled {}\n", rel.generic_string());
return 0;
}
catch (std::exception const& e)
{
std::cerr << fmt::format("{} at {}\n", e.what(), file.generic_string());
return 1;
}
}

auto parse_file(game game, mach mach, fs::path file, fs::path rel) -> void
auto parse_file(game game, mach mach, fs::path file, fs::path rel) -> int
{
try
{
Expand All @@ -356,26 +366,28 @@ auto parse_file(game game, mach mach, fs::path file, fs::path rel) -> void
auto prog = contexts[game][mach]->source().parse_program(file.string(), data);
utils::file::save(fs::path{ "parsed" } / rel, contexts[game][mach]->source().dump(*prog));
std::cout << fmt::format("parsed {}\n", rel.generic_string());
return 1;
}
catch (std::exception const& e)
{
std::cerr << fmt::format("{} at {}\n", e.what(), file.generic_string());
return 0;
}
}

auto rename_file(game game, mach mach, fs::path file, fs::path rel) -> void
auto rename_file(game game, mach mach, fs::path file, fs::path rel) -> int
{
try
{
if (file.extension() != ".cgsc" && file.extension() != ".gsc" && file.extension() != ".csc" && file.extension() != ".gscbin" && file.extension() != ".cscbin")
return;
return 0;

auto ext = file.extension();
auto zt = file.extension() == ".cgsc";

if (game == game::iw9)
{
return;
return 0;
}
else
{
Expand Down Expand Up @@ -411,10 +423,13 @@ auto rename_file(game game, mach mach, fs::path file, fs::path rel) -> void
utils::file::save(fs::path{ "renamed" } / rel.replace_extension(".cgsc.stack"), stack);
std::cout << fmt::format("renamed {} -> {}\n", file.filename().generic_string(), rel.generic_string());
}

return 0;
}
catch (std::exception const& e)
{
std::cerr << fmt::format("{} at {}\n", e.what(), file.generic_string());
return 1;
}
}

Expand Down Expand Up @@ -686,9 +701,9 @@ namespace arc
{

std::map<game, std::map<mach, std::unique_ptr<context>>> contexts;
std::map<mode, std::function<void(game game, mach mach, fs::path const& file, fs::path rel)>> funcs;
std::map<mode, std::function<int(game game, mach mach, fs::path const& file, fs::path rel)>> funcs;

void assemble_file(game game, mach mach, fs::path const& file, fs::path rel)
auto assemble_file(game game, mach mach, fs::path const& file, fs::path rel) -> int
{
try
{
Expand All @@ -706,14 +721,16 @@ void assemble_file(game game, mach mach, fs::path const& file, fs::path rel)

utils::file::save(fs::path{ "assembled" } / rel, outbin.data, outbin.size);
std::cout << fmt::format("assembled {}\n", rel.generic_string());
return 0;
}
catch (std::exception const& e)
{
std::cerr << fmt::format("{} at {}\n", e.what(), file.generic_string());
return 1;
}
}

void disassemble_file(game game, mach mach, fs::path const& file, fs::path rel)
auto disassemble_file(game game, mach mach, fs::path const& file, fs::path rel) -> int
{
try
{
Expand All @@ -728,14 +745,16 @@ void disassemble_file(game game, mach mach, fs::path const& file, fs::path rel)

utils::file::save(fs::path{ "disassembled" } / rel, outsrc);
std::cout << fmt::format("disassembled {}\n", rel.generic_string());
return 0;
}
catch (std::exception const& e)
{
std::cerr << fmt::format("{} at {}\n", e.what(), file.generic_string());
return 1;
}
}

void compile_file(game game, mach mach, fs::path const& file, fs::path rel)
auto compile_file(game game, mach mach, fs::path const& file, fs::path rel) -> int
{
try
{
Expand All @@ -753,14 +772,16 @@ void compile_file(game game, mach mach, fs::path const& file, fs::path rel)

utils::file::save(fs::path{ "compiled" } / rel, outbin.data, outbin.size);
std::cout << fmt::format("compiled {}\n", rel.generic_string());
return 0;
}
catch (std::exception const& e)
{
std::cerr << fmt::format("{} at {}\n", e.what(), file.generic_string());
return 1;
}
}

void decompile_file(game game, mach mach, fs::path const& file, fs::path rel)
auto decompile_file(game game, mach mach, fs::path const& file, fs::path rel) -> int
{
try
{
Expand All @@ -777,21 +798,25 @@ void decompile_file(game game, mach mach, fs::path const& file, fs::path rel)

utils::file::save(fs::path{ "decompiled" } / rel, output);
std::cout << fmt::format("decompiled {}\n", rel.generic_string());
return 1;
}
catch (std::exception const& e)
{
std::cerr << fmt::format("{} at {}\n", e.what(), file.generic_string());
return 0;
}
}

void parse_file(game, mach, fs::path const&, fs::path)
auto parse_file(game, mach, fs::path const&, fs::path) -> int
{
std::cerr << fmt::format("not implemented for treyarch\n");
return 1;
}

void rename_file(game, mach, fs::path const&, fs::path)
auto rename_file(game, mach, fs::path const&, fs::path) -> int
{
std::cerr << fmt::format("not implemented for treyarch\n");
return 1;
}

auto fs_read(std::string const& name) -> std::vector<u8>
Expand Down Expand Up @@ -915,36 +940,41 @@ auto init(game game, mach mach) -> void

} // namespace xsk::arc

auto execute(mode mode, game game, mach mach, fs::path const& path) -> void
auto execute(mode mode, game game, mach mach, fs::path const& path) -> int
{
gsc::init(game, mach);
arc::init(game, mach);

if (fs::is_directory(path))
{
auto exit_code = 0;

for (auto const& entry : fs::recursive_directory_iterator(path))
{
if (entry.is_regular_file() && entry.path().extension() != ".stack")
{
auto rel = fs::relative(entry, path).remove_filename();

if (game < game::t6)
gsc::funcs[mode](game, mach, entry.path().generic_string(), rel);
exit_code |= gsc::funcs[mode](game, mach, entry.path().generic_string(), rel);
else
arc::funcs[mode](game, mach, fs::path{ entry.path().generic_string(), fs::path::format::generic_format }, rel);
exit_code |= arc::funcs[mode](game, mach, fs::path{ entry.path().generic_string(), fs::path::format::generic_format }, rel);
}
}

return exit_code;
}
else if (fs::is_regular_file(path))
{
if (game < game::t6)
gsc::funcs[mode](game, mach, path, fs::path{});
return gsc::funcs[mode](game, mach, path, fs::path{});
else
arc::funcs[mode](game, mach, fs::path(path, fs::path::format::generic_format), fs::path{});
return arc::funcs[mode](game, mach, fs::path(path, fs::path::format::generic_format), fs::path{});
}
else
{
std::cerr << fmt::format("bad path '{}'\n", path.generic_string());
return 255;
}
}

Expand Down Expand Up @@ -1022,7 +1052,7 @@ auto branding() -> void
std::cout << fmt::format("GSC Tool {} created by xensik\n\n", XSK_VERSION_STR);
}

auto main(u32 argc, char** argv) -> void
auto main(u32 argc, char** argv) -> int
{
auto path = fs::path{};
auto mode = mode::_;
Expand All @@ -1033,16 +1063,16 @@ auto main(u32 argc, char** argv) -> void

if (!parse_flags(argc, argv, mode, game, mach, path))
{
return print_usage();
print_usage();
return 2;
}

execute(mode, game, mach, path);
return execute(mode, game, mach, path);
}

} // namespace xsk

int main(int argc, char** argv)
{
xsk::main(argc, argv);
return 0;
return xsk::main(argc, argv);
}

0 comments on commit d24fc65

Please sign in to comment.