summaryrefslogtreecommitdiff
path: root/tools/msgenc/msgenc.cpp
blob: 5caaf31c478ce98908b45df580ae9281f5dfb7f0 (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
/*
 * MSGENC: Encodes a Pokemon DP message file to binary
 *
 * Usage:
 *     msgenc TXTFILE KEYFILE CHARMAP OUTFILE
 */

#include <iostream>
#include "MessagesDecoder.h"
#include "MessagesEncoder.h"

static const char* progname = "msgenc";
static const char* version = "2021.08.27";


static inline void usage() {
    cout << progname << " v" << version << endl;
    cout << "Usage: " << progname << " [-h] [-v] -d|-e [-k KEY] -c CHARMAP INFILE OUTFILE" << endl;
    cout << endl;
    cout << "INFILE        Required: Path to the input file to convert (-e: plaintext; -d: binary)." << endl;
    cout << "OUTFILE       Required: Path to the output file (-e: binary; -d: plaintext)." << endl;
    cout << "-c CHARMAP    Required: Path to a text file with a character mapping, for example pokeheartgold/charmap.txt." << endl;
    cout << "-d            Decode from binary to text, also print the key" << endl;
    cout << "-e            Encode from text to binary using the provided key" << endl;
    cout << "-k KEY        The 16-bit encryption key for this message bank. Default: computes it from the binary file name" << endl;
    cout << "-v            Print the program version and exit." << endl;
    cout << "-h            Print this message and exit." << endl;
    cout << "-D DUMPNAME   Dump the intermediate binary (after decryption or before encryption)." << endl;
}

struct Options {
    ConvertMode mode = CONV_INVALID;
    int key = 0;
    vector<string> posargs;
    string failReason;
    string charmap;
    bool printUsage = false;
    bool printVersion = false;
    string dumpBinary;
    Options(int argc, char ** argv) {
        for (int i = 1; i < argc; i++) {
            string arg(argv[i]);
            if (arg == "-d") {
                mode = CONV_DECODE;
            } else if (arg == "-e") {
                mode = CONV_ENCODE;
            } else if (arg == "-h") {
                printUsage = true;
                return;
            } else if (arg == "-v") {
                printVersion = true;
                return;
            } else if (arg == "-k") {
                key = stoi(argv[++i], nullptr, 0);
                // If the key is 0, ensure that it is not overridden by the CRC.
                key &= 0xFFFF;
                key |= 0x10000;
            } else if (arg == "-c") {
                charmap = argv[++i];
            } else if (arg == "-D") {
                dumpBinary = argv[++i];
            } else if (arg[0] != '-') {
                posargs.push_back(arg);
            } else {
                failReason = "unrecognized option: " + arg;
                break;
            }
        }
        if (posargs.size() < 2) {
            failReason = "missing required positional argument: " + (string[]){"INFILE", "OUTFILE"}[posargs.size()];
        }
        if (mode == CONV_INVALID) {
            failReason = "missing mode flag: -d or -e is required";
        }
        if (charmap.empty()) {
            failReason = "missing charmap file: -c CHARMAP is required";
        }
    }
};

int main(int argc, char ** argv) {
    try {
        Options options(argc, argv);
        if (options.printUsage || !options.failReason.empty()) {
            usage();
            if (!options.failReason.empty()) {
                throw invalid_argument(options.failReason);
            }
            return 0;
        } else if (options.printVersion) {
            cout << progname << " v" << version << endl;
            return 0;
        }

        MessagesConverter *converter;
        if (options.mode == CONV_DECODE)
        {
            converter = new MessagesDecoder(options.posargs[1], options.key, options.charmap, options.posargs[0]);
        }
        else
        {
            converter = new MessagesEncoder(options.posargs[0], options.key, options.charmap, options.posargs[1]);
        }
        converter->ReadInput();
        converter->ReadCharmap();
        converter->Convert();
        if (!options.dumpBinary.empty())
            converter->WriteBinaryDecoded(options.dumpBinary);
        converter->WriteOutput();
        if (options.mode == CONV_DECODE) {
            cout << "Key: " << hex << converter->GetKey() << endl;
        }
        delete converter;
    } catch (invalid_argument& ia) {
        cerr << "Invalid Argument: " << ia.what() << endl;
        return 1;
    } catch (ios_base::failure& iof) {
        cerr << "IO Failure: " << iof.what() << endl;
        return 1;
    } catch (runtime_error& exc) {
        cerr << "Runtime Error: " << exc.what() << endl;
        return 1;
    }
    return 0;
}