Skip to content

Commit 978a3e0

Browse files
committed
cage-files tool
1 parent 26480ce commit 978a3e0

File tree

7 files changed

+132
-6
lines changed

7 files changed

+132
-6
lines changed

sources/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,9 @@ endmacro()
171171
cage_define_tool(cage-asset-analyze "asset-analyze/*")
172172
cage_define_tool(cage-asset-database "asset-database/*")
173173
cage_define_tool(cage-asset-processor "asset-processor/*")
174-
cage_define_tool(cage-image-convert "image-convert/*")
174+
cage_define_tool(cage-files "files/*")
175175
cage_define_tool(cage-image-channels "image-channels/*")
176+
cage_define_tool(cage-image-convert "image-convert/*")
176177
cage_define_tool(cage-image-info "image-info/*")
177178
cage_define_tool(cage-image-resize "image-resize/*")
178179
cage_define_tool(cage-image-untile "image-untile/*")

sources/files/main.cpp

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
}

sources/image-convert/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ int main(int argc, const char *args[])
3434
{
3535
Holder<Ini> cmd = newIni();
3636
cmd->parseCmd(argc, args);
37-
const auto &paths = cmd->cmdArray(0, "--");
37+
const auto paths = cmd->cmdArray(0, "--");
3838
const bool preserveOriginal = cmd->cmdBool('p', "preserve", false);
3939
const String format = cmd->cmdString('f', "format", ".png");
4040
if (cmd->cmdBool('?', "help", false))

sources/image-info/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ int main(int argc, const char *args[])
3131
{
3232
Holder<Ini> cmd = newIni();
3333
cmd->parseCmd(argc, args);
34-
const auto &paths = cmd->cmdArray(0, "--");
34+
const auto paths = cmd->cmdArray(0, "--");
3535
if (cmd->cmdBool('?', "help", false))
3636
{
3737
cmd->logHelp();

sources/image-resize/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ int main(int argc, const char *args[])
2626
{
2727
Holder<Ini> cmd = newIni();
2828
cmd->parseCmd(argc, args);
29-
const auto &paths = cmd->cmdArray(0, "--");
29+
const auto paths = cmd->cmdArray(0, "--");
3030
Vec2i resolution;
3131
resolution[0] = cmd->cmdUint32('w', "width");
3232
resolution[1] = cmd->cmdUint32('h', "height");

sources/mesh-convert/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ int main(int argc, const char *args[])
339339
convertToCage = cmd->cmdBool('c', "cage", convertToCage);
340340
generateObject = cmd->cmdBool('O', "objects", generateObject);
341341
scale = cmd->cmdFloat('s', "scale", scale.value);
342-
const auto &inPaths = cmd->cmdArray(0, "--");
342+
const auto inPaths = cmd->cmdArray(0, "--");
343343
if (cmd->cmdBool('?', "help", false))
344344
{
345345
cmd->logHelp();

sources/mesh-info/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ int main(int argc, const char *args[])
110110
{
111111
Holder<Ini> cmd = newIni();
112112
cmd->parseCmd(argc, args);
113-
const auto &paths = cmd->cmdArray(0, "--");
113+
const auto paths = cmd->cmdArray(0, "--");
114114
const bool normalizeFormats = cmd->cmdBool('n', "normalize", false);
115115
const bool convertToCage = cmd->cmdBool('c', "cage", false);
116116
if (cmd->cmdBool('?', "help", false))

0 commit comments

Comments
 (0)