-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
304 lines (272 loc) · 7.72 KB
/
index.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
const fs = require('fs')
const http = require('http')
const fileExists = require('file-exists')
const decompress = require('decompress');
const rimraf = require('rimraf')
const xml2js = require('xml2js')
const pkg = require('./package.json')
const arraySort = require('array-sort')
function downloadDB(url, filename) {
return new Promise((resolve, reject) => {
if (fileExists.sync(filename)) {
console.log('skip download')
return resolve()
}
var file = fs.createWriteStream(filename);
var request = http.get(url, function(response) {
console.log(response)
response.pipe(file);
})
file.on('finish', () => {
resolve()
})
file.on('error', () => {
console.log("nooooooo")
reject()
})
})
}
async function extractDB(filename) {
await decompress(filename, 'dist')
}
function readDB(filename = 'dist/wiitdb.xml') {
return new Promise((resolve, reject) => {
fs.readFile(filename, (err, data) => {
if (err) {
return reject(err)
}
data = data.toString().replace(' <3DSTDB', ' <version')
xml2js.parseString(data, (err, result) => {
if (err) {
return reject(err)
}
resolve(result.datafile.game)
})
})
})
}
function gameToEntry(game, type) {
output = {}
output.name = game['$'] ? game['$'].name : ''
let nameReplacements = {
'EN': 'En',
'FR': 'Fr',
'IT': 'It',
'ES': 'Es',
'DE': 'De',
'NL': 'Nl',
'PT': 'Pt',
'JA': 'Ja',
'SV': 'Sv',
'FI': 'Fi',
'PL': 'Pl',
'CS': 'Cs',
'HU': 'Hu',
'°': ''
}
for (let original in nameReplacements) {
output.name = output.name.replace(original, nameReplacements[original])
}
output.region = game.region ? game.region[0] : 'NTSC-U'
switch (output.region) {
case 'NTSC-U':
output.regionName = 'USA'
break;
case 'NTSC-J':
output.regionName = 'Japan'
break;
case 'PAL':
output.regionName = 'Europe'
break
}
for (let id in game.locale) {
if (game.locale[id].$.lang == 'EN' && game.locale[id].title && game.locale[id].title[0]) {
output.name = game.locale[id].title[0]
if (output.regionName) {
output.name += ' (' + output.regionName + ')'
}
}
}
// Clean up Japanese titles
if (output.name == " (Japan) (Ja)" || output.name == " (Korea) (KO)") {
for (let id in game.locale) {
if (game.locale[id].$.lang == 'JA' || game.locale[id].$.lang == 'KO') {
output.name = game.locale[id].title[0]
}
}
}
// Amount of players/users
if (game.input) {
let input = game.input
if (input[0] && input[0]['$'] && input[0]['$'].players) {
output.users = input[0]['$'].players
}
}
// ESRB Rating
if (game.rating) {
if (game.rating[0]['$'].type == 'ESRB') {
output.esrb_rating = game.rating[0]['$'].value
}
}
// Serial Number
output.serial = game.id ? game.id[0] : ''
// Developer
output.developer = game.developer ? game.developer[0] : ''
if (game.date && game.date[0] && game.date[0].$) {
output.releaseyear = game.date[0].$.year
output.releasemonth = game.date[0].$.month
output.releaseday = game.date[0].$.day
}
output.publisher = game.publisher ? game.publisher[0] : ''
if (type == 'Wii' || type == 'GameCube') {
output.type = game.type ? game.type[0] : 'Wii'
if (output.type != 'GameCube') {
output.type = 'Wii'
}
}
return output.serial ? output : null
}
function header(name, vendor, consoleParent) {
return `clrmamepro (
name "${vendor} - ${consoleParent} ${name}"
description "${vendor} - ${consoleParent} ${name}"
version "${pkg.version}"
homepage "${pkg.homepage}"
)\n`
}
/**
* Clean the given value to be DAT file safe.
*/
function cleanValue(val) {
return val.replace(new RegExp('"', 'g'), '\'')
}
function cleanTitle(val) {
return val
.replaceAll('é', 'e')
.replaceAll('Ō', 'O')
.replaceAll('ō', 'o')
}
/**
* Construct a DAT entry based on the given game.
*/
function datEntry(game, extension) {
gameEntries = ''
if (game.developer) {
gameEntries += `\n developer "${cleanValue(game.developer)}"`
}
if (game.publisher) {
gameEntries += `\n publisher "${cleanValue(game.publisher)}"`
}
if (game.releaseyear) {
gameEntries += `\n releaseyear ${cleanValue(game.releaseyear)}`
}
if (game.releasemonth) {
gameEntries += `\n releasemonth ${cleanValue(game.releasemonth)}`
}
if (game.releaseday) {
gameEntries += `\n releaseday ${cleanValue(game.releaseday)}`
}
if (game.users > 0) {
gameEntries += `\n users ${cleanValue(game.users)}`
}
if (game.esrb_rating) {
gameEntries += `\n esrb_rating "${cleanValue(game.esrb_rating)}"`
}
let gameName = cleanTitle(cleanValue(game.name))
return `
game (
name "${gameName}"
serial "${game.serial}"${gameEntries}
rom (
name "${gameName}.${extension}"
serial "${cleanValue(game.serial)}"
)
)
`
}
function getDatabase(games, type = null) {
database = []
for (id in games) {
game = gameToEntry(games[id], type)
if (type == null || game.type == type) {
database.push(game)
}
}
database = arraySort(database, ['name', 'serial'])
return database
}
function getDat(database, name, vendor, consoleParent, extension) {
output = header(name, vendor, consoleParent)
for (id in database) {
output += datEntry(database[id], extension)
}
return output
}
async function engage() {
try {
console.log('Wii/GameCube')
await downloadDB('http://www.gametdb.com/wiitdb.zip', 'wiitdb.zip')
await extractDB('wiitdb.zip')
let games = await readDB('dist/wiitdb.xml')
let types = ['Wii', 'GameCube'];
for (let index = 0; index < types.length; ++index) {
let type = types[index];
let database = getDatabase(games, type)
let dat = getDat(database, type, 'Nintendo', 'Nintendo', 'iso')
fs.writeFileSync('libretro-database/dat/Nintendo - ' + type + '.dat', dat)
}
console.log('Sony PlayStation 3')
await downloadDB('http://www.gametdb.com/ps3tdb.zip', 'ps3tdb.zip')
await extractDB('ps3tdb.zip')
games = await readDB('dist/ps3tdb.xml')
let ps3database = getDatabase(games)
let ps3dat = getDat(ps3database, '3', 'Sony', 'PlayStation', 'iso')
fs.writeFileSync('libretro-database/dat/Sony - PlayStation 3.dat', ps3dat)
console.log('Nintendo Wii U')
await downloadDB('http://www.gametdb.com/wiiutdb.zip', 'wiiutdb.zip')
await extractDB('wiiutdb.zip')
games = await readDB('dist/wiiutdb.xml')
let wiiudatabase = getDatabase(games)
let wiiudat = getDat(wiiudatabase, 'Wii U', 'Nintendo', 'Nintendo', 'wux')
fs.writeFileSync('libretro-database/dat/Nintendo - Wii U.dat', wiiudat)
/*
TODO: Add a Switch libretro core.
console.log('Nintendo Switch')
await downloadDB('http://www.gametdb.com/switchtdb.zip', 'switchtdb.zip')
await extractDB('switchtdb.zip')
games = await readDB('dist/switchtdb.xml')
let switchdatabase = getDatabase(games)
let switchdat = getDat(switchdatabase, 'Switch', 'Nintendo', 'Nintendo')
fs.writeFileSync('libretro-database/dat/Nintendo - Switch.dat', switchdat)
*/
/*
TODO: Add serial scanning to Nintendo DS?
console.log('Nintendo DS')
await downloadDB('http://www.gametdb.com/dstdb.zip?LANG=EN', 'dstdb.zip')
await extractDB('dstdb.zip')
games = await readDB('dist/dstdb.xml')
let dsdatabase = getDatabase(games)
let dsdat = getDat(dsdatabase, 'DS', 'Nintendo', 'Nintendo')
fs.writeFileSync('libretro-database/dat/Nintendo - Nintendo DS.dat', dsdat)
*/
/*
// Nintendo 3DS -- GameTDB doesn't provide CRCs, which is what 3DS REtroArch scanning uses currently
console.log('Nintendo 3DS')
await downloadDB('http://www.gametdb.com/3dstdb.zip?LANG=EN', '3dstdb.zip')
await extractDB('3dstdb.zip')
games = await readDB('dist/3dstdb.xml')
let threedsdatabase = getDatabase(games)
let threedsdat = getDat(threedsdatabase, '3DS', 'Nintendo', 'Nintendo', '3ds')
fs.writeFileSync('libretro-database/dat/Nintendo - Nintendo 3DS.dat', threedsdat)
*/
}
catch (e) {
console.error(e)
}
}
try {
engage()
}
catch (e) {
console.error(e)
}