Update error logging

This commit is contained in:
Bill 2021-02-17 19:10:33 -05:00
parent 224d7ac736
commit adbcec6e80
8 changed files with 236 additions and 395 deletions

View File

@ -42,6 +42,4 @@ import { Mapper } from './mapping/mappers/asset/Mapper';
// const effectConverter = container.resolve(EffectConverter);
// await effectConverter.convertAsync();
// }
console.log('finished!');
})();

View File

@ -1,10 +1,29 @@
import { wrap } from 'bytebuffer';
import { writeFile } from 'fs/promises';
import { parseStringPromise } from 'xml2js';
import { IAssetData } from '../../mapping/json';
import { HabboAssetSWF } from '../../swf/HabboAssetSWF';
import { DefineBinaryDataTag } from '../../swf/tags/DefineBinaryDataTag';
import { NitroBundle } from '../../utils/NitroBundle';
import { SpriteBundle } from '../bundle/SpriteBundle';
export class SWFConverter
{
protected async fromHabboAsset(habboAssetSWF: HabboAssetSWF, outputFolder: string, type: string, assetData: IAssetData, spriteBundle: SpriteBundle): Promise<void>
{
if(spriteBundle && (spriteBundle.spritesheet !== undefined)) assetData.spritesheet = spriteBundle.spritesheet;
const name = habboAssetSWF.getDocumentClass();
const path = outputFolder + '/' + name + '.nitro';
const nitroBundle = new NitroBundle();
nitroBundle.addFile((name + '.json'), Buffer.from(JSON.stringify(assetData)));
if(spriteBundle && (spriteBundle.imageData !== undefined)) nitroBundle.addFile(spriteBundle.imageData.name, spriteBundle.imageData.buffer);
await writeFile(path, await nitroBundle.toBufferAsync());
}
private static getBinaryData(habboAssetSWF: HabboAssetSWF, type: string, documentNameTwice: boolean): DefineBinaryDataTag
{
let binaryName = habboAssetSWF.getFullClassName(type, documentNameTwice);
@ -19,51 +38,6 @@ export class SWFConverter
return tag;
}
protected static getPalette(habboAssetSWF: HabboAssetSWF, paletteName: string): [ number, number, number ][]
{
const binaryData = SWFConverter.getBinaryData(habboAssetSWF, paletteName, false);
if(!binaryData || !binaryData.binaryDataBuffer) return null;
const byteBuffer = wrap(binaryData.binaryDataBuffer);
const paletteColors: [ number, number, number ][] = [];
try
{
let R = 0;
let G = 0;
let B = 0;
let counter = 1;
while((binaryData.binaryDataBuffer.length - byteBuffer.offset) > 0)
{
if(counter == 1) R = byteBuffer.readUint8();
else if(counter == 2) G = byteBuffer.readUint8();
else if(counter == 3)
{
B = byteBuffer.readUint8();
paletteColors.push([ R, G, B ]);
counter = 0;
}
counter++;
}
return paletteColors;
}
catch (err)
{
console.log(err);
}
return null;
}
protected static async getAssetsXML(habboAssetSWF: HabboAssetSWF): Promise<any>
{
const binaryData = SWFConverter.getBinaryData(habboAssetSWF, 'assets', true);
@ -99,4 +73,40 @@ export class SWFConverter
return await parseStringPromise(binaryData.binaryData);
}
protected static getPalette(habboAssetSWF: HabboAssetSWF, paletteName: string): [ number, number, number ][]
{
const binaryData = SWFConverter.getBinaryData(habboAssetSWF, paletteName, false);
if(!binaryData || !binaryData.binaryDataBuffer) return null;
const byteBuffer = wrap(binaryData.binaryDataBuffer);
const paletteColors: [ number, number, number ][] = [];
let R = 0;
let G = 0;
let B = 0;
let counter = 1;
while((binaryData.binaryDataBuffer.length - byteBuffer.offset) > 0)
{
if(counter == 1) R = byteBuffer.readUint8();
else if(counter == 2) G = byteBuffer.readUint8();
else if(counter == 3)
{
B = byteBuffer.readUint8();
paletteColors.push([ R, G, B ]);
counter = 0;
}
counter++;
}
return paletteColors;
}
}

View File

@ -1,8 +1,6 @@
import { writeFile } from 'fs/promises';
import * as ora from 'ora';
import { singleton } from 'tsyringe';
import { BundleProvider } from '../../common/bundle/BundleProvider';
import { SpriteBundle } from '../../common/bundle/SpriteBundle';
import { Configuration } from '../../common/config/Configuration';
import { SWFConverter } from '../../common/converters/SWFConverter';
import { IAssetData } from '../../mapping/json';
@ -10,7 +8,6 @@ import { AssetMapper, IndexMapper, LogicMapper, VisualizationMapper } from '../.
import { HabboAssetSWF } from '../../swf/HabboAssetSWF';
import File from '../../utils/File';
import Logger from '../../utils/Logger';
import { NitroBundle } from '../../utils/NitroBundle';
import { FurnitureDownloader } from './FurnitureDownloader';
@singleton()
@ -33,63 +30,29 @@ export class FurnitureConverter extends SWFConverter
const outputFolder = new File(this._configuration.getValue('output.folder.furniture'));
if(!outputFolder.isDirectory())
if(!outputFolder.isDirectory()) outputFolder.mkdirs();
try
{
spinner.text = `Creating Folder: ${ outputFolder.path }`;
spinner.render();
outputFolder.mkdirs();
}
await this._furniDownloader.download(async (habboAssetSwf: HabboAssetSWF) =>
{
spinner.text = 'Parsing Furniture: ' + habboAssetSwf.getDocumentClass();
spinner.render();
try
{
const assetData = await this.mapXML2JSON(habboAssetSwf, 'furniture');
const spriteBundle = await this._bundleProvider.generateSpriteSheet(habboAssetSwf);
await this.fromHabboAsset(habboAssetSwf, outputFolder.path, 'furniture', spriteBundle);
await this.fromHabboAsset(habboAssetSwf, outputFolder.path, 'furniture', assetData, spriteBundle);
});
spinner.succeed(`Furniture finished in ${ Date.now() - now }ms`);
}
catch (error)
{
console.log();
console.error(error);
spinner.fail('Furniture failed: ' + error.message);
}
});
spinner.succeed(`Furniture Finished in ${ Date.now() - now }ms`);
}
private async fromHabboAsset(habboAssetSWF: HabboAssetSWF, outputFolder: string, type: string, spriteBundle: SpriteBundle): Promise<void>
{
const assetData = await this.mapXML2JSON(habboAssetSWF, 'furniture');
if(!assetData) return;
const name = habboAssetSWF.getDocumentClass();
const path = outputFolder + '/' + name + '.nitro';
const nitroBundle = new NitroBundle();
nitroBundle.addFile((name + '.json'), Buffer.from(JSON.stringify(assetData)));
if(spriteBundle && (spriteBundle.spritesheet !== undefined))
{
if(spriteBundle.spritesheet && spriteBundle.imageData)
{
assetData.spritesheet = spriteBundle.spritesheet;
nitroBundle.addFile(spriteBundle.imageData.name, spriteBundle.imageData.buffer);
}
}
const buffer = await nitroBundle.toBufferAsync();
await writeFile(path, buffer);
}
private async mapXML2JSON(habboAssetSWF: HabboAssetSWF, assetType: string): Promise<IAssetData>
@ -100,8 +63,6 @@ export class FurnitureConverter extends SWFConverter
assetData.type = assetType;
try
{
const indexXML = await FurnitureConverter.getIndexXML(habboAssetSWF);
if(indexXML) IndexMapper.mapXML(indexXML, assetData);
@ -120,11 +81,4 @@ export class FurnitureConverter extends SWFConverter
return assetData;
}
catch (error)
{
console.log();
console.error(error);
}
}
}

View File

@ -2,29 +2,22 @@ import { singleton } from 'tsyringe';
import { Configuration } from '../../common/config/Configuration';
import { IFurnitureData } from '../../mapping/json';
import { HabboAssetSWF } from '../../swf/HabboAssetSWF';
import File from '../../utils/File';
import { FileUtilities } from '../../utils/FileUtilities';
import Logger from '../../utils/Logger';
@singleton()
export class FurnitureDownloader
{
constructor(
private readonly _configuration: Configuration,
private readonly _logger: Logger)
constructor(private readonly _configuration: Configuration)
{}
public async download(callback: (habboAssetSwf: HabboAssetSWF, className: string) => Promise<void>): Promise<void>
{
try
{
const furniData = await this.parseFurniData();
if(!furniData) throw new Error('invalid_furnidata');
const classNames: string[] = [];
const outputFolder = new File(this._configuration.getValue('output.folder.furniture'));
if(!outputFolder.isDirectory()) outputFolder.mkdirs();
if(furniData.roomitemtypes !== undefined)
{
if(furniData.roomitemtypes.furnitype !== undefined)
@ -46,7 +39,7 @@ export class FurnitureDownloader
catch (error)
{
console.log();
console.error(error);
console.error(error.message);
}
}
}
@ -73,28 +66,19 @@ export class FurnitureDownloader
catch (error)
{
console.log();
console.error(error);
console.error(`Error parsing ${ className }: ` + error.message);
}
}
}
}
}
catch (error)
{
console.log();
console.error(error);
}
}
public async parseFurniData(): Promise<IFurnitureData>
{
const url = this._configuration.getValue('furnidata.url');
if(!url || !url.length) return null;
try
{
const content = await FileUtilities.readFileAsString(url);
if(!content || !content.length) return null;
@ -102,13 +86,6 @@ export class FurnitureDownloader
return (JSON.parse(content) as IFurnitureData);
}
catch (error)
{
console.log();
console.error(error);
}
}
public async extractFurniture(revision: number, className: string, callback: (habboAssetSwf: HabboAssetSWF, className: string) => Promise<void>): Promise<void>
{
let url = this._configuration.getValue('dynamic.download.url.furniture');
@ -118,8 +95,6 @@ export class FurnitureDownloader
url = url.replace('%revision%', revision.toString());
url = url.replace('%className%', className);
try
{
const buffer = await FileUtilities.readFileAsBuffer(url);
if(!buffer) return;
@ -130,11 +105,4 @@ export class FurnitureDownloader
await callback(newHabboAssetSWF, className);
}
catch (error)
{
console.log();
console.error(error);
}
}
}

View File

@ -1,8 +1,6 @@
import { writeFile } from 'fs/promises';
import * as ora from 'ora';
import { singleton } from 'tsyringe';
import { BundleProvider } from '../../common/bundle/BundleProvider';
import { SpriteBundle } from '../../common/bundle/SpriteBundle';
import { Configuration } from '../../common/config/Configuration';
import { SWFConverter } from '../../common/converters/SWFConverter';
import { IAssetData } from '../../mapping/json';
@ -10,7 +8,6 @@ import { AssetMapper, IndexMapper, LogicMapper, VisualizationMapper } from '../.
import { HabboAssetSWF } from '../../swf/HabboAssetSWF';
import File from '../../utils/File';
import Logger from '../../utils/Logger';
import { NitroBundle } from '../../utils/NitroBundle';
import { PetDownloader } from './PetDownloader';
@singleton()
@ -18,7 +15,7 @@ export class PetConverter extends SWFConverter
{
constructor(
private readonly _petDownloader: PetDownloader,
private readonly _config: Configuration,
private readonly _configuration: Configuration,
private readonly _bundleProvider: BundleProvider,
private readonly _logger: Logger)
{
@ -31,69 +28,30 @@ export class PetConverter extends SWFConverter
const spinner = ora('Preparing Pets').start();
const outputFolder = new File(this._config.getValue('output.folder.pet'));
const outputFolder = new File(this._configuration.getValue('output.folder.pet'));
if(!outputFolder.isDirectory())
if(!outputFolder.isDirectory()) outputFolder.mkdirs();
try
{
spinner.text = `Creating Folder: ${ outputFolder.path }`;
spinner.render();
outputFolder.mkdirs();
}
await this._petDownloader.download(async (habboAssetSwf: HabboAssetSWF) =>
{
spinner.text = 'Parsing Pet: ' + habboAssetSwf.getDocumentClass();
spinner.render();
try
{
const assetData = await this.mapXML2JSON(habboAssetSwf, 'pet');
const spriteBundle = await this._bundleProvider.generateSpriteSheet(habboAssetSwf);
await this.fromHabboAsset(habboAssetSwf, outputFolder.path, 'pet', spriteBundle);
await this.fromHabboAsset(habboAssetSwf, outputFolder.path, 'pet', assetData, spriteBundle);
});
spinner.succeed(`Pets finished in ${ Date.now() - now }ms`);
}
catch (error)
{
console.log();
console.error(error);
}
});
spinner.succeed(`Pets Finished in ${ Date.now() - now }ms`);
}
private async fromHabboAsset(habboAssetSWF: HabboAssetSWF, outputFolder: string, type: string, spriteBundle: SpriteBundle): Promise<void>
{
try
{
const assetData = await this.mapXML2JSON(habboAssetSWF, 'pet');
if(!assetData) return;
if(spriteBundle && (spriteBundle.spritesheet !== undefined)) assetData.spritesheet = spriteBundle.spritesheet;
const name = habboAssetSWF.getDocumentClass();
const path = outputFolder + '/' + name + '.nitro';
const nitroBundle = new NitroBundle();
nitroBundle.addFile((name + '.json'), Buffer.from(JSON.stringify(assetData)));
if(spriteBundle.imageData !== undefined)
{
nitroBundle.addFile(spriteBundle.imageData.name, spriteBundle.imageData.buffer);
}
const buffer = await nitroBundle.toBufferAsync();
await writeFile(path, buffer);
}
catch (err)
{
this._logger.logErrorAsync(err);
spinner.fail('Pet failed: ' + error.message);
}
}
@ -105,8 +63,6 @@ export class PetConverter extends SWFConverter
assetData.type = assetType;
try
{
const indexXML = await PetConverter.getIndexXML(habboAssetSWF);
if(indexXML) IndexMapper.mapXML(indexXML, assetData);
@ -151,11 +107,4 @@ export class PetConverter extends SWFConverter
return assetData;
}
catch (error)
{
console.log();
console.error(error);
}
}
}

View File

@ -12,11 +12,12 @@ export class PetDownloader
private readonly _logger: Logger)
{}
public async download(callback: (habboAssetSwf: HabboAssetSWF, className: string) => Promise<void>): Promise<void>
{
try
public async download(callback: (habboAssetSwf: HabboAssetSWF) => Promise<void>): Promise<void>
{
const petTypes = await this.parsePetTypes();
if(!petTypes) throw new Error('invalid_pets');
const classNames: string[] = [];
for(const petType of petTypes)
@ -38,16 +39,7 @@ export class PetDownloader
}
}
catch (error)
{
console.log();
console.error(error);
}
}
public async parsePetTypes(): Promise<string[]>
{
try
{
await this._config.loadExternalVariables();
@ -65,14 +57,7 @@ export class PetDownloader
return petTypes;
}
catch (error)
{
console.log();
console.error(error);
}
}
public async extractPet(className: string, callback: (habboAssetSwf: HabboAssetSWF, className: string) => Promise<void>): Promise<void>
public async extractPet(className: string, callback: (habboAssetSwf: HabboAssetSWF) => Promise<void>): Promise<void>
{
let url = this._config.getValue('dynamic.download.url.pet');
@ -80,19 +65,14 @@ export class PetDownloader
url = url.replace('%className%', className);
try
{
const buffer = await FileUtilities.readFileAsBuffer(url);
if(!buffer) return;
const newHabboAssetSWF = new HabboAssetSWF(buffer);
await newHabboAssetSWF.setupAsync();
await callback(newHabboAssetSWF, className);
}
catch (error)
{
console.log();
console.error(error);
}
await callback(newHabboAssetSWF);
}
}

View File

@ -12,8 +12,6 @@ export class FileUtilities
let content: Buffer = null;
try
{
if(url.startsWith('http'))
{
const data = await fetch.default(url);
@ -29,21 +27,12 @@ export class FileUtilities
return content;
}
catch (error)
{
console.log();
console.error(error);
}
}
public static async readFileAsString(url: string): Promise<string>
{
if(!url) return null;
let content = null;
try
{
if(url.startsWith('http'))
{
const data = await fetch.default(url);
@ -59,11 +48,4 @@ export class FileUtilities
return content;
}
catch (error)
{
console.log();
console.error(error);
}
}
}