summaryrefslogtreecommitdiff
path: root/utils/json2rostertbl.c
blob: a46f688140f99091e84c286085ae2c6b40c6ae23 (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
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <json_tokener.h>
#include <json_util.h>

#include "constants.h"

void printpokemon(struct json_object *);

void
required8int(struct json_object *k, struct json_object *l, const char *s)
{
	if (!json_object_object_get_ex(k, s, &l)) {
		errx(1, "Needs a '%s'!", s);
	}
	fputc(json_object_get_int(l), stdout);
}

void
required8id(struct json_object *k, struct json_object *l, const char *s,
    int (*f)(const char *))
{
	if (!json_object_object_get_ex(k, s, &l)) {
		errx(1, "Needs a '%s'!", s);
	}
	fputc(f(json_object_get_string(l)), stdout);
}

void
optional8id(struct json_object *k, struct json_object *l, const char *s,
    int (*f)(const char *))
{
	if (!json_object_object_get_ex(k, s, &l)) {
		fputc(0, stdout);
	} else {
		fputc(f(json_object_get_string(l)), stdout);
	}
}

void
optional8strint(struct json_object *k, struct json_object *l, const char *s)
{
	if (!json_object_object_get_ex(k, s, &l)) {
		fputc(0, stdout);
	} else {
		char *ep;
		unsigned long x;
		x = strtoul(json_object_get_string(l), &ep, 0);
		if (json_object_get_string(l)[0] == '\0' || *ep != '\0') {
			errx(1, "invalid number for '%s'", s);
		}
		if (errno == ERANGE && x == ULONG_MAX) {
			errx(1, "invalid number for '%s'", s);
		}
		fputc(x, stdout);
	}
}

void
required16strint(struct json_object *k, struct json_object *l, const char *s)
{
	if (!json_object_object_get_ex(k, s, &l)) {
		errx(1, "Needs a '%s'!", s);
	} else {
		char *ep;
		unsigned long x;
		x = strtoul(json_object_get_string(l), &ep, 0);
		if (json_object_get_string(l)[0] == '\0' || *ep != '\0') {
			errx(1, "invalid number for '%s'", s);
		}
		if (errno == ERANGE && x == ULONG_MAX) {
			errx(1, "invalid number for '%s'", s);
		}
		fputc(x >> 8, stdout);
		fputc(x & 0xff, stdout);
	}
}

int
main(int argc, char *argv[])
{
	struct json_object *j, *k, *l;
	int len, i;

	if (argc != 2) {
		errx(1, "usage: json2rostertbl file");
	}

	j = json_object_from_file(argv[1]);

	len = json_object_array_length(j);

	if (len > 50) {
		/* rentals */
		fputc(len, stdout);
		fputc(0, stdout);
		fputc(0, stdout);
		fputc(0, stdout);
		for (i = 0; i < len; ++i) {
			printpokemon(json_object_array_get_idx(j, i));
		}
	} else {
		/* rosters */
		fputc(len, stdout);
		fputc(0, stdout);
		fputc(0, stdout);
		fputc(0, stdout);
		for (i = 0; i < len; ++i) {
			k = json_object_array_get_idx(j, i);
			required8id(k, l, "group", strtogroup);
			required8id(k, l, "trainer", strtotrainer);
			optional8strint(k, l, "unknown1");
			required8id(k, l, "text", strtotext);

			if (!json_object_object_get_ex(k, "pokemon", &l)) {
				errx(1, "Needs a pokemon!");
			}
			fputc(json_object_array_length(l), stdout);

			struct json_object *m;
			optional8strint(k, m, "unknown3");
			optional8strint(k, m, "unknown4");
			optional8strint(k, m, "unknown5");

			int z;
			struct json_object *o;
			for (z = 0; z < json_object_array_length(l); ++z) {
				printpokemon(json_object_array_get_idx(l, z));
			}
			while (z++ < 6) {
				/*
				 * XXX should create a json string for generic
				 * pokemon, parse it here, and pass it to
				 * printpokemon()
				 */
				fputc(1, stdout);
				fputc(1, stdout);
				int A;
				for (A = 0; A < 7; ++A) {
					fputc(0, stdout);
				}
				fputc(0x7f, stdout);
				for (A = 0; A < 10; ++A) {
					fputc(0, stdout);
				}
				fputc(0x67, stdout);
				fputc(0x77, stdout);
				fputc(0, stdout);
				fputc(0, stdout);
			}
		}
	}

	return 0;
}

void
printpokemon(struct json_object *k)
{
	struct json_object *l;
	required8int(k, l, "level");
	required8id(k, l, "species", strtopokemon);
	optional8id(k, l, "item", strtoitem);
	optional8strint(k, l, "unknown1");
	required8id(k, l, "move1", strtomove);
	optional8id(k, l, "move2", strtomove);
	optional8id(k, l, "move3", strtomove);
	optional8id(k, l, "move4", strtomove);
	optional8strint(k, l, "unknown2");
	optional8strint(k, l, "happiness");
	required16strint(k, l, "hp exp");
	required16strint(k, l, "attack exp");
	required16strint(k, l, "defense exp");
	required16strint(k, l, "speed exp");
	required16strint(k, l, "special exp");
	required16strint(k, l, "dvs");
	optional8strint(k, l, "unknown4");
	optional8strint(k, l, "unknown5");
}