Open Source Tomb Raider Engine
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Script.cpp 8.5KB

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