-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcit2xml.c
151 lines (124 loc) · 2.68 KB
/
cit2xml.c
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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include "item_names.h"
static int read_short(FILE *fp)
{
short i = 0;
fread(&i, 2, 1, fp);
return i;
}
static int read_int(FILE *fp)
{
int i = 0;
fread(&i, 4, 1, fp);
return i;
}
static void indent(FILE *fp, int ind)
{
for (int i = 0; i < ind; i++)
{
fputc('\t', fp);
}
}
static int print_short(FILE *ifp, FILE *ofp, int ind, const char *name)
{
int i = read_short(ifp);
indent(ofp, ind);
fprintf(ofp, "<i16 name=\"%s\" value=\"%d\"/>\n", name, i);
return i;
}
static int print_int(FILE *ifp, FILE *ofp, int ind, const char *name)
{
int i = read_int(ifp);
indent(ofp, ind);
fprintf(ofp, "<i32 name=\"%s\" value=\"%d\"/>\n", name, i);
return i;
}
static void print_item_array(FILE *ifp, FILE *ofp, int ind, const char *name, int count)
{
indent(ofp, ind);
fprintf(ofp, "<array name=\"%s\">\n", name);
int in = ind + 1;
for (int i = 0; i < count; i++)
{
indent(ofp, in);
unsigned it = read_short(ifp);
fprintf(ofp, "<i16 index=\"%d\" value=\"%d\"/>", i, it);
if (it < 1901) fprintf(ofp, "<!-- %s -->", item_names[it]);
fputc('\n', ofp);
}
indent(ofp, ind);
fprintf(ofp, "</array>\n");
}
static void print_short_array(FILE *ifp, FILE *ofp, int ind, const char *name, int count)
{
indent(ofp, ind);
fprintf(ofp, "<array name=\"%s\">\n", name);
int in = ind + 1;
for (int i = 0; i < count; i++)
{
indent(ofp, in);
fprintf(ofp, "<i16 index=\"%d\" value=\"%d\"/>\n", i, read_short(ifp));
}
indent(ofp, ind);
fprintf(ofp, "</array>\n");
}
static int cit2xml(FILE *ifp, char *file)
{
if (read_int(ifp) != 7629155)
{
printf("Bad magic in file %s\n", file);
return 1;
}
char path[PATH_MAX];
snprintf(path, PATH_MAX, "%s.xml", file);
FILE *ofp = fopen(path, "w");
if (!ofp)
{
perror(path);
return 1;
}
fprintf(ofp, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
fprintf(ofp, "<cit magic=\"0x746963\">\n");
print_int(ifp, ofp, 1, "version");
int n = read_int(ifp);
indent(ofp, 1);
fprintf(ofp, "<array count=\"%d\">\n", n);
for (int i = 0; i < n; i++)
{
indent(ofp, 2);
fprintf(ofp, "<class index=\"%d\">\n", i);
print_short(ifp, ofp, 3, "mLevel");
print_item_array(ifp, ofp, 3, "mGetItemNo", 20);
print_short_array(ifp, ofp, 3, "mGetItemRate", 20);
indent(ofp, 2);
fprintf(ofp, "</class>\n");
}
indent(ofp, 1);
fprintf(ofp, "</array>\n");
fprintf(ofp, "</cit>\n");
fclose(ofp);
return 0;
}
int main(int argc, char **argv)
{
int ret = 0;
for (int i = 1; i < argc; i++)
{
FILE *fp = fopen(argv[i], "rb");
if (!fp)
{
perror(argv[i]);
ret = 1;
continue;
}
if (cit2xml(fp, argv[i]))
{
ret = 1;
}
fclose(fp);
}
return ret;
}