Skip to content
This repository has been archived by the owner on Jun 6, 2022. It is now read-only.

Commit

Permalink
Save grids, heroes when importing
Browse files Browse the repository at this point in the history
  • Loading branch information
doZennn committed Dec 2, 2019
1 parent 47da8f1 commit bed5bc3
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 54 deletions.
27 changes: 16 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"iconv-lite": "^0.5.0",
"js-yaml": "^3.13.1",
"jsonminify": "^0.4.1",
"lnf": "^1.3.9",
"lodash": "^4.17.15",
"metrohash": "^2.6.0",
"promise-reflect": "^1.1.0",
Expand All @@ -112,7 +113,7 @@
"react-uwp": "^1.2.31",
"steam-categories": "^1.1.0",
"steam-shortcut-editor": "^3.1.1",
"steamgriddb": "^1.3.0",
"steamgriddb": "^1.3.1",
"steamid": "^1.1.3",
"winreg": "^1.2.4",
"xml-js": "^1.6.11"
Expand Down
4 changes: 2 additions & 2 deletions src/js/Components/Import/ImportList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ class ImportList extends React.Component {
}

let thumb;
if (this.grids[i].length > 0) {
thumb = this.grids[i][0].thumb;
if (this.grids[i]) {
thumb = this.grids[i].thumb;
}

return (
Expand Down
102 changes: 86 additions & 16 deletions src/js/Import.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const Store = window.require('electron-store');
const SGDB = window.require('steamgriddb');
const { metrohash64 } = window.require('metrohash');
const log = window.require('electron-log');
const { join, extname, dirname } = window.require('path');
const Lnf = window.require('lnf');

class Import extends React.Component {
constructor(props) {
Expand All @@ -28,6 +30,8 @@ class Import extends React.Component {
class: platformModules[key].default,
games: [],
grids: [],
posters: [],
heroes: [],
installed: false,
error: false,
}));
Expand Down Expand Up @@ -72,22 +76,7 @@ class Import extends React.Component {
// Get grids for each platform
const ids = platform.games.map((x) => encodeURIComponent(x.id));
const getGrids = this.SGDB.getGrids({ type: platform.id, id: ids.join(',') }).then((res) => {
let formatted;
// if only single id then return first grid
if (ids.length === 1) {
if (res.length > 0) {
formatted = [[res[0]]];
}
} else {
// if multiple ids treat each object as a request
formatted = res.map((x) => {
if (x.success) {
return x.data;
}
return false;
});
}
platform.grids = formatted;
platform.grids = this._formatResponse(ids, res);
}).catch(() => {
// show an error toast
});
Expand Down Expand Up @@ -115,6 +104,26 @@ class Import extends React.Component {
this.store.set('games', gamesStorage);
}

// @todo this is horrible but can't be arsed right now
_formatResponse(ids, res) {
let formatted = false;
// if only single id then return first grid
if (ids.length === 1) {
if (res.length > 0) {
formatted = [res[0]];
}
} else {
// if multiple ids treat each object as a request
formatted = res.map((x) => {
if (x.success) {
if (x.data[0]) return x.data[0];
}
return false;
});
}
return formatted;
}

addGames(games, platform) {
this.saveImportedGames(games);

Expand Down Expand Up @@ -142,6 +151,67 @@ class Import extends React.Component {
</p>
),
});
}).then(() => {
// Download images
PubSub.publish('toast', {
logoNode: 'Download',
title: 'Downloading Images...',
contents: (<p>Downloading images for imported games...</p>),
});

const ids = games.map((x) => encodeURIComponent(x.id));
let posters = [];
let heroes = [];

// Get posters
const getPosters = this.SGDB.getGrids({ type: platform.id, id: ids.join(','), dimensions: ['600x900'] }).then((res) => {
posters = this._formatResponse(ids, res);
}).catch(() => {
// show an error toast
});

// Get heroes
const getHeroes = this.SGDB.getHeroes({ type: platform.id, id: ids.join(',') }).then((res) => {
heroes = this._formatResponse(ids, res);
}).catch(() => {
// show an error toast
});

Promise.all([getPosters, getHeroes]).then(() => {
const downloadPromises = [];
games.forEach((game, i) => {
const appId = Steam.generateNewAppId(game.exe, game.name);

// Take (legacy) grids from when we got them for the ImportList
const savedGrid = platform.grids[platform.games.indexOf(games[i])];
if (platform.grids[i] && savedGrid) {
const appIdOld = Steam.generateAppId(game.exe, game.name);
const saveGrids = Steam.addAsset('horizontalGrid', appId, savedGrid.url).then((dest) => {
// Symlink to old appid so it works in BPM
Lnf.sync(dest, join(dirname(dest), `${appIdOld}${extname(dest)}`));
});
downloadPromises.push(saveGrids);
}

// Download posters
if (posters[i]) {
downloadPromises.push(Steam.addAsset('verticalGrid', appId, posters[i].url));
}

// Download heroes
if (heroes[i]) {
downloadPromises.push(Steam.addAsset('hero', appId, heroes[i].url));
}
});

Promise.all(downloadPromises).then(() => {
PubSub.publish('toast', {
logoNode: 'Download',
title: 'Downloadeds Complete',
contents: (<p>All Images Downloaded!</p>),
});
});
});
}).catch((err) => {
if (err.type === 'OpenError') {
PubSub.publish('toast', {
Expand Down
7 changes: 1 addition & 6 deletions src/js/Search.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,7 @@ class Search extends React.Component {
}

this.setIsDownloading(true);
const itemsClone = { ...this.state.items };
Steam.addGrid(props.appid, props.image, (progress) => {
this.setState({ downloadProgress: progress });
itemsClone[props.index].progress = progress;
this.setState({ itemsClone });
}).then((dest) => {
Steam.addAsset('horizontalGrid', props.appid, props.image).then((dest) => {
this.setImageDownloaded(props.appid, props.name, dest);
}).catch(() => {
this.setIsDownloading(false);
Expand Down
42 changes: 24 additions & 18 deletions src/js/Steam.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { crc32 } from 'crc';
const Registry = window.require('winreg');
const Store = window.require('electron-store');
const fs = window.require('fs');
const { join } = window.require('path');
const { join, extname } = window.require('path');
const VDF = window.require('@node-steam/vdf');
const shortcut = window.require('steam-shortcut-editor');
const https = window.require('https');
Expand Down Expand Up @@ -265,13 +265,6 @@ class Steam {
return image;
}

static deleteCustomGridImage(userdataGridPath, appid) {
const imagePath = this.getCustomGridImage(userdataGridPath, appid);
if (imagePath) {
fs.unlinkSync(imagePath);
}
}

static getShortcutFile() {
return new Promise((resolve) => {
this.getSteamPath().then((steamPath) => {
Expand All @@ -284,12 +277,27 @@ class Steam {
});
}

static addGrid(appId, url, onProgress = () => {}) {
static addAsset(type, appId, url) {
return new Promise((resolve, reject) => {
this.getCurrentUserGridPath().then((userGridPath) => {
const imageUrl = url;
const imageExt = imageUrl.substr(imageUrl.lastIndexOf('.') + 1);
const dest = join(userGridPath, `${appId}.${imageExt}`);
const imageExt = extname(imageUrl);

let dest;

switch (type) {
case 'horizontalGrid':
dest = join(userGridPath, `${appId}${imageExt}`);
break;
case 'verticalGrid':
dest = join(userGridPath, `${appId}p${imageExt}`);
break;
case 'hero':
dest = join(userGridPath, `${appId}_hero${imageExt}`);
break;
default:
reject();
}

let cur = 0;
const data = new Stream();
Expand All @@ -298,21 +306,19 @@ class Steam {
https.get(url, (response) => {
const len = parseInt(response.headers['content-length'], 10);

response.on('end', () => {
this.deleteCustomGridImage(userGridPath, appId);
fs.writeFileSync(dest, data.read());
resolve(dest);
});

response.on('data', (chunk) => {
cur += chunk.length;
data.push(chunk);
progress = Math.round((cur / len) * 10) / 10;
if (progress !== lastProgress) {
lastProgress = progress;
onProgress(progress);
}
});

response.on('end', () => {
fs.writeFileSync(dest, data.read());
resolve(dest);
});
}).on('error', (err) => {
fs.unlink(dest);
reject(err);
Expand Down

0 comments on commit bed5bc3

Please sign in to comment.