Skip to content

Commit

Permalink
Reimplement things on latest upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
ishland committed Jan 29, 2022
1 parent b758dc0 commit 46f77bd
Show file tree
Hide file tree
Showing 20 changed files with 269 additions and 61 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## Chunky-fork

Used for benchmarking and pressure-testing C2ME

# New features
- Chunked chunk generation pattern to make better use of half-generated chunks (ProtoChunk technically) resulting in a much higher pregen speed
Usage: `chunky pattern chunked_<pattern>`
[Available values for \<pattern\>](https://github.com/pop4959/Chunky/wiki/Patterns)
Note that the default pattern is still `concentric`

## Chunky

Pre-generates chunks, quickly, efficiently, and safely
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ subprojects {
plugins.apply("com.github.johnrengelman.shadow")

group = "${project.property("group")}"
version = "${project.property("version")}.${commitsSinceLastTag()}"
version = "${project.property("version")}.${commitsSinceLastTag()}" + "+ishlandfork"

repositories {
mavenCentral()
Expand Down
18 changes: 15 additions & 3 deletions common/src/main/java/org/popcraft/chunky/GenerationTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@

import java.util.Deque;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicLong;

public class GenerationTask implements Runnable {
private static final int MAX_WORKING_COUNT = Input.tryInteger(System.getProperty("chunky.maxWorkingCount")).orElse(50);
private static final int MAX_WORKING_COUNT = Input.tryInteger(System.getProperty("chunky.maxWorkingCount")).orElse(768);
private final Chunky chunky;
private final Selection selection;
private final Shape shape;
Expand All @@ -28,6 +29,7 @@ public class GenerationTask implements Runnable {
private final Deque<Pair<Long, AtomicLong>> updateSamples = new ConcurrentLinkedDeque<>();
private final Progress progress;
private final RegionCache.WorldState worldState;
private final ConcurrentLinkedQueue<Runnable> tasks = new ConcurrentLinkedQueue<>();
private ChunkIterator chunkIterator;
private boolean stopped, cancelled;
private long prevTime;
Expand Down Expand Up @@ -116,16 +118,26 @@ public void run() {
update(chunk.x, chunk.z, true);
continue;
}
{
Runnable runnable;
while ((runnable = this.tasks.poll()) != null) {
try {
runnable.run();
} catch (Throwable t) {
t.printStackTrace();
}
}
}
try {
working.acquire();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
stop(cancelled);
break;
}
selection.world().getChunkAtAsync(chunk.x, chunk.z).whenComplete((ignored, throwable) -> {
selection.world().getChunkAtAsync(chunk.x, chunk.z).whenCompleteAsync((ignored, throwable) -> {
working.release();
update(chunk.x, chunk.z, true);
tasks.add(() -> update(chunk.x, chunk.z, true));
});
}
if (stopped) {
Expand Down
2 changes: 1 addition & 1 deletion common/src/main/java/org/popcraft/chunky/Selection.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Selection {
private final int diameterChunksX;
private final int diameterChunksZ;

private Selection(World world, double centerX, double centerZ, double radiusX, double radiusZ, String pattern, String shape) {
public Selection(World world, double centerX, double centerZ, double radiusX, double radiusZ, String pattern, String shape) {
this.world = world;
this.centerX = centerX;
this.centerZ = centerZ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,13 @@ public void execute(Sender sender, String[] args) {
chunky.getScheduler().runTask(generationTask);
sender.sendMessagePrefixed(TranslationKey.FORMAT_START, current.world().getName(), translate("shape_" + current.shape()), Formatting.number(current.centerX()), Formatting.number(current.centerZ()), Formatting.radius(current));
};
if (chunky.getConfig().loadTask(current.world()).isPresent()) {
chunky.setPendingAction(sender, startAction);
sender.sendMessagePrefixed(TranslationKey.FORMAT_START_CONFIRM, "/chunky continue", "/chunky confirm");
} else {
startAction.run();
}
// if (chunky.getConfig().loadTask(current.world()).isPresent()) {
// chunky.setPendingAction(sender, startAction);
// sender.sendMessagePrefixed(TranslationKey.FORMAT_START_CONFIRM, "/chunky continue", "/chunky confirm");
// } else {
// startAction.run();
// }
startAction.run();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ private ChunkIteratorFactory() {
}

public static ChunkIterator getChunkIterator(Selection selection, long count) {
if (selection.pattern().startsWith("chunked_"))
return new WrappingChunkedChunkIterator(new Selection(
selection.world(),
selection.centerX(),
selection.centerZ(),
selection.radiusX(),
selection.radiusZ(),
selection.pattern().substring("chunked_".length()),
selection.shape()
), count);
switch (selection.shape()) {
case ShapeType.RECTANGLE:
case ShapeType.ELLIPSE:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package org.popcraft.chunky.iterator;

import org.popcraft.chunky.Selection;
import org.popcraft.chunky.util.ChunkCoordinate;

import java.util.LinkedList;
import java.util.Queue;

public class WrappingChunkedChunkIterator implements ChunkIterator {

private static final int CHUNK_SIZE = 8;
private final ChunkIterator delegatingIterator;
private final Queue<ChunkCoordinate> queue = new LinkedList<>();
private final int centerChunkX;
private final int centerChunkZ;

public WrappingChunkedChunkIterator(Selection selection, long count) {
this.centerChunkX = selection.centerChunkX();
this.centerChunkZ = selection.centerChunkZ();
delegatingIterator = ChunkIteratorFactory.getChunkIterator(new Selection(
selection.world(),
0,
0,
Math.ceil(selection.radiusX() / (double) CHUNK_SIZE),
Math.ceil(selection.radiusZ() / (double) CHUNK_SIZE),
selection.pattern(),
selection.shape()
), (long) Math.ceil(count / (double) (CHUNK_SIZE * CHUNK_SIZE)));
System.out.println(String.format("WrappingChunkedIterator: %s", this.name()));
}

@Override
public long total() {
return delegatingIterator.total() * CHUNK_SIZE * CHUNK_SIZE;
}

@Override
public String name() {
return String.format("chunked_%s", delegatingIterator.name());
}

@Override
public boolean hasNext() {
return !queue.isEmpty() || delegatingIterator.hasNext();
}

private void fillQueue() {
final ChunkCoordinate areaCoordinate = delegatingIterator.next();
for (int x = 0; x < CHUNK_SIZE; x ++)
for (int z = 0; z < CHUNK_SIZE; z ++)
queue.add(new ChunkCoordinate(areaCoordinate.x * CHUNK_SIZE + x + centerChunkX, areaCoordinate.z * CHUNK_SIZE + z + centerChunkZ));
}

@Override
public synchronized ChunkCoordinate next() {
if (queue.isEmpty()) fillQueue();
assert !queue.isEmpty();
return queue.poll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public GsonConfig(Supplier<Chunky> chunky, File configFile) {
} else {
reload();
}
if (this.configModel.tasks != null)
this.configModel.tasks.clear();
Translator.setLanguage(getLanguage());
}

Expand Down
2 changes: 2 additions & 0 deletions common/src/main/java/org/popcraft/chunky/util/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public static Optional<String> tryPattern(String input) {
return Optional.empty();
}
String inputLower = input.toLowerCase();
if (inputLower.startsWith("chunked_"))
return tryPattern(inputLower.substring("chunked_".length())).map(s -> "chunked_" + s);
if (PatternType.ALL.contains(inputLower)) {
return Optional.of(inputLower);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static boolean isValidLanguage(String language) {

public static String translateKey(String key, boolean prefixed, Object... args) {
StringBuilder translation = new StringBuilder();
String message = translations.getOrDefault(key, fallbackTranslations.getOrDefault(key, "Missing translation"));
String message = translations.getOrDefault(key, fallbackTranslations.getOrDefault(key, key));
if (prefixed) {
translation.append("[Chunky] ");
}
Expand Down
2 changes: 1 addition & 1 deletion fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies {
minecraft(group = "com.mojang", name = "minecraft", version = "1.18")
mappings(group = "net.fabricmc", name = "yarn", version = "1.18+build.1", classifier = "v2")
modImplementation(group = "net.fabricmc", name = "fabric-loader", version = "0.12.6")
modImplementation(group = "net.fabricmc.fabric-api", name = "fabric-api", version = "0.43.1+1.18")
// modImplementation(group = "net.fabricmc.fabric-api", name = "fabric-api", version = "0.43.1+1.18")
implementation(project(":chunky-common"))
shade(project(":chunky-common"))
}
Expand Down
26 changes: 16 additions & 10 deletions fabric/src/main/java/org/popcraft/chunky/ChunkyFabric.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package org.popcraft.chunky;

import com.mojang.brigadier.Command;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.ServerCommandSource;
import org.popcraft.chunky.command.ChunkyCommand;
import org.popcraft.chunky.command.CommandLiteral;
Expand All @@ -21,6 +20,8 @@
import java.io.File;
import java.util.Arrays;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

import static com.mojang.brigadier.arguments.IntegerArgumentType.integer;
import static com.mojang.brigadier.arguments.StringArgumentType.string;
Expand All @@ -32,22 +33,27 @@
public class ChunkyFabric implements ModInitializer {
private Chunky chunky;

public static Consumer<MinecraftServer> SERVER_STARTED = unused -> {};
public static Consumer<MinecraftServer> SERVER_STOPPING = unused -> {};
public static Consumer<MinecraftServer> SERVER_TICK_END = unused -> {};
public static Consumer<CommandDispatcher<ServerCommandSource>> COMMAND_REGISTER = unused -> {};

@Override
public void onInitialize() {
ServerLifecycleEvents.SERVER_STARTED.register(minecraftServer -> {
SERVER_STARTED = minecraftServer -> {
File configFile = new File(FabricLoader.getInstance().getConfigDir().toFile(), "chunky.json");
this.chunky = new Chunky(new FabricServer(this, minecraftServer), new GsonConfig(() -> chunky, configFile));
if (chunky.getConfig().getContinueOnRestart()) {
chunky.getCommands().get(CommandLiteral.CONTINUE).execute(chunky.getServer().getConsole(), new String[]{});
}
});
ServerLifecycleEvents.SERVER_STOPPING.register(minecraftServer -> {
};
SERVER_STOPPING = minecraftServer -> {
if (chunky != null) {
chunky.disable();
}
});
ServerTickEvents.END_SERVER_TICK.register(server -> BossBarProgress.tick(chunky, server));
CommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> {
};
SERVER_TICK_END = server -> BossBarProgress.tick(chunky, server);
COMMAND_REGISTER = dispatcher -> {
final LiteralArgumentBuilder<ServerCommandSource> command = literal(CommandLiteral.CHUNKY)
.requires(serverCommandSource -> serverCommandSource.hasPermissionLevel(2))
.executes(context -> {
Expand Down Expand Up @@ -108,7 +114,7 @@ public void onInitialize() {
registerArguments(command, literal(CommandLiteral.WORLD),
argument(CommandLiteral.WORLD, dimension()));
dispatcher.register(command);
});
};
}

@SafeVarargs
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.popcraft.chunky.events;

import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.Executor;

public class SchedulingUtil {

private static final ConcurrentLinkedQueue<Runnable> tickEnd = new ConcurrentLinkedQueue<>();
public static final Executor tickEndExecutor = tickEnd::add;

public static void invokeTickEnd() {
Runnable runnable;
while ((runnable = tickEnd.poll()) != null) {
try {
runnable.run();
} catch (Throwable t) {
t.printStackTrace();
}
}
}

private SchedulingUtil() {
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

@Mixin(ServerChunkManager.class)
public interface ServerChunkManagerMixin {
@Invoker("tick")
@Invoker
@SuppressWarnings({"UnusedReturnValue", "UnnecessaryInterfaceModifier"})
public boolean tick();
public boolean invokeTick();
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@
@SuppressWarnings("UnnecessaryInterfaceModifier")
@Mixin(ThreadedAnvilChunkStorage.class)
public interface ThreadedAnvilChunkStorageMixin {
@Invoker("getChunkHolder")
public ChunkHolder getChunkHolder(long pos);
@Invoker
public ChunkHolder invokeGetChunkHolder(long pos);

@Invoker("getUpdatedChunkNbt")
public NbtCompound getUpdatedChunkNbt(ChunkPos pos);
@Invoker
public NbtCompound invokeGetUpdatedChunkNbt(ChunkPos pos);

@Accessor("chunksToUnload")
@Accessor
public Long2ObjectLinkedOpenHashMap<ChunkHolder> getChunksToUnload();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.popcraft.chunky.mixin.events;

import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import org.popcraft.chunky.ChunkyFabric;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
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(CommandManager.class)
public class MixinCommandManager {

@Shadow @Final private CommandDispatcher<ServerCommandSource> dispatcher;

@Inject(method = "<init>", at = @At(value = "INVOKE", target = "Lcom/mojang/brigadier/CommandDispatcher;findAmbiguities(Lcom/mojang/brigadier/AmbiguityConsumer;)V", shift = At.Shift.BEFORE))
private void onCommandRegister(CallbackInfo ci) {
try {
ChunkyFabric.COMMAND_REGISTER.accept(this.dispatcher);
} catch (Throwable t) {
t.printStackTrace();
}
}

}
Loading

0 comments on commit 46f77bd

Please sign in to comment.