Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixed history rollback #2367

Merged
merged 5 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,19 @@ public Iterable<Supplier<RollbackOptimizedHistory>> getEdits(
Future<Integer> future = call(() -> {
try {
int count = 0;
String stmtStr = ascending ? uuid == null ? "SELECT * FROM`" + this.prefix + "edits` WHERE `time`>? AND `x2`>=? AND" +
" `x1`<=? AND `z2`>=? AND `z1`<=? AND `y2`>=? AND `y1`<=? ORDER BY `time` , `id`" :
"SELECT * FROM`" + this.prefix + "edits` WHERE `time`>? AND" +
" `x2`>=? AND `x1`<=? AND `z2`>=? AND `z1`<=? AND `y2`>=? AND `y1`<=? AND `player`=? ORDER BY `time` ASC, `id` ASC" :
uuid == null ? "SELECT * FROM`" + this.prefix + "edits` WHERE `time`>? AND `x2`>=? AND `x1`<=? AND `z2`>=? " +
"AND `z1`<=? AND `y2`>=? AND `y1`<=? ORDER BY `time` DESC, `id` DESC" :
"SELECT * FROM`" + this.prefix + "edits` WHERE `time`>? AND `x2`>=? AND `x1`<=? AND" +
" `z2`>=? AND `z1`<=? AND `y2`>=? AND `y1`<=? AND `player`=? ORDER BY `time` DESC, `id` DESC";
String stmtStr;
if (ascending) {
if (uuid == null) {
stmtStr = "SELECT * FROM`%sedits` WHERE `time`>? AND `x2`>=? AND `x1`<=? AND `z2`>=? AND `z1`<=? AND " +
"`y2`>=? AND `y1`<=? ORDER BY `time` , `id`";
} else {
stmtStr = "SELECT * FROM`%sedits` WHERE `time`>? AND `x2`>=? AND `x1`<=? AND `z2`>=? AND `z1`<=? AND " +
"`y2`>=? AND `y1`<=? AND `player`=? ORDER BY `time` ASC, `id` ASC";
}
} else {
stmtStr = "SELECT * FROM`%sedits` WHERE `time`>? AND `x2`>=? AND `x1`<=? AND `z2`>=? AND `z1`<=? AND " +
"`y2`>=? AND `y1`<=? AND `player`=? ORDER BY `time` DESC, `id` DESC";
}
try (PreparedStatement stmt = connection.prepareStatement(stmtStr)) {
stmt.setInt(1, (int) (minTime / 1000));
stmt.setInt(2, pos1.getBlockX());
Expand All @@ -193,20 +198,20 @@ public Iterable<Supplier<RollbackOptimizedHistory>> getEdits(
if (delete && uuid != null) {
try (PreparedStatement stmt = connection.prepareStatement("DELETE FROM`" + this.prefix +
"edits` WHERE `player`=? AND `time`>? AND `x2`>=? AND `x1`<=? AND `y2`>=? AND `y1`<=? AND `z2`>=? AND `z1`<=?")) {
stmt.setInt(1, (int) (minTime / 1000));
stmt.setInt(2, pos1.getBlockX());
stmt.setInt(3, pos2.getBlockX());
stmt.setInt(4, pos1.getBlockZ());
stmt.setInt(5, pos2.getBlockZ());
// Keep 128 offset for backwards-compatibility
stmt.setInt(6, pos1.getBlockY() - 128);
stmt.setInt(7, pos2.getBlockY() - 128);
byte[] uuidBytes = ByteBuffer
.allocate(16)
.putLong(uuid.getMostSignificantBits())
.putLong(uuid.getLeastSignificantBits())
.array();
stmt.setBytes(8, uuidBytes);
stmt.setBytes(1, uuidBytes);
stmt.setInt(2, (int) (minTime / 1000));
stmt.setInt(3, pos1.getBlockX());
stmt.setInt(4, pos2.getBlockX());
stmt.setInt(5, pos1.getBlockZ());
stmt.setInt(6, pos2.getBlockZ());
// Keep 128 offset for backwards-compatibility
stmt.setInt(7, pos1.getBlockY() - 128);
stmt.setInt(8, pos2.getBlockY() - 128);
}
}
return count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.function.operation.ChangeSetExecutor;
import com.sk89q.worldedit.internal.util.LogManagerCompat;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.world.World;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -35,6 +37,7 @@
*/
public class DiskStorageHistory extends FaweStreamChangeSet {

private static final Logger LOGGER = LogManagerCompat.getLogger();
private static final Map<String, Map<UUID, Integer>> NEXT_INDEX = new ConcurrentHashMap<>();

private UUID uuid;
Expand Down Expand Up @@ -141,9 +144,9 @@ public void undo(Actor actor, Region[] regions) {
e.printStackTrace();
return;
}
EditSession session = toEditSession(actor, regions);
session.setBlocks(this, ChangeSetExecutor.Type.UNDO);
deleteFiles();
try (EditSession session = toEditSession(actor, regions)) {
session.setBlocks(this, ChangeSetExecutor.Type.UNDO);
}
}

public void undo(Actor actor) {
Expand Down Expand Up @@ -371,49 +374,79 @@ public FaweInputStream getBlockIS() throws IOException {
if (!bdFile.exists()) {
return null;
}
FaweInputStream is = MainUtil.getCompressedIS(new FileInputStream(bdFile));
readHeader(is);
return is;
try {
FaweInputStream is = MainUtil.getCompressedIS(new FileInputStream(bdFile));
readHeader(is);
return is;
} catch (IOException e) {
LOGGER.error("Could not load block history file {}", bdFile);
throw e;
}
}

@Override
public FaweInputStream getBiomeIS() throws IOException {
if (!bioFile.exists()) {
return null;
}
return MainUtil.getCompressedIS(new FileInputStream(bioFile));
try {
return MainUtil.getCompressedIS(new FileInputStream(bioFile));
} catch (IOException e) {
LOGGER.error("Could not load biome history file {}", bdFile);
throw e;
}
}

@Override
public NBTInputStream getEntityCreateIS() throws IOException {
if (!enttFile.exists()) {
return null;
}
return new NBTInputStream(MainUtil.getCompressedIS(new FileInputStream(enttFile)));
try {
return new NBTInputStream(MainUtil.getCompressedIS(new FileInputStream(enttFile)));
} catch (IOException e) {
LOGGER.error("Could not load entity create history file {}", bdFile);
throw e;
}
}

@Override
public NBTInputStream getEntityRemoveIS() throws IOException {
if (!entfFile.exists()) {
return null;
}
return new NBTInputStream(MainUtil.getCompressedIS(new FileInputStream(entfFile)));
try {
return new NBTInputStream(MainUtil.getCompressedIS(new FileInputStream(entfFile)));
} catch (IOException e) {
LOGGER.error("Could not load entity remove history file {}", bdFile);
throw e;
}
}

@Override
public NBTInputStream getTileCreateIS() throws IOException {
if (!nbttFile.exists()) {
return null;
}
return new NBTInputStream(MainUtil.getCompressedIS(new FileInputStream(nbttFile)));
try {
return new NBTInputStream(MainUtil.getCompressedIS(new FileInputStream(nbttFile)));
} catch (IOException e) {
LOGGER.error("Could not load tile create history file {}", bdFile);
throw e;
}
}

@Override
public NBTInputStream getTileRemoveIS() throws IOException {
if (!nbtfFile.exists()) {
return null;
}
return new NBTInputStream(MainUtil.getCompressedIS(new FileInputStream(nbtfFile)));
try {
return new NBTInputStream(MainUtil.getCompressedIS(new FileInputStream(nbtfFile)));
} catch (IOException e) {
LOGGER.error("Could not load tile remove history file {}", bdFile);
throw e;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.world.World;
import com.sk89q.worldedit.world.biome.BiomeType;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
Expand Down Expand Up @@ -125,6 +126,26 @@ public void add(int x, int y, int z, int combinedFrom, int combinedTo) {
}
}

@Override
public void addBiomeChange(int x, int y, int z, BiomeType from, BiomeType to) {
super.addBiomeChange(x, y, z, from, to);
if (x < minX) {
minX = x;
} else if (x > maxX) {
maxX = x;
}
if (y < minY) {
minY = y;
} else if (y > maxY) {
maxY = y;
}
if (z < minZ) {
minZ = z;
} else if (z > maxZ) {
maxZ = z;
}
}

@Override
public void writeHeader(OutputStream os, int x, int y, int z) throws IOException {
minX = x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,12 +257,14 @@ public EditSession toEditSession(Actor actor) {
}

public EditSession toEditSession(Actor actor, Region[] regions) {
EditSessionBuilder builder = WorldEdit.getInstance().newEditSessionBuilder().world(getWorld()).actor(actor).
fastMode(false).checkMemory(false).changeSet(this).limitUnlimited();
if (regions != null) {
builder.allowedRegions(regions);
} else {
builder.allowedRegionsEverywhere();
EditSessionBuilder builder = WorldEdit.getInstance().newEditSessionBuilder().world(world)
.checkMemory(false)
.changeSetNull()
.fastMode(false)
.limitUnprocessed(actor)
.actor(actor);
if (!actor.getLimit().RESTRICT_HISTORY_TO_REGIONS) {
builder = builder.allowedRegionsEverywhere();
}
EditSession editSession = builder.build();
editSession.setSize(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public void write(OutputStream out, int x, int y, int z) throws IOException {
@Override
public int readX(FaweInputStream in) throws IOException {
in.readFully(buffer);
return lx = lx + ((((buffer[1] & 0xFF) + ((MathMan.unpair16x(buffer[3])) << 8)) << 20) >> 20);
return lx = lx + ((((buffer[1] & 0xFF) | ((MathMan.unpair16x(buffer[3])) << 8)) << 20) >> 20);
}

@Override
Expand All @@ -177,7 +177,7 @@ public int readY(FaweInputStream in) {

@Override
public int readZ(FaweInputStream in) throws IOException {
return lz = lz + ((((buffer[2] & 0xFF) + ((MathMan.unpair16y(buffer[3])) << 8)) << 20) >> 20);
return lz = lz + ((((buffer[2] & 0xFF) | ((MathMan.unpair16y(buffer[3])) << 8)) << 20) >> 20);
}
};
} else {
Expand All @@ -203,17 +203,17 @@ public void write(OutputStream stream, int x, int y, int z) throws IOException {
@Override
public int readX(FaweInputStream is) throws IOException {
is.readFully(buffer);
return lx = (lx + (buffer[0] & 0xFF) + (buffer[1] << 8));
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));
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));
return lz = lz + ((buffer[2] & 0xFF) | (buffer[3]) << 8);
}
};
}
Expand Down Expand Up @@ -353,7 +353,7 @@ public void addBiomeChange(int bx, int by, int bz, BiomeType from, BiomeType to)
os.write((byte) (z));
// only need to store biomes in the 4x4x4 chunks so only need one byte for y still (signed byte -128 -> 127)
// means -512 -> 508. Add 128 to avoid negative value casting.
os.write((byte) (y + 128));
os.write((byte) (y + 32));
os.writeVarInt(from.getInternalId());
os.writeVarInt(to.getInternalId());
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,22 @@
import com.fastasyncworldedit.core.util.StringMan;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.sk89q.worldedit.EditSession;
import com.sk89q.worldedit.EditSessionBuilder;
import com.sk89q.worldedit.LocalSession;
import com.sk89q.worldedit.WorldEdit;
import com.sk89q.worldedit.WorldEditException;
import com.sk89q.worldedit.command.argument.Arguments;
import com.sk89q.worldedit.command.util.CommandPermissions;
import com.sk89q.worldedit.command.util.CommandPermissionsConditionGenerator;
import com.sk89q.worldedit.command.util.annotation.AllowedRegion;
import com.sk89q.worldedit.command.util.annotation.Confirm;
import com.sk89q.worldedit.command.util.annotation.Time;
import com.sk89q.worldedit.entity.Player;
import com.sk89q.worldedit.extension.platform.Actor;
import com.sk89q.worldedit.function.operation.ChangeSetExecutor;
import com.sk89q.worldedit.history.changeset.ChangeSet;
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.Region;
import com.sk89q.worldedit.util.Countable;
import com.sk89q.worldedit.util.Direction;
import com.sk89q.worldedit.util.Identifiable;
Expand Down Expand Up @@ -80,7 +82,6 @@ public HistorySubCommands(HistoryCommands parent) {
@Confirm
public synchronized void rerun(
Player player, World world, RollbackDatabase database,
@AllowedRegion Region[] allowedRegions,
@ArgFlag(name = 'u', desc = "String user", def = "me")
UUID other,
@ArgFlag(name = 'r', def = "0", desc = "radius")
Expand All @@ -90,7 +91,7 @@ public synchronized void rerun(
@Time
long timeDiff
) throws WorldEditException {
rollback(player, world, database, allowedRegions, other, radius, timeDiff, true);
rollback(player, world, database, other, radius, timeDiff, true);
}

@Command(
Expand All @@ -102,7 +103,6 @@ public synchronized void rerun(
@Confirm
public synchronized void rollback(
Player player, World world, RollbackDatabase database,
@AllowedRegion Region[] allowedRegions,
@ArgFlag(name = 'u', desc = "String user", def = "")
UUID other,
@ArgFlag(name = 'r', def = "0", desc = "radius")
Expand Down Expand Up @@ -149,9 +149,9 @@ public synchronized void rollback(
count++;
RollbackOptimizedHistory edit = supplier.get();
if (restore) {
edit.redo(player, allowedRegions);
edit.redo(player);
} else {
edit.undo(player, allowedRegions);
edit.undo(player);
}
String path = edit.getWorld().getName() + "/" + finalOther + "-" + edit.getIndex();
player.print(Caption.of("fawe.worldedit.rollback.rollback.element", path));
Expand Down Expand Up @@ -201,8 +201,7 @@ public synchronized void importdb(Actor actor) throws WorldEditException {
.at(summary.maxX, world.getMaxY(), summary.maxZ)
);
rollback.setTime(historyFile.lastModified());
RollbackDatabase db = DBHandler.IMP
.getDatabase(world);
RollbackDatabase db = DBHandler.dbHandler().getDatabase(world);
db.logEdit(rollback);
actor.print(TextComponent.of("Logging: " + historyFile));
}
Expand Down
3 changes: 2 additions & 1 deletion worldedit-core/src/main/resources/lang/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
"fawe.worldedit.brush.brush.source.mask": "Brush source mask set",
"fawe.worldedit.brush.brush.transform.disabled": "Brush transform disabled",
"fawe.worldedit.brush.brush.transform": "Brush transform set",
"fawe.worldedit.rollback.rollback.element": "Undoing {0}",
"fawe.worldedit.rollback.rollingback.index": "Undoing {0} ...",
"fawe.worldedit.rollback.rollback.element": "{0} undone.",
"fawe.worldedit.tool.tool.inspect": "Inspect tool bound to {0}.",
"fawe.worldedit.tool.tool.inspect.info": "{0} changed {1} to {2} {3} ago",
"fawe.worldedit.tool.tool.inspect.info.footer": "Total: {0} changes",
Expand Down
Loading