Skip to content

Commit

Permalink
History: Read change positions at once (#2542)
Browse files Browse the repository at this point in the history
  • Loading branch information
SirYwell committed Oct 26, 2024
1 parent c252850 commit 28bd065
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.fastasyncworldedit.core.history.change;

import com.sk89q.worldedit.history.change.Change;
import org.jetbrains.annotations.ApiStatus;

/**
* Represents a change that is associated with {@code (x, y, z)} block coordinates.
* @since TODO
*/
@ApiStatus.Internal
public sealed abstract class BlockPositionChange implements Change
permits MutableBlockChange, MutableFullBlockChange {
public int x;
public int y;
public int z;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@

import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.history.UndoContext;
import com.sk89q.worldedit.history.change.Change;
import com.sk89q.worldedit.world.block.BlockState;
import org.jetbrains.annotations.ApiStatus;

public class MutableBlockChange implements Change {

public int z;
public int y;
public int x;
@ApiStatus.Internal
public final class MutableBlockChange extends BlockPositionChange {
public int ordinal;


public MutableBlockChange(int x, int y, int z, int ordinal) {
this.x = x;
this.y = y;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
import com.sk89q.worldedit.extent.inventory.BlockBag;
import com.sk89q.worldedit.extent.inventory.BlockBagException;
import com.sk89q.worldedit.history.UndoContext;
import com.sk89q.worldedit.history.change.Change;
import com.sk89q.worldedit.world.block.BlockState;
import com.sk89q.worldedit.world.block.BlockTypesCache;
import org.jetbrains.annotations.ApiStatus;

public class MutableFullBlockChange implements Change {

public int z;
public int y;
public int x;
@ApiStatus.Internal
public final class MutableFullBlockChange extends BlockPositionChange {
public int from;
public int to;
public BlockBag blockBag;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.fastasyncworldedit.core.history.change.MutableEntityChange;
import com.fastasyncworldedit.core.history.change.MutableFullBlockChange;
import com.fastasyncworldedit.core.history.change.MutableTileChange;
import com.fastasyncworldedit.core.history.change.BlockPositionChange;
import com.fastasyncworldedit.core.internal.exception.FaweSmallEditUnsupportedException;
import com.fastasyncworldedit.core.internal.io.FaweInputStream;
import com.fastasyncworldedit.core.internal.io.FaweOutputStream;
Expand All @@ -22,6 +23,7 @@
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.biome.BiomeType;
import com.sk89q.worldedit.world.block.BlockTypes;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -30,7 +32,6 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
Expand All @@ -42,6 +43,7 @@
/**
* FAWE stream ChangeSet offering support for extended-height worlds
*/
@ApiStatus.Internal
public abstract class FaweStreamChangeSet extends AbstractChangeSet {

public static final int HEADER_SIZE = 9;
Expand Down Expand Up @@ -85,19 +87,15 @@ private void init(boolean storeRedo, boolean smallLoc) {
}
}

public interface FaweStreamPositionDelegate {
interface FaweStreamPositionDelegate {

void write(OutputStream out, int x, int y, int z) throws IOException;

int readX(FaweInputStream in) throws IOException;

int readY(FaweInputStream in) throws IOException;

int readZ(FaweInputStream in) throws IOException;
void read(FaweInputStream in, BlockPositionChange change) throws IOException;

}

public interface FaweStreamIdDelegate {
interface FaweStreamIdDelegate {

void writeChange(FaweOutputStream out, int from, int to) throws IOException;

Expand Down Expand Up @@ -155,6 +153,7 @@ public void readCombined(FaweInputStream is, MutableFullBlockChange change) thro
}
if (mode == 1 || mode == 4) { // small
posDel = new FaweStreamPositionDelegate() {
final byte[] buffer = new byte[4];
int lx;
int ly;
int lz;
Expand All @@ -179,23 +178,14 @@ public void write(OutputStream out, int x, int y, int z) throws IOException {
out.write(b4);
}

final byte[] buffer = new byte[4];

@Override
public int readX(FaweInputStream in) throws IOException {
public void read(final FaweInputStream in, final BlockPositionChange change) throws IOException {
in.readFully(buffer);
return lx = lx + ((((buffer[1] & 0xFF) | ((MathMan.unpair16x(buffer[3])) << 8)) << 20) >> 20);
change.x = lx = lx + ((((buffer[1] & 0xFF) | ((MathMan.unpair16x(buffer[3])) << 8)) << 20) >> 20);
change.y = (ly = ly + buffer[0]) & 0xFF;
change.z = lz = lz + ((((buffer[2] & 0xFF) | ((MathMan.unpair16y(buffer[3])) << 8)) << 20) >> 20);
}

@Override
public int readY(FaweInputStream in) {
return (ly = ly + buffer[0]) & 0xFF;
}

@Override
public int readZ(FaweInputStream in) throws IOException {
return lz = lz + ((((buffer[2] & 0xFF) | ((MathMan.unpair16y(buffer[3])) << 8)) << 20) >> 20);
}
};
} else {
posDel = new FaweStreamPositionDelegate() {
Expand Down Expand Up @@ -232,25 +222,11 @@ public void write(OutputStream stream, int x, int y, int z) throws IOException {
}

@Override
public int readX(FaweInputStream is) throws IOException {
is.readFully(buffer);
// Don't break reading version 1 history (just in case)
if (version == 2 && Arrays.equals(buffer, MAGIC_NEW_RELATIVE)) {
lx = ((is.read() << 24) + (is.read() << 16) + (is.read() << 8) + is.read());
lz = ((is.read() << 24) + (is.read() << 16) + (is.read() << 8) + is.read());
is.readFully(buffer);
}
return lx = lx + ((buffer[0] & 0xFF) | (buffer[1] << 8));
}

@Override
public int readY(FaweInputStream is) throws IOException {
return ly = ly + ((buffer[4] & 0xFF) | (buffer[5]) << 8);
}

@Override
public int readZ(FaweInputStream is) throws IOException {
return lz = lz + ((buffer[2] & 0xFF) | (buffer[3]) << 8);
public void read(final FaweInputStream in, final BlockPositionChange change) throws IOException {
in.readFully(buffer);
change.x = lx = lx + ((buffer[0] & 0xFF) | (buffer[1] << 8));
change.z = lz = lz + ((buffer[2] & 0xFF) | (buffer[3]) << 8);
change.y = ly = ly + ((buffer[4] & 0xFF) | (buffer[5]) << 8);
}
};
}
Expand Down Expand Up @@ -453,9 +429,9 @@ public Iterator<MutableBlockChange> getBlockIterator(final boolean dir) throws I

public MutableBlockChange read() {
try {
change.x = posDel.readX(is) + originX;
change.y = posDel.readY(is);
change.z = posDel.readZ(is) + originZ;
posDel.read(is, change);
change.x += originX;
change.z += originZ;
idDel.readCombined(is, change, dir);
return change;
} catch (EOFException ignored) {
Expand Down Expand Up @@ -570,9 +546,9 @@ public Iterator<MutableFullBlockChange> getFullBlockIterator(BlockBag blockBag,

public MutableFullBlockChange read() {
try {
change.x = posDel.readX(is) + originX;
change.y = posDel.readY(is);
change.z = posDel.readZ(is) + originZ;
posDel.read(is, change);
change.x += originX;
change.z += originZ;
idDel.readCombined(is, change);
return change;
} catch (EOFException ignored) {
Expand Down Expand Up @@ -872,10 +848,10 @@ class Populator implements ChangePopulator<MutableFullBlockChange> {
@Override
public @Nullable MutableFullBlockChange populate(@NotNull final MutableFullBlockChange change) {
try {
change.x = posDel.readX(is) + originX;
change.y = posDel.readY(is);
change.z = posDel.readZ(is) + originZ;
posDel.read(is, change);
idDel.readCombined(is, change);
change.x += originX;
change.z += originZ;
return change;
} catch (EOFException ignored) {
} catch (Exception e) {
Expand Down Expand Up @@ -914,10 +890,10 @@ class Populator implements ChangePopulator<MutableBlockChange> {
@Override
public @Nullable MutableBlockChange populate(@NotNull final MutableBlockChange change) {
try {
change.x = posDel.readX(is) + originX;
change.y = posDel.readY(is);
change.z = posDel.readZ(is) + originZ;
posDel.read(is, change);
idDel.readCombined(is, change, dir);
change.x += originX;
change.z += originZ;
return change;
} catch (EOFException ignored) {
} catch (Exception e) {
Expand Down Expand Up @@ -1068,11 +1044,9 @@ public SimpleChangeSetSummary summarize(Region region, boolean shallow) {
int amount = (Settings.settings().HISTORY.BUFFER_SIZE - HEADER_SIZE) / 9;
MutableFullBlockChange change = new MutableFullBlockChange(null, 0, false);
for (int i = 0; i < amount; i++) {
int x = posDel.readX(fis) + ox;
int y = posDel.readY(fis);
int z = posDel.readZ(fis) + ox;
posDel.read(fis, change);
idDel.readCombined(fis, change);
summary.add(x, z, change.to);
summary.add(change.x + ox, change.z + oz, change.to);
}
}
} catch (EOFException ignored) {
Expand Down

0 comments on commit 28bd065

Please sign in to comment.