45 lines
1.4 KiB
JavaScript
45 lines
1.4 KiB
JavaScript
'use strict';
|
|
|
|
const fs = require('fs');
|
|
const os = require('os');
|
|
const path = require('path');
|
|
|
|
function parseWorkshopPublishData(text) {
|
|
const sourceFolder = String(text).match(/"source_folder"\s+"([^"]+)"/)?.[1]?.trim() || '';
|
|
const publishTime = String(text).match(/"publish_time"\s+"(\d+)"/)?.[1] || '';
|
|
if (!sourceFolder) return null;
|
|
return { sourceFolder, publishTime };
|
|
}
|
|
|
|
function defaultWorkshopRoots() {
|
|
const home = os.homedir();
|
|
return [
|
|
process.env.DOTA_WORKSHOP_DIR,
|
|
path.join(home, '.local', 'share', 'Steam', 'steamapps', 'workshop', 'content', '570'),
|
|
path.join(home, '.steam', 'steam', 'steamapps', 'workshop', 'content', '570'),
|
|
].filter(Boolean);
|
|
}
|
|
|
|
function resolveLocalWorkshopMetadata(customGameId, roots = defaultWorkshopRoots()) {
|
|
const id = String(customGameId || '').trim();
|
|
if (!/^\d+$/.test(id)) return null;
|
|
|
|
for (const root of [...new Set(roots)]) {
|
|
const file = path.join(root, id, 'publish_data.txt');
|
|
try {
|
|
const metadata = parseWorkshopPublishData(fs.readFileSync(file, 'utf8'));
|
|
if (metadata) return { ...metadata, file };
|
|
} catch (err) {
|
|
if (err.code !== 'ENOENT' && err.code !== 'ENOTDIR') {
|
|
console.warn(`warn: could not read Workshop metadata ${file}: ${err.message}`);
|
|
}
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
module.exports = {
|
|
parseWorkshopPublishData,
|
|
resolveLocalWorkshopMetadata,
|
|
};
|