Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*!
  2. * \file test/Script.cpp
  3. * \brief Game script loader unit test
  4. *
  5. * \author xythobuz
  6. */
  7. #include <iostream>
  8. #include <cstring>
  9. #include <cstdlib>
  10. #include <zlib.h>
  11. #include "global.h"
  12. #include "utils/strings.h"
  13. #include "Script.h"
  14. #include "ScriptTest.h"
  15. #define printStrings(cnt, acc, name) { \
  16. std::cout << name << " (" << cnt << ")" << std::endl; \
  17. for (unsigned int i = 0; i < cnt; i++) { \
  18. std::cout << " " << acc(i) << std::endl; \
  19. } \
  20. std::cout << std::endl; \
  21. }
  22. #define printStrings2D(c, cnt, acc, name) { \
  23. std::cout << name << " (" << c << "*" << cnt << ")" << std::endl; \
  24. for (unsigned int a = 0; a < cnt; a++) { \
  25. std::cout << " "; \
  26. for (unsigned int i = 0; i < c; i++) { \
  27. std::cout << acc(i, a); \
  28. if (i < (c - 1)) \
  29. std::cout << " | "; \
  30. } \
  31. std::cout << std::endl; \
  32. } \
  33. std::cout << std::endl; \
  34. }
  35. #define registerLambda(x, y) { \
  36. s.registerScriptHandler(x, [](bool hasOperand, uint16_t operand) { \
  37. std::cout << "\t" << y; \
  38. if (hasOperand) \
  39. std::cout << " (" << operand << ")"; \
  40. std::cout << std::endl; \
  41. return 0; \
  42. }); \
  43. }
  44. namespace {
  45. int printDataScript(Script &s, bool strings) {
  46. if (strings) {
  47. printStrings(s.levelCount(), s.getLevelName, "Level Names");
  48. printStrings(s.levelCount(), s.getLevelFilename, "Level Filenames");
  49. printStrings(s.pictureCount(), s.getPictureFilename, "Picture Filenames");
  50. printStrings(s.cutsceneCount(), s.getCutsceneFilename, "Cutscenes");
  51. printStrings(s.titleCount(), s.getTitleFilename, "Titles");
  52. printStrings(s.videoCount(), s.getVideoFilename, "Videos");
  53. printStrings(s.gameStringCount(), s.getGameString, "Game Strings");
  54. printStrings(s.pcStringCount(), s.getPCString, "PC Strings");
  55. printStrings2D(4, s.levelCount(), s.getPuzzleString, "Puzzles");
  56. printStrings2D(2, s.levelCount(), s.getPickupString, "Pickups");
  57. printStrings2D(4, s.levelCount(), s.getKeyString, "Keys");
  58. } else {
  59. registerLambda(Script::OP_PICTURE, "Picture");
  60. registerLambda(Script::OP_PSX_TRACK, "PSX-Track");
  61. registerLambda(Script::OP_PSX_FMV, "PSX-FMV");
  62. registerLambda(Script::OP_FMV, "Show FMV");
  63. registerLambda(Script::OP_GAME, "Load level");
  64. registerLambda(Script::OP_CUT, "Cutscene");
  65. registerLambda(Script::OP_COMPLETE, "Level finished");
  66. registerLambda(Script::OP_DEMO, "Demo sequence");
  67. registerLambda(Script::OP_PSX_DEMO, "PSX-Demo");
  68. registerLambda(Script::OP_END, "End of script");
  69. registerLambda(Script::OP_TRACK, "Sound Track");
  70. registerLambda(Script::OP_SUNSET, "Sunset");
  71. registerLambda(Script::OP_LOAD_PIC, "Load picture");
  72. registerLambda(Script::OP_DEADLY_WATER, "Deadly water");
  73. registerLambda(Script::OP_REMOVE_WEAPONS, "Remove weapons");
  74. registerLambda(Script::OP_GAMECOMPLETE, "End of game!");
  75. registerLambda(Script::OP_CUTANGLE, "Cutscene angle");
  76. registerLambda(Script::OP_NOFLOOR, "No floor, fall death");
  77. registerLambda(Script::OP_STARTINV, "Inventory/Bonus");
  78. registerLambda(Script::OP_STARTANIM, "Start animation");
  79. registerLambda(Script::OP_SECRETS, "Secrets");
  80. registerLambda(Script::OP_KILLTOCOMPLETE, "Kill to complete level");
  81. registerLambda(Script::OP_REMOVE_AMMO, "Remove ammo");
  82. for (unsigned int i = 0; i < (s.levelCount() + 1); i++) {
  83. if (i == 0)
  84. std::cout << "Script for Title:" << std::endl;
  85. else
  86. std::cout << "Script for \"" << s.getLevelName(i - 1) << "\" (" << i - 1 << "):" << std::endl;
  87. int error = s.runScript(i);
  88. if (error != 0) {
  89. std::cout << "Returned " << error << "..." << std::endl;
  90. return error;
  91. }
  92. std::cout << std::endl;
  93. }
  94. }
  95. return 0;
  96. }
  97. int test(const char *file, unsigned int n) {
  98. Script s;
  99. std::cout << "Testing " << testDescription[n] << std::endl;
  100. if (s.load(file) != 0) {
  101. std::cout << "Could not open file " << file << std::endl;
  102. return 1;
  103. }
  104. if (s.gameStringCount() != testExpectedGameStringCount[n]) {
  105. std::cout << "Game String Count " << s.gameStringCount() << " != " << testExpectedGameStringCount[n] << std::endl;
  106. return 2;
  107. }
  108. if (s.pcStringCount() != testExpectedPlatformStringCount[n]) {
  109. std::cout << "Platform String Count " << s.pcStringCount() << " != " << testExpectedPlatformStringCount[n] << std::endl;
  110. return 3;
  111. }
  112. std::cout << "Success!" << std::endl << std::endl;
  113. return 0;
  114. }
  115. int readPayloadChunk(const unsigned char *data, unsigned int size, const char *file) {
  116. static const unsigned int bufferSize = 16384; // 16K should be enough for everybody :)
  117. unsigned char buffer[bufferSize];
  118. // Initialize decompression
  119. z_stream stream;
  120. stream.zalloc = Z_NULL;
  121. stream.zfree = Z_NULL;
  122. stream.opaque = Z_NULL;
  123. int error = inflateInit(&stream);
  124. if (error != Z_OK) {
  125. std::cout << "inflateInit() Error " << error << std::endl;
  126. return 1;
  127. }
  128. // Inflate data in one go
  129. stream.avail_in = size;
  130. stream.next_in = const_cast<unsigned char *>(data);
  131. stream.avail_out = bufferSize;
  132. stream.next_out = buffer;
  133. error = inflate(&stream, Z_FINISH);
  134. if (error != Z_STREAM_END) {
  135. std::cout << "inflate() Error " << error << std::endl;
  136. return 2;
  137. }
  138. inflateEnd(&stream);
  139. // Write buffer to file
  140. std::ofstream s(file, std::ios_base::out | std::ios_base::binary);
  141. s.write(reinterpret_cast<const char *>(buffer), bufferSize - stream.avail_out);
  142. return 0;
  143. }
  144. int runForPayload(unsigned int n, bool print, bool printData) {
  145. assert(n < testPayloadCount);
  146. // Get temp file name
  147. char tmpFile[] = "/tmp/openraider_unit_test_0";
  148. FILE *f;
  149. while ((f = fopen(tmpFile, "r")) != NULL) {
  150. fclose(f);
  151. tmpFile[26]++;
  152. }
  153. std::cout << "Temporary test file: " << tmpFile << std::endl;
  154. int error = readPayloadChunk(testPayloads[n], testSizes[n], tmpFile);
  155. if (error == 0) {
  156. if (print) {
  157. Script s;
  158. error = s.load(tmpFile);
  159. if (error == 0)
  160. error = printDataScript(s, printData);
  161. else
  162. std::cout << "Error loading script!" << std::endl;
  163. } else {
  164. error = test(tmpFile, n);
  165. }
  166. }
  167. remove(tmpFile);
  168. return error;
  169. }
  170. }
  171. int main(int argc, char *argv[]) {
  172. bool printHelp = false;
  173. bool print = false;
  174. bool printData = true;
  175. int whichFile = -1;
  176. if (argc == 3) {
  177. if ((strcmp(argv[1], "--printData") == 0)
  178. || (strcmp(argv[1], "--printScript") == 0)) {
  179. print = true;
  180. if (strcmp(argv[1], "--printScript") == 0) {
  181. printData = false;
  182. }
  183. assert(testPayloadCount < 10);
  184. if ((argv[2][0] >= '0') && ((unsigned int)argv[2][0] <= (testPayloadCount + '0'))) {
  185. whichFile = argv[2][0] - '0';
  186. }
  187. } else {
  188. printHelp = true;
  189. }
  190. } else if (argc != 1) {
  191. printHelp = true;
  192. }
  193. if (printHelp) {
  194. std::cout << "Usage:" << std::endl;
  195. std::cout << "\t" << argv[0] << " [--printData | --printScript] [N | /path]" << std::endl;
  196. return 1;
  197. }
  198. if (print) {
  199. // Print single script
  200. if (whichFile == -1) {
  201. // From given path
  202. Script s;
  203. assertEqual(s.load(argv[2]), 0);
  204. return printDataScript(s, printData);
  205. } else {
  206. // From payload
  207. return runForPayload((unsigned int)whichFile, true, printData);
  208. }
  209. } else {
  210. // Run test on all scripts in payload
  211. for (unsigned int i = 0; i < testPayloadCount; i++) {
  212. int error = runForPayload(i, false, false);
  213. if (error != 0)
  214. return error;
  215. }
  216. return 0;
  217. }
  218. }