|
| 1 | +#include <cage-core/files.h> |
| 2 | +#include <cage-core/ini.h> |
| 3 | +#include <cage-core/logger.h> |
| 4 | + |
| 5 | +using namespace cage; |
| 6 | + |
| 7 | +void performList(PointerRange<const String> paths) |
| 8 | +{ |
| 9 | + for (const String &p : paths) |
| 10 | + { |
| 11 | + if (paths.size() > 1) |
| 12 | + CAGE_LOG(SeverityEnum::Info, "files", Stringizer() + p + ":"); |
| 13 | + const auto contents = pathListDirectory(p); |
| 14 | + for (const String &it : contents) |
| 15 | + CAGE_LOG(SeverityEnum::Info, "files", Stringizer() + pathToRel(it, p)); |
| 16 | + CAGE_LOG(SeverityEnum::Info, "files", ""); |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +void performMove(PointerRange<const String> paths) |
| 21 | +{ |
| 22 | + if (paths.size() != 2) |
| 23 | + CAGE_THROW_ERROR(Exception, "move requires exactly two paths"); |
| 24 | + pathMove(paths[0], paths[1]); |
| 25 | +} |
| 26 | + |
| 27 | +void performCopy(PointerRange<const String> paths) |
| 28 | +{ |
| 29 | + if (paths.size() != 2) |
| 30 | + CAGE_THROW_ERROR(Exception, "copy requires exactly two paths"); |
| 31 | + pathMove(paths[0], paths[1]); |
| 32 | +} |
| 33 | + |
| 34 | +void performRemove(PointerRange<const String> paths) |
| 35 | +{ |
| 36 | + for (const String &p : paths) |
| 37 | + pathRemove(p); |
| 38 | +} |
| 39 | + |
| 40 | +void performExtract(PointerRange<const String> paths) |
| 41 | +{ |
| 42 | + if (paths.size() != 2) |
| 43 | + CAGE_THROW_ERROR(Exception, "extract requires exactly two paths"); |
| 44 | + if (none(pathType(paths[0]) & PathTypeFlags::Archive)) |
| 45 | + CAGE_THROW_ERROR(Exception, "source path is not an archive"); |
| 46 | + const auto dt = pathType(paths[1]); |
| 47 | + if (any(dt & (PathTypeFlags::Invalid | PathTypeFlags::Archive | PathTypeFlags::File))) |
| 48 | + CAGE_THROW_ERROR(Exception, "target path is invalid"); |
| 49 | + CAGE_ASSERT(any(dt & (PathTypeFlags::NotFound | PathTypeFlags::Directory))); |
| 50 | + const auto contents = pathListDirectory(paths[0]); |
| 51 | + for (const String &it : contents) |
| 52 | + { |
| 53 | + const String p = pathJoin(paths[1], pathToRel(it, paths[0])); |
| 54 | + pathCopy(it, p); |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +void performArchive(PointerRange<const String> paths) |
| 59 | +{ |
| 60 | + if (paths.size() != 2) |
| 61 | + CAGE_THROW_ERROR(Exception, "archive requires exactly two paths"); |
| 62 | + if (none(pathType(paths[0]) & PathTypeFlags::Directory)) |
| 63 | + CAGE_THROW_ERROR(Exception, "source path is not a directory"); |
| 64 | + const auto dt = pathType(paths[1]); |
| 65 | + if (none(dt & (PathTypeFlags::Archive | PathTypeFlags::NotFound))) |
| 66 | + CAGE_THROW_ERROR(Exception, "target path is not an archive"); |
| 67 | + if (any(dt & PathTypeFlags::NotFound)) |
| 68 | + pathCreateArchiveCarch(paths[1]); |
| 69 | + const auto contents = pathListDirectory(paths[0]); |
| 70 | + for (const String &it : contents) |
| 71 | + { |
| 72 | + const String p = pathJoin(paths[1], pathToRel(it, paths[0])); |
| 73 | + pathCopy(it, p); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +int main(int argc, const char *args[]) |
| 78 | +{ |
| 79 | + initializeConsoleLogger(); |
| 80 | + try |
| 81 | + { |
| 82 | + Holder<Ini> cmd = newIni(); |
| 83 | + cmd->parseCmd(argc, args); |
| 84 | + const auto paths = cmd->cmdArray(0, "--"); |
| 85 | + const bool list = cmd->cmdBool('l', "list", false); |
| 86 | + const bool move = cmd->cmdBool('m', "move", false); |
| 87 | + const bool copy = cmd->cmdBool('c', "copy", false); |
| 88 | + const bool remove = cmd->cmdBool('r', "remove", false); |
| 89 | + const bool extract = cmd->cmdBool('e', "extract", false); |
| 90 | + const bool archive = cmd->cmdBool('a', "archive", false); |
| 91 | + if (cmd->cmdBool('?', "help", false)) |
| 92 | + { |
| 93 | + cmd->logHelp(); |
| 94 | + CAGE_LOG(SeverityEnum::Info, "help", Stringizer() + "examples:"); |
| 95 | + CAGE_LOG(SeverityEnum::Info, "help", Stringizer() + args[0] + " -l -- ."); |
| 96 | + CAGE_LOG(SeverityEnum::Info, "help", Stringizer() + args[0] + " -c -- assets.carch/source_file.txt extracted/target_file.txt"); |
| 97 | + CAGE_LOG(SeverityEnum::Info, "help", Stringizer() + args[0] + " -e -- source.carch target_folder"); |
| 98 | + CAGE_LOG(SeverityEnum::Info, "help", Stringizer() + args[0] + " -a -- source_folder target.carch"); |
| 99 | + return 0; |
| 100 | + } |
| 101 | + cmd->checkUnusedWithHelp(); |
| 102 | + if ((list + move + copy + remove + extract + archive) != 1) |
| 103 | + CAGE_THROW_ERROR(Exception, "exactly one operation is required"); |
| 104 | + if (paths.empty()) |
| 105 | + CAGE_THROW_ERROR(Exception, "no paths"); |
| 106 | + if (list) |
| 107 | + performList(paths); |
| 108 | + else if (move) |
| 109 | + performMove(paths); |
| 110 | + else if (copy) |
| 111 | + performCopy(paths); |
| 112 | + else if (remove) |
| 113 | + performRemove(paths); |
| 114 | + else if (extract) |
| 115 | + performExtract(paths); |
| 116 | + else if (archive) |
| 117 | + performArchive(paths); |
| 118 | + return 0; |
| 119 | + } |
| 120 | + catch (...) |
| 121 | + { |
| 122 | + detail::logCurrentCaughtException(); |
| 123 | + } |
| 124 | + return 1; |
| 125 | +} |
0 commit comments