|
@@ -6,10 +6,14 @@
|
6
|
6
|
*/
|
7
|
7
|
|
8
|
8
|
#include <iostream>
|
|
9
|
+#include <cstring>
|
|
10
|
+#include <cstdlib>
|
|
11
|
+#include <zlib.h>
|
9
|
12
|
|
10
|
13
|
#include "global.h"
|
11
|
14
|
#include "utils/strings.h"
|
12
|
15
|
#include "Script.h"
|
|
16
|
+#include "ScriptTest.h"
|
13
|
17
|
|
14
|
18
|
#define printStrings(cnt, acc, name) { \
|
15
|
19
|
std::cout << name << " (" << cnt << ")" << std::endl; \
|
|
@@ -44,10 +48,7 @@
|
44
|
48
|
}
|
45
|
49
|
|
46
|
50
|
namespace {
|
47
|
|
- int test(const char *f, bool strings) {
|
48
|
|
- Script s;
|
49
|
|
- assertEqual(s.load(f), 0);
|
50
|
|
-
|
|
51
|
+ int printDataScript(Script &s, bool strings) {
|
51
|
52
|
if (strings) {
|
52
|
53
|
printStrings(s.levelCount(), s.getLevelName, "Level Names");
|
53
|
54
|
printStrings(s.levelCount(), s.getLevelFilename, "Level Filenames");
|
|
@@ -92,46 +93,142 @@ namespace {
|
92
|
93
|
else
|
93
|
94
|
std::cout << "Script for \"" << s.getLevelName(i - 1) << "\" (" << i - 1 << "):" << std::endl;
|
94
|
95
|
int error = s.runScript(i);
|
95
|
|
- if (error != 0)
|
|
96
|
+ if (error != 0) {
|
96
|
97
|
std::cout << "Returned " << error << "..." << std::endl;
|
|
98
|
+ return error;
|
|
99
|
+ }
|
97
|
100
|
std::cout << std::endl;
|
98
|
101
|
}
|
99
|
102
|
}
|
100
|
103
|
|
101
|
104
|
return 0;
|
102
|
105
|
}
|
103
|
|
-}
|
104
|
106
|
|
105
|
|
-int main(int argc, char *argv[]) {
|
106
|
|
- std::cout << "Tomb Raider 2:" << std::endl;
|
107
|
|
- char *f = fullPath("~/.OpenRaider/paks/tr2/TOMBPC.DAT", 0);
|
108
|
|
- int error = test(f, !((argc > 1) && (argv[1][0] == 's')));
|
109
|
|
- delete [] f;
|
|
107
|
+ int test(const char *file, unsigned int n) {
|
|
108
|
+ Script s;
|
110
|
109
|
|
111
|
|
- if (error != 0)
|
112
|
|
- return error;
|
|
110
|
+ std::cout << "Testing " << testDescription[n] << std::endl;
|
113
|
111
|
|
114
|
|
- std::cout << std::endl << "Tomb Raider 3:" << std::endl;
|
115
|
|
- f = fullPath("~/.OpenRaider/paks/tr3/TOMBPC.DAT", 0);
|
116
|
|
- error = test(f, !((argc > 1) && (argv[1][0] == 's')));
|
117
|
|
- delete [] f;
|
|
112
|
+ if (s.load(file) != 0) {
|
|
113
|
+ std::cout << "Could not open file " << file << std::endl;
|
|
114
|
+ return 1;
|
|
115
|
+ }
|
118
|
116
|
|
119
|
|
- if (error != 0)
|
120
|
|
- return error;
|
|
117
|
+ if (s.gameStringCount() != testExpectedGameStringCount[n]) {
|
|
118
|
+ std::cout << "Game String Count " << s.gameStringCount() << " != " << testExpectedGameStringCount[n] << std::endl;
|
|
119
|
+ return 2;
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ if (s.pcStringCount() != testExpectedPlatformStringCount[n]) {
|
|
123
|
+ std::cout << "Platform String Count " << s.pcStringCount() << " != " << testExpectedPlatformStringCount[n] << std::endl;
|
|
124
|
+ return 3;
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ std::cout << "Success!" << std::endl << std::endl;
|
|
128
|
+ return 0;
|
|
129
|
+ }
|
|
130
|
+
|
|
131
|
+ void readPayloadChunk(const unsigned char *data, unsigned int size, const char *file) {
|
|
132
|
+ static const unsigned int bufferSize = 16384; // 16K should be enough for everybody :)
|
|
133
|
+ unsigned char buffer[bufferSize];
|
|
134
|
+
|
|
135
|
+ z_stream stream;
|
|
136
|
+ stream.zalloc = Z_NULL;
|
|
137
|
+ stream.zfree = Z_NULL;
|
|
138
|
+ stream.opaque = Z_NULL;
|
|
139
|
+ assertEqual(inflateInit2(&stream, 16), Z_OK); // 16 -> gzip header
|
|
140
|
+
|
|
141
|
+ // Inflate data in one go
|
|
142
|
+ stream.avail_in = size;
|
|
143
|
+ stream.next_in = const_cast<unsigned char *>(data);
|
|
144
|
+ stream.avail_out = bufferSize;
|
|
145
|
+ stream.next_out = buffer;
|
|
146
|
+ assertEqual(inflate(&stream, Z_FINISH), Z_STREAM_END);
|
|
147
|
+ inflateEnd(&stream);
|
|
148
|
+
|
|
149
|
+ // Write buffer to file
|
|
150
|
+ std::ofstream s(file, std::ios_base::out | std::ios_base::binary);
|
|
151
|
+ s.write(reinterpret_cast<const char *>(buffer), bufferSize - stream.avail_out);
|
|
152
|
+ }
|
|
153
|
+
|
|
154
|
+ int runForPayload(unsigned int n, bool print, bool printData) {
|
|
155
|
+ assert(n < testPayloadCount);
|
|
156
|
+ // Get temp file name
|
|
157
|
+ char tmpFile[] = "/tmp/openraider_unit_test_0";
|
|
158
|
+ FILE *f;
|
|
159
|
+ while ((f = fopen(tmpFile, "r")) != NULL) {
|
|
160
|
+ fclose(f);
|
|
161
|
+ tmpFile[25]++;
|
|
162
|
+ }
|
|
163
|
+
|
|
164
|
+ std::cout << "Temporary test file: " << tmpFile << std::endl;
|
121
|
165
|
|
122
|
|
- std::cout << std::endl << "Tomb Raider 2 PSX:" << std::endl;
|
123
|
|
- f = fullPath("~/.OpenRaider/paks/tr2_psx/TOMBPSX.DAT", 0);
|
124
|
|
- error = test(f, !((argc > 1) && (argv[1][0] == 's')));
|
125
|
|
- delete [] f;
|
|
166
|
+ readPayloadChunk(testPayloads[n], testSizes[n], tmpFile);
|
126
|
167
|
|
127
|
|
- if (error != 0)
|
|
168
|
+ int error = 0;
|
|
169
|
+ if (print) {
|
|
170
|
+ Script s;
|
|
171
|
+ error = s.load(tmpFile);
|
|
172
|
+ if (error == 0)
|
|
173
|
+ error = printDataScript(s, printData);
|
|
174
|
+ } else {
|
|
175
|
+ error = test(tmpFile, n);
|
|
176
|
+ }
|
|
177
|
+
|
|
178
|
+ remove(tmpFile);
|
128
|
179
|
return error;
|
|
180
|
+ }
|
|
181
|
+}
|
|
182
|
+
|
|
183
|
+int main(int argc, char *argv[]) {
|
|
184
|
+ bool printHelp = false;
|
|
185
|
+ bool print = false;
|
|
186
|
+ bool printData = true;
|
|
187
|
+ int whichFile = -1;
|
|
188
|
+
|
|
189
|
+ if (argc == 3) {
|
|
190
|
+ if ((strcmp(argv[1], "--printData") == 0)
|
|
191
|
+ || (strcmp(argv[1], "--printScript") == 0)) {
|
|
192
|
+ print = true;
|
|
193
|
+ if (strcmp(argv[1], "--printScript") == 0) {
|
|
194
|
+ printData = false;
|
|
195
|
+ }
|
|
196
|
+ assert(testPayloadCount < 10);
|
|
197
|
+ if ((argv[2][0] >= '0') && ((unsigned int)argv[2][0] <= (testPayloadCount + '0'))) {
|
|
198
|
+ whichFile = argv[2][0] - '0';
|
|
199
|
+ }
|
|
200
|
+ } else {
|
|
201
|
+ printHelp = true;
|
|
202
|
+ }
|
|
203
|
+ } else if (argc != 1) {
|
|
204
|
+ printHelp = true;
|
|
205
|
+ }
|
129
|
206
|
|
130
|
|
- std::cout << std::endl << "Tomb Raider 3 PSX:" << std::endl;
|
131
|
|
- f = fullPath("~/.OpenRaider/paks/tr3_psx/TOMBPSX.DAT", 0);
|
132
|
|
- error = test(f, !((argc > 1) && (argv[1][0] == 's')));
|
133
|
|
- delete [] f;
|
|
207
|
+ if (printHelp) {
|
|
208
|
+ std::cout << "Usage:" << std::endl;
|
|
209
|
+ std::cout << "\t" << argv[0] << " [--printData | --printScript] [N | /path]" << std::endl;
|
|
210
|
+ return 1;
|
|
211
|
+ }
|
134
|
212
|
|
135
|
|
- return error;
|
|
213
|
+ if (print) {
|
|
214
|
+ // Print single script
|
|
215
|
+ if (whichFile == -1) {
|
|
216
|
+ // From given path
|
|
217
|
+ Script s;
|
|
218
|
+ assertEqual(s.load(argv[2]), 0);
|
|
219
|
+ return printDataScript(s, printData);
|
|
220
|
+ } else {
|
|
221
|
+ // From payload
|
|
222
|
+ return runForPayload((unsigned int)whichFile, true, printData);
|
|
223
|
+ }
|
|
224
|
+ } else {
|
|
225
|
+ // Run test on all scripts in payload
|
|
226
|
+ for (unsigned int i = 0; i < testPayloadCount; i++) {
|
|
227
|
+ int error = runForPayload(i, false, false);
|
|
228
|
+ if (error != 0)
|
|
229
|
+ return error;
|
|
230
|
+ }
|
|
231
|
+ return 0;
|
|
232
|
+ }
|
136
|
233
|
}
|
137
|
234
|
|