Skip to content

Commit

Permalink
fix: handle reentrance in ServerEntityManager#unloadChunks
Browse files Browse the repository at this point in the history
  • Loading branch information
ishland committed Dec 21, 2024
1 parent 4c2d74d commit 74afd31
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.ishland.c2me.rewrites.chunksystem.mixin.fixes;

import it.unimi.dsi.fastutil.longs.LongLinkedOpenHashSet;
import it.unimi.dsi.fastutil.longs.LongSet;
import net.minecraft.server.world.ServerEntityManager;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ServerEntityManager.class)
public abstract class MixinServerEntityManager {

@Mutable
@Shadow @Final private LongSet pendingUnloads;

@Shadow protected abstract boolean unload(long chunkPos);

@Inject(method = "<init>", at = @At("RETURN"))
private void replacePendingUnloads(CallbackInfo ci) {
this.pendingUnloads = new LongLinkedOpenHashSet(this.pendingUnloads);
}

/**
* @author ishland
* @reason use alternative method for unloading
*/
@Overwrite
private void unloadChunks() {
LongSet pendingUnloads = this.pendingUnloads;
if (!(pendingUnloads instanceof LongLinkedOpenHashSet)) {
// set is replaced by someone else, replace it again
pendingUnloads = this.pendingUnloads = new LongLinkedOpenHashSet(pendingUnloads);
}

// apparently vanilla also have a `this.trackingStatuses.get(pos) != EntityTrackingStatus.HIDDEN` check?????
// removed here because chunks in pendingUnloads should always satisfy that constraint
// if it does not, you have other serious problems
LongLinkedOpenHashSet linkedOpenHashSet = (LongLinkedOpenHashSet) pendingUnloads;
while (!linkedOpenHashSet.isEmpty()) {
long pos = linkedOpenHashSet.removeFirstLong();
this.unload(pos);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"async_serialization.MixinSerializingRegionBasedStorage",
"async_serialization.MixinStorageIoWorker",
"async_serialization.MixinThreadedAnvilChunkStorage",
"async_serialization.gc_free_serializer.MixinChunkDataSerializer"
"async_serialization.gc_free_serializer.MixinChunkDataSerializer",
"fixes.MixinServerEntityManager"
]
}

0 comments on commit 74afd31

Please sign in to comment.