Leave custom data untouched when a versioned transformer fails

This commit is contained in:
Ruling-0 committed 2026-11-25 14:45:59 -06:00
1 parent c7f139c53a
commit 93c77c7e5f
6 files changed
+125 -53

No files matched your search

@@ -1,13 +1,10 @@
package com.gtnewhorizons.postea.api;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ReportedException;
import com.gtnewhorizons.postea.Postea;
import com.gtnewhorizons.postea.utility.ChunkFixerUtility;
import com.gtnewhorizons.postea.utility.IDRegistry;
import com.gtnewhorizons.postea.utility.VersionStamps;
import com.gtnewhorizons.postea.utility.VersionedTransformerLog;
import com.gtnewhorizons.postea.utility.VersionedPass;
import cpw.mods.fml.common.FMLCommonHandler;
@@ -21,9 +18,11 @@ import cpw.mods.fml.common.FMLCommonHandler;
/// Custom data belongs to a world, so [#transform] and [#stamp] do nothing unless a server runs in this JVM.
public abstract class CustomDataReplacementManager {
/// Transforms `root` in place through every registered transformer whose stamp differs from its current
/// version, then writes the updated stamps onto `root`. Throws [IllegalStateException] when called before the
/// world's info and saved id map have loaded.
/// Transforms `root` in place through every registered transformer whose stamp differs from its current version,
/// then writes the updated stamps onto `root`.
///
/// A failing transformer throws [net.minecraft.util.ReportedException] and leaves `root` as read, without stamps
/// added. A call before the world's info and saved id map have loaded throws [IllegalStateException].
public static void transform(String storageId, NBTTagCompound root) {
if (!serverRunning()) return;
if (!IDRegistry.isMappingApplied()) {
@@ -36,20 +35,13 @@ public abstract class CustomDataReplacementManager {
/// [#transform(String, NBTTagCompound)] through `transformers`, without the server and id mapping checks.
static void transform(String storageId, NBTTagCompound root, Iterable<IVersionedTransformer> transformers) {
NBTTagCompound stamps = VersionStamps.read(root);
for (IVersionedTransformer transformer : transformers) {
int current = transformer.currentVersion();
int stored = VersionStamps.stored(stamps, transformer.key());
if (stored == current) continue;
VersionedTransformerLog.announce(transformer, stored, current);
Postea.LOG.debug("{}: custom data {} from version {} to {}", transformer.key(), storageId, stored, current);
try {
transformer.transformCustomData(new CustomDataTransformContext(storageId, root, stored));
} catch (Throwable t) {
throw new ReportedException(
ChunkFixerUtility.describe(t, transformer, stored, current, "Custom data", storageId));
}
}
NBTTagCompound stamps = VersionedPass.runAtomically(
root,
transformers,
"Custom data",
storageId,
(transformer, data, stored) -> transformer
.transformCustomData(new CustomDataTransformContext(storageId, data, stored)));
VersionStamps.write(root, stamps, transformers);
}
@@ -6,11 +6,8 @@ import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import net.minecraft.crash.CrashReport;
import net.minecraft.crash.CrashReportCategory;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.ReportedException;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.storage.ExtendedBlockStorage;
@@ -80,14 +77,13 @@ public class ChunkFixerUtility {
try {
transformer.transformChunk(ctx);
} catch (Throwable t) {
throw new ReportedException(
describe(
t,
transformer,
stored,
current,
"Chunk",
chunk.xPosition + "," + chunk.zPosition + " in dimension " + world.provider.dimensionId));
throw VersionedPass.failure(
t,
transformer,
stored,
current,
"Chunk",
chunk.xPosition + "," + chunk.zPosition + " in dimension " + world.provider.dimensionId);
}
chunkMixin.Postea$setVersionedDirty(true);
blockIdsChanged |= ctx.blockIdsChanged();
@@ -102,17 +98,6 @@ public class ChunkFixerUtility {
chunk.isLightPopulated = false;
}
public static CrashReport describe(Throwable cause, IVersionedTransformer transformer, int stored, int current,
String subjectName, String subject) {
CrashReport report = CrashReport.makeCrashReport(cause, "Running versioned transformer " + transformer.key());
CrashReportCategory category = report.makeCategory("Postea versioned transformer");
category.addCrashSection("Key", transformer.key());
category.addCrashSection("Stored version", stored);
category.addCrashSection("Current version", current);
category.addCrashSection(subjectName, subject);
return report;
}
public static void onChunkLoaded(Chunk chunk) {
if (chunk instanceof IChunkMixin iChunkMixin) {
boolean stale = !hasChunkBeenUpdated(iChunkMixin.Postea$getPosteaCode());
@@ -2,7 +2,6 @@ package com.gtnewhorizons.postea.utility;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ReportedException;
import com.gtnewhorizons.postea.api.IVersionedTransformer;
import com.gtnewhorizons.postea.api.PlayerDataTransformContext;
@@ -26,14 +25,13 @@ public final class PlayerDataFixerUtility {
try {
transformer.transformPlayer(new PlayerDataTransformContext(player, tag, stored));
} catch (Throwable t) {
throw new ReportedException(
ChunkFixerUtility.describe(
t,
transformer,
stored,
current,
"Player",
player.getCommandSenderName() + " (" + player.getUniqueID() + ")"));
throw VersionedPass.failure(
t,
transformer,
stored,
current,
"Player",
player.getCommandSenderName() + " (" + player.getUniqueID() + ")");
}
}
return stamps == null ? null : (NBTTagCompound) stamps.copy();
@@ -0,0 +1,70 @@
package com.gtnewhorizons.postea.utility;
import java.util.ArrayList;
import net.minecraft.crash.CrashReport;
import net.minecraft.crash.CrashReportCategory;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ReportedException;
import com.gtnewhorizons.postea.Postea;
import com.gtnewhorizons.postea.api.IVersionedTransformer;
/// Runs versioned transformers over saved NBT and reports their failures.
public final class VersionedPass {
private VersionedPass() {}
/// One transformer's work on `root`, which holds data stamped at `stored`.
@FunctionalInterface
public interface Step {
void apply(IVersionedTransformer transformer, NBTTagCompound root, int stored);
}
/// Runs `step` for every transformer whose stamp on `root` differs from its current version. The steps work on a
/// copy of `root`, which replaces the contents of `root` once every step has succeeded. A failing step throws the
/// [#failure] for `subjectName` and `subject`, and leaves `root` as read.
///
/// @return a copy of the stamps `root` was read with, or null if unstamped
public static NBTTagCompound runAtomically(NBTTagCompound root, Iterable<IVersionedTransformer> transformers,
String subjectName, String subject, Step step) {
NBTTagCompound stamps = VersionStamps.read(root);
NBTTagCompound carried = stamps == null ? null : (NBTTagCompound) stamps.copy();
NBTTagCompound working = null;
for (IVersionedTransformer transformer : transformers) {
int current = transformer.currentVersion();
int stored = VersionStamps.stored(carried, transformer.key());
if (stored == current) continue;
VersionedTransformerLog.announce(transformer, stored, current);
Postea.LOG
.debug("{}: {} {} from version {} to {}", transformer.key(), subjectName, subject, stored, current);
if (working == null) working = (NBTTagCompound) root.copy();
try {
step.apply(transformer, working, stored);
} catch (Throwable t) {
throw failure(t, transformer, stored, current, subjectName, subject);
}
}
if (working != null) replaceContents(root, working);
return carried;
}
/// The exception a failed transformer run throws, with a crash report naming the transformer, both versions and
/// the subject.
public static ReportedException failure(Throwable cause, IVersionedTransformer transformer, int stored, int current,
String subjectName, String subject) {
CrashReport report = CrashReport.makeCrashReport(cause, "Running versioned transformer " + transformer.key());
CrashReportCategory category = report.makeCategory("Postea versioned transformer");
category.addCrashSection("Key", transformer.key());
category.addCrashSection("Stored version", stored);
category.addCrashSection("Current version", current);
category.addCrashSection(subjectName, subject);
return new ReportedException(report);
}
private static void replaceContents(NBTTagCompound target, NBTTagCompound source) {
for (Object key : new ArrayList<Object>(target.func_150296_c())) target.removeTag((String) key);
for (Object key : source.func_150296_c()) target.setTag((String) key, source.getTag((String) key));
}
}
@@ -1,6 +1,7 @@
package com.gtnewhorizons.postea.api;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.util.Arrays;
import java.util.Collections;
@@ -30,4 +31,22 @@ class CustomDataReplacementManagerTest {
assertEquals(5, VersionStamps.stored(stamps, "mod:absent"));
assertEquals(2, VersionStamps.stored(saved, "mod:present"));
}
@Test
void aThrowingTransformerLeavesTheRootAsRead() {
StubTransformer rewrites = new StubTransformer("mod:rewrites", 2);
rewrites.customData = ctx -> ctx.root()
.setString("data", "new");
StubTransformer fails = new StubTransformer("mod:fails", 2);
fails.customData = ctx -> { throw new IllegalStateException("broken"); };
NBTTagCompound root = new NBTTagCompound();
root.setString("data", "old");
NBTTagCompound asRead = (NBTTagCompound) root.copy();
// The ReportedException's CrashReport initializes FML, which fails outside a launched game.
assertThrows(
Throwable.class,
() -> CustomDataReplacementManager.transform("mod:storage", root, Arrays.asList(rewrites, fails)));
assertEquals(asRead, root);
}
}
@@ -1,10 +1,13 @@
package com.gtnewhorizons.postea.api;
/// An [IVersionedTransformer] that changes nothing, at a version the test sets.
import java.util.function.Consumer;
/// An [IVersionedTransformer] at a version the test sets, running the test's custom data transform.
public final class StubTransformer implements IVersionedTransformer {
private final String key;
public int version;
public Consumer<CustomDataTransformContext> customData = ctx -> {};
public StubTransformer(String key, int version) {
this.key = key;
@@ -20,4 +23,9 @@ public final class StubTransformer implements IVersionedTransformer {
public int currentVersion() {
return version;
}
@Override
public void transformCustomData(CustomDataTransformContext ctx) {
customData.accept(ctx);
}
}