diff --git a/src/tool/main.cpp b/src/tool/main.cpp index fa9163b2..b76650e1 100644 --- a/src/tool/main.cpp +++ b/src/tool/main.cpp @@ -146,10 +146,10 @@ namespace gsc { std::map>> contexts; -std::map> funcs; +std::map> 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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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; } } @@ -686,9 +701,9 @@ namespace arc { std::map>> contexts; -std::map> funcs; +std::map> 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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 @@ -915,13 +940,15 @@ 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") @@ -929,22 +956,25 @@ auto execute(mode mode, game game, mach mach, fs::path const& path) -> void 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; } } @@ -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::_; @@ -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); }