-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathselect.js
234 lines (152 loc) · 6.12 KB
/
select.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
const fs = require('fs');
const path = require('path');
const levenshtein = require('js-levenshtein');
const canvas = require('canvas');
const {execSync} = require('child_process');
const inquirer = require('inquirer');
function randomIntFromInterval(min, max) { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min)
}
const numb = randomIntFromInterval(100, 999)
console.log('TID :', '05' + numb + '00000000000')
inquirer
.prompt([
{
name: 'con',
message: 'Type your console choice: gb, gba, etc.. or leave empty for all consoles.'
},
])
.then(answers => {
console.info('choice :', answers.con);
let inputDirs = [`./input/${answers.con}`]
let imageDirs = [`./images/${answers.con}`]
let tempDir = './temp'
let tidPrefix = `05${numb}`
let tidSuffix = '0000'
let coresPrefix = 'sdmc:/retroarch/cores'
let romsPrefix = 'sdmc:/roms'
let platforms = require('./platforms.json') // I probably missed a bunch
async function main () {
const con = await inquirer
let inputFiles = []
let imageFiles = []
let tidCounter = 0
let titles = {}
for(dir of inputDirs) {
for(file of readDirRecursive(dir)) {
inputFiles.push({name: file.split('/').pop().split('\\').pop(), path: file})
}
}
for(dir of imageDirs) {
for(file of readDirRecursive(dir)) {
imageFiles.push({name: file.split('/').pop().split('\\').pop(), path: file})
}
}
for(file of inputFiles) {
let name = file.name.split('.').slice(0, -1).join('.')
let ext = file.name.split('.').pop()
let emuNro
let romFolder
for(platform of platforms) {
if(platform.extensions.includes(ext)) {
emuNro = platform.emuNro
romFolder = platform.romFolder
break
}
}
let gameName = name.split(' ')
for(let i = 0; i < gameName.length; i++) {
if(gameName[i].startsWith('(')) {
gameName = gameName.slice(0, i)
break
}
}
gameName = gameName.join(' ')
let bestMatch = {name: '', path: '', distance: Infinity}
for(image of imageFiles) {
let imageGameName = image.name.split(' ')
for(let i = 0; i < imageGameName.length; i++) {
if(imageGameName[i].startsWith('(')) {
imageGameName = imageGameName.slice(0, i)
break
}
}
imageGameName = imageGameName.join(' ')
let distance = levenshtein(gameName, imageGameName)
if(distance < bestMatch.distance) {
bestMatch = {name: image.name, path: image.path, distance: distance}
}
}
console.log(`Best ${answers.con} match for ${file.name} is ${bestMatch.name} with a distance of ${bestMatch.distance}`)
console.log(bestMatch.path)
// create TID
let tid = tidPrefix + tidCounter.toString(16).padStart(16 - (tidPrefix.length) - (tidSuffix.length), '0') + tidSuffix
tidCounter++
console.log(`TID: ${tid}`)
// add to titledb
titles[tid.toUpperCase()] = {
"id": tid.toUpperCase(),
"name": name + ` [${answers.con}]`,
"iconUrl": `https://raw.githubusercontent.com/teknik-app/forwarder-images/main/${encodeURI(bestMatch.path.split('\\').join('/').split('/').slice(1).join('/'))}`,
"region": "US",
"description": `RetroArch forwarder for roms/${romFolder}/${name}.${ext}`,
}
// figure out paths
let tempPath = path.join(tempDir, tid)
let outputPath = path.join('output', tid)
// create temp and output directory
fs.mkdirSync(tempPath, {recursive: true})
fs.mkdirSync(outputPath, {recursive: true})
// copy everything from the template directory to the temp directory
let templateFiles = fs.readdirSync(path.join(__dirname, 'template'))
for(file of templateFiles) {
fs.cpSync(path.join(__dirname, 'template', file), path.join(__dirname, tempPath, file), {recursive: true})
}
// create canvas, center and resize image
{
let imageCanvas = canvas.createCanvas(256, 256)
let ctx = imageCanvas.getContext('2d')
let image = await canvas.loadImage(bestMatch.path)
let imageRatio = image.width / image.height
let canvasRatio = imageCanvas.width / imageCanvas.height
if(imageRatio > canvasRatio) {
ctx.drawImage(image, 0, 0, image.width, image.height, 0, (imageCanvas.height - (imageCanvas.width / imageRatio)) / 2, imageCanvas.width, imageCanvas.width / imageRatio)
} else {
ctx.drawImage(image, 0, 0, image.width, image.height, (imageCanvas.width - (imageCanvas.height * imageRatio)) / 2, 0, imageCanvas.height * imageRatio, imageCanvas.height)
}
// write canvas to file
let jpegStream = imageCanvas.createJPEGStream()
let jpegData = await jpegStream.read()
fs.writeFileSync(path.join(tempPath, 'control', 'icon_AmericanEnglish.dat'), jpegData)
}
let nroPath = `${coresPrefix}/${emuNro}`;
let argv = `${nroPath} "${romsPrefix}/${romFolder}/${name}.${ext}"`
console.log(nroPath)
console.log(argv)
fs.writeFileSync(path.join(tempPath, 'romfs', 'nextNroPath'), nroPath)
fs.writeFileSync(path.join(tempPath, 'romfs', 'nextArgv'), argv)
// run hacbrewpack
let hacbrewpackCommand = `${path.join(__dirname, 'hacbrewpack.exe')} --titleid ${tid} --titlename "${name}" --titlepublisher TK --nspdir ${path.join(__dirname, outputPath)} -k ${path.join(__dirname, 'prod.keys')}`
execSync(hacbrewpackCommand, {cwd: tempPath})
fs.renameSync(path.join(__dirname, outputPath, `${tid}.nsp`), path.join(__dirname, `output/${answers.con}`, `${name} [${tid}].nsp`))
// cleanup
fs.rmSync(tempPath, {recursive: true})
fs.rmSync(outputPath, {recursive: true})
}
fs.writeFileSync(path.join(__dirname, `titles/${answers.con}.json`), JSON.stringify(titles, null, 2))
function readDirRecursive (dir) { // reads a directory recursively and returns an array of files
let files = []
for(file of fs.readdirSync(dir)) {
let filePath = path.join(dir, file)
let stat = fs.statSync(filePath)
if(stat.isDirectory()) {
files.push(...readDirRecursive(filePath))
} else {
files.push(filePath)
}
}
return files
}
}
main()
});