forked from nezroy/sdd2json
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathram.c
More file actions
381 lines (341 loc) · 12.9 KB
/
Copy pathram.c
File metadata and controls
381 lines (341 loc) · 12.9 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include <stdio.h>
#include <string.h>
#include "compat.h"
#include <sql.h>
#include <sqlext.h>
#include <yaml.h>
#include "sdd2json.h"
// all tables this creates
unsigned int create_ramBlueprints(FILE*, FILE*, const char*);
unsigned int create_ramActivities(FILE*, FILE*);
unsigned int create_ramAssemblyLineStations(FILE*, FILE*);
unsigned int create_ramAssemblyLineTypes(FILE*, FILE*);
unsigned int create_ramInstallationTypeContents(FILE*, FILE*);
unsigned int create_ram(FILE *mf, const char *blueprints_yaml) {
int rc;
char *src;
FILE *f = NULL;
// ramData
src = "ramData";
if (!(f = open_datafile(src))) return 1;
rc = create_ramActivities(mf, f); if (0 != rc) return rc;
TBLSEP(f, mf); rc = create_ramAssemblyLineStations(mf, f); if (0 != rc) return rc;
TBLSEP(f, mf); rc = create_ramAssemblyLineTypes(mf, f); if (0 != rc) return rc;
TBLSEP(f, mf); rc = create_ramInstallationTypeContents(mf, f); if (0 != rc) return rc;
TBLSEP(f, mf); rc = create_ramBlueprints(mf, f, blueprints_yaml); if (0 != rc) return rc;
if (close_datafile(src, f)) return 1;
return 0;
}
unsigned int create_ramActivities(FILE *mf, FILE *f) {
SQLRETURN rc;
SQLHSTMT stmt;
unsigned int count = 0;
char txtbuf[1001];
// meta
fprintf(mf, "\"ramActivities\":{\n");
fprintf(mf, "\"j\":\"ramData\",\n");
fprintf(mf, "\"c\":[\"activityName\",\"iconNo\",\"description\",\"published\"],\n");
fprintf(mf, "\"k\":\"activityID\",\n");
fprintf(mf, "\"t\":\"sql\",\n");
// data
fprintf(f, "\"ramActivities\":{\n");
fprintf(f, "\"d\":{\n");
rc = SQLAllocHandle(SQL_HANDLE_STMT, H_DBC, &stmt);
if (!SQL_SUCCEEDED(rc)) return dump_sql_error(rc, SQL_HANDLE_DBC, H_DBC, 1);
rc = SQLExecDirect(stmt, "SELECT * FROM ramActivities ORDER BY activityID", SQL_NTS);
if (!SQL_SUCCEEDED(rc)) return dump_sql_error(rc, SQL_HANDLE_STMT, stmt, 1);
rc = SQLFetch(stmt);
while (SQL_SUCCEEDED(rc)) {
fprintf(f, "\"%ld\":[", sql_column_long(stmt, 0));
sql_column_text(txtbuf, stmt, 1, 101);
fprintf(f, "\"%s\",", txtbuf);
sql_column_text(txtbuf, stmt, 2, 6);
fprintf(f, "\"%s\",", txtbuf);
sql_column_text(txtbuf, stmt, 3, 1001);
fprintf(f, "\"%s\",", txtbuf);
fprintf(f, "%s", sql_column_bit(stmt, 4) == 0 ? "false" : "true");
rc = SQLFetch(stmt);
if (SQL_SUCCEEDED(rc)) fprintf(f, "],\n");
else fprintf(f, "]\n");
count++;
}
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
fprintf(f, "}\n}");
fprintf(mf, "\"l\":%u\n}", count);
return 0;
}
unsigned int create_ramAssemblyLineStations(FILE *mf, FILE *f) {
SQLRETURN rc;
SQLHSTMT stmt;
SQLINTEGER prev_station;
SQLINTEGER stationID;
SQLINTEGER lineTypeID;
unsigned int count = 0;
// meta
fprintf(mf, "\"ramAssemblyLineStations\":{\n");
fprintf(mf, "\"j\":\"ramData\",\n");
fprintf(mf, "\"c\":[\"quantity\",\"stationTypeID\",\"ownerID\",\"solarSystemID\",\"regionID\"],\n");
fprintf(mf, "\"k\":\"stationID:assemblyLineTypeID\",\n");
fprintf(mf, "\"t\":\"sql\",\n");
// data
fprintf(f, "\"ramAssemblyLineStations\":{\n");
fprintf(f, "\"d\":{\n");
rc = SQLAllocHandle(SQL_HANDLE_STMT, H_DBC, &stmt);
if (!SQL_SUCCEEDED(rc)) return dump_sql_error(rc, SQL_HANDLE_DBC, H_DBC, 1);
rc = SQLExecDirect(stmt, "SELECT * FROM ramAssemblyLineStations ORDER BY stationID,assemblyLineTypeID", SQL_NTS);
if (!SQL_SUCCEEDED(rc)) return dump_sql_error(rc, SQL_HANDLE_STMT, stmt, 1);
rc = SQLFetch(stmt);
prev_station = 0;
while (SQL_SUCCEEDED(rc)) {
stationID = sql_column_long(stmt, 0);
lineTypeID = sql_column_long(stmt, 1);
if (prev_station != stationID) {
if (0 != prev_station) {
fprintf(f, "\n},\n");
}
fprintf(f, "\"%ld\":{\n\"%ld\":[", stationID, lineTypeID);
prev_station = stationID;
count++;
}
else {
fprintf(f, ",\n\"%ld\":[", lineTypeID);
}
fprintf(f, "%ld,", sql_column_long(stmt, 2)); // quantity
fprintf(f, "%ld,", sql_column_long(stmt, 3)); // stationTypeID
fprintf(f, "%ld,", sql_column_long(stmt, 4)); // ownerID
fprintf(f, "%ld,", sql_column_long(stmt, 5)); // solarSystemID
fprintf(f, "%ld", sql_column_long(stmt, 6)); // regionID
rc = SQLFetch(stmt);
if (SQL_SUCCEEDED(rc)) fprintf(f, "]");
else fprintf(f, "]\n}\n");
}
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
fprintf(f, "}\n}");
fprintf(mf, "\"l\":%u\n}", count);
return 0;
}
unsigned int create_ramAssemblyLineTypes(FILE *mf, FILE *f) {
SQLRETURN rc;
SQLHSTMT stmt;
SQLHSTMT substmt;
SQLINTEGER ID = 0;
unsigned int count = 0;
char sql[BUFLEN] = NULLSTR;
char txtbuf[1001];
// meta
fprintf(mf, "\"ramAssemblyLineTypes\":{\n");
fprintf(mf, "\"j\":\"ramData\",\n");
fprintf(mf, "\"c\":[\"assemblyLineTypeName\",\"description\",\"baseTimeMultiplier\",\"baseMaterialMultiplier\",\"baseCostMultiplier\",\"volume\",\"activityID\",\"minCostPerHour\",\"categoryMulti\",\"groupMulti\"],\n");
fprintf(mf, "\"k\":\"assemblyLineTypeID\",\n");
fprintf(mf, "\"m\":{\"categoryMulti\":\"{ categoryID: [timeMultiplier, materialMultiplier, costMultiplier], ... }\",\"groupMulti\":\"{ groupID: [timeMultiplier, materialMultiplier, costMultiplier], ... }\"},\n");
fprintf(mf, "\"t\":\"sql\",\n");
// data
fprintf(f, "\"ramAssemblyLineTypes\":{\n");
fprintf(f, "\"d\":{\n");
rc = SQLAllocHandle(SQL_HANDLE_STMT, H_DBC, &stmt);
if (!SQL_SUCCEEDED(rc)) return dump_sql_error(rc, SQL_HANDLE_DBC, H_DBC, 1);
rc = SQLExecDirect(stmt, "SELECT * FROM ramAssemblyLineTypes ORDER BY assemblyLineTypeID", SQL_NTS);
if (!SQL_SUCCEEDED(rc)) return dump_sql_error(rc, SQL_HANDLE_STMT, stmt, 1);
rc = SQLFetch(stmt);
while (SQL_SUCCEEDED(rc)) {
ID = sql_column_long(stmt, 0);
fprintf(f, "\"%ld\":[", ID);
sql_column_text(txtbuf, stmt, 1, 101);
fprintf(f, "\"%s\",", txtbuf); // assemblyLineTypeName
sql_column_text(txtbuf, stmt, 2, 1001);
fprintf(f, "\"%s\",", txtbuf); // description
fprintf(f, "%g,", sql_column_float(stmt, 3)); // baseTimeMultiplier
fprintf(f, "%g,", sql_column_float(stmt, 4)); // baseMaterialMultiplier
fprintf(f, "%g,", sql_column_float(stmt, 5)); // baseCostMultiplier
fprintf(f, "%g,", sql_column_float(stmt, 6)); // volume
fprintf(f, "%ld,", sql_column_long(stmt, 7)); // activityID
fprintf(f, "%g,", sql_column_float(stmt, 8)); // minCostPerHour
// category
fprintf(f, "{");
rc = SQLAllocHandle(SQL_HANDLE_STMT, H_DBC2, &substmt);
if (!SQL_SUCCEEDED(rc)) return dump_sql_error(rc, SQL_HANDLE_DBC, H_DBC2, 1);
SNPRINTF(sql, BUFLEN, "SELECT * FROM ramAssemblyLineTypeDetailPerCategory WHERE assemblyLineTypeID = %ld ORDER BY categoryID\0", ID);
rc = SQLExecDirect(substmt, sql, SQL_NTS);
if (!SQL_SUCCEEDED(rc)) return dump_sql_error(rc, SQL_HANDLE_STMT, substmt, 1);
rc = SQLFetch(substmt);
while (SQL_SUCCEEDED(rc)) {
fprintf(f, "\"%ld\":[", sql_column_long(substmt, 1));
fprintf(f, "%g,", sql_column_float(substmt, 2));
fprintf(f, "%g,", sql_column_float(substmt, 3));
fprintf(f, "%g]", sql_column_float(substmt, 4));
rc = SQLFetch(substmt);
if (SQL_SUCCEEDED(rc)) fprintf(f, ",");
}
fprintf(f, "},");
SQLFreeHandle(SQL_HANDLE_STMT, substmt);
// group
fprintf(f, "{");
rc = SQLAllocHandle(SQL_HANDLE_STMT, H_DBC2, &substmt);
if (!SQL_SUCCEEDED(rc)) return dump_sql_error(rc, SQL_HANDLE_DBC, H_DBC2, 1);
SNPRINTF(sql, BUFLEN, "SELECT * FROM ramAssemblyLineTypeDetailPerGroup WHERE assemblyLineTypeID = %ld ORDER BY groupID\0", ID);
rc = SQLExecDirect(substmt, sql, SQL_NTS);
if (!SQL_SUCCEEDED(rc)) return dump_sql_error(rc, SQL_HANDLE_STMT, substmt, 1);
rc = SQLFetch(substmt);
while (SQL_SUCCEEDED(rc)) {
fprintf(f, "\"%ld\":[", sql_column_long(substmt, 1));
fprintf(f, "%g,", sql_column_float(substmt, 2));
fprintf(f, "%g,", sql_column_float(substmt, 3));
fprintf(f, "%g]", sql_column_float(substmt, 4));
rc = SQLFetch(substmt);
if (SQL_SUCCEEDED(rc)) fprintf(f, ",");
}
fprintf(f, "}");
SQLFreeHandle(SQL_HANDLE_STMT, substmt);
rc = SQLFetch(stmt);
if (SQL_SUCCEEDED(rc)) fprintf(f, "],\n");
else fprintf(f, "]\n");
count++;
}
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
fprintf(f, "}\n}");
fprintf(mf, "\"l\":%u\n}", count);
return 0;
}
unsigned int create_ramInstallationTypeContents(FILE *mf, FILE *f) {
SQLRETURN rc;
SQLHSTMT stmt;
SQLINTEGER prev_type;
SQLINTEGER typeID;
SQLINTEGER lineTypeID;
unsigned int count = 0;
// meta
fprintf(mf, "\"ramInstallationTypeContents\":{\n");
fprintf(mf, "\"j\":\"ramData\",\n");
fprintf(mf, "\"c\":[\"quantity\"],\n");
fprintf(mf, "\"k\":\"typeID:assemblyLineTypeID\",\n");
fprintf(mf, "\"t\":\"sql\",\n");
// data
fprintf(f, "\"ramInstallationTypeContents\":{\n");
fprintf(f, "\"d\":{\n");
rc = SQLAllocHandle(SQL_HANDLE_STMT, H_DBC, &stmt);
if (!SQL_SUCCEEDED(rc)) return dump_sql_error(rc, SQL_HANDLE_DBC, H_DBC, 1);
rc = SQLExecDirect(stmt, "SELECT * FROM ramInstallationTypeContents ORDER BY installationTypeID,assemblyLineTypeID", SQL_NTS);
if (!SQL_SUCCEEDED(rc)) return dump_sql_error(rc, SQL_HANDLE_STMT, stmt, 1);
rc = SQLFetch(stmt);
prev_type = 0;
while (SQL_SUCCEEDED(rc)) {
typeID = sql_column_long(stmt, 0);
lineTypeID = sql_column_long(stmt, 1);
if (prev_type != typeID) {
if (0 != prev_type) {
fprintf(f, "\n},\n");
}
fprintf(f, "\"%ld\":{\n\"%ld\":[", typeID, lineTypeID);
prev_type = typeID;
count++;
}
else {
fprintf(f, ",\n\"%ld\":[", lineTypeID);
}
fprintf(f, "%ld", sql_column_long(stmt, 2)); // quantity
rc = SQLFetch(stmt);
if (SQL_SUCCEEDED(rc)) fprintf(f, "]");
else fprintf(f, "]\n}\n");
}
SQLFreeHandle(SQL_HANDLE_STMT, stmt);
fprintf(f, "}\n}");
fprintf(mf, "\"l\":%u\n}", count);
return 0;
}
unsigned int create_ramBlueprints(FILE *mf, FILE *f, const char *yaml) {
FILE *f_yaml = NULL;
int yret;
unsigned int count = 0;
yaml_node_t *node;
yaml_node_t *bufnode;
yaml_node_t *valnode;
int actnode_idx;
yaml_node_pair_t *pair;
yaml_node_pair_t *bufpair;
yaml_parser_t parser;
yaml_document_t ydoc;
int bpTypeID = 0;
int bpTypeIDcheck = 0;
int prodLimit = 0;
// meta
fprintf(mf, "\"ramBlueprints\":{\n");
fprintf(mf, "\"j\":\"ramData\",\n");
fprintf(mf, "\"c\":[\"maxProductionLimit\",\"activities\"],\n");
fprintf(mf, "\"k\":\"blueprintTypeID\",\n");
fprintf(mf, "\"m\":{\"activities\":\"{ activityID: { #see blueprints.yaml# }, ... }\"},\n");
fprintf(mf, "\"t\":\"yaml\",\n");
// open yaml for parsing
f_yaml = fopen(yaml, "rb");
if (f_yaml == NULL) {
printf("err\n");
fprintf(stderr, "error opening blueprints file\n");
return 1;
}
yaml_parser_initialize(&parser);
yaml_parser_set_input_file(&parser, f_yaml);
yret = yaml_parser_load(&parser, &ydoc);
if (!yret) {
printf("err\n");
fprintf(stderr, "error parsing blueprints file\n");
yaml_parser_delete(&parser);
return 1;
}
// ramBlueprints
fprintf(f, "\"ramBlueprints\":{\n");
fprintf(f, "\"d\":{\n");
// expect first node to be a mapping node for the whole document (blueprintTypeIDs to mappings)
node = yaml_document_get_root_node(&ydoc);
if (!node || node->type != YAML_MAPPING_NODE) {
printf("err\n");
fprintf(stderr, "error parsing blueprints yaml - unexpected root node type\n");
yaml_parser_delete(&parser);
return 1;
}
for (pair = node->data.mapping.pairs.start; pair < node->data.mapping.pairs.top; pair++) {
// get the key node for this mapping; should be the blueprintTypeID
bufnode = yaml_document_get_node(&ydoc, pair->key);
if (bufnode->type != YAML_SCALAR_NODE) continue;
bpTypeID = atoi(bufnode->data.scalar.value);
// get the value node for this mapping; should be another mapping
bufnode = yaml_document_get_node(&ydoc, pair->value);
if (bufnode->type != YAML_MAPPING_NODE) continue;
actnode_idx = 0;
bpTypeIDcheck = 0;
prodLimit = 0;
for (bufpair = bufnode->data.mapping.pairs.start; bufpair < bufnode->data.mapping.pairs.top; bufpair++) {
// get the key node for this mapping; test for a few things to handle
valnode = yaml_document_get_node(&ydoc, bufpair->key);
if (valnode->type != YAML_SCALAR_NODE) continue;
if (strncmp(valnode->data.scalar.value, "activities", 10) == 0) actnode_idx = bufpair->value;
else if (strncmp(valnode->data.scalar.value, "blueprintTypeID", 15) == 0) {
valnode = yaml_document_get_node(&ydoc, bufpair->value);
if (!valnode || valnode->type != YAML_SCALAR_NODE) continue;
bpTypeIDcheck = atoi(valnode->data.scalar.value);
}
else if (strncmp(valnode->data.scalar.value, "maxProductionLimit", 18) == 0) {
valnode = yaml_document_get_node(&ydoc, bufpair->value);
if (!valnode || valnode->type != YAML_SCALAR_NODE) continue;
prodLimit = atoi(valnode->data.scalar.value);
}
}
if (bpTypeIDcheck != bpTypeID) {
printf("err\n");
fprintf(stderr, "error parsing blueprints yaml - typeID mismatch: %d != %d\n", bpTypeIDcheck, bpTypeID);
yaml_parser_delete(&parser);
return 1;
}
if (count) fprintf(f, ",\n");
count++;
fprintf(f, "\"%d\":[", bpTypeID);
fprintf(f, "%d,", prodLimit);
if (actnode_idx) yaml_json_mapping(f, &ydoc, yaml_document_get_node(&ydoc, actnode_idx));
else fprintf(f, "null");
fprintf(f, "]");
}
yaml_document_delete(&ydoc);
yaml_parser_delete(&parser);
fclose(f_yaml);
fprintf(f, "}\n}");
fprintf(mf, "\"l\":%u\n}", count);
return 0;
}