-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
241 lines (210 loc) · 5.25 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
var fs = require('fs')
var path = require('path')
var acceptableExtensions = require('./lib/acceptableExtensions.json')
var rejectNames = require('./lib/rejectNames.json')
var rejectGames = require('./lib/rejectGames.json')
var sortArray = require('sort-array')
var datfile = require('robloach-datfile')
const download = require('download')
async function downloadFile() {
if (!fs.existsSync('tdc_daily.dat')) {
const downloadOpts = {
extract: true,
filename: 'tdc_daily.dat'
}
await download('http://www.totaldoscollection.org/nugnugnug/tdc_daily.zip', '.', downloadOpts)
}
}
async function goTime() {
await downloadFile()
// Options for reading the dat file.
let opts = {
ignoreHeader: true
}
// Parse the TDC.dat file.
datfile.parseFile('tdc_daily.dat', opts).then(function (dat) {
let database = []
for (let i in dat) {
let game = dat[i]
let finalFile = null
if (!acceptableGame(game)) {
continue
}
for (let i in game.entries || []) {
let file = game.entries[i]
let cleanFilename = acceptableFile(file)
if (cleanFilename) {
file.cleanFilename = cleanFilename
finalFile = file
}
}
if (finalFile) {
finalFile.meta = getMetadata(game)
database.push({
name: cleanGameName(game.name),
filename: finalFile.cleanFilename,
size: finalFile.size,
crc: finalFile.crc.toLowerCase(),
meta: finalFile.meta
})
}
}
// Sort the database.
database = sortArray(database, {
by: 'name'
})
// Output the new DAT.
var pkg = require('./package.json')
var output = `clrmamepro (
name "DOS"
description "DOS"
version ${pkg.version}
comment "libretro | www.libretro.com"
homepage "${pkg.homepage}"
)
`
for (let i in database) {
let game = database[i]
let meta = ''
for (let metaEntry in game.meta) {
meta += `\n\t${metaEntry} "${game.meta[metaEntry]}"`
}
output += `
game (
name "${game.name}"
description "${game.name.replace(/\([\d\d\d\d]+\)/gm, '').trim()}"${meta}
rom ( name "${game.filename}" size ${game.size} crc ${game.crc} )
)
`
}
// Save the file.
fs.writeFileSync('libretro-database/dat/DOS.dat', output)
}).catch(function (err) {
// Output any errors.
console.error(err)
})
}
/**
* Checks to see if the given file is an acceptable game to index.
*/
function acceptableGame(game) {
for (let i in rejectGames) {
if (rejectGames[i] && game.name.indexOf(rejectGames[i]) >= 0) {
return false
}
}
return true
}
/**
* Checks if the given file is acceptable.
*/
function acceptableFile(file) {
var filename = file.name
filename = replaceAll(filename, '\\\\', '/')
if (filename) {
var pathObject = path.parse(filename)
if (pathObject.dir) {
return false
}
var extension = pathObject.ext.toLowerCase()
if (acceptableExtensions.indexOf(extension) < 0) {
return false
}
var name = pathObject.name.toLowerCase()
// Reject setup or install files.
let skipEntries = name.includes('setup') ||
name.includes('install') ||
name.includes('Crack]') ||
name.includes('[Codes]') ||
name.includes('[Solve]') ||
name.includes('[Documentation]') ||
name.includes('[Manual]') ||
name.includes('Docs]') ||
name.includes('[Walkthrough]') ||
name.includes('[Hints]')
if (skipEntries) {
return false
}
// Check against the rejected names.
if (rejectNames.indexOf(name) >= 0) {
return false
}
return pathObject.base
}
return false
}
/**
* Cleans the given game name.
*/
function cleanGameName(name) {
// Remove the suffixing file extension.
let output = name.replace('.zip', '')
// Remove an entry that breaks brackets.
output = output.replace('(Interactive Television Entertainment)', '')
// Remove all [] data.
output = output.replace(/\[.*?\]/g, '')
// Remove all languages. (En)(It)
//output = output.replace(/(\(..\))/gm, '')
// Remove all () data, except year
output = output.replace(/\([^\d\d\d\d]+\)/gm, '')
// Remove the version.
var ver = output.match(/ v[0-9.]{3,9}/g)
if (ver && ver[0]) {
output = output.replace(ver[0], '')
}
output = output.replaceAll('"', '')
// Replace any double spaces with a single space.
output = output.split(' ').join(' ')
return output.trim()
}
/**
* Retrieves any meta entries for the game.
*/
function getMetadata(game) {
let meta = {}
let name = game.name
let genres = {
'Action': 'Action',
'Adventure': 'Adventure',
'Strategy': 'Strategy',
'Sports': 'Sports',
'Role-Playing (RPG)': 'RPG',
'Educational': 'Educational',
'Simulation': 'Simulation',
'Trainer': 'Trainer'
}
for (let genre in genres) {
if (name.indexOf('[' + genre + ']') > 3) {
meta.genre = genres[genre]
break
}
}
for (let i = 1980; i < 2030; i++) {
if (name.indexOf('(' + i + ')') > 3) {
meta.year = i
break
}
}
// TODO: Find languages
//name.search(output.replace(/(\(..\))/gm, '')
// Find the developer
let companyNumber = 0
let match = name.match(/\([a-zA-Z\s,.]{3,}\)/gm)
if (match) {
let companyName = match[0]
meta.developer = companyName.replaceAll('(', '').replaceAll(')', '')
}
return meta
}
/**
* Replace all instances of a given string with another one.
*/
function replaceAll(str, find, replace) {
if (str && str.replace) {
return str.replace(new RegExp(find, 'g'), replace);
}
else {
return str
}
}
goTime()