Added uncompressed-json exporter

This commit is contained in:
Lucas Dower 2023-03-25 14:27:08 +00:00
parent ffb2b78876
commit 97ea5146b0
No known key found for this signature in database
GPG Key ID: B3EE6B8499593605
3 changed files with 51 additions and 4 deletions

View File

@ -1,11 +1,16 @@
import { ASSERT } from '../util/error_util';
import { IExporter } from './base_exporter'; import { IExporter } from './base_exporter';
import { Litematic } from './litematic_exporter'; import { Litematic } from './litematic_exporter';
import { NBTExporter } from './nbt_exporter'; import { NBTExporter } from './nbt_exporter';
import { SchemExporter } from './schem_exporter'; import { SchemExporter } from './schem_exporter';
import { Schematic } from './schematic_exporter'; import { Schematic } from './schematic_exporter';
import { UncompressedJSONExporter } from './uncompressed_json_exporter';
export type TExporters = 'schematic' | 'litematic' | 'schem' | 'nbt'; export type TExporters =
'schematic' |
'litematic' |
'schem' |
'nbt' |
'uncompressed_json';
export class ExporterFactory { export class ExporterFactory {
public static GetExporter(voxeliser: TExporters): IExporter { public static GetExporter(voxeliser: TExporters): IExporter {
@ -18,8 +23,8 @@ export class ExporterFactory {
return new SchemExporter(); return new SchemExporter();
case 'nbt': case 'nbt':
return new NBTExporter(); return new NBTExporter();
default: case 'uncompressed_json':
ASSERT(false); return new UncompressedJSONExporter();
} }
} }
} }

View File

@ -0,0 +1,38 @@
import { BlockMesh } from '../block_mesh';
import { IExporter } from './base_exporter';
export class UncompressedJSONExporter extends IExporter {
public override getFormatFilter() {
return {
name: 'Uncompressed JSON',
extension: 'json',
};
}
public override export(blockMesh: BlockMesh): Buffer {
const blocks = blockMesh.getBlocks();
const lines = new Array<string>();
lines.push('[');
// Serialise all block except for the last one.
for (let i = 0; i < blocks.length - 1; ++i) {
const block = blocks[i];
const pos = block.voxel.position;
lines.push(`{ "x": ${pos.x}, "y": ${pos.y}, "z": ${pos.z}, "block_name": "${block.blockInfo.name}" },`);
}
// Serialise the last block but don't include the comma at the end.
{
const block = blocks[blocks.length - 1];
const pos = block.voxel.position;
lines.push(`{ "x": ${pos.x}, "y": ${pos.y}, "z": ${pos.z}, "block_name": "${block.blockInfo.name}" }`);
}
lines.push(']');
const json = lines.join('');
return Buffer.from(json);
}
}

View File

@ -280,6 +280,10 @@ export class UI {
displayText: 'Structure blocks (.nbt)', displayText: 'Structure blocks (.nbt)',
payload: 'nbt', payload: 'nbt',
}, },
{
displayText: 'Uncompressed JSON (.json)',
payload: 'uncompressed_json',
},
]) ])
.setLabel('Exporter'), .setLabel('Exporter'),
}, },