Compare commits
2 Commits
0f1fbc4b88
...
Schematics
| Author | SHA1 | Date | |
|---|---|---|---|
| 0b689c90eb | |||
| da4aa0aef5 |
@ -0,0 +1,333 @@
|
|||||||
|
/*
|
||||||
|
* This file is a part of the SteamWar software.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2025 SteamWar.de-Serverteam
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package de.steamwar.core;
|
||||||
|
|
||||||
|
import com.sk89q.worldedit.WorldEdit;
|
||||||
|
import com.sk89q.worldedit.extension.factory.BlockFactory;
|
||||||
|
import com.sk89q.worldedit.extension.input.ParserContext;
|
||||||
|
import com.sk89q.worldedit.extension.platform.Capability;
|
||||||
|
import com.sk89q.worldedit.extension.platform.Platform;
|
||||||
|
import com.sk89q.worldedit.extent.clipboard.BlockArrayClipboard;
|
||||||
|
import com.sk89q.worldedit.extent.clipboard.Clipboard;
|
||||||
|
import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader;
|
||||||
|
import com.sk89q.worldedit.math.BlockVector3;
|
||||||
|
import com.sk89q.worldedit.regions.CuboidRegion;
|
||||||
|
import com.sk89q.worldedit.regions.Region;
|
||||||
|
import com.sk89q.worldedit.world.DataFixer;
|
||||||
|
import com.sk89q.worldedit.world.block.BlockState;
|
||||||
|
import org.enginehub.linbus.common.LinTagId;
|
||||||
|
import org.enginehub.linbus.tree.*;
|
||||||
|
|
||||||
|
import java.io.DataInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class ChaosSchemReader implements ClipboardReader {
|
||||||
|
|
||||||
|
private final DataInputStream stream;
|
||||||
|
|
||||||
|
public ChaosSchemReader(InputStream stream) {
|
||||||
|
if (stream instanceof DataInputStream dis) {
|
||||||
|
this.stream = dis;
|
||||||
|
} else {
|
||||||
|
this.stream = new DataInputStream(stream);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Clipboard read() throws IOException {
|
||||||
|
byte tag = stream.readByte();
|
||||||
|
|
||||||
|
if (tag != 0x0A) error("Invalid NBT");
|
||||||
|
|
||||||
|
String name = stream.readUTF();
|
||||||
|
|
||||||
|
if (name.isEmpty()) {
|
||||||
|
tag = stream.readByte();
|
||||||
|
if (tag != 0x0A) error("Invalid NBT");
|
||||||
|
|
||||||
|
stream.readUTF();
|
||||||
|
|
||||||
|
readSchemTag();
|
||||||
|
|
||||||
|
stream.readByte();
|
||||||
|
} else {
|
||||||
|
readSchemTag();
|
||||||
|
}
|
||||||
|
|
||||||
|
return constructSchematic();
|
||||||
|
}
|
||||||
|
|
||||||
|
private int version;
|
||||||
|
private int dataVersion;
|
||||||
|
private DataFixer fixer;
|
||||||
|
|
||||||
|
private int width;
|
||||||
|
private int height;
|
||||||
|
private int length;
|
||||||
|
|
||||||
|
private int[] offset = new int[3];
|
||||||
|
|
||||||
|
private Map<Integer, BlockState> palette = new HashMap<>();
|
||||||
|
private int[] blocks;
|
||||||
|
|
||||||
|
private LinCompoundTag metadata;
|
||||||
|
|
||||||
|
private Map<BlockVector3, LinCompoundTag> tileEntityMap = new HashMap<>();
|
||||||
|
|
||||||
|
private Clipboard constructSchematic() {
|
||||||
|
BlockVector3 min = BlockVector3.at(offset[0], offset[1], offset[2]);
|
||||||
|
|
||||||
|
Region region = new CuboidRegion(min, min.add(width, height, length).subtract(BlockVector3.ONE));
|
||||||
|
|
||||||
|
BlockArrayClipboard clipboard = new BlockArrayClipboard(region);
|
||||||
|
|
||||||
|
clipboard.setOrigin(min);
|
||||||
|
|
||||||
|
int index = 0;
|
||||||
|
for (int block : blocks) {
|
||||||
|
int y = index / (width * length);
|
||||||
|
int z = (index % (width * length)) / width;
|
||||||
|
int x = (index++ % (width * length)) % width;
|
||||||
|
|
||||||
|
BlockState state = palette.get(block);
|
||||||
|
BlockVector3 pt = BlockVector3.at(x, y, z);
|
||||||
|
|
||||||
|
if (tileEntityMap.containsKey(pt)) {
|
||||||
|
clipboard.setBlock(clipboard.getMinimumPoint().add(pt), state.toBaseBlock(tileEntityMap.get(pt)));
|
||||||
|
} else {
|
||||||
|
clipboard.setBlock(clipboard.getMinimumPoint().add(pt), state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return clipboard;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void readSchemTag() throws IOException {
|
||||||
|
parseCompoundTag((type, name) -> {
|
||||||
|
switch (name) {
|
||||||
|
case "Version" -> version = stream.readInt();
|
||||||
|
case "DataVersion" -> {
|
||||||
|
dataVersion = stream.readInt();
|
||||||
|
|
||||||
|
Platform platform = WorldEdit.getInstance().getPlatformManager()
|
||||||
|
.queryCapability(Capability.WORLD_EDITING);
|
||||||
|
|
||||||
|
if (dataVersion < platform.getDataVersion()) {
|
||||||
|
this.fixer = platform.getDataFixer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case "Metadata" -> metadata = readCompound();
|
||||||
|
case "Width" -> width = stream.readUnsignedShort();
|
||||||
|
case "Height" -> height = stream.readUnsignedShort();
|
||||||
|
case "Length" -> length = stream.readUnsignedShort();
|
||||||
|
case "Offset" -> offset = readIntArray().value();
|
||||||
|
case "PaletteMax" -> stream.skipNBytes(4);
|
||||||
|
case "Palette" -> readPalette();
|
||||||
|
case "BlockData" -> readBlockData();
|
||||||
|
case "Blocks" -> readBlockContainer();
|
||||||
|
case "BlockEntities" -> readTileEntities();
|
||||||
|
|
||||||
|
default -> skipByType(type);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void readBlockContainer() throws IOException {
|
||||||
|
parseCompoundTag((type, name) -> {
|
||||||
|
switch (name) {
|
||||||
|
case "Palette" -> readPalette();
|
||||||
|
case "Data" -> readBlockData();
|
||||||
|
case "BlockEntities" -> readTileEntities();
|
||||||
|
|
||||||
|
default -> skipByType(type);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void readTileEntities() throws IOException {
|
||||||
|
LinListTag<LinCompoundTag> list = (LinListTag<LinCompoundTag>) readArray();
|
||||||
|
|
||||||
|
list.value().forEach(linCompoundTag -> {
|
||||||
|
List<LinIntTag> pos = linCompoundTag.getListTag("Pos", LinTagType.intTag()).value();
|
||||||
|
|
||||||
|
BlockVector3 pt = BlockVector3.at(pos.get(0).value(), pos.get(1).value(), pos.get(2).value());
|
||||||
|
|
||||||
|
if (fixer != null) {
|
||||||
|
linCompoundTag = fixer.fixUp(DataFixer.FixTypes.BLOCK_ENTITY, LinCompoundTag.of(linCompoundTag.value()), dataVersion);
|
||||||
|
}
|
||||||
|
|
||||||
|
tileEntityMap.put(pt, linCompoundTag);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void readPalette() throws IOException {
|
||||||
|
BlockFactory bf = WorldEdit.getInstance().getBlockFactory();
|
||||||
|
|
||||||
|
ParserContext context = new ParserContext();
|
||||||
|
context.setRestricted(true);
|
||||||
|
context.setTryLegacy(false);
|
||||||
|
context.setPreferringWildcard(false);
|
||||||
|
|
||||||
|
parseCompoundTag((aByte, string) -> palette.put(stream.readInt(), bf.parseFromInput(string, context).toImmutableState()));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final int SEGMENT_BITS = 0x7F;
|
||||||
|
private static final int CONTINUE_BIT = 0x80;
|
||||||
|
|
||||||
|
private void readBlockData() throws IOException {
|
||||||
|
byte[] data = readByteArray();
|
||||||
|
|
||||||
|
int[] blocks = new int[data.length];
|
||||||
|
|
||||||
|
int x = 0;
|
||||||
|
int position = 0;
|
||||||
|
|
||||||
|
for (byte current : data) {
|
||||||
|
blocks[x] |= (current & SEGMENT_BITS) << position;
|
||||||
|
|
||||||
|
if ((current & CONTINUE_BIT) == 0) {
|
||||||
|
x++;
|
||||||
|
position = 0;
|
||||||
|
} else {
|
||||||
|
position += 7;
|
||||||
|
|
||||||
|
if (position >= 32) throw new RuntimeException("VarInt is too big");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.blocks = blocks;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void parseCompoundTag(CompoundReader tag) throws IOException {
|
||||||
|
byte type;
|
||||||
|
while ((type = stream.readByte()) != 0x00) {
|
||||||
|
String name = stream.readUTF();
|
||||||
|
|
||||||
|
tag.read(type, name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] readByteArray() throws IOException {
|
||||||
|
return stream.readNBytes(stream.readInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void skipByType(byte type, String str) throws IOException {
|
||||||
|
skipByType(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void skipByType(byte type) throws IOException {
|
||||||
|
switch (type) {
|
||||||
|
case 0x00 -> stream.skipNBytes(0);
|
||||||
|
case 0x01 -> stream.skipNBytes(1);
|
||||||
|
case 0x02 -> stream.skipNBytes(2);
|
||||||
|
case 0x03, 0x05 -> stream.skipNBytes(4);
|
||||||
|
case 0x04, 0x06 -> stream.skipNBytes(8);
|
||||||
|
case 0x07 -> stream.skipNBytes(stream.readInt());
|
||||||
|
case 0x08 -> stream.readUTF();
|
||||||
|
case 0x09 -> {
|
||||||
|
byte t = stream.readByte();
|
||||||
|
int length = stream.readInt();
|
||||||
|
for (int i = 0; i < length; i++) { skipByType(t); }
|
||||||
|
}
|
||||||
|
case 0x0A -> parseCompoundTag(this::skipByType);
|
||||||
|
case 0x0B -> stream.skipNBytes(4L * stream.readInt());
|
||||||
|
case 0x0C -> stream.skipNBytes(8L * stream.readInt());
|
||||||
|
default -> error("Invalid Type");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private LinTag<?> readByType(byte type) throws IOException {
|
||||||
|
return switch (type) {
|
||||||
|
case 0x00 -> null;
|
||||||
|
case 0x01 -> LinByteTag.of(stream.readByte());
|
||||||
|
case 0x02 -> LinShortTag.of(stream.readShort());
|
||||||
|
case 0x03 -> LinIntTag.of(stream.readInt());
|
||||||
|
case 0x04 -> LinLongTag.of(stream.readLong());
|
||||||
|
case 0x05 -> LinFloatTag.of(stream.readFloat());
|
||||||
|
case 0x06 -> LinDoubleTag.of(stream.readDouble());
|
||||||
|
case 0x07 -> LinByteArrayTag.of(stream.readNBytes(stream.readInt()));
|
||||||
|
case 0x08 -> LinStringTag.of(stream.readUTF());
|
||||||
|
case 0x09 -> readArray();
|
||||||
|
case 0x0A -> readCompound();
|
||||||
|
case 0x0B -> readIntArray();
|
||||||
|
case 0x0C -> readLongArray();
|
||||||
|
default -> error("Invalid Type");
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
private LinIntArrayTag readIntArray() throws IOException {
|
||||||
|
int length = stream.readInt();
|
||||||
|
|
||||||
|
int[] ints = new int[length];
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
ints[i] = stream.readInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
return LinIntArrayTag.of(ints);
|
||||||
|
}
|
||||||
|
|
||||||
|
private LinLongArrayTag readLongArray() throws IOException {
|
||||||
|
int length = stream.readInt();
|
||||||
|
|
||||||
|
long[] longs = new long[length];
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
longs[i] = stream.readInt();
|
||||||
|
}
|
||||||
|
|
||||||
|
return LinLongArrayTag.of(longs);
|
||||||
|
}
|
||||||
|
|
||||||
|
private LinListTag<?> readArray() throws IOException {
|
||||||
|
byte t = stream.readByte();
|
||||||
|
int length = stream.readInt();
|
||||||
|
List<LinTag<?>> obj = new ArrayList<>(length);
|
||||||
|
for (int i = 0; i < length; i++) {
|
||||||
|
obj.add(readByType(t));
|
||||||
|
}
|
||||||
|
|
||||||
|
return LinListTag.of(LinTagType.fromId(LinTagId.fromId(t)), obj);
|
||||||
|
}
|
||||||
|
|
||||||
|
private LinCompoundTag readCompound() throws IOException {
|
||||||
|
Map<String, LinTag<?>> entries = new HashMap<>();
|
||||||
|
|
||||||
|
parseCompoundTag((type, name) -> entries.put(name, readByType(type)));
|
||||||
|
|
||||||
|
return LinCompoundTag.of(entries);
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> T error(String message) {
|
||||||
|
throw new IllegalStateException("Could not read Schem: " + message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
stream.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private interface CompoundReader {
|
||||||
|
void read(byte type, String name) throws IOException;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -71,8 +71,7 @@ public class WorldEditWrapper21 implements WorldEditWrapper {
|
|||||||
public Clipboard getClipboard(InputStream is, NodeData.SchematicFormat schemFormat) throws IOException {
|
public Clipboard getClipboard(InputStream is, NodeData.SchematicFormat schemFormat) throws IOException {
|
||||||
return switch (schemFormat) {
|
return switch (schemFormat) {
|
||||||
case MCEDIT -> new MCEditSchematicReader(new NBTInputStream(is)).read();
|
case MCEDIT -> new MCEditSchematicReader(new NBTInputStream(is)).read();
|
||||||
case SPONGE_V2 -> new SpongeSchematicV2Reader(LinBinaryIO.read(new DataInputStream(is))).read();
|
case SPONGE_V2, SPONGE_V3 -> new ChaosSchemReader(is).read();
|
||||||
case SPONGE_V3 -> new SpongeSchematicV3Reader(LinBinaryIO.read(new DataInputStream(is))).read();
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user