forked from jordan-evens/einstein-puzzle
/
mkres.cpp
167 lines (139 loc) · 4.95 KB
/
mkres.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// This file is part of Einstein Puzzle
// Einstein Puzzle
// Copyright (C) 2003-2005 Flowix Games
// Einstein Puzzle is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// Einstein Puzzle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <cstring>
#include "args.h"
#include "compressor.h"
#include "exceptions.h"
#include "unicode.h"
#include "table.h"
#include <string.h>
static std::string sourceFile, outputFile;
static bool verbose = false;
static bool showDeps = false;
static void printHelp(int terminate)
{
std::cerr << "USAGE:" << std::endl;
std::cerr << " mkres [options]" << std::endl;
std::cerr << "OPTIONS:" << std::endl;
std::cerr << " --verbose print more messages" << std::endl;
std::cerr << " --deps print make(1) dependencies" << std::endl;
std::cerr << " --source <file> read source file" << std::endl;
std::cerr << " --output <file> write result to file" << std::endl;
std::cerr << " --version show version number" << std::endl;
std::cerr << " --help this help screen" << std::endl;
if (terminate >= 0)
exit(terminate);
}
static void printVersion(int terminate)
{
std::cout << "Resource File Compressor" << std::endl;
std::cout << "Version 1.2" << std::endl;
std::cout << "Copyright (c) 2003-2005 Alexander Babichev" << std::endl;
if (terminate >= 0)
exit(terminate);
}
static void parseArgs(int argc, char *argv[])
{
if (argc == 1)
printHelp(0);
for (int i = 1; i < argc; i++) {
if (! argv[i])
continue;
if ((! strcmp(argv[i], "--source")) && (i < argc - 1))
sourceFile = std::string(argv[++i]);
else if ((! strcmp(argv[i], "--output")) && (i < argc - 1))
outputFile = std::string(argv[++i]);
else if (! strcmp(argv[i], "--help"))
printHelp(0);
else if (! strcmp(argv[i], "--version"))
printVersion(0);
else if (! strcmp(argv[i], "--verbose"))
verbose = true;
else if (! strcmp(argv[i], "--deps"))
showDeps = true;
else {
std::cerr << "Invalid option '" << argv[i] << "'" << std::endl;
printHelp(1);
}
}
if (! sourceFile.length()) {
std::cerr << "Source file not specified" << std::endl;
exit(1);
}
if (! outputFile.length()) {
std::cerr << "Output file not specified" << std::endl;
exit(1);
}
}
static ResourceCompressor compressor;
static void parseFile(const std::string &fileName)
{
Table table(fileName);
compressor.setPriority(table.getInt(L"priority", 1000));
Table *res = table.getTable(L"resources");
if (res) {
for (const auto& re : *res)
{
Value *value = re.second;
if ((! value) || (! (Value::Table == value->getType())))
throw Exception(L"Invalid resource entry");
Table *t = value->asTable();
const std::wstring &name = t->getString(L"name");
if (0 == name.length())
throw Exception(L"Unnamed resource entry");
const std::wstring &format = t->getString(L"format");
Formatter *formatter = formatRegistry.get(format);
if ((0 < format.length()) && (! formatter))
throw Exception(L"Unknown format '" + format + L"'");
compressor.add(Entry(name, t->getInt(L"compr", 9),
t->getString(L"group", L""),
t->getString(L"file", name), formatter));
}
}
}
#ifdef WIN32
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow)
#else
int main(int argc, char *argv[])
#endif
{
#ifdef WIN32
int argc;
LPSTR* argv = CommandLineToArgvA(GetCommandLineA(), &argc);
#endif //ifdef WIN32
int res = 0;
//lua_State *lua = nullptr;
parseArgs(argc, argv);
try {
parseFile(sourceFile);
if (! showDeps)
compressor.compress(outputFile, verbose);
else
compressor.printDeps(outputFile, sourceFile);
} catch (Exception &e) {
std::cerr << e.getMessage() << std::endl;
res = 1;
} catch (std::string &s) {
std::cerr << s << std::endl;
res = 1;
} catch (...) {
std::cerr << L"Error: unknown exception" << std::endl;
res = 1;
}
/* if (lua) {
lua_setgcthreshold(lua, 0);
lua_close(lua);
}*/
return res;
}