-
Notifications
You must be signed in to change notification settings - Fork 6
/
read_fxt.cpp
147 lines (133 loc) · 4.07 KB
/
read_fxt.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/************************************************************************
* Copyright (c) 2005-2007 [email protected] *
* *
* This file contains code derived from information copyrighted by *
* DMA Design. It may not be used in a commercial product. *
* *
* See license.txt for details. *
* *
* This notice may not be removed or altered. *
************************************************************************/
#include <stdio.h>
#include <iostream>
#include "opengta.h"
#include "m_exceptions.h"
#include "log.h"
namespace OpenGTA {
MessageDB::MessageDB() {
load("ENGLISH.FXT");
_error = "ERROR";
}
MessageDB::MessageDB(const std::string &file) {
load(file);
}
MessageDB::~MessageDB() {
messages.clear();
}
void MessageDB::load(const std::string &file) {
PHYSFS_file* f = PHYSFS_openRead(file.c_str());
if (f == NULL) {
std::string f2(file);
transform(f2.begin(), f2.end(), f2.begin(), tolower);
f = PHYSFS_openRead(f2.c_str());
}
if (f == NULL) {
throw E_FILENOTFOUND(file);
}
messages.clear();
unsigned char v;
int c = -1;
unsigned char addVal = 0;
char buff[200];
int i = 0;
std::string tmp;
while (!PHYSFS_eof(f)) {
PHYSFS_read(f, static_cast<void*>(&v), 1, 1);
/* thanks to: Michael Mendelsohn
* http://gta.mendelsohn.de/
*/
v--; // decode: decrease by one
c++; // helper: count bytes read
if (c <= 7) // polyalphabetic code for the first 8 bytes
v -= (0x63 << c);
/* another twist: skip and add 64 (minus decoding) to next */
if (v == 195) {
addVal = 64;
}
else {
v += addVal;
addVal = 0;
if (v == '[') {
i = 0;
}
else if (v == ']') {
buff[i] = 0x00;
tmp = std::string(buff);
i = 0;
}
else if (v == 0x00) {
buff[i] = 0x00;
if (tmp.length() > 0)
messages[tmp] = std::string(buff);
//std::cout << tmp << " : " << buff << std::endl;
/*else
std::cout << "Skipping: " << tmp << ": " << buff << std::endl;*/
#ifdef FXT_TEST
std::cout << tmp << " : " << buff << std::endl;
#endif
}
else {
buff[i] = v;
i++;
if (i>199)
i=0;
}
}
}
PHYSFS_close(f);
}
const std::string& MessageDB::getText(const char* id) {
std::map<std::string, std::string>::iterator i = messages.find(std::string(id));
if (i == messages.end()) {
ERROR << "string lookup failed for key: " << id << std::endl;
return _error;
}
return i->second;
}
const std::string& MessageDB::getText(const std::string &id) {
std::map<std::string, std::string>::iterator i = messages.find(id);
if (i == messages.end()) {
ERROR << "string lookup failed for key: " << id << std::endl;
return _error;
}
return i->second;
}
const std::string& MessageDB::getText(const uint32_t id) {
char tmp[10];
snprintf(reinterpret_cast<char*>(&tmp), 10, "%i", id);
std::map<std::string, std::string>::iterator i = messages.find(std::string(tmp));
if (i == messages.end()) {
ERROR << "string lookup failed for key: " << id << std::endl;
return _error;
}
return i->second;
}
}
#if FXT_TEST
#include "file_helper.h"
int main(int argc, char* argv[]) {
PHYSFS_init(argv[0]);
PHYSFS_addToSearchPath("gtadata.zip", 1);
const char* lang = getenv("OGTA_LANG");
if (!lang)
lang = getenv("LANG");
if (!lang)
lang = "en";
OpenGTA::MessageDB* strings = new OpenGTA::MessageDB(
Util::FileHelper::lang2MsgFilename(lang)
);
std::cout << strings->getText(1001) << std::endl;
delete strings;
PHYSFS_deinit();
}
#endif