-
Notifications
You must be signed in to change notification settings - Fork 0
/
Retroarch Steam Grid.js
141 lines (115 loc) · 4.92 KB
/
Retroarch Steam Grid.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
// Variables used by Scriptable.
// These must be at the very top of the file. Do not edit.
// icon-color: light-brown; icon-glyph: th;
// @ts-ignore
// eslint-disable-next-line
try { require; } catch(e) { require = importModule; }
const {
getInput, string, readJson, output, error, pathJoin, listFiles,
isFile, log, copyFile, makeDirectory, makeCrc32Table, getCrc32
} = require('./lib/lib.js');
const nameOutput = 'config';
const nameImages = 'images';
const nameImagesType = 'Named_Boxarts';
const nameGrid = 'grid';
/**
* Given a string, replace all keys of a map with their values.
* @param {ObjectMap<string>} map replace all of these keys with these values
* @param {string} str make the replacements within this string
* @return {string} the string with substitutions made
*/
const substitute = (map, str) =>
Object.entries(map).reduce(
(val, [k, v]) => val.replace(new RegExp(k, 'g'), v),
str
);
const help = '' +
`Copies images:
FROM: WD/${nameOutput}/${nameImages}/<OUTPUT>/${nameImagesType}/*.png
TO: WD/${nameOutput}/${nameGrid}/<hash>.png
It reads those images and copies them into ${nameGrid} with specific
hash-based names that allows Steam to display them.
To use these images in Steam, copy:
FROM: WD/${nameOutput}/${nameGrid}
TO: %STEAM_INSTALL$\\Steam\\userdata\\%USER_ID%\\config
Setup: Use "Retroarch Download Images.js" to download "FROM" images.
Setup: Use RetroArch Images JSON from "Retroarch Download Images.js".
Setup: Use RetroArch Steam JSON from "Retroarch Steam Config.js".`;
const main = async () => {
const input = await getInput({
name: 'Retroarch Steam Grid',
help,
inScriptable: true,
args: [{
name: 'folder',
shortName: 'f',
type: 'pathFolder',
bookmarkName: 'retroarch-steam-grid-folder',
help: 'The working directory (WD) to scan for images.'
}, {
name: 'images',
shortName: 'i',
type: 'pathFile',
bookmarkName: 'retroarch-steam-grid-images',
help: 'The config file used by Retroarch Download Images.'
}, {
name: 'config',
shortName: 'c',
type: 'pathFile',
bookmarkName: 'retroarch-steam-grid-config',
help: 'The config file used by Retroarch Steam Config.'
}]
});
if (!input) { return; }
const folder = string(input.folder);
const json = await readJson(string(input.config));
const config = /** @type {RetroArch.SteamConfig|null} */(json);
const imagesJson = await readJson(string(input.images));
const images = /** @type {RetroArch.ImageConfig|null} */(imagesJson);
if (!config) {
return error('Retroarch Steam Grid', 'Could not read config.');
}
if (!images) {
return error('Retroarch Steam Grid', 'Could not read images config.');
}
const pathImages = pathJoin(folder, nameOutput, nameImages);
const pathTo = pathJoin(folder, nameOutput, nameGrid);
await makeDirectory(pathTo);
const crc32Table = makeCrc32Table();
const subMap = config.substitutions;
let copied = 0;
let missing = 0;
for(const emulator of config.emulators) {
const nameRoms = emulator.directory;
const pathRoms = pathJoin(folder, nameRoms);
const imageMapConfig = images.folderMap[emulator.directory];
const nameFrom = imageMapConfig ? imageMapConfig.out : nameRoms;
const pathFrom = pathJoin(pathImages, nameFrom, nameImagesType);
const suffixes = emulator.extensions.map(x => '.' + x.toLowerCase());
for(const fileName of await listFiles(pathRoms)) {
const fileLower = fileName.toLowerCase();
const suffix = suffixes.find(x => fileLower.endsWith(x));
if (typeof suffix === 'string') {
const nameLength = fileName.length - suffix.length;
const name = fileName.substring(0, nameLength);
const nameApp = name.replace(/\(.*?\)|\[.*?\]/g, '').trim();
const pathFromImage = pathJoin(pathFrom, name + '.png');
if (!await isFile(pathFromImage)) {
log(`WARNING: Could not find image for: ${name}.png`);
missing += 1;
} else {
const exe = substitute(subMap, emulator.exe);
const checksum = getCrc32(crc32Table, exe + nameApp) >>> 0;
const id32 = BigInt(checksum) | BigInt(0x80000000);
const id = (id32 << BigInt(32)) | BigInt(0x02000000);
const nameToImage = id.toString() + '.png';
const pathToImage = pathJoin(pathTo, nameToImage);
await copyFile(pathFromImage, pathToImage);
copied += 1;
}
}
}
}
output('Retroarch Steam Grid', `Copied: ${copied}, missing: ${missing}`);
};
main();