mirror of
https://github.com/an-anime-team/an-anime-game-launcher.git
synced 2025-02-18 08:12:16 +03:00
Several changes
- updated dependencies - disabled app hot reloading - improved `Downloader.download` method - updated readme
This commit is contained in:
parent
2d1eaa6eb4
commit
7a5a0e572b
6 changed files with 94 additions and 60 deletions
|
@ -89,6 +89,7 @@ They're required only for some specific functions
|
|||
git clone https://gitlab.com/KRypt0n_/an-anime-game-launcher
|
||||
cd an-anime-game-launcher
|
||||
yarn
|
||||
yarn neu update
|
||||
```
|
||||
|
||||
## Run
|
||||
|
|
|
@ -9,6 +9,8 @@
|
|||
|
||||
<body>
|
||||
<div id="app" theme="light">
|
||||
<h1 id="progress"></h1>
|
||||
|
||||
<img class="background">
|
||||
|
||||
<div id="downloader-panel" class="dark" style="display: none">
|
||||
|
|
|
@ -9,8 +9,8 @@
|
|||
"enableNativeAPI": true,
|
||||
"exportAuthInfo": false,
|
||||
"logging": {
|
||||
"enabled": false,
|
||||
"writeToLogFile": false
|
||||
"enabled": true,
|
||||
"writeToLogFile": true
|
||||
},
|
||||
"nativeBlockList": [],
|
||||
"globalVariables": {},
|
||||
|
|
|
@ -2,16 +2,18 @@
|
|||
"name": "an-anime-game-launcher",
|
||||
"version": "3.0.0",
|
||||
"scripts": {
|
||||
"dev": "vite build && neu run",
|
||||
"neu": "neu",
|
||||
"dev": "vite build && neu run --disable-auto-reload",
|
||||
"build": "vite build && neu build --release"
|
||||
},
|
||||
"dependencies": {
|
||||
"sass": "^1.45.0",
|
||||
"es6-promisify": "^7.0.0",
|
||||
"vue": "^3.2.25"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@neutralinojs/neu": "^8.0.0",
|
||||
"@vitejs/plugin-vue": "^2.0.1",
|
||||
"vite": "^2.7.3"
|
||||
"vite": "^2.7.4",
|
||||
"sass": "^1.45.1"
|
||||
}
|
||||
}
|
||||
|
|
|
@ -4,26 +4,28 @@ import Window from '../ts/neutralino/Window';
|
|||
|
||||
import Downloader from '../ts/Downloader';
|
||||
|
||||
Downloader.download('https://vitejs.dev/', 'test.html').then((stream) => {
|
||||
stream.progress((current: number, total: number) => {
|
||||
console.log(`${Math.round(current / total * 100)}%`);
|
||||
});
|
||||
|
||||
stream.finish(() => {
|
||||
console.log('finished');
|
||||
});
|
||||
});
|
||||
|
||||
Vue.createApp({
|
||||
data: () => {
|
||||
return {
|
||||
title: 'index'
|
||||
};
|
||||
},
|
||||
const app = Vue.createApp({
|
||||
data: () => ({
|
||||
progress: '0%'
|
||||
}),
|
||||
|
||||
methods: {
|
||||
showAbout: () => Window.open('about')
|
||||
},
|
||||
|
||||
mounted: () => Window.current.show()
|
||||
}).mount('#app');
|
||||
mounted: () => {
|
||||
Window.current.show();
|
||||
|
||||
Downloader.download('https://autopatchhk.yuanshen.com/client_app/download/pc_zip/20211117173404_G0gLRnxvOd4PvSu9/Audio_English(US)_2.3.0.zip').then((stream) => {
|
||||
stream.progress((current: number, total: number) => {
|
||||
document.getElementById('progress').innerHTML = `${Math.round(current / total * 100)}%`;
|
||||
});
|
||||
|
||||
stream.finish(() => {
|
||||
console.log('finished');
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
app.mount('#app');
|
||||
|
|
|
@ -1,63 +1,80 @@
|
|||
class Stream
|
||||
{
|
||||
protected uri: string;
|
||||
protected total: number|null;
|
||||
public progressInterval: number = 200;
|
||||
|
||||
protected onProgress?: (current: number, total: number) => void;
|
||||
protected uri: string;
|
||||
protected total: number;
|
||||
protected previous: number = 0;
|
||||
|
||||
protected onProgress?: (current: number, total: number, difference: number) => void;
|
||||
protected onFinish?: () => void;
|
||||
|
||||
protected finished: boolean = false;
|
||||
|
||||
public constructor(uri: string, output: string, total: number|null = null)
|
||||
public constructor(uri: string, output: string, total: number)
|
||||
{
|
||||
this.uri = uri;
|
||||
this.total = total;
|
||||
|
||||
// @ts-expect-error
|
||||
Neutralino.os.execCommand(`wget -O "${output}" -nv "${uri}"`).then(() => {
|
||||
this.finished = true;
|
||||
|
||||
this.onFinish();
|
||||
});
|
||||
|
||||
if (total !== null)
|
||||
{
|
||||
const updateProgress = () => {
|
||||
Neutralino.os.execCommand('pidof curl').then((stats) => {
|
||||
if (stats.stdOut == '')
|
||||
{
|
||||
// @ts-expect-error
|
||||
Neutralino.filesystem.getStats(output).then((stats) => {
|
||||
if (this.onProgress)
|
||||
this.onProgress(stats.size, this.total);
|
||||
|
||||
if (!this.finished)
|
||||
setTimeout(updateProgress, 100);
|
||||
Neutralino.os.execCommand(`curl -s -L -N -o "${output}" "${uri}"`, {
|
||||
background: true
|
||||
});
|
||||
};
|
||||
|
||||
updateProgress();
|
||||
}
|
||||
const updateProgress = () => {
|
||||
// @ts-expect-error
|
||||
Neutralino.filesystem.getStats(output).then((stats) => {
|
||||
if (this.onProgress)
|
||||
this.onProgress(stats.size, this.total, this.previous - stats.size);
|
||||
|
||||
this.previous = stats.size;
|
||||
|
||||
if (stats.size >= this.total)
|
||||
{
|
||||
this.finished = true;
|
||||
|
||||
if (this.onFinish)
|
||||
this.onFinish();
|
||||
}
|
||||
|
||||
if (!this.finished)
|
||||
setTimeout(updateProgress, this.progressInterval);
|
||||
}).catch(() => {
|
||||
if (!this.finished)
|
||||
setTimeout(updateProgress, this.progressInterval);
|
||||
});
|
||||
};
|
||||
|
||||
setTimeout(updateProgress, this.progressInterval);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify event that will be called every 100 ms during the file downloading
|
||||
* Specify event that will be called every [this.progressInterval] ms during the file downloading
|
||||
*
|
||||
* @param callable
|
||||
* @param callback
|
||||
*/
|
||||
public progress(callable: (current: number, total: number) => void)
|
||||
public progress(callback: (current: number, total: number, difference: number) => void)
|
||||
{
|
||||
this.onProgress = callable;
|
||||
this.onProgress = callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify event that will be called after the file will be downloaded
|
||||
*
|
||||
* @param callable
|
||||
* @param callback
|
||||
*/
|
||||
public finish(callable: () => void)
|
||||
public finish(callback: () => void)
|
||||
{
|
||||
this.onFinish = callable;
|
||||
this.onFinish = callback;
|
||||
|
||||
if (this.finished)
|
||||
callable();
|
||||
callback();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -71,25 +88,35 @@ export default class Downloader
|
|||
*
|
||||
* @returns Promise<Stream>
|
||||
*/
|
||||
public static async download(uri: string, output: string): Promise<Stream>
|
||||
public static async download(uri: string, output: string|null = null): Promise<Stream>
|
||||
{
|
||||
return new Promise(async (resolve) => {
|
||||
// @ts-expect-error
|
||||
let statsRaw = await Neutralino.os.execCommand(`wget --spider "${uri}"`);
|
||||
let statsRaw = await Neutralino.os.execCommand(`curl -s -I -L "${uri}"`);
|
||||
|
||||
if (statsRaw.stdOut == '')
|
||||
statsRaw = statsRaw.stdErr;
|
||||
|
||||
else statsRaw = statsRaw.stdOut;
|
||||
|
||||
let length: any = /Length: ([\d]+)/.exec(statsRaw)[1] ?? null;
|
||||
const length = parseInt(/content-length: ([\d]+)/i.exec(statsRaw)[1]);
|
||||
|
||||
if (length !== null)
|
||||
length = parseInt(length);
|
||||
|
||||
resolve(new Stream(uri, output, length));
|
||||
resolve(new Stream(uri, output ?? this.fileFromUri(uri), length));
|
||||
});
|
||||
}
|
||||
|
||||
protected static fileFromUri(uri: string): string
|
||||
{
|
||||
const file = uri.split('/').pop().split('#')[0].split('?')[0];
|
||||
|
||||
if (file === '')
|
||||
return 'index.html';
|
||||
|
||||
else if (`https://${file}` != uri && `http://${file}` != uri)
|
||||
return file;
|
||||
|
||||
else 'index.html';
|
||||
}
|
||||
}
|
||||
|
||||
export { Stream };
|
||||
|
|
Loading…
Add table
Reference in a new issue