diff --git a/src/exporters/exporters.ts b/src/exporters/exporters.ts index 2d47af2..0449af6 100644 --- a/src/exporters/exporters.ts +++ b/src/exporters/exporters.ts @@ -1,11 +1,16 @@ -import { ASSERT } from '../util/error_util'; import { IExporter } from './base_exporter'; import { Litematic } from './litematic_exporter'; import { NBTExporter } from './nbt_exporter'; import { SchemExporter } from './schem_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 { public static GetExporter(voxeliser: TExporters): IExporter { @@ -18,8 +23,8 @@ export class ExporterFactory { return new SchemExporter(); case 'nbt': return new NBTExporter(); - default: - ASSERT(false); + case 'uncompressed_json': + return new UncompressedJSONExporter(); } } } diff --git a/src/exporters/uncompressed_json_exporter.ts b/src/exporters/uncompressed_json_exporter.ts new file mode 100644 index 0000000..e905cba --- /dev/null +++ b/src/exporters/uncompressed_json_exporter.ts @@ -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(); + 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); + } +} diff --git a/src/ui/layout.ts b/src/ui/layout.ts index 820e4a4..5212478 100644 --- a/src/ui/layout.ts +++ b/src/ui/layout.ts @@ -280,6 +280,10 @@ export class UI { displayText: 'Structure blocks (.nbt)', payload: 'nbt', }, + { + displayText: 'Uncompressed JSON (.json)', + payload: 'uncompressed_json', + }, ]) .setLabel('Exporter'), },