Parcourir la source

Improved Script reader and its Unit Test

Thomas Buck il y a 9 ans
Parent
révision
7cff58d7cd
9 fichiers modifiés avec 142 ajouts et 23 suppressions
  1. 3
    0
      ChangeLog.md
  2. 4
    0
      include/Script.h
  3. 2
    1
      include/utils/binary.h
  4. 42
    8
      src/Script.cpp
  5. 22
    4
      src/utils/binary.cpp
  6. 1
    1
      test/CMakeLists.txt
  7. 45
    2
      test/Script.cpp
  8. 3
    7
      test/binary.cpp
  9. 20
    0
      test/test.h

+ 3
- 0
ChangeLog.md Voir le fichier

2
 
2
 
3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
3
 ## OpenRaider (0.1.3) xythobuz <xythobuz@xythobuz.de>
4
 
4
 
5
+    [ 20140806 ]
6
+    * Improved Script reader and its Unit Test
7
+
5
     [ 20140804 ]
8
     [ 20140804 ]
6
     * Added preliminary Tomb Raider Script parser
9
     * Added preliminary Tomb Raider Script parser
7
     * Moved Unit Test folder
10
     * Moved Unit Test folder

+ 4
- 0
include/Script.h Voir le fichier

52
     unsigned int pcStringCount();
52
     unsigned int pcStringCount();
53
     std::string getPCString(unsigned int i);
53
     std::string getPCString(unsigned int i);
54
 
54
 
55
+    std::string getPuzzleString(unsigned int i, unsigned int j);
56
+    std::string getPickupString(unsigned int i, unsigned int j);
57
+    std::string getKeyString(unsigned int i, unsigned int j);
58
+
55
 private:
59
 private:
56
 
60
 
57
     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);

+ 2
- 1
include/utils/binary.h Voir le fichier

13
 class BinaryFile {
13
 class BinaryFile {
14
 public:
14
 public:
15
 
15
 
16
-    BinaryFile(const char *f);
17
     ~BinaryFile();
16
     ~BinaryFile();
18
 
17
 
18
+    int open(const char *f);
19
+
19
     long long tell();
20
     long long tell();
20
     void seek(long long pos);
21
     void seek(long long pos);
21
 
22
 

+ 42
- 8
src/Script.cpp Voir le fichier

8
 #include "global.h"
8
 #include "global.h"
9
 #include "Script.h"
9
 #include "Script.h"
10
 
10
 
11
-Script::Script() {
12
-
11
+Script::Script() : puzzles(4), pickups(2), keys(4) {
13
 }
12
 }
14
 
13
 
15
 Script::~Script() {
14
 Script::~Script() {
17
 }
16
 }
18
 
17
 
19
 int Script::load(const char *file) {
18
 int Script::load(const char *file) {
20
-    BinaryFile f(file);
19
+    BinaryFile f;
20
+
21
+    if (f.open(file) != 0)
22
+        return 1;
21
 
23
 
22
     version = f.readU32();
24
     version = f.readU32();
23
 
25
 
74
     readStringPackage(f, script, numLevels + 1);
76
     readStringPackage(f, script, numLevels + 1);
75
 
77
 
76
     numGameStrings = f.readU16();
78
     numGameStrings = f.readU16();
79
+    numGameStrings = 89; // Ignored?
80
+
77
     readStringPackage(f, gameStrings, numGameStrings);
81
     readStringPackage(f, gameStrings, numGameStrings);
78
 
82
 
79
     readStringPackage(f, pcStrings, 41);
83
     readStringPackage(f, pcStrings, 41);
80
 
84
 
81
-    //! \todo read puzzle strings
82
-    //std::vector<std::vector<std::string>> puzzles; // 4 * numLevels
83
-    //std::vector<std::vector<std::string>> pickups; // 2 * numLevels
84
-    //std::vector<std::vector<std::string>> keys; // 4 * numLevels
85
+    readStringPackage(f, puzzles[0], numLevels);
86
+    readStringPackage(f, puzzles[1], numLevels);
87
+    readStringPackage(f, puzzles[2], numLevels);
88
+    readStringPackage(f, puzzles[3], numLevels);
89
+
90
+    readStringPackage(f, pickups[0], numLevels);
91
+    readStringPackage(f, pickups[1], numLevels);
92
+
93
+    readStringPackage(f, keys[0], numLevels);
94
+    readStringPackage(f, keys[1], numLevels);
95
+    readStringPackage(f, keys[2], numLevels);
96
+    readStringPackage(f, keys[3], numLevels);
85
 
97
 
86
     return 0;
98
     return 0;
87
 }
99
 }
94
     uint16_t numBytes = f.readU16();
106
     uint16_t numBytes = f.readU16();
95
 
107
 
96
     char *list = new char[numBytes];
108
     char *list = new char[numBytes];
97
-    for (uint16_t i = 0; i < numBytes; i++)
109
+    for (uint16_t i = 0; i < numBytes; i++) {
98
         list[i] = f.read8();
110
         list[i] = f.read8();
111
+        if (flags & S_UseSecurityTag) {
112
+            list[i] ^= cypherCode;
113
+        }
114
+    }
99
 
115
 
100
     for (unsigned int i = 0; i < n; i++) {
116
     for (unsigned int i = 0; i < n; i++) {
101
         std::string tmp(list + offset[i]);
117
         std::string tmp(list + offset[i]);
165
     return pcStrings.at(i);
181
     return pcStrings.at(i);
166
 }
182
 }
167
 
183
 
184
+std::string Script::getPuzzleString(unsigned int i, unsigned int j) {
185
+    assert(i < 4);
186
+    assert(j < numLevels);
187
+    return puzzles.at(i).at(j);
188
+}
189
+
190
+std::string Script::getPickupString(unsigned int i, unsigned int j) {
191
+    assert(i < 2);
192
+    assert(j < numLevels);
193
+    return pickups.at(i).at(j);
194
+}
195
+
196
+std::string Script::getKeyString(unsigned int i, unsigned int j) {
197
+    assert(i < 4);
198
+    assert(j < numLevels);
199
+    return keys.at(i).at(j);
200
+}
201
+

+ 22
- 4
src/utils/binary.cpp Voir le fichier

5
  * \author xythobuz
5
  * \author xythobuz
6
  */
6
  */
7
 
7
 
8
+#include "global.h"
8
 #include "utils/binary.h"
9
 #include "utils/binary.h"
9
 
10
 
10
-BinaryFile::BinaryFile(const char *f) {
11
-    file.open(f, std::ios_base::in | std::ios_base::binary);
11
+BinaryFile::~BinaryFile() {
12
+    if (file.is_open())
13
+        file.close();
12
 }
14
 }
13
 
15
 
14
-BinaryFile::~BinaryFile() {
15
-    file.close();
16
+int BinaryFile::open(const char *f) {
17
+    if (file.is_open()) {
18
+        return 1;
19
+    } else {
20
+        file.open(f, std::ios_base::in | std::ios_base::binary);
21
+        return (file ? 0 : 1);
22
+    }
16
 }
23
 }
17
 
24
 
18
 long long BinaryFile::tell() {
25
 long long BinaryFile::tell() {
26
+    assert(file.is_open());
19
     return file.tellg();
27
     return file.tellg();
20
 }
28
 }
21
 
29
 
22
 void BinaryFile::seek(long long pos) {
30
 void BinaryFile::seek(long long pos) {
31
+    assert(file.is_open());
23
     file.seekg(pos);
32
     file.seekg(pos);
24
 }
33
 }
25
 
34
 
26
 uint8_t BinaryFile::readU8() {
35
 uint8_t BinaryFile::readU8() {
36
+    assert(file.is_open());
27
     uint8_t ret;
37
     uint8_t ret;
28
     char *c = reinterpret_cast<char *>(&ret);
38
     char *c = reinterpret_cast<char *>(&ret);
29
     file.read(c, 1);
39
     file.read(c, 1);
31
 }
41
 }
32
 
42
 
33
 uint16_t BinaryFile::readU16() {
43
 uint16_t BinaryFile::readU16() {
44
+    assert(file.is_open());
34
     uint8_t a = readU8();
45
     uint8_t a = readU8();
35
     uint8_t b = readU8();
46
     uint8_t b = readU8();
36
     return ((uint16_t)a | (uint16_t)(b << 8));
47
     return ((uint16_t)a | (uint16_t)(b << 8));
37
 }
48
 }
38
 
49
 
39
 uint32_t BinaryFile::readU32() {
50
 uint32_t BinaryFile::readU32() {
51
+    assert(file.is_open());
40
     uint16_t a = readU16();
52
     uint16_t a = readU16();
41
     uint16_t b = readU16();
53
     uint16_t b = readU16();
42
     return ((uint32_t)a | (uint32_t)(b << 16));
54
     return ((uint32_t)a | (uint32_t)(b << 16));
43
 }
55
 }
44
 
56
 
45
 uint64_t BinaryFile::readU64() {
57
 uint64_t BinaryFile::readU64() {
58
+    assert(file.is_open());
46
     uint32_t a = readU32();
59
     uint32_t a = readU32();
47
     uint32_t b = readU32();
60
     uint32_t b = readU32();
48
     return ((uint64_t)a | ((uint64_t)b << 32));
61
     return ((uint64_t)a | ((uint64_t)b << 32));
49
 }
62
 }
50
 
63
 
51
 float BinaryFile::readFloat() {
64
 float BinaryFile::readFloat() {
65
+    assert(file.is_open());
52
     uint32_t val = readU32();
66
     uint32_t val = readU32();
53
     char *a = reinterpret_cast<char *>(&val);
67
     char *a = reinterpret_cast<char *>(&val);
54
 
68
 
82
 }
96
 }
83
 
97
 
84
 int8_t BinaryFile::read8() {
98
 int8_t BinaryFile::read8() {
99
+    assert(file.is_open());
85
     int8_t ret;
100
     int8_t ret;
86
     char *p = reinterpret_cast<char *>(&ret);
101
     char *p = reinterpret_cast<char *>(&ret);
87
     file.read(p, sizeof(ret));
102
     file.read(p, sizeof(ret));
89
 }
104
 }
90
 
105
 
91
 int16_t BinaryFile::read16() {
106
 int16_t BinaryFile::read16() {
107
+    assert(file.is_open());
92
     int16_t ret;
108
     int16_t ret;
93
     char *p = reinterpret_cast<char *>(&ret);
109
     char *p = reinterpret_cast<char *>(&ret);
94
     file.read(p, sizeof(ret));
110
     file.read(p, sizeof(ret));
97
 }
113
 }
98
 
114
 
99
 int32_t BinaryFile::read32() {
115
 int32_t BinaryFile::read32() {
116
+    assert(file.is_open());
100
     int32_t ret;
117
     int32_t ret;
101
     char *p = reinterpret_cast<char *>(&ret);
118
     char *p = reinterpret_cast<char *>(&ret);
102
     file.read(p, sizeof(ret));
119
     file.read(p, sizeof(ret));
105
 }
122
 }
106
 
123
 
107
 int64_t BinaryFile::read64() {
124
 int64_t BinaryFile::read64() {
125
+    assert(file.is_open());
108
     int64_t ret;
126
     int64_t ret;
109
     char *p = reinterpret_cast<char *>(&ret);
127
     char *p = reinterpret_cast<char *>(&ret);
110
     file.read(p, sizeof(ret));
128
     file.read(p, sizeof(ret));

+ 1
- 1
test/CMakeLists.txt Voir le fichier

16
 
16
 
17
 add_executable (tester_script EXCLUDE_FROM_ALL
17
 add_executable (tester_script EXCLUDE_FROM_ALL
18
     "Script.cpp" "../src/Script.cpp" "../src/main.cpp"
18
     "Script.cpp" "../src/Script.cpp" "../src/main.cpp"
19
-    "../src/utils/binary.cpp"
19
+    "../src/utils/binary.cpp" "../src/utils/strings.cpp"
20
 )
20
 )
21
 add_dependencies (check tester_script)
21
 add_dependencies (check tester_script)
22
 add_test (NAME test_script COMMAND tester_script)
22
 add_test (NAME test_script COMMAND tester_script)

+ 45
- 2
test/Script.cpp Voir le fichier

1
 /*!
1
 /*!
2
- * \file src/Script.cpp
3
- * \brief Game script loader
2
+ * \file test/Script.cpp
3
+ * \brief Game script loader unit test
4
  *
4
  *
5
  * \author xythobuz
5
  * \author xythobuz
6
  */
6
  */
7
 
7
 
8
+#include <iostream>
9
+
8
 #include "global.h"
10
 #include "global.h"
11
+#include "test.h"
12
+#include "utils/strings.h"
9
 #include "Script.h"
13
 #include "Script.h"
10
 
14
 
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
+
23
+#define printStrings2D(c, cnt, acc, name) { \
24
+    std::cout << name << " (" << c << "*" << cnt << ")" << std::endl; \
25
+    for (unsigned int a = 0; a < cnt; a++) { \
26
+        std::cout << "    "; \
27
+        for (unsigned int i = 0; i < c; i++) { \
28
+            std::cout << acc(i, a) << " "; \
29
+        } \
30
+        std::cout << std::endl; \
31
+    } \
32
+    std::cout << std::endl; \
33
+}
34
+
11
 int main() {
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;
40
+
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");
48
+
49
+    printStrings2D(4, s.levelCount(), s.getPuzzleString, "Puzzles");
50
+    printStrings2D(2, s.levelCount(), s.getPickupString, "Pickups");
51
+    printStrings2D(4, s.levelCount(), s.getKeyString, "Keys");
52
+
53
+    //std::cout << "This test always fails so you can see its output!" << std::endl;
54
+    //return 1;
12
 
55
 
13
     return 0;
56
     return 0;
14
 }
57
 }

+ 3
- 7
test/binary.cpp Voir le fichier

10
 #include <fstream>
10
 #include <fstream>
11
 
11
 
12
 #include "global.h"
12
 #include "global.h"
13
+#include "test.h"
13
 #include "utils/binary.h"
14
 #include "utils/binary.h"
14
 
15
 
15
 
16
 
38
         file.write(reinterpret_cast<char *>(&f2), sizeof(f2));
39
         file.write(reinterpret_cast<char *>(&f2), sizeof(f2));
39
     }
40
     }
40
 
41
 
41
-#define assertEqual(x, y) if (x != y) {\
42
-    std::cout << "Assertion failed:" << std::endl; \
43
-    std::cout << #x << " == " << #y << " (" << x << ", " << y << ")" << std::endl; \
44
-    return 1; \
45
-}
46
-
47
     int test(const char *name) {
42
     int test(const char *name) {
48
-        BinaryFile file(name);
43
+        BinaryFile file;
44
+        assertEqual(file.open(name), 0);
49
 
45
 
50
         assertEqual(file.readU8(), 255);
46
         assertEqual(file.readU8(), 255);
51
         assertEqual(file.readU16(), 65535);
47
         assertEqual(file.readU16(), 65535);

+ 20
- 0
test/test.h Voir le fichier

1
+/*!
2
+ * \file test/test.h
3
+ * \brief Common Unit Test helpers
4
+ *
5
+ * \author xythobuz
6
+ */
7
+
8
+#ifndef _TEST_H
9
+#define _TEST_H
10
+
11
+#include <iostream>
12
+
13
+#define assertEqual(x, y) if (x != y) { \
14
+    std::cout << "Assertion failed:" << std::endl; \
15
+    std::cout << #x << " == " << #y << " (" << x << ", " << y << ")" << std::endl; \
16
+    return 1; \
17
+}
18
+
19
+#endif
20
+

Chargement…
Annuler
Enregistrer