API improvements

- now pre-downloaded game will be automatically unpacked
This commit is contained in:
Observer KRypt0n_ 2021-12-29 19:32:09 +02:00
parent 77969868e3
commit 2a4dcc03e3
No known key found for this signature in database
GPG key ID: DC5D4EC1303465DA
3 changed files with 108 additions and 59 deletions

View file

@ -18,9 +18,9 @@ declare const Neutralino;
class Stream extends AbstractInstaller class Stream extends AbstractInstaller
{ {
public constructor(uri: string) public constructor(uri: string, predownloaded: boolean = false)
{ {
super(uri, constants.paths.gameDir); super(uri, constants.paths.gameDir, predownloaded);
} }
} }
@ -170,9 +170,24 @@ export default class Game
}); });
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
(version === null ? this.latest : this.getDiff(version)) this.isUpdatePredownloaded().then(async (predownloaded) => {
.then((data: Latest|Diff|null) => resolve(data === null ? null : new Stream(data.path))) if (predownloaded)
.catch((error) => reject(error)); {
Debug.log({
function: 'Game.update',
message: 'Update is already pre-downloaded. Unpacking started'
});
resolve(new Stream(`${await constants.paths.launcherDir}/game-predownloaded.zip`, true));
}
else
{
(version === null ? this.latest : this.getDiff(version))
.then((data: Latest|Diff|null) => resolve(data === null ? null : new Stream(data.path)))
.catch((error) => reject(error));
}
});
}); });
} }

View file

@ -12,9 +12,9 @@ declare const Neutralino;
class Stream extends AbstractInstaller class Stream extends AbstractInstaller
{ {
public constructor(uri: string) public constructor(uri: string, predownloaded: boolean = false)
{ {
super(uri, constants.paths.gameDir); super(uri, constants.paths.gameDir, predownloaded);
} }
} }
@ -129,19 +129,34 @@ export default class Voice
}); });
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
(version === null ? this.latest : this.getDiff(version)) this.isUpdatePredownloaded(lang).then(async (predownloaded) => {
.then((data: VoicePack[]|null) => { if (predownloaded)
if (data === null) {
resolve(null); Debug.log({
function: 'Voice.update',
message: 'Voice package update is already pre-downloaded. Unpacking started'
});
else resolve(new Stream(`${await constants.paths.launcherDir}/voice-${lang}-predownloaded.zip`, true));
{ }
const voice = data.filter(voice => voice.language === lang);
resolve(voice.length === 1 ? new Stream(voice[0].path) : null); else
} {
}) (version === null ? this.latest : this.getDiff(version))
.catch((error) => reject(error)); .then((data: VoicePack[]|null) => {
if (data === null)
resolve(null);
else
{
const voice = data.filter(voice => voice.language === lang);
resolve(voice.length === 1 ? new Stream(voice[0].path) : null);
}
})
.catch((error) => reject(error));
}
});
}); });
} }

View file

@ -32,81 +32,100 @@ export default abstract class Installer
protected downloadFinished: boolean = false; protected downloadFinished: boolean = false;
protected unpackFinished: boolean = false; protected unpackFinished: boolean = false;
public constructor(uri: string, unpackDir: string|Promise<string>) /**
* @param uri URI to the archive we need to download
* @param unpackDir path to unpack this archive to
*
* @param alreadyDownloaded specifies whether the archive was already downloaded
* If true, then URI will be used as a path to the archive. Otherwise stream will download it
*/
public constructor(uri: string, unpackDir: string|Promise<string>, alreadyDownloaded: boolean = false)
{ {
const shouldResolve = typeof unpackDir !== 'string';
const debugThread = new DebugThread('AbstractInstaller', { const debugThread = new DebugThread('AbstractInstaller', {
message: { message: {
'uri': uri, 'uri': uri,
'unpack dir': typeof unpackDir === 'string' ? unpackDir : '<promise>' 'unpack dir': shouldResolve ? '<promise>' : unpackDir,
'already downloaded': alreadyDownloaded ? 'true' : 'false'
} }
}); });
constants.paths.launcherDir.then((launcherDir) => { constants.paths.launcherDir.then((launcherDir) => {
const archivePath = `${launcherDir}/${Downloader.fileFromUri(uri)}`; const archivePath = alreadyDownloaded ? uri : `${launcherDir}/${Downloader.fileFromUri(uri)}`;
// Download archive // And then unpack it
Downloader.download(uri, archivePath).then((stream) => { const unpackArchive = () => {
stream.progressInterval = this.downloadProgressInterval; Promise.resolve(unpackDir)
.then((unpackDir) => {
if (shouldResolve)
debugThread.log(`Resolved unpack dir: ${unpackDir}`);
stream.start(() => {
this.downloadStarted = true;
if (this.onDownloadStart)
this.onDownloadStart();
});
stream.progress((current, total, difference) => {
if (this.onDownloadProgress)
this.onDownloadProgress(current, total, difference);
});
stream.finish(() => {
this.downloadFinished = true;
if (this.onDownloadFinish)
this.onDownloadFinish();
// And then unpack it
const unpackArchive = (unpackDir: string) => {
Archive.unpack(archivePath, unpackDir).then((stream) => { Archive.unpack(archivePath, unpackDir).then((stream) => {
stream.progressInterval = this.unpackProgressInterval; stream.progressInterval = this.unpackProgressInterval;
stream.start(() => { stream.start(() => {
this.unpackStarted = true; this.unpackStarted = true;
if (this.onUnpackStart) if (this.onUnpackStart)
this.onUnpackStart(); this.onUnpackStart();
}); });
stream.progress((current, total, difference) => { stream.progress((current, total, difference) => {
if (this.onUnpackProgress) if (this.onUnpackProgress)
this.onUnpackProgress(current, total, difference); this.onUnpackProgress(current, total, difference);
}); });
stream.finish(() => { stream.finish(() => {
this.unpackFinished = true; this.unpackFinished = true;
Neutralino.filesystem.removeFile(archivePath); Neutralino.filesystem.removeFile(archivePath);
debugThread.log('Installation finished'); debugThread.log('Installation finished');
if (this.onUnpackFinish) if (this.onUnpackFinish)
this.onUnpackFinish(); this.onUnpackFinish();
}); });
}); });
}; });
};
const shouldResolve = typeof unpackDir !== 'string'; // Download archive
if (!alreadyDownloaded)
{
Downloader.download(uri, archivePath).then((stream) => {
stream.progressInterval = this.downloadProgressInterval;
Promise.resolve(unpackDir) stream.start(() => {
.then((unpackDir) => { this.downloadStarted = true;
if (shouldResolve)
debugThread.log(`Resolved unpack dir: ${unpackDir}`);
unpackArchive(unpackDir); if (this.onDownloadStart)
}); this.onDownloadStart();
});
stream.progress((current, total, difference) => {
if (this.onDownloadProgress)
this.onDownloadProgress(current, total, difference);
});
stream.finish(() => {
this.downloadFinished = true;
if (this.onDownloadFinish)
this.onDownloadFinish();
Promise.resolve(unpackDir)
.then((unpackDir) => {
if (shouldResolve)
debugThread.log(`Resolved unpack dir: ${unpackDir}`);
unpackArchive();
});
});
}); });
}); }
else unpackArchive();
}); });
} }