nitro-converter/src/common/converters/SWFConverter.ts

135 lines
4.5 KiB
TypeScript
Raw Normal View History

2021-02-17 06:14:07 +01:00
import { wrap } from 'bytebuffer';
2021-02-18 01:10:33 +01:00
import { writeFile } from 'fs/promises';
2021-02-17 06:14:07 +01:00
import { parseStringPromise } from 'xml2js';
2021-02-18 01:10:33 +01:00
import { IAssetData } from '../../mapping/json';
2021-02-17 06:14:07 +01:00
import { HabboAssetSWF } from '../../swf/HabboAssetSWF';
import { DefineBinaryDataTag } from '../../swf/tags/DefineBinaryDataTag';
2021-02-18 01:10:33 +01:00
import { NitroBundle } from '../../utils/NitroBundle';
import { SpriteBundle } from '../bundle/SpriteBundle';
2021-02-21 06:27:46 +01:00
import { Converter } from './Converter';
2021-02-17 06:14:07 +01:00
2021-02-21 06:27:46 +01:00
export class SWFConverter extends Converter
2021-02-17 06:14:07 +01:00
{
2021-02-18 01:10:33 +01:00
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());
}
2021-02-17 06:14:07 +01:00
private static getBinaryData(habboAssetSWF: HabboAssetSWF, type: string, documentNameTwice: boolean): DefineBinaryDataTag
{
let binaryName = habboAssetSWF.getFullClassName(type, documentNameTwice);
let tag = habboAssetSWF.getBinaryTagByName(binaryName);
if(!tag)
{
binaryName = habboAssetSWF.getFullClassNameSnake(type, documentNameTwice, true);
tag = habboAssetSWF.getBinaryTagByName(binaryName);
}
return tag;
}
2021-02-18 07:03:08 +01:00
protected static async getManifestXML(habboAssetSWF: HabboAssetSWF): Promise<any>
2021-02-17 06:14:07 +01:00
{
2021-02-18 07:03:08 +01:00
const binaryData = SWFConverter.getBinaryData(habboAssetSWF, 'manifest', false);
2021-02-17 06:14:07 +01:00
if(!binaryData) return null;
2021-02-17 06:30:41 +01:00
2021-02-17 06:14:07 +01:00
return await parseStringPromise(binaryData.binaryData);
}
2021-02-18 07:03:08 +01:00
protected static async getIndexXML(habboAssetSWF: HabboAssetSWF): Promise<any>
2021-02-17 06:14:07 +01:00
{
2021-02-18 07:03:08 +01:00
const binaryData = SWFConverter.getBinaryData(habboAssetSWF, 'index', false);
2021-02-17 06:14:07 +01:00
if(!binaryData) return null;
2021-02-17 06:30:41 +01:00
2021-03-11 20:42:33 +01:00
//Will fix some malformed comments exception.
const removedComments: string = binaryData.binaryData.replace(/<!--.*?-->/sg, '');
return await parseStringPromise(removedComments);
2021-02-17 06:14:07 +01:00
}
2021-02-18 07:03:08 +01:00
protected static async getAssetsXML(habboAssetSWF: HabboAssetSWF): Promise<any>
2021-02-17 06:14:07 +01:00
{
2021-02-18 07:03:08 +01:00
const binaryData = SWFConverter.getBinaryData(habboAssetSWF, 'assets', true);
if(!binaryData) return null;
return await parseStringPromise(binaryData.binaryData);
}
protected static async getLogicXML(habboAssetSWF: HabboAssetSWF): Promise<any>
{
const binaryData = SWFConverter.getBinaryData(habboAssetSWF, 'logic', true);
2021-02-17 06:14:07 +01:00
if(!binaryData) return null;
2021-02-17 06:30:41 +01:00
2021-02-17 06:14:07 +01:00
return await parseStringPromise(binaryData.binaryData);
}
protected static async getVisualizationXML(habboAssetSWF: HabboAssetSWF): Promise<any>
{
const binaryData = SWFConverter.getBinaryData(habboAssetSWF, 'visualization', true);
if(!binaryData) return null;
2021-02-17 06:30:41 +01:00
2021-02-20 05:10:06 +01:00
return await parseStringPromise(binaryData.binaryData);
}
protected static async getAnimationXML(habboAssetSWF: HabboAssetSWF): Promise<any>
{
const binaryData = SWFConverter.getBinaryData(habboAssetSWF, 'animation', false);
if(!binaryData) return null;
2021-02-17 06:14:07 +01:00
return await parseStringPromise(binaryData.binaryData);
}
2021-02-18 01:10:33 +01:00
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;
}
2021-02-17 06:14:07 +01:00
}