浏览代码

Parser working with TR2 Script

Thomas Buck 9 年前
父节点
当前提交
10b62cf4d6
共有 4 个文件被更改,包括 53 次插入43 次删除
  1. 1
    0
      ChangeLog.md
  2. 1
    1
      include/Script.h
  3. 26
    23
      src/Script.cpp
  4. 25
    19
      test/Script.cpp

+ 1
- 0
ChangeLog.md 查看文件

@@ -4,6 +4,7 @@
4 4
 
5 5
     [ 20140806 ]
6 6
     * Improved Script reader and its Unit Test
7
+    * Successfully parsing the TR2 Script strings!
7 8
 
8 9
     [ 20140804 ]
9 10
     * Added preliminary Tomb Raider Script parser

+ 1
- 1
include/Script.h 查看文件

@@ -58,7 +58,7 @@ public:
58 58
 
59 59
 private:
60 60
 
61
-    void readStringPackage(BinaryFile &f, std::vector<std::string> &v, unsigned int n);
61
+    void readStringPackage(BinaryFile &f, std::vector<std::string> &v, unsigned int n, bool tag, uint16_t off);
62 62
 
63 63
     enum ScriptFlags {
64 64
         S_DemoVersion            = (1 << 0),  //!< Don't load a MAIN.SFX

+ 26
- 23
src/Script.cpp 查看文件

@@ -55,6 +55,7 @@ int Script::load(const char *file) {
55 55
     f.seek(f.tell() + 32);
56 56
 
57 57
     flags = f.readU16();
58
+    bool tag = (flags & S_UseSecurityTag) != 0;
58 59
 
59 60
     // Filler 3 (6 bytes)
60 61
     f.seek(f.tell() + 6);
@@ -67,48 +68,50 @@ int Script::load(const char *file) {
67 68
     f.seek(f.tell() + 4);
68 69
 
69 70
     // Strings
70
-    readStringPackage(f, levelNames, numLevels);
71
-    readStringPackage(f, pictureFilenames, numPictures);
72
-    readStringPackage(f, titleFilenames, numTitles);
73
-    readStringPackage(f, fmvFilenames, numFMVs);
74
-    readStringPackage(f, levelFilenames, numLevels);
75
-    readStringPackage(f, cutsceneFilenames, numCutscenes);
76
-    readStringPackage(f, script, numLevels + 1);
71
+    readStringPackage(f, levelNames, numLevels, tag, 0);
72
+    readStringPackage(f, pictureFilenames, numPictures, tag, 0);
73
+    readStringPackage(f, titleFilenames, numTitles, tag, 0);
74
+    readStringPackage(f, fmvFilenames, numFMVs, tag, 0);
75
+    readStringPackage(f, levelFilenames, numLevels, tag, 0);
76
+    readStringPackage(f, cutsceneFilenames, numCutscenes, tag, 0);
77
+
78
+    // Offset definitely TR2 specific?!
79
+    readStringPackage(f, script, numLevels + 1, false, 6);
77 80
 
78 81
     numGameStrings = f.readU16();
79
-    numGameStrings = 89; // Ignored?
82
+    assert(numGameStrings == 89);
80 83
 
81
-    readStringPackage(f, gameStrings, numGameStrings);
84
+    readStringPackage(f, gameStrings, numGameStrings, tag, 0);
82 85
 
83
-    readStringPackage(f, pcStrings, 41);
86
+    readStringPackage(f, pcStrings, 41, tag, 0);
84 87
 
85
-    readStringPackage(f, puzzles[0], numLevels);
86
-    readStringPackage(f, puzzles[1], numLevels);
87
-    readStringPackage(f, puzzles[2], numLevels);
88
-    readStringPackage(f, puzzles[3], numLevels);
88
+    readStringPackage(f, puzzles[0], numLevels, tag, 0);
89
+    readStringPackage(f, puzzles[1], numLevels, tag, 0);
90
+    readStringPackage(f, puzzles[2], numLevels, tag, 0);
91
+    readStringPackage(f, puzzles[3], numLevels, tag, 0);
89 92
 
90
-    readStringPackage(f, pickups[0], numLevels);
91
-    readStringPackage(f, pickups[1], numLevels);
93
+    readStringPackage(f, pickups[0], numLevels, tag, 0);
94
+    readStringPackage(f, pickups[1], numLevels, tag, 0);
92 95
 
93
-    readStringPackage(f, keys[0], numLevels);
94
-    readStringPackage(f, keys[1], numLevels);
95
-    readStringPackage(f, keys[2], numLevels);
96
-    readStringPackage(f, keys[3], numLevels);
96
+    readStringPackage(f, keys[0], numLevels, tag, 0);
97
+    readStringPackage(f, keys[1], numLevels, tag, 0);
98
+    readStringPackage(f, keys[2], numLevels, tag, 0);
99
+    readStringPackage(f, keys[3], numLevels, tag, 0);
97 100
 
98 101
     return 0;
99 102
 }
100 103
 
101
-void Script::readStringPackage(BinaryFile &f, std::vector<std::string> &v, unsigned int n) {
104
+void Script::readStringPackage(BinaryFile &f, std::vector<std::string> &v, unsigned int n, bool tag, uint16_t off) {
102 105
     uint16_t *offset = new uint16_t[n];
103 106
     for (unsigned int i = 0; i < n; i++)
104 107
         offset[i] = f.readU16();
105 108
 
106
-    uint16_t numBytes = f.readU16();
109
+    uint16_t numBytes = f.readU16() + off;
107 110
 
108 111
     char *list = new char[numBytes];
109 112
     for (uint16_t i = 0; i < numBytes; i++) {
110 113
         list[i] = f.read8();
111
-        if (flags & S_UseSecurityTag) {
114
+        if (tag) {
112 115
             list[i] ^= cypherCode;
113 116
         }
114 117
     }

+ 25
- 19
test/Script.cpp 查看文件

@@ -25,34 +25,40 @@
25 25
     for (unsigned int a = 0; a < cnt; a++) { \
26 26
         std::cout << "    "; \
27 27
         for (unsigned int i = 0; i < c; i++) { \
28
-            std::cout << acc(i, a) << " "; \
28
+            std::cout << acc(i, a); \
29
+            if (i < (c - 1)) \
30
+                std::cout << " | "; \
29 31
         } \
30 32
         std::cout << std::endl; \
31 33
     } \
32 34
     std::cout << std::endl; \
33 35
 }
34 36
 
35
-int main() {
36
-    Script s;
37
-    char *f = fullPath("~/.OpenRaider/paks/tr2/TOMBPC.DAT", 0);
38
-    assertEqual(s.load(f), 0);
39
-    delete [] f;
37
+namespace {
38
+    int test(const char *f) {
39
+        Script s;
40
+        assertEqual(s.load(f), 0);
40 41
 
41
-    printStrings(s.levelCount(), s.getLevelName, "Level Names");
42
-    printStrings(s.levelCount(), s.getLevelFilename, "Level Filenames");
43
-    printStrings(s.cutsceneCount(), s.getCutsceneFilename, "Cutscenes");
44
-    printStrings(s.titleCount(), s.getTitleFilename, "Titles");
45
-    printStrings(s.videoCount(), s.getVideoFilename, "Videos");
46
-    printStrings(s.gameStringCount(), s.getGameString, "Game Strings");
47
-    printStrings(s.pcStringCount(), s.getPCString, "PC Strings");
42
+        printStrings(s.levelCount(), s.getLevelName, "Level Names");
43
+        printStrings(s.levelCount(), s.getLevelFilename, "Level Filenames");
44
+        printStrings(s.cutsceneCount(), s.getCutsceneFilename, "Cutscenes");
45
+        printStrings(s.titleCount(), s.getTitleFilename, "Titles");
46
+        printStrings(s.videoCount(), s.getVideoFilename, "Videos");
47
+        printStrings(s.gameStringCount(), s.getGameString, "Game Strings");
48
+        printStrings(s.pcStringCount(), s.getPCString, "PC Strings");
48 49
 
49
-    printStrings2D(4, s.levelCount(), s.getPuzzleString, "Puzzles");
50
-    printStrings2D(2, s.levelCount(), s.getPickupString, "Pickups");
51
-    printStrings2D(4, s.levelCount(), s.getKeyString, "Keys");
50
+        printStrings2D(4, s.levelCount(), s.getPuzzleString, "Puzzles");
51
+        printStrings2D(2, s.levelCount(), s.getPickupString, "Pickups");
52
+        printStrings2D(4, s.levelCount(), s.getKeyString, "Keys");
52 53
 
53
-    //std::cout << "This test always fails so you can see its output!" << std::endl;
54
-    //return 1;
54
+        return 0;
55
+    }
56
+}
55 57
 
56
-    return 0;
58
+int main() {
59
+    char *f = fullPath("~/.OpenRaider/paks/tr2/TOMBPC.DAT", 0);
60
+    int error = test(f);
61
+    delete [] f;
62
+    return error;
57 63
 }
58 64
 

正在加载...
取消
保存