-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwrite.js
430 lines (399 loc) · 21.2 KB
/
write.js
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//Dal1980 -v1.0 (beta)
var fs = require('fs');
var sqlite3 = require('sqlite3').verbose();
process.stdout.write('\x1B[2J\x1B[0f');
console.log("");
console.log(" _ _ __ __ __ ____ ____ __ ___ ____ _____ ____ ");
console.log(" ( \\/ )( \\/ )( ) (_ _)( _ \\ /__\\ / __)(_ _)( _ )( _ \\");
console.log(" ) ( ) ( )(__ )( ) / /(__)\\( (__ )( )(_)( ) /");
console.log(" (_/\\_)(_/\\/\\_)(____)(__) (_)\\_)(__)(__)\\___) (__) (_____)(_)\\_)");
console.log(" for M/\\ME's XML ");
console.log(" MULTIPLE / ARCADE MACHINE EMULATOR");
console.log(" ---------------------------------------------------------------");
console.log(" ----WRITE (db)------------Dal1980--------------Version 1.0-----");
console.log(" -beta ");
console.log("");
console.log(" This may take a few minutes... ...please be patient");
console.log("");
console.log(" Please note:");
console.log(" The process looks slow to start with but will quickly complete.");
console.log("");
var dbName = "roms.db"; //SQLite db file (in db folder)
var taskOps = [];
process.argv.forEach(function (val, index) {
if(index > 1){
if(val == "-all" || val == '-roms_info' || val == '-1') taskOps.push("roms_info");
if(val == "-all" || val == '-roms_tech' || val == '-2') taskOps.push("roms_tech");
if(val == "-all" || val == '-roms_displays' || val == '-3') taskOps.push("roms_displays");
if(val == "-all" || val == '-roms_controls' || val == '-4') taskOps.push("roms_controls");
if(val == "-all" || val == '-manufacturers' || val == '-5') taskOps.push("manufacturers");
if(val == "-all" || val == '-p1i' || val == '-6') taskOps.push("p1i");
if(val == '-db'){
if(typeof process.argv[index + 1] !== "undefined"){
if(process.argv[index + 1].indexOf('-') != 1){
if(process.argv[index + 1].indexOf('.db') < 0) var ext = '.db';
else var ext = '';
dbName = process.argv[index + 1].replace(/[^A-Za-z0-9\-\_\.]/g, "");
if(dbName == process.argv[index + 1]){
dbName += ext;
console.log( " Using: " + dbName);
}
else {
console.log( " ** Error: -db filename issue: Illigal Character(s) found: " + process.argv[index + 1].replace(/[A-Za-z0-9\-\_\.]/g, ""));
taskOp = ""
}
}
else{
console.log( "** Error: -db flag requires a filename");
taskOp = "";
}
}
else {
console.log( " ** Error: -db flag requires a filename" );
taskOp = "";
}
}
}
});
if(taskOps.length == 0){
console.log("");
console.log(" ----- Available options are: ------------------------------------------");
console.log(" -db {filename}{.db} ~roms.db is default if not provided. ");
console.log(" ------ + (only 1 of) --------------------------------------------------");
console.log(" -all ~All tables will be written ");
console.log(" -1 |or| -roms_info ~Only roms_info will be wrote ");
console.log(" -2 |or| -roms_tech ~Only roms_tech will be wrote ");
console.log(" -3 |or| -roms_displays ~Only roms_displays will be wrote ");
console.log(" -4 |or| -roms_controls ~Only roms_controls will be wrote ");
console.log(" -5 |or| -manufacturers ~Only manufacturers will be wrote ");
console.log(" -6 |or| -p1i ~Only p1i will be wrote ");
console.log("");
process.exit();
}
//tables
//1: roms_info -core table
//create_roms_info_table();
//2: roms_tech -core table
//create_roms_tech_table();
//3: roms_displays -detail table
//create_roms_displays_table();
//4: roms_controls -detail table
//create_roms_controls_table();
//5: manufacturers -detail table
//create_manufacturers_table();
//6: p1i (player1-illustrated) -core table
//create_p1i_table();
//Further cross-reference, list, and lookup tables can be generated
//from the initial scope of the above tables in the future.
//schema (example: this is not a real rom)
//roms.600 Table Example Values Further Details
//roms.600.sourcefile 2 "galaxian.cpp"
//roms.600.cloneof 2 "turtles"
//roms.600.romof 2 "turtles"
//roms.600.isdevice 2 "yes"
//roms.600.runnable 2 "no"
//roms.600.description_part_1 1 "600 is not a real rom"
//roms.600.description_part_2 1 "(It just looks like one)"
//roms.600.set 1 "A"
//roms.600.ver 1 "1.0"
//roms.600.localisation [ Obj Array ]
// roms.600.localisation[0] 2 "US"
// roms.600.localisation[1] 2 "Asia"
// roms.600.localisation[2] 2 "Japan"
// roms.600.localisation[3] 2 "France"
// roms.600.localisation[4] 2 "New Zealand"
//roms.600.region 1 "World"
//roms.600.year 1 "1981"
//roms.600.manufacturers [ Obj Array ]
//
// roms.600.manufacturers[0] 1 "Konami"
// roms.600.manufacturers[1] 2 "Capcom"
// roms.600.manufacturers[2] 2 "Atari Games"
// roms.600.manufacturers[3] 2 "Atari Games"
// roms.600.manufacturers[4] 2 "Atari Games"
//
//roms.600.bootleg 1 true
//roms.600.licensee 1 "Atari Games"
//roms.600.display": [ Obj Array ]
//
// roms.600.display[0].tag 3 "screen"
// roms.600.display[0].type 3 "raster"
// roms.600.display[0].rotate 3 "90"
// roms.600.display[0].width 3 "768"
// roms.600.display[0].height 3 "224"
// roms.600.display[0].refresh 3 "60.606061"
// roms.600.display[0].pixclock 3 "18432000"
//
//roms.600.total_displays 1 1
//roms.600.players 1 "2"
//roms.600.coins 2 "2"
//roms.600.service 2 "yes"
//roms.600.tilt 2 "yes"
//roms.600.input [ Obj Array ]
//
// roms.600.input[0].players 4 "1"
// roms.600.input[0].controls 4 "joy"
// roms.600.input[0].buttons 4 "1"
// roms.600.input[0].ways 4 "8"
// roms.600.input[0].ways2 4 "4"
// roms.600.input[0].minimum 4 "0"
// roms.600.input[0].maximum 4 "255"
// roms.600.input[0].sensitivity 4 "50"
// roms.600.input[0].keydelta 4 "10"
// roms.600.input[0].reverse 4 "yes"
//
// roms.600.input[1].player 4 "2"
// roms.600.input[1].controls 4 "joy"
// roms.600.input[1].buttons 4 "1"
// roms.600.input[1].ways 4 "4"
//
// ...
//
//roms.600.status 1 3 {0: not set, 1: preliminary, 2: imperfect, 3: good }
//roms.600.emulation 2 3 {0: not set, 1: preliminary, 2: imperfect, 3: good }
//roms.600.color 2 3 {0: not set, 1: preliminary, 2: imperfect, 3: good }
//roms.600.sound 2 3 {0: not set, 1: preliminary, 2: imperfect, 3: good }
//roms.600.graphic 2 3 {0: not set, 1: preliminary, 2: imperfect, 3: good }
//roms.600.savestate 2 1 {0: false, 1: true }
//roms.600.categories
// roms.600.categories.primary 1 "Maze" Import from Catver.ini
// roms.600.categories.secondary 1 "Platformer" Import from Catver.ini
// roms.600.categories.mature 1 true Import from Catver.ini
//roms.600.nPlayers 1 "2P alt" Import from nplayers.ini
//roms.600.p1i
// roms.600.element_1 6 "joy,2hn,0,0,red," csv details for an input (player1-illustrated code)
// roms.600.element_2 6 "dial,,0,0,red,"
// roms.600.element_3 6 "btn,na,0,0,red,"
// roms.600.element_4 6 "btn,na,0,0,red,"
// roms.600.element_5 6 "btn,na,0,0,red,"
// roms.600.element_6 6 "btn,na,0,0,red,"
// roms.600.element_7 6 ""
// roms.600.element_8 6 ""
// roms.600.element_9 6 ""
// roms.600.version" 6 "0" version for tracking purposes (0 signifies auto-populated)
var roms;
var totalRoms = 0;
var dataRoms = 0;
var totalManufacturers = 0;
var displaysDIDCount = 0; //(D)isplay ID
var controlsCIDCount = 0; //(C)ontrols ID
var countInfo = 0;
var countTech = 0;
var countDisplays = 0;
var countManufacturers = 0;
var errorlog = "";
var db = new sqlite3.Database('db\\' + dbName);
console.log(" Writing to: " + dbName);
fs.readFile('exports\\roms.json', 'utf8', function (err, data) {
if (err) throw err;
roms = JSON.parse(data); //kaboom - our data object is back
var romNames = Object.keys(roms);
//console.log(JSON.stringify(roms, null, "\t"));
totalRoms = romNames.length;
db.run("begin transaction");
romNames.forEach( function(romName, index){
//console.log("Preparing: " + index + " / " + totalRoms);
updateConsole(" [ Importing: " + (index + 1) + " / " + totalRoms + " ]");
if(typeof roms[romName].categories === "undefined") {
roms[romName].categories = {"mature" : 0 };
}
for(var n = 0; n <= 5; n++){
if (typeof roms[romName].manufacturers == "undefined" ||
!(roms[romName].manufacturers instanceof Array) ) {
roms[romName].manufacturers = [];
}
if(typeof roms[romName].manufacturers[n] === "undefined") {
roms[romName].manufacturers[n] = { "index": 0 };
}
}
if(typeof roms[romName].localisation === "undefined") {
roms[romName].localisation = [];
}
for(var n = 0; n <= 5; n++){
if(typeof roms[romName].localisation[n] === "undefined") {
roms[romName].localisation[n] = null;
}
}
if(taskOps.includes("roms_info")) insert_roms_info_table(romName);
if(taskOps.includes("roms_tech")) insert_roms_tech_table(romName);
if(taskOps.includes("p1i")) insert_p1i_table(romName);
if(taskOps.includes("roms_displays")){
displaysDIDCount = 0;
for(var x = 0; x < roms[romName].total_displays; x++){
insert_roms_displays_table(romName, displaysDIDCount);
displaysDIDCount++;
}
}
if(taskOps.includes("roms_controls")){
controlsCIDCount = 0;
for(var x = 0; x < roms[romName].total_inputs; x++){
insert_roms_controls_table(romName, controlsCIDCount);
controlsCIDCount++;
}
}
});
if(taskOps.includes("manufacturers")){
fs.readFile('exports\\manufacturers.json', 'utf8', function (err, data) {
if (err) throw err;
manufacturersJSON = JSON.parse(data); //our data object
Object.keys(manufacturersJSON).forEach(function(key) {
var val = manufacturersJSON[key];
//console.log(key + ": " + val);
insert_manufacturers_table(key, val);
});
});
}
db.run("commit");
});
console.log(errorlog);
function insertRow(sql, params){
dataRoms++;
db.run(sql, params, function(err){
errorlog += "\n" + err;
if(countInfo == 0) console.log("");
countInfo++;
updateConsole(" [ Inserting data : " + countInfo + " / " + dataRoms + " ]");
});
}
function insert_roms_info_table(romName){
insertRow(
"INSERT INTO `roms_info`" +
"( `name`, `desc_1`, `desc_2`, `year`, `players`, `nplayers`, `displays`, `status`," +
" `cat_primary`, `cat_secondary`, `manufacturer_1`, `licensee`, `mature`," +
" `bootleg`, `region`, `setnum`, `rev`, `ver` ) " +
"VALUES " +
"( $name, $desc_1, $desc_2, $year, $players, $nplayers, $displays, $status," +
" $cat_primary, $cat_secondary, $manufacturer_1, $licensee, $mature," +
" $bootleg, $region, $setnum, $rev, $ver )", {
$name: romName,
$desc_1: roms[romName].description_part_1,
$desc_2: roms[romName].description_part_2,
$year: roms[romName].year,
$players: roms[romName].players,
$nplayers: roms[romName].nPlayers,
$displays: roms[romName].total_displays,
$status: roms[romName].status,
$cat_primary: roms[romName].categories.primary,
$cat_secondary: roms[romName].categories.secondary,
$manufacturer_1: roms[romName].manufacturers[0].name,
$licensee: roms[romName].licensee,
$mature: roms[romName].categories.mature,
$bootleg: roms[romName].bootleg,
$region: roms[romName].region,
$setnum: roms[romName].set,
$rev: roms[romName].rev,
$ver: roms[romName].ver
});
}
function insert_roms_tech_table(romName){
insertRow(
"INSERT INTO `roms_tech`" +
"( `name`, `localisation_1`, `localisation_2`, `localisation_3`, `localisation_4`," +
" `localisation_5`, `manufacturer_2`, `manufacturer_3`, `manufacturer_4`," +
" `manufacturer_5`, `sourcefile`, `cloneof`, `romof`, `isdevice`, `runnable`, `coins`," +
" `service`, `tilt`, `emulation`, `color`, `sound`, `graphic`, `savestate` )" +
"VALUES " +
"( $name, $localisation_1, $localisation_2, $localisation_3, $localisation_4," +
" $localisation_5, $manufacturer_2, $manufacturer_3, $manufacturer_4," +
" $manufacturer_5, $sourcefile, $cloneof, $romof, $isdevice, $runnable, $coins, " +
" $service, $tilt, $emulation, $color, $sound, $graphic, $savestate )", {
$name: romName,
$localisation_1: roms[romName].localisation[0],
$localisation_2: roms[romName].localisation[1],
$localisation_3: roms[romName].localisation[2],
$localisation_4: roms[romName].localisation[3],
$localisation_5: roms[romName].localisation[4],
$manufacturer_2: roms[romName].manufacturers[1].name,
$manufacturer_3: roms[romName].manufacturers[2].name,
$manufacturer_4: roms[romName].manufacturers[3].name,
$manufacturer_5: roms[romName].manufacturers[4].name,
$sourcefile: roms[romName].sourcefile,
$cloneof: roms[romName].cloneof,
$romof: roms[romName].romof,
$isdevice: roms[romName].isdevice,
$runnable: roms[romName].runnable,
$coins: roms[romName].coins,
$service: roms[romName].service,
$tilt: roms[romName].tilt,
$emulation: roms[romName].emulation,
$color: roms[romName].color,
$sound: roms[romName].sound,
$graphic: roms[romName].graphic,
$savestate: roms[romName].savestate
});
}
function insert_roms_displays_table(romName, displaysDIDCount){
insertRow(
"INSERT INTO `roms_displays`" +
"( `name`, `did`, `tag`, `type`, `rotate`, `width`, `height`, `refresh`, `pixclock` )" +
"VALUES " +
"( $name, $did, $tag, $type, $rotate, $width, $height, $refresh, $pixclock )", {
$name: romName,
$did: displaysDIDCount,
$tag: roms[romName].display[ displaysDIDCount ].tag,
$type: roms[romName].display[ displaysDIDCount ].type,
$rotate: roms[romName].display[ displaysDIDCount ].rotate,
$width: roms[romName].display[ displaysDIDCount ].width,
$height: roms[romName].display[ displaysDIDCount ].height,
$refresh: roms[romName].display[ displaysDIDCount ].refresh,
$pixclock: roms[romName].display[ displaysDIDCount ].pixclock
});
}
function insert_roms_controls_table(romName, controlsIDCount){
insertRow(
"INSERT INTO `roms_controls`" +
"( `name`, `cid`, `player`, `controls`, `buttons`, `ways`, `ways2`, `minimum`, `maximum`, `sensitivity`, `keydelta`, `reverse` )" +
"VALUES " +
"( $name, $cid, $player, $controls, $buttons, $ways, $ways2, $minimum, $maximum, $sensitivity, $keydelta, $reverse )", {
$name: romName,
$cid: controlsIDCount,
$player: roms[romName].input[ controlsIDCount ].player,
$controls: roms[romName].input[ controlsIDCount ].controls,
$buttons: roms[romName].input[ controlsIDCount ].buttons,
$ways: roms[romName].input[ controlsIDCount ].ways,
$ways2: roms[romName].input[ controlsIDCount ].ways2,
$minimum: roms[romName].input[ controlsIDCount ].minimum,
$maximum: roms[romName].input[ controlsIDCount ].maximum,
$sensitivity: roms[romName].input[ controlsIDCount ].sensitivity,
$keydelta: roms[romName].input[ controlsIDCount ].keydelta,
$reverse: roms[romName].input[ controlsIDCount ].reverse
});
}
function insert_manufacturers_table(id, name){
insertRow(
"INSERT INTO `manufacturers`" +
"( `id`, `manufacturer` )" +
"VALUES " +
"( $id, $manufacturer )", {
$id: id,
$manufacturer: name
});
}
function insert_p1i_table(romName){
insertRow(
"INSERT INTO `p1i`" +
"( `name`, `element_1`, `element_2`, `element_3`, `element_4`," +
" `element_5`, `element_6`, `element_7`, `element_8`," +
" `element_9`, `version`)" +
"VALUES " +
"( $name, $element_1, $element_2, $element_3, $element_4," +
" $element_5, $element_6, $element_7, $element_8," +
" $element_9, $version)", {
$name: romName,
$element_1: roms[romName].p1i.element_1,
$element_2: roms[romName].p1i.element_2,
$element_3: roms[romName].p1i.element_3,
$element_4: roms[romName].p1i.element_4,
$element_5: roms[romName].p1i.element_5,
$element_6: roms[romName].p1i.element_6,
$element_7: roms[romName].p1i.element_7,
$element_8: roms[romName].p1i.element_8,
$element_9: roms[romName].p1i.element_9,
$version: roms[romName].p1i.version
});
}
function updateConsole(text){
//process.stdout.clearLine(); // clear current text
process.stdout.cursorTo(0); // move cursor to beginning of line
process.stdout.write(text); // write text
}