Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
xensik committed Jan 5, 2024
1 parent c8a0e75 commit 32cba32
Showing 1 changed file with 42 additions and 38 deletions.
80 changes: 42 additions & 38 deletions src/tool/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@

namespace fs = std::filesystem;

enum class result : int { ok = 0, error = 1 };

namespace xsk
{

enum class result : i32 { success = 0, failure = 1 };
enum class encd { _, source, assembly, binary };
enum class mode { _, assemble, disassemble, compile, decompile, parse, rename };
enum class game { _, iw5, iw6, iw7, iw8, iw9, s1, s2, s4, h1, h2, t6, t7, t8, t9 };
Expand Down Expand Up @@ -118,6 +117,11 @@ std::map<mode, encd> const encds =
{ mode::decompile, encd::binary },
};

auto operator |=(result& lhs, result rhs) -> void
{
lhs = static_cast<result>(static_cast<i32>(lhs) | static_cast<i32>(rhs));
}

auto overwrite_prompt(std::string const& file) -> bool
{
auto overwrite = true;
Expand Down Expand Up @@ -195,12 +199,12 @@ auto assemble_file(game game, mach mach, fs::path file, fs::path rel) -> result
}
}

return result::ok;
return result::success;
}
catch (std::exception const& e)
{
std::cerr << fmt::format("{} at {}\n", e.what(), file.generic_string());
return result::error;
return result::failure;
}
}

Expand Down Expand Up @@ -243,12 +247,12 @@ auto disassemble_file(game game, mach mach, fs::path file, fs::path rel) -> resu

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

Expand All @@ -259,7 +263,7 @@ auto compile_file(game game, mach mach, fs::path file, fs::path rel) -> result
if (file.extension() != ".gsc")
{
std::cerr << fmt::format("{} at {}\n", "expected .gsc file", file.generic_string());
return result::ok;
return result::success;
}

rel = fs::path{ games_rev.at(game) } / rel / file.filename().replace_extension((zonetool ? ".cgsc" : ".gscbin"));
Expand Down Expand Up @@ -299,12 +303,12 @@ auto compile_file(game game, mach mach, fs::path file, fs::path rel) -> result
}
}

return result::ok;
return result::success;
}
catch (std::exception const& e)
{
std::cerr << fmt::format("{} at {}\n", e.what(), file.generic_string());
return result::error;
return result::failure;
}
}

Expand Down Expand Up @@ -348,12 +352,12 @@ auto decompile_file(game game, mach mach, fs::path file, fs::path rel) -> result

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

Expand All @@ -364,7 +368,7 @@ auto parse_file(game game, mach mach, fs::path file, fs::path rel) -> result
if (file.extension() != ".gsc")
{
std::cerr << fmt::format("{} at {}\n", "expected .gsc file", file.generic_string());
return result::ok;
return result::success;
}

rel = fs::path{ games_rev.at(game) } / rel / file.filename();
Expand All @@ -374,12 +378,12 @@ auto parse_file(game game, mach mach, fs::path file, fs::path rel) -> result
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 result::ok;
return result::success;
}
catch (std::exception const& e)
{
std::cerr << fmt::format("{} at {}\n", e.what(), file.generic_string());
return result::error;
return result::failure;
}
}

Expand All @@ -388,14 +392,14 @@ auto rename_file(game game, mach mach, fs::path file, fs::path rel) -> result
try
{
if (file.extension() != ".cgsc" && file.extension() != ".gsc" && file.extension() != ".csc" && file.extension() != ".gscbin" && file.extension() != ".cscbin")
return result::ok;
return result::success;

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

if (game == game::iw9)
{
return result::ok;
return result::success;
}
else
{
Expand Down Expand Up @@ -432,12 +436,12 @@ auto rename_file(game game, mach mach, fs::path file, fs::path rel) -> result
std::cout << fmt::format("renamed {} -> {}\n", file.filename().generic_string(), rel.generic_string());
}

return result::ok;
return result::success;
}
catch (std::exception const& e)
{
std::cerr << fmt::format("{} at {}\n", e.what(), file.generic_string());
return result::error;
return result::failure;
}
}

Expand Down Expand Up @@ -729,12 +733,12 @@ auto 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 result::ok;
return result::success;
}
catch (std::exception const& e)
{
std::cerr << fmt::format("{} at {}\n", e.what(), file.generic_string());
return result::error;
return result::failure;
}
}

Expand All @@ -753,12 +757,12 @@ auto 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 result::ok;
return result::success;
}
catch (std::exception const& e)
{
std::cerr << fmt::format("{} at {}\n", e.what(), file.generic_string());
return result::error;
return result::failure;
}
}

Expand All @@ -772,7 +776,7 @@ auto compile_file(game game, mach mach, fs::path const& file, fs::path rel) -> r
if (file.extension() != ".gsc" && file.extension() != ".csc")
{
std::cerr << fmt::format("{} at {}\n", "expected .gsc or .csc file", file.generic_string());
return result::ok;
return result::success;
}

rel = fs::path{ games_rev.at(game) } / rel / file.filename();
Expand All @@ -782,20 +786,20 @@ auto compile_file(game game, mach mach, fs::path const& file, fs::path rel) -> r
if (!std::memcmp(&data[0], "\x80GSC", 4))
{
std::cerr << fmt::format("{} at {}\n", "already compiled", file.generic_string());
return result::ok;
return result::success;
}

auto outasm = contexts[game][mach]->compiler().compile(file.string(), data);
auto outbin = contexts[game][mach]->assembler().assemble(*outasm);

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

Expand All @@ -816,25 +820,25 @@ auto 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 result::ok;
return result::success;
}
catch (std::exception const& e)
{
std::cerr << fmt::format("{} at {}\n", e.what(), file.generic_string());
return result::error;
return result::failure;
}
}

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

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

auto fs_read(std::string const& name) -> std::vector<u8>
Expand Down Expand Up @@ -965,7 +969,7 @@ auto execute(mode mode, game game, mach mach, fs::path const& path) -> result

if (fs::is_directory(path))
{
auto exit_code = static_cast<int>(result::ok);
auto exit_code = result::success;

for (auto const& entry : fs::recursive_directory_iterator(path))
{
Expand All @@ -974,13 +978,13 @@ auto execute(mode mode, game game, mach mach, fs::path const& path) -> result
auto rel = fs::relative(entry, path).remove_filename();

if (game < game::t6)
exit_code |= static_cast<int>(gsc::funcs[mode](game, mach, entry.path().generic_string(), rel));
exit_code |= gsc::funcs[mode](game, mach, entry.path().generic_string(), rel);
else
exit_code |= static_cast<int>(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 static_cast<result>(exit_code);
return exit_code;
}
else if (fs::is_regular_file(path))
{
Expand All @@ -992,7 +996,7 @@ auto execute(mode mode, game game, mach mach, fs::path const& path) -> result
else
{
std::cerr << fmt::format("bad path '{}'\n", path.generic_string());
return result::error;
return result::failure;
}
}

Expand Down Expand Up @@ -1056,7 +1060,7 @@ auto parse_flags(u32 argc, char** argv, mode& mode, game& game, mach& mach, fs::
return true;
}

auto print_usage() -> void
auto usage() -> void
{
std::cout << "usage: gsc-tool <mode> <game> <system> <path>\n";
std::cout << "\t* mode: asm, disasm, comp, decomp, parse, rename\n";
Expand All @@ -1081,8 +1085,8 @@ auto main(u32 argc, char** argv) -> result

if (!parse_flags(argc, argv, mode, game, mach, path))
{
print_usage();
return result::error;
usage();
return result::failure;
}

return execute(mode, game, mach, path);
Expand Down

0 comments on commit 32cba32

Please sign in to comment.