From 6ab7fa37f11636451db5bb1c2e73ade9a0613a9c Mon Sep 17 00:00:00 2001 From: Bill Date: Thu, 23 Dec 2021 02:42:40 -0500 Subject: [PATCH] Update converters --- src/converters/EffectConverter.ts | 17 ++- src/converters/FigureConverter.ts | 16 +-- src/converters/FurnitureConverter.ts | 183 +++------------------------ src/converters/PetConverter.ts | 16 +-- 4 files changed, 43 insertions(+), 189 deletions(-) diff --git a/src/converters/EffectConverter.ts b/src/converters/EffectConverter.ts index 1398d32..87a3fa0 100644 --- a/src/converters/EffectConverter.ts +++ b/src/converters/EffectConverter.ts @@ -1,4 +1,3 @@ -import { writeFile } from 'fs/promises'; import ora from 'ora'; import { singleton } from 'tsyringe'; import { Configuration, File, FileUtilities, IConverter } from '../common'; @@ -21,10 +20,9 @@ export class EffectConverter implements IConverter const now = Date.now(); const spinner = ora('Preparing Effects').start(); - const baseUrl = this._configuration.getValue('dynamic.download.effect.url'); + const downloadBase = this._configuration.getValue('dynamic.download.effect.url'); + const saveDirectory = await FileUtilities.getDirectory('./assets/bundled/effect'); const effectMap = this._effectMapConverter.effectMap; - const directory = await FileUtilities.getDirectory('./assets/bundled/effect'); - const classNames: string[] = []; if(effectMap.effects !== undefined) @@ -51,14 +49,15 @@ export class EffectConverter implements IConverter try { - const path = new File(directory.path + '/' + className + '.nitro'); + const saveFile = new File(saveDirectory.path + '/' + className + '.nitro'); - if(path.exists()) continue; + if(saveFile.exists()) continue; spinner.text = `Converting: ${ className } (${ (i + 1) } / ${ totalClassNames })`; spinner.render(); - const habboAssetSWF = await SWFDownloader.download(baseUrl, className, -1); + const downloadUrl = SWFDownloader.getDownloadUrl(downloadBase, className, -1); + const habboAssetSWF = await SWFDownloader.downloadFromUrl(downloadUrl); if(!habboAssetSWF) { @@ -70,9 +69,9 @@ export class EffectConverter implements IConverter const nitroBundle = await GenerateEffectBundle(habboAssetSWF, className, this.effectTypes.get(className)); - await writeFile(path.path, await nitroBundle.toBufferAsync()); + await saveFile.writeData(await nitroBundle.toBufferAsync()); - spinner.text = 'Converted: ' + className; + spinner.text = `Converted: ${ className }`; spinner.render(); } diff --git a/src/converters/FigureConverter.ts b/src/converters/FigureConverter.ts index 69cd8dc..da17815 100644 --- a/src/converters/FigureConverter.ts +++ b/src/converters/FigureConverter.ts @@ -1,4 +1,3 @@ -import { writeFile } from 'fs/promises'; import ora from 'ora'; import { singleton } from 'tsyringe'; import { Configuration, File, FileUtilities, IConverter } from '../common'; @@ -21,9 +20,9 @@ export class FigureConverter implements IConverter const now = Date.now(); const spinner = ora('Preparing Figure').start(); - const baseUrl = this._configuration.getValue('dynamic.download.figure.url'); + const downloadBase = this._configuration.getValue('dynamic.download.figure.url'); + const saveDirectory = await FileUtilities.getDirectory('./assets/bundled/figure'); const figureMap = this._figureMapConverter.figureMap; - const directory = await FileUtilities.getDirectory('./assets/bundled/figure'); const classNames: string[] = []; if(figureMap.libraries !== undefined) @@ -52,14 +51,15 @@ export class FigureConverter implements IConverter try { - const path = new File(directory.path + '/' + className + '.nitro'); + const saveFile = new File(`${ saveDirectory.path }/${ className }.nitro`); - if(path.exists()) continue; + if(saveFile.exists()) continue; spinner.text = `Converting: ${ className } (${ (i + 1) } / ${ totalClassNames })`; spinner.render(); - const habboAssetSWF = await SWFDownloader.download(baseUrl, className, -1); + const downloadUrl = SWFDownloader.getDownloadUrl(downloadBase, className, -1); + const habboAssetSWF = await SWFDownloader.downloadFromUrl(downloadUrl); if(!habboAssetSWF) { @@ -71,9 +71,9 @@ export class FigureConverter implements IConverter const nitroBundle = await GenerateFigureBundle(habboAssetSWF, className, this.figureTypes.get(className)); - await writeFile(path.path, await nitroBundle.toBufferAsync()); + await saveFile.writeData(await nitroBundle.toBufferAsync()); - spinner.text = 'Converted: ' + className; + spinner.text = `Converted: ${ className }`; spinner.render(); } diff --git a/src/converters/FurnitureConverter.ts b/src/converters/FurnitureConverter.ts index 3802151..4ba2de3 100644 --- a/src/converters/FurnitureConverter.ts +++ b/src/converters/FurnitureConverter.ts @@ -1,6 +1,6 @@ import ora from 'ora'; import { singleton } from 'tsyringe'; -import { Configuration, ConverterResult, File, FileUtilities, IConverter, NitroBundle } from '../common'; +import { Configuration, File, FileUtilities, IConverter } from '../common'; import { GenerateFurnitureBundle, SWFDownloader } from '../swf'; import { FurnitureDataConverter } from './FurnitureDataConverter'; @@ -12,11 +12,8 @@ export class FurnitureConverter implements IConverter private readonly _configuration: Configuration) {} - public async convertAsync(bundle: boolean = false, extract: boolean = false): Promise + public async convertAsync(): Promise { - if(bundle) await this.convertInputToNitro(); - if(extract) await this.extractNitroToFolder(); - if(!this._configuration.getBoolean('convert.furniture')) return; const now = Date.now(); @@ -36,31 +33,36 @@ export class FurnitureConverter implements IConverter try { + const saveFile = new File(`${ saveDirectory.path }/${ className }.nitro`); + + if(saveFile.exists()) continue; + spinner.text = `Converting: ${ className } (${ (i + 1) } / ${ totalClassNames })`; spinner.render(); const downloadUrl = SWFDownloader.getDownloadUrl(downloadBase, className, revision); - const result = await this.convertSwfToNitro(className, downloadUrl, saveDirectory, false); + const habboAssetSWF = await SWFDownloader.downloadFromUrl(downloadUrl); - switch(result) + if(!habboAssetSWF) { - case ConverterResult.OK: - spinner.text = 'Converted: ' + className; - spinner.render(); - break; - case ConverterResult.INVALID_SWF: - console.log(); - console.error(`Invalid SWF: ${ downloadUrl }`); - break; + console.log(); + console.error(`Invalid SWF: ${ downloadUrl }`); + + continue; } + + const nitroBundle = await GenerateFurnitureBundle(habboAssetSWF); + + await saveFile.writeData(await nitroBundle.toBufferAsync()); + + spinner.text = `Converted: ${ className }`; + spinner.render(); } catch (error) { console.log(); console.error(`Error Converting: ${ className } - ${ error.message }`); - - continue; } } } @@ -68,153 +70,6 @@ export class FurnitureConverter implements IConverter spinner.succeed(`Furniture: Finished in ${ Date.now() - now }ms`); } - public async convertInputToNitro(): Promise - { - const now = Date.now(); - const spinner = ora('Preparing Furniture Inputs').start(); - const saveDirectory = await FileUtilities.getDirectory('./assets/bundled/furniture'); - const inputDirectory = await FileUtilities.getDirectory('./assets/bundle/furniture'); - const files = await inputDirectory.getFileList(); - - for await (const name of files) - { - try - { - const [ className, extension, ...rest ] = name.split('.'); - const file = new File(`${ inputDirectory.path}/${ name }`); - - if(await file.isDirectory()) - { - spinner.text = `Bundling: ${ className }`; - spinner.render(); - - const nitroBundle = new NitroBundle(); - const childFiles = await file.getFileList(); - - if(childFiles && childFiles.length) - { - for await (const childName of childFiles) - { - const childFile = new File(`${ file.path }/${ childName }`); - - nitroBundle.addFile(childName, await childFile.getContentsAsBuffer()); - } - } - - if(nitroBundle.totalFiles) - { - const path = new File(`${ saveDirectory.path }/${ className }.nitro`); - - await path.writeData(await nitroBundle.toBufferAsync()); - } - else - { - console.log(); - console.error(`Error Bundling: ${ name } - The bundle was empty`); - - continue; - } - - spinner.text = 'Bundled: ' + name; - spinner.render(); - } - - else if(extension === 'swf') - { - spinner.text = `Converting: ${ className }`; - spinner.render(); - - const downloadUrl = `${ inputDirectory.path }/${ name }`; - const result = await this.convertSwfToNitro(className, file.path, saveDirectory, true); - - switch(result) - { - case ConverterResult.OK: - spinner.text = 'Converted: ' + className; - spinner.render(); - break; - case ConverterResult.INVALID_SWF: - console.log(); - console.error(`Invalid SWF: ${ downloadUrl }`); - break; - } - } - } - - catch (error) - { - console.log(); - console.error(`Error Converting: ${ name } - ${ error.message }`); - } - } - - spinner.succeed(`Furniture Inputs: Finished in ${ Date.now() - now }ms`); - } - - public async extractNitroToFolder(): Promise - { - const now = Date.now(); - const spinner = ora('Preparing Furniture Extraction').start(); - const saveBaseDirectory = await FileUtilities.getDirectory('./assets/extracted/furniture'); - const inputDirectory = await FileUtilities.getDirectory('./assets/extract/furniture'); - const files = await inputDirectory.getFileList(); - - for await (const name of files) - { - try - { - const [ className, extension, ...rest ] = name.split('.'); - const saveDirectory = await FileUtilities.getDirectory(`${ saveBaseDirectory.path }/${ className }`); - const file = new File(`${ inputDirectory.path }/${ name }`); - - spinner.text = `Extracting: ${ className }`; - spinner.render(); - - if(extension === 'nitro') - { - const nitroBundle = NitroBundle.from((await file.getContentsAsBuffer()).buffer); - - for await (const [ bundleName, bundleBuffer ] of nitroBundle.files.entries()) - { - const saveFile = new File(`${ saveDirectory.path }/${ bundleName }`); - - await saveFile.writeData(bundleBuffer); - } - } - } - - catch (error) - { - console.log(); - console.error(`Error Extracting: ${ name } - ${ error.message }`); - } - } - - spinner.succeed(`Furniture Extraction: Finished in ${ Date.now() - now }ms`); - } - - private async convertSwfToNitro(className: string, url: string, parentFile: File, overwrite: boolean = false): Promise - { - if(!className || !url || !parentFile) return ConverterResult.BAD_ARGS; - - const file = new File(`${ parentFile.path }/${ className }.nitro`); - - if(!overwrite) - { - if(file.exists()) return ConverterResult.FILE_EXISTS; - } - - const habboAssetSWF = await SWFDownloader.downloadFromUrl(url); - - if(!habboAssetSWF) return ConverterResult.INVALID_SWF; - - const nitroBundle = await GenerateFurnitureBundle(habboAssetSWF); - - await file.writeData(await nitroBundle.toBufferAsync()); - - return ConverterResult.OK; - } - public get floorOnly(): boolean { return (this._configuration.getBoolean('convert.furniture.floor.only') || false); diff --git a/src/converters/PetConverter.ts b/src/converters/PetConverter.ts index 763c6a5..76077cc 100644 --- a/src/converters/PetConverter.ts +++ b/src/converters/PetConverter.ts @@ -1,4 +1,3 @@ -import { writeFile } from 'fs/promises'; import ora from 'ora'; import { singleton } from 'tsyringe'; import { Configuration, File, FileUtilities, IConverter } from '../common'; @@ -17,9 +16,9 @@ export class PetConverter implements IConverter const now = Date.now(); const spinner = ora('Preparing Pets').start(); - const baseUrl = this._configuration.getValue('dynamic.download.pet.url'); + const downloadBase = this._configuration.getValue('dynamic.download.pet.url'); + const saveDirectory = await FileUtilities.getDirectory('./assets/bundled/pet'); const classNames = this.getPetTypes(); - const directory = await FileUtilities.getDirectory('./assets/bundled/pet'); if(classNames) { @@ -31,14 +30,15 @@ export class PetConverter implements IConverter try { - const path = new File(directory.path + '/' + className + '.nitro'); + const saveFile = new File(`${ saveDirectory.path }/${ className }.nitro`); - if(path.exists()) continue; + if(saveFile.exists()) continue; spinner.text = `Converting: ${ className } (${ (i + 1) } / ${ totalClassNames })`; spinner.render(); - const habboAssetSWF = await SWFDownloader.download(baseUrl, className, -1); + const downloadUrl = SWFDownloader.getDownloadUrl(downloadBase, className, -1); + const habboAssetSWF = await SWFDownloader.downloadFromUrl(downloadUrl); if(!habboAssetSWF) { @@ -50,9 +50,9 @@ export class PetConverter implements IConverter const nitroBundle = await GeneratePetBundle(habboAssetSWF); - await writeFile(path.path, await nitroBundle.toBufferAsync()); + await saveFile.writeData(await nitroBundle.toBufferAsync()); - spinner.text = 'Converted: ' + className; + spinner.text = `Converted: ${ className }`; spinner.render(); }