an-anime-game-launcher/src/index.svelte
Observer KRypt0n_ 3904a86381
API improvements
- added `folders` field in config file
  and now you can specify some pathes
- added `constants.paths.tempDir` field
2022-01-06 15:46:49 +02:00

152 lines
5.1 KiB
Svelte

<script context="module" lang="ts">
declare const Neutralino;
</script>
<script lang="ts">
import { onMount } from 'svelte';
import { _, locale } from 'svelte-i18n';
import Window from './ts/neutralino/Window';
import Launcher from './ts/Launcher';
import constants from './ts/Constants';
import Game from './ts/Game';
import Background from './ts/launcher/Background';
import Archive from './ts/core/Archive';
import Debug from './ts/core/Debug';
import Downloader from './ts/core/Downloader';
import IPC from './ts/core/IPC';
import Process from './ts/neutralino/Process';
import Gear from './assets/images/gear.png';
import GearActive from './assets/images/gear-active.png';
import Download from './assets/images/cloud-download.png';
constants.paths.launcherDir.then((dir) => {
// Create launcher folder if it doesn't exist
Neutralino.filesystem.getStats(dir)
.catch(() => Neutralino.filesystem.createDirectory(dir));
// Create logs folder if it doesn't exist
Neutralino.filesystem.getStats(`${dir}/logs`)
.catch(() => Neutralino.filesystem.createDirectory(`${dir}/logs`));
});
const launcher = new Launcher(onMount);
Neutralino.events.on('ready', () => {
Window.open('splash', {
title: 'Splash',
width: 300,
height: 400,
borderless: true,
exitProcessOnClose: false
});
});
Neutralino.events.on('windowClose', () => {
Downloader.closeStreams(true);
Archive.closeStreams(true);
constants.paths.tempDir.then(async (tempDir) => {
// Remove IPC file
await IPC.purge();
// Turn off Discord RPC
if (launcher.rpc)
await launcher.rpc.stop(true);
// Remove .tmp files from the temp folder
await Neutralino.os.execCommand(`rm -f "${Process.addSlashes(`${tempDir}/*.tmp`)}"`);
// Save logs
const log = Debug.get().join("\r\n");
if (log != '')
await Neutralino.filesystem.writeFile(`${await constants.paths.launcherDir}/logs/${Debug.startedAt.getDate()}-${Debug.startedAt.getMonth() + 1}-${Debug.startedAt.getFullYear()}-${Debug.startedAt.getHours()}-${Debug.startedAt.getMinutes()}-${Debug.startedAt.getSeconds()}.log`, log);
// And close the launcher when they was saved
Neutralino.app.exit();
});
});
// Save logs
let logSavingStarted = false;
Debug.handler(() => {
if (!logSavingStarted)
{
logSavingStarted = true;
setTimeout(async () => {
const log = `=== Log can be incomplete ===\r\n\r\n${Debug.get().join("\r\n")}`;
if (log != '')
await Neutralino.filesystem.writeFile(`${await constants.paths.launcherDir}/logs/${Debug.startedAt.getDate()}-${Debug.startedAt.getMonth() + 1}-${Debug.startedAt.getFullYear()}-${Debug.startedAt.getHours()}-${Debug.startedAt.getMinutes()}-${Debug.startedAt.getSeconds()}.log`, log);
logSavingStarted = false;
}, 5000);
}
});
// Do some stuff when all the content will be loaded
onMount(() => {
/**
* Update launcher's title
*/
Game.latest.then((game) => {
Window.current.setTitle(`${constants.placeholders.uppercase.full} Linux Launcher - ${game.version} (beta revision)`);
});
/**
* Add some events to some elements
*/
const settingsButton = document.getElementById('settings');
settingsButton!.onclick = () => launcher.showSettings();
settingsButton!.onmouseenter = () => {
settingsButton?.classList.add('hovered');
};
settingsButton!.onmouseleave = () => {
settingsButton?.classList.remove('hovered');
};
});
</script>
<main>
{#await Background.get() then uri}
<img class="background" src="{uri}" alt="">
{/await}
<div class="downloader-panel" data-theme="light">
<div class="downloader-label">
<span id="downloaded"></span>
<span id="speed"></span>
<span id="eta"></span>
</div>
<div class="progress-bar">
<div class="progress"></div>
</div>
</div>
<div id="launcher-content">
{#await launcher.getSocial() then uri}
<iframe title="Launcher-iframe" src="{uri}" scrolling="no" style="position: absolute; border: 0; top: 0; left: 0;" width="100%" height="100%"></iframe>
{/await}
</div>
<div id="settings">
<img src={Gear} class="unactive" alt="Settings">
<img src={GearActive} class="active" alt="Settings">
</div>
<button class="button hint--left hint--small" aria-label="{typeof $locale === 'string' ? $_('launcher.predownload') : ''}" id="predownload">
<img src={Download} alt="Download" />
</button>
<button class="button hint--top hint--large" aria-label="" id="launch">Launch</button>
</main>