summaryrefslogtreecommitdiff
path: root/tools/msgenc/msgenc.cpp
blob: 232a3bee92b3c90a87cfbeff86c575cc484e772b (plain)
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * MSGENC: Encodes a Pokemon DP message file to binary
 *
 * Usage:
 *     msgenc TXTFILE KEYFILE CHARMAP OUTFILE
 */

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>
#include <map>
#include <vector>
#include <algorithm>

struct MsgArcHeader
{
    uint16_t count;
    uint16_t key;
};

struct MsgAlloc
{
    uint32_t offset;
    uint32_t length;
};

using namespace std;

string ReadTextFile(string filename) {
    fstream file(filename);
    if (!file.good()) {
        stringstream s;
        s << "unable to open file \"" << filename << "\" for reading";
        throw runtime_error(s.str());
    }
    stringstream ss;
    ss << file.rdbuf();
    file.close();
    return ss.str();
}

static map<string, uint16_t> charmap;

void read_charmap(string filename) {
    string raw = ReadTextFile(filename);
    size_t pos, eqpos, last_pos = 0;
    while (last_pos != string::npos && (pos = raw.find_first_of("\r\n", last_pos)) != string::npos) {
        eqpos = raw.find('=', last_pos);
        if (eqpos == string::npos)
        {
            stringstream s;
            s << "charmap syntax error at " << (charmap.size() + 1);
            throw(runtime_error(s.str()));
        }
        string value = raw.substr(last_pos, eqpos - last_pos);
        string code = raw.substr(eqpos + 1, pos - eqpos - 1);
        uint16_t value_i = stoi(value, nullptr, 16);
        charmap[code] = value_i;
        last_pos = raw.find_last_of("\r\n", pos + 1, 2) + 1;
    }
}

static MsgArcHeader header;
vector<MsgAlloc> alloc_table;
static vector<string> files;
static vector<u16string> outfiles;

void read_key(string keyfname) {
    fstream keyfile(keyfname, ios_base::in | ios_base::binary);
    if (!keyfile.good()) {
        stringstream s;
        s << "unable to open file \"" << keyfname << "\" for reading";
        throw runtime_error(s.str());
    }
    keyfile.read((char *)&header.key, 2);
}

void read_msgs(string fname) {
    string text = ReadTextFile(fname);
    size_t pos = 0;
    do {
        text = text.substr(pos);
        if (text.empty())
            break;
        pos = text.find_first_of("\r\n");
        files.push_back(text.substr(0, pos));
        pos = text.find_last_of("\r\n", pos + 1, 2);
        if (pos == string::npos)
            break;
        pos++;
    } while (pos != string::npos);
    header.count = files.size();
}

uint16_t enc_short(uint16_t value, uint16_t & seed) {
    value ^= seed;
    seed += 18749;
    return value;
}

static map<string, uint16_t> cmdmap = {
    {"STRVAR", 0x0100},
    {"YESNO", 0x200},
    {"PAUSE", 0x201},
    {"WAIT", 0x202},
    {"CURSOR_X", 0x203},
    {"CURSOR_Y", 0x204},
    {"COLOR", 0xFF00},
    {"SIZE", 0xFF01}
};

void encode_messages() {
    int i = 1;
    for (auto message : files) {
        u16string encoded;
        uint16_t seed = i * 596947;
        for (size_t j = 0; j < message.size(); j++) {
            if (message[j] == '{') {
                size_t k = message.find('}', j);
                string enclosed = message.substr(j + 1, k - j - 1);
                j = k;
                size_t pos = enclosed.find(' ');
                string command = enclosed.substr(0, pos);
                enclosed = enclosed.substr(pos + 1);
                if (cmdmap.find(command) != cmdmap.end()) {
                    uint16_t command_i = cmdmap[command];
                    encoded += enc_short(0xFFFE, seed);
                    vector<uint16_t> args;
                    do {
                        k = enclosed.find(',');
                        string num = enclosed.substr(0, k);
                        uint16_t num_i = stoi(num);
                        args.push_back(num_i);
                        enclosed = enclosed.substr(k + 1);
                    } while (k++ != string::npos);

                    if (command == "STRVAR") {
                        command_i |= args[0];
                        args.erase(args.begin());
                    }
                    encoded += enc_short(command_i, seed);
                    encoded += enc_short(args.size(), seed);
                    for (auto num_i : args) {
                        encoded += enc_short(num_i, seed);
                    }
                } else {
                    encoded += enc_short(stoi(enclosed, nullptr, 16), seed);
                }
            } else {
                uint16_t code = 0;
                size_t k;
                string substr;
                for (k = 0; k < message.size() - j; k++) {
                    substr = message.substr(j, k + 1);
                    code = charmap[substr];
                    if (code != 0 || substr == "\\x0000")
                        break;
                }
                if (code == 0 && substr != "\\x0000") {
                    stringstream ss;
                    ss << "unrecognized character: file " << i << " pos " << (j + 1);
                    throw runtime_error(ss.str());
                }
                encoded += enc_short(code, seed);
                j += k;
            }
        }
        encoded += enc_short(0xFFFF, seed);
        MsgAlloc alloc {0, 0};
        if (i > 1) {
            alloc.offset = alloc_table[i - 2].offset + alloc_table[i - 2].length * 2;
        } else {
            alloc.offset = sizeof(header) + sizeof(MsgAlloc) * header.count;
        }
        alloc.length = encoded.size();
        outfiles.push_back(encoded);
        alloc_table.push_back(alloc);
        i++;
    }
    i = 1;
    for (auto & x : alloc_table) {
        uint32_t alloc_key = (765 * i * header.key) & 0xFFFF;
        alloc_key |= alloc_key << 16;
        x.offset ^= alloc_key;
        x.length ^= alloc_key;
        i++;
    }
}

void write_messages(string filename) {
    ofstream outfile(filename, ios_base::binary);
    outfile.write((char *)&header, sizeof(header));
    outfile.write((char *)alloc_table.data(), sizeof(MsgAlloc) * alloc_table.size());
    for (auto m : outfiles) {
        outfile.write((char *)m.c_str(), m.size() * 2);
    }
    outfile.close();
}

int main(int argc, char ** argv) {
    // msgenc TXTFILE KEYFILE CHARMAP OUTFILE
    if (argc < 5)
        throw invalid_argument("usage: msgenc TXTFILE KEYFILE CHARMAP OUTFILE");
    read_msgs(argv[1]);
    read_key(argv[2]);
    read_charmap(argv[3]);
    encode_messages();
    write_messages(argv[4]);
    return 0;
}