Skip to content

Commit

Permalink
Clean up some mixins by using mixinextras @Local
Browse files Browse the repository at this point in the history
Allowing us to capture local variables removes the need
for hacks in @reDIrect working around lack of local variable
capture.
  • Loading branch information
Spottedleaf committed May 27, 2024
1 parent 0fa6d07 commit 845e8fb
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 113 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public final class MoonriseCommon {
}

WORKER_POOL = new PrioritisedThreadPool(
"Moonrise Chunk System Worker Pool", workerThreads,
"Moonrise Worker Pool", workerThreads,
(final Thread thread, final Integer id) -> {
thread.setName("Moonrise Chunk System Worker #" + id.intValue());
thread.setName("Moonrise Common Worker #" + id.intValue());
thread.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(final Thread thread, final Throwable throwable) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ca.spottedleaf.moonrise.mixin.chunk_system;

import ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemLevelReader;
import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.world.level.LevelReader;
import net.minecraft.world.level.StructureManager;
import net.minecraft.world.level.chunk.ChunkAccess;
Expand All @@ -21,7 +22,7 @@
public abstract class ChunkGeneratorMixin {

/**
* @reason Pass the supplier to the mixin below so that we can change the executor to the parameter provided
* @reason Use the provided executor, chunk system sets this to something specific
* @author Spottedleaf
*/
@Redirect(
Expand All @@ -31,27 +32,9 @@ public abstract class ChunkGeneratorMixin {
target = "Ljava/util/concurrent/CompletableFuture;supplyAsync(Ljava/util/function/Supplier;Ljava/util/concurrent/Executor;)Ljava/util/concurrent/CompletableFuture;"
)
)
private <U> CompletableFuture<U> passSupplier(Supplier<U> supplier, Executor executor) {
return (CompletableFuture<U>)CompletableFuture.completedFuture(supplier);
}

/**
* @reason Retrieve the supplier from the mixin above so that we can change the executor to the parameter provided
* @author Spottedleaf
*/
@Inject(
method = "createBiomes",
cancellable = true,
at = @At(
value = "RETURN"
)
)
private void unpackSupplier(Executor executor, RandomState randomState, Blender blender,
StructureManager structureManager, ChunkAccess chunkAccess,
CallbackInfoReturnable<CompletableFuture<ChunkAccess>> cir) {
cir.setReturnValue(
CompletableFuture.supplyAsync(((CompletableFuture<Supplier<ChunkAccess>>)(CompletableFuture)cir.getReturnValue()).join(), executor)
);
private <U> CompletableFuture<U> redirectBiomesExecutor(final Supplier<U> supplier, final Executor badExecutor,
@Local(ordinal = 0, argsOnly = true) final Executor executor) {
return CompletableFuture.supplyAsync(supplier, executor);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ca.spottedleaf.moonrise.common.list.IteratorSafeOrderedReferenceSet;
import ca.spottedleaf.moonrise.common.util.TickThread;
import com.llamalad7.mixinextras.sugar.Local;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.entity.EntityTickList;
Expand Down Expand Up @@ -68,25 +69,9 @@ private <V> V hookAdd(final Int2ObjectMap<V> instance, final int key, final V va
return null;
}

/**
* @reason Route to new entity list
* @author Spottedleaf
*/
@Inject(
method = "remove",
at = @At(
value = "INVOKE",
target = "Lit/unimi/dsi/fastutil/ints/Int2ObjectMap;remove(I)Ljava/lang/Object;",
remap = false
)
)
private void hookRemove(final Entity entity, final CallbackInfo ci) {
this.entities.remove(entity);
}


/**
* @reason Avoid NPE on accessing old state
* @reason Route to new entity list
* @author Spottedleaf
*/
@Redirect(
Expand All @@ -97,7 +82,9 @@ private void hookRemove(final Entity entity, final CallbackInfo ci) {
remap = false
)
)
private <V> V hookRemoveAvoidNPE(final Int2ObjectMap<V> instance, final int key) {
private <V> V hookRemoveAvoidNPE(final Int2ObjectMap<V> instance, final int key,
final @Local(ordinal = 0, argsOnly = true) Entity entity) {
this.entities.remove(entity);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ca.spottedleaf.moonrise.mixin.chunk_system;

import ca.spottedleaf.moonrise.common.real_dumb_shit.HolderCompletableFuture;
import com.llamalad7.mixinextras.sugar.Local;
import net.minecraft.world.level.StructureManager;
import net.minecraft.world.level.chunk.ChunkAccess;
import net.minecraft.world.level.levelgen.NoiseBasedChunkGenerator;
Expand All @@ -20,7 +20,7 @@
public abstract class NoiseBasedChunkGeneratorMixin {

/**
* @reason Pass the supplier to the mixin below so that we can change the executor to the parameter provided
* @reason Use the provided executor, chunk system sets this to something specific
* @author Spottedleaf
*/
@Redirect(
Expand All @@ -30,32 +30,13 @@ public abstract class NoiseBasedChunkGeneratorMixin {
target = "Ljava/util/concurrent/CompletableFuture;supplyAsync(Ljava/util/function/Supplier;Ljava/util/concurrent/Executor;)Ljava/util/concurrent/CompletableFuture;"
)
)
private <U> CompletableFuture<U> passSupplierBiomes(Supplier<U> supplier, Executor executor) {
return (CompletableFuture<U>)CompletableFuture.completedFuture(supplier);
private <U> CompletableFuture<U> redirectBiomesExecutor(final Supplier<U> supplier, final Executor badExecutor,
@Local(ordinal = 0, argsOnly = true) final Executor executor) {
return CompletableFuture.supplyAsync(supplier, executor);
}

/**
* @reason Retrieve the supplier from the mixin above so that we can change the executor to the parameter provided
* @author Spottedleaf
*/
@Inject(
method = "createBiomes",
cancellable = true,
at = @At(
value = "RETURN"
)
)
private void unpackSupplierBiomes(Executor executor, RandomState randomState, Blender blender,
StructureManager structureManager, ChunkAccess chunkAccess,
CallbackInfoReturnable<CompletableFuture<ChunkAccess>> cir) {
cir.setReturnValue(
CompletableFuture.supplyAsync(((CompletableFuture<Supplier<ChunkAccess>>)(CompletableFuture)cir.getReturnValue()).join(), executor)
);
}


/**
* @reason Pass the executor tasks to the mixin below so that we can change the executor to the parameter provided
* @reason Use the provided executor, chunk system sets this to something specific
* @author Spottedleaf
*/
@Redirect(
Expand All @@ -65,40 +46,8 @@ private void unpackSupplierBiomes(Executor executor, RandomState randomState, Bl
target = "Ljava/util/concurrent/CompletableFuture;supplyAsync(Ljava/util/function/Supplier;Ljava/util/concurrent/Executor;)Ljava/util/concurrent/CompletableFuture;"
)
)
private <U> CompletableFuture<U> passSupplierNoise(Supplier<U> supplier, Executor executor) {
final HolderCompletableFuture<U> ret = new HolderCompletableFuture<>();

ret.toExecute.add(() -> {
try {
ret.complete(supplier.get());
} catch (final Throwable throwable) {
ret.completeExceptionally(throwable);
}
});

return ret;
}

/**
* @reason Retrieve the executor tasks from the mixin above so that we can change the executor to the parameter provided
* @author Spottedleaf
*/
@Redirect(
method = "fillFromNoise",
at = @At(
value = "INVOKE",
target = "Ljava/util/concurrent/CompletableFuture;whenCompleteAsync(Ljava/util/function/BiConsumer;Ljava/util/concurrent/Executor;)Ljava/util/concurrent/CompletableFuture;"
)
)
private <T> CompletableFuture<T> unpackSupplierNoise(final CompletableFuture<T> instance, final BiConsumer<? super T, ? super Throwable> action,
final Executor executor) {
final HolderCompletableFuture<T> casted = (HolderCompletableFuture<T>)instance;

for (final Runnable run : casted.toExecute) {
executor.execute(run);
}

// note: executor is the parameter we want
return instance.whenCompleteAsync(action, executor);
private <U> CompletableFuture<U> redirectNoiseExecutor(final Supplier<U> supplier, final Executor badExecutor,
@Local(ordinal = 0, argsOnly = true) final Executor executor) {
return CompletableFuture.supplyAsync(supplier, executor);
}
}

0 comments on commit 845e8fb

Please sign in to comment.