Skip to content

Commit

Permalink
Created Windows app on Electron
Browse files Browse the repository at this point in the history
  • Loading branch information
NSGolova committed Jan 31, 2022
1 parent f09da40 commit 0347848
Show file tree
Hide file tree
Showing 15 changed files with 2,870 additions and 916 deletions.
109 changes: 109 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
// Modules to control application life and create native browser window
const { app, BrowserWindow, dialog } = require('electron');
const path = require('path');
const serve = require('electron-serve');
const loadURL = serve({ directory: 'public' });

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;

function isDev() {
return !app.isPackaged;
}

function UpsertKeyValue(obj, keyToChange, value) {
const keyToChangeLower = keyToChange.toLowerCase();
for (const key of Object.keys(obj)) {
if (key.toLowerCase() === keyToChangeLower) {
// Reassign old key
obj[key] = value;
// Done
return;
}
}
// Insert at end instead
obj[keyToChange] = value;
}

function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1400,
height: 800,
webPreferences: {
nodeIntegration: true,
preload: path.join(__dirname, 'preload.js'),
// enableRemoteModule: true,
// contextIsolation: false
},
icon: path.join(__dirname, 'public/favicon.png'),
show: false
});

// This block of code is intended for development purpose only.
// Delete this entire block of code when you are ready to package the application.
if (isDev()) {
mainWindow.loadURL('http://localhost:5000/');
} else {
loadURL(mainWindow);
}

// Uncomment the following line of code when app is ready to be packaged.
// loadURL(mainWindow);

// Open the DevTools and also disable Electron Security Warning.
// process.env['ELECTRON_DISABLE_SECURITY_WARNINGS'] = true;
// mainWindow.webContents.openDevTools();

// Emitted when the window is closed.
mainWindow.on('closed', function () {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
});

// Emitted when the window is ready to be shown
// This helps in showing the window gracefully.
mainWindow.once('ready-to-show', () => {
mainWindow.show()
});

mainWindow.webContents.session.webRequest.onBeforeSendHeaders(
(details, callback) => {
const { requestHeaders } = details;
UpsertKeyValue(requestHeaders, 'Access-Control-Allow-Origin', ['*']);
callback({ requestHeaders });
},
);

mainWindow.webContents.session.webRequest.onHeadersReceived((details, callback) => {
const { responseHeaders } = details;
UpsertKeyValue(responseHeaders, 'Access-Control-Allow-Origin', ['*']);
UpsertKeyValue(responseHeaders, 'Access-Control-Allow-Headers', ['*']);
callback({
responseHeaders,
});
});
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);

// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') app.quit()
});

app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) createWindow()
});
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
25 changes: 23 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
"build": "rollup -c",
"dev": "rollup -c -w",
"start": "sirv public --no-clear --single",
"validate": "svelte-check"
"validate": "svelte-check",
"electron": "wait-on http://localhost:9999 && electron .",
"electron-dev": "concurrently \"yarn run dev\" \"yarn run electron\"",
"preelectron-pack": "yarn run build",
"electron-pack": "electron-builder"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^17.0.0",
Expand All @@ -18,6 +22,9 @@
"chartjs-adapter-luxon": "^1.1.0",
"chartjs-plugin-zoom": "^1.1.1",
"comlink": "^4.3.1",
"concurrently": "^7.0.0",
"electron": "16.0.8",
"electron-builder": "22.14.5",
"eventemitter3": "^4.0.7",
"idb": "^6.1.2",
"immer": "^9.0.5",
Expand All @@ -35,10 +42,24 @@
"svelte-preprocess": "^4.0.0",
"svelte-routing": "^1.6.0",
"tslib": "^2.0.0",
"typescript": "^4.0.0"
"typescript": "^4.0.0",
"wait-on": "^6.0.0"
},
"dependencies": {
"electron-serve": "^1.1.0",
"sirv-cli": "^1.0.0",
"svelte-simple-modal": "^1.1.1"
},
"main": "main.js",
"build": {
"icon": "public/assets/favicon.png",
"productName": "BeatLeader",
"files": [
"public/**/*",
"main.js"
],
"win": {},
"linux": {},
"mac": {}
}
}
1 change: 1 addition & 0 deletions preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('I am the app now!');
Binary file added public/assets/favicon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/components/Common/Flag.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</script>

{#if country && country.length}
<img src={`/cors/score-saber/imports/images/flags/${country ? country.toLowerCase() : '' }.png`} loading="lazy"
<img src={`https://scoresaber.com/imports/images/flags/${country ? country.toLowerCase() : '' }.png`} loading="lazy"
class="country"
on:click|preventDefault={() => dispatch('flag-click', {country: country.toLowerCase()})}>
{/if}
Expand Down
2 changes: 1 addition & 1 deletion src/network/clients/scoresaber/player/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const process = response => {
avatar = response[1].response.players[0].avatarfull;
}
let externalProfileUrl = stemProfile ? response[1].response.players[0].profileurl : null;
let externalProfileCorsUrl = externalProfileUrl ? externalProfileUrl.replace('https://steamcommunity.com/', '/cors/steamcommunity/') : null
let externalProfileCorsUrl = externalProfileUrl

return {playerId, name, playerInfo: {
avatar,
Expand Down
3 changes: 0 additions & 3 deletions src/network/clients/scoresaber/players/utils/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ export default response => {
if (avatar) {
if (!avatar.startsWith('http'))
avatar = `${queue.SCORESABER_API.SS_API_HOST}${!avatar.startsWith('/') ? '/' : ''}${avatar}`;
else {
avatar = avatar.replace('https://cdn.scoresaber.com/', '/cors/score-saber-cdn/')
}
}

return {
Expand Down
2 changes: 1 addition & 1 deletion src/network/queues/beatmaps/api-queue.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {default as createQueue, PRIORITY} from '../http-queue';
import {substituteVars} from "../../../utils/format";

const BEATMAPS_API_URL = 'https://api.beatsaver.com/';
const BEATMAPS_API_URL = 'https://api.beatsaver.com';
const SONG_BY_HASH_URL = BEATMAPS_API_URL + '/maps/hash/${hash}';
const SONG_BY_KEY_URL = BEATMAPS_API_URL + '/maps/id/${key}'

Expand Down
2 changes: 1 addition & 1 deletion src/network/queues/beatsavior/api-queue.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {default as createQueue, PRIORITY} from '../http-queue';
import {substituteVars} from "../../../utils/format";

const BEATSAVIOR_API_URL = '/cors/beat-savior';
const BEATSAVIOR_API_URL = 'https://www.beatsavior.io/api/livescores/player';
const PLAYER_URL = BEATSAVIOR_API_URL + '/${playerId}';

export default (options = {}) => {
Expand Down
4 changes: 2 additions & 2 deletions src/network/queues/scoresaber/api-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {default as createQueue, PRIORITY} from '../http-queue';
import {substituteVars} from '../../../utils/format'
import {PLAYER_SCORES_PER_PAGE, PLAYERS_PER_PAGE} from '../../../utils/scoresaber/consts'

const SS_CORS_HOST = '/cors/score-saber';
const SS_CORS_HOST = 'https://scoresaber.com';
export const SS_API_URL = `${SS_CORS_HOST}/api`;
export const STEAM_API_URL = '/cors/steamapi'
export const STEAM_API_URL = 'https://api.steampowered.com'
export const STEAM_KEY = 'B0A7AF33E804D0ABBDE43BA9DD5DAB48';

export const SS_API_PLAYER_INFO_URL = SS_API_URL + '/player/${playerId}/full';
Expand Down
6 changes: 3 additions & 3 deletions src/network/queues/scoresaber/page-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import {dateFromString} from '../../../utils/date'
import {LEADERBOARD_SCORES_PER_PAGE} from '../../../utils/scoresaber/consts'
import {formatDateRelative} from '../../../utils/date'

export const SS_HOST = '/cors/score-saber';
export const SS_CDN_HOST = '/cors/score-saber-cdn';
const SS_CORS_HOST = '/cors/score-saber';
export const SS_HOST = 'https://scoresaber.com';
export const SS_CDN_HOST = 'https://cdn.scoresaber.com';
const SS_CORS_HOST = 'https://scoresaber.com';
const RANKEDS_URL = SS_CORS_HOST + '/api.php?function=get-leaderboards&cat=1&limit=5000&ranked=1&page=${page}';
const PLAYER_PROFILE_URL = SS_CORS_HOST + '/u/${playerId}?page=1&sort=2'
const COUNTRY_RANKING_URL = SS_CORS_HOST + '/api/players?page=${page}&countries=${country}'
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Leaderboard.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@
.with-badge {
height: 100%;
text-align: center;
text-align: center
}
.pp.with-badge {
Expand Down
8 changes: 7 additions & 1 deletion src/services/beatmaps.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,13 @@ export default () => {
let bsSuspension = await getCurrentSuspension();

try {
if (isSuspended(bsSuspension) || (hash && await isHashUnavailable(hash))) return null;
if (isSuspended(bsSuspension)) {
log.warn(`Song "${errSongId}" is suspended.`);
}

if ((hash && await isHashUnavailable(hash))) {
log.warn(`Song "${errSongId}" hash unavailable.`);
}

const songInfo = await fetchFunc();
if (!songInfo) {
Expand Down
2 changes: 1 addition & 1 deletion src/services/scoresaber/replays.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import createScoresService from './scores'
import makePendingPromisePool from '../../utils/pending-promises'
import {getTotalPpFromSortedPps} from '../../utils/scoresaber/pp'

const SS_CORS_HOST = '/cors/score-saber';
const SS_CORS_HOST = 'https://scoresaber.com';

let service = null;
export default () => {
Expand Down
Loading

0 comments on commit 0347848

Please sign in to comment.