-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathra3mes.c
More file actions
160 lines (127 loc) · 4.5 KB
/
ra3mes.c
File metadata and controls
160 lines (127 loc) · 4.5 KB
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
// Copyright (c) 2026 SombrAbsol
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#ifdef _WIN32
#include <windows.h>
#endif
#include "utils.h"
// convert pokémon ranger 3 mes format (offset table) to json
static int mes_to_json(const char *input, const char *output) {
size_t size;
unsigned char *buf = read_file(input, &size);
if (!buf || size < 8) goto error;
uint32_t file_size = read_u32_le(buf);
uint32_t count = read_u32_le(buf + 4);
// validate header and offset table bounds
if (file_size != size || 8 + count * 4 > size) goto error;
char **strings = calloc(count, sizeof(char *));
if (!strings) goto error;
for (uint32_t i = 0; i < count; i++) {
uint32_t off = read_u32_le(buf + 8 + i * 4);
if (off >= size) goto error_strings;
strings[i] = xstrdup((char *)(buf + off));
if (!strings[i]) goto error_strings;
}
int res = write_json_strings(output, strings, count);
free_string_array(strings, count);
free(buf);
return res;
error_strings:
free_string_array(strings, count);
error:
free(buf);
return EXIT_FAILURE;
}
// convert json to pokémon ranger 3 mes format
static int json_to_mes(const char *input, const char *output) {
uint32_t count;
char **strings = read_json_strings(input, &count);
if (!strings) return EXIT_FAILURE;
uint32_t *offsets = malloc(count * sizeof(uint32_t));
if (!offsets) goto error;
// header + offset table size
uint32_t header = 8 + count * 4;
uint32_t cur = header;
// precompute string offsets
for (uint32_t i = 0; i < count; i++) {
offsets[i] = cur;
uint32_t len = (uint32_t)strlen(strings[i]) + 1;
cur += len + pad4(len);
}
FILE *f = fopen(output, "wb");
if (!f) goto error;
unsigned char b[4];
write_u32_le(b, cur); fwrite(b, 1, 4, f);
write_u32_le(b, count); fwrite(b, 1, 4, f);
// write offset table
for (uint32_t i = 0; i < count; i++) {
write_u32_le(b, offsets[i]);
fwrite(b, 1, 4, f);
}
// write strings sequentially
for (uint32_t i = 0; i < count; i++) {
uint32_t len = (uint32_t)strlen(strings[i]) + 1;
fwrite(strings[i], 1, len, f);
// zero padding to 4-byte alignment
static const unsigned char zero_pad[4] = {0};
uint32_t pad = pad4(len);
if (pad)
fwrite(zero_pad, 1, pad, f);
}
fclose(f);
free(offsets);
free_string_array(strings, count);
return EXIT_SUCCESS;
error:
free(offsets);
free_string_array(strings, count);
return EXIT_FAILURE;
}
int main(int argc, char **argv) {
#ifdef _WIN32
SetConsoleOutputCP(CP_UTF8); // force utf-8 on windows
#endif
if (argc >= 2 && (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h"))) {
printf("ra3mes - MES text file converter for Pokémon Ranger: Guardian Signs\n");
printf("Copyright (c) 2026 SombrAbsol\n\n");
printf("Usage:\n");
printf(" %s --to-json <in.mes> [out.json] convert MES to JSON\n", argv[0]);
printf(" %s --to-mes <in.json> [out.mes] convert JSON to MES\n", argv[0]);
printf(" %s -h|--help show this help\n", argv[0]);
return EXIT_SUCCESS;
}
if (argc != 3 && argc != 4) {
fprintf(stderr, "Invalid arguments\n");
fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
return EXIT_FAILURE;
}
if (!file_exists(argv[2])) {
fprintf(stderr, "Invalid path: '%s'\n", argv[2]);
return EXIT_FAILURE;
}
char *output = NULL;
int result = EXIT_FAILURE;
const char *input = argv[2];
const char *outarg = (argc == 4) ? argv[3] : NULL;
if (!file_exists(input)) {
fprintf(stderr, "Invalid path: '%s'\n", input);
return EXIT_FAILURE;
}
if (strcmp(argv[1], "--to-json") == 0) { // mes to json
output = outarg ? xstrdup(outarg) : make_output_path(input, ".json");
if (!output) return EXIT_FAILURE;
result = mes_to_json(input, output);
} else if (strcmp(argv[1], "--to-mes") == 0) { // json to mes
output = outarg ? xstrdup(outarg) : make_output_path(input, ".mes");
if (!output) return EXIT_FAILURE;
result = json_to_mes(input, output);
} else {
fprintf(stderr, "Unknown option: %s\n", argv[1]);
fprintf(stderr, "Try '%s --help' for more information.\n", argv[0]);
}
free(output);
return result;
}