From 6fca33e4963fe8ffd7015d76a426c6bfa11086cf Mon Sep 17 00:00:00 2001 From: TomTheFurry Date: Sun, 2 Jul 2023 18:32:15 +0800 Subject: [PATCH 1/2] Fix a crash on game startup, and fix world gen compat with TFC (TerraFirmaCraft) --- .../BatchGenerationEnvironment.java | 110 +++++++++++++----- .../mimicObject/LightedWorldGenRegion.java | 2 +- .../worldGeneration/step/StepFeatures.java | 2 + coreSubProjects | 2 +- .../mixins/client/MixinGameRenderer.java | 15 ++- forge/build.gradle | 2 + .../mixins/client/MixinGameRenderer.java | 15 ++- versionProperties/1.18.2.properties | 1 + 8 files changed, 113 insertions(+), 36 deletions(-) diff --git a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/BatchGenerationEnvironment.java b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/BatchGenerationEnvironment.java index 228ef9e64..924d059df 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/BatchGenerationEnvironment.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/BatchGenerationEnvironment.java @@ -20,6 +20,8 @@ package com.seibel.distanthorizons.common.wrappers.worldGeneration; +import com.google.common.collect.ImmutableMap; +import com.seibel.distanthorizons.api.enums.worldGeneration.EDhApiDistantGeneratorMode; import com.seibel.distanthorizons.api.enums.worldGeneration.EDhApiWorldGenerationStep; import com.seibel.distanthorizons.common.wrappers.world.ServerLevelWrapper; import com.seibel.distanthorizons.common.wrappers.worldGeneration.mimicObject.*; @@ -191,7 +193,21 @@ public final class BatchGenerationEnvironment extends AbstractBatchGenerationEnv } public static ThreadLocal isDistantGeneratorThread = new ThreadLocal<>(); + public static ThreadLocal onDistantGenerationMixinData = new ThreadLocal<>(); public static boolean isCurrentThreadDistantGeneratorThread() { return (isDistantGeneratorThread.get() != null); } + public static void putDistantGenerationMixinData(Object data) { + LodUtil.assertTrue(isCurrentThreadDistantGeneratorThread()); + onDistantGenerationMixinData.set(data); + } + public static Object getDistantGenerationMixinData() { + LodUtil.assertTrue(isCurrentThreadDistantGeneratorThread()); + return onDistantGenerationMixinData.get(); + } + + public static void clearDistantGenerationMixinData() { + LodUtil.assertTrue(isCurrentThreadDistantGeneratorThread()); + onDistantGenerationMixinData.remove(); + } public static final DhThreadFactory threadFactory = new DhThreadFactory("DH-Gen-Worker-Thread", Thread.MIN_PRIORITY); @@ -200,10 +216,35 @@ public final class BatchGenerationEnvironment extends AbstractBatchGenerationEnv //==============// // constructors // //==============// - + + public static ImmutableMap BorderNeeded; + public static int MaxBorderNeeded; + static { DependencySetupDoneCheck.getIsCurrentThreadDistantGeneratorThread = BatchGenerationEnvironment::isCurrentThreadDistantGeneratorThread; + + boolean isTerraFirmaCraft = false; + try { + Class.forName("net.dries007.tfc.world.TFCChunkGenerator"); + isTerraFirmaCraft = true; + } catch (ClassNotFoundException e) { + //Ignore + } + EVENT_LOGGER.info("DH TerraFirmaCraft detection: " + isTerraFirmaCraft); + ImmutableMap.Builder builder = ImmutableMap.builder(); + builder.put(EDhApiWorldGenerationStep.EMPTY, 1); + builder.put(EDhApiWorldGenerationStep.STRUCTURE_START, 0); + builder.put(EDhApiWorldGenerationStep.STRUCTURE_REFERENCE, 0); + builder.put(EDhApiWorldGenerationStep.BIOMES, isTerraFirmaCraft ? 1 : 0); + builder.put(EDhApiWorldGenerationStep.NOISE, isTerraFirmaCraft ? 1 : 0); + builder.put(EDhApiWorldGenerationStep.SURFACE, 0); + builder.put(EDhApiWorldGenerationStep.CARVERS, 0); + builder.put(EDhApiWorldGenerationStep.LIQUID_CARVERS, 0); + builder.put(EDhApiWorldGenerationStep.FEATURES, 0); + builder.put(EDhApiWorldGenerationStep.LIGHT, 0); + BorderNeeded = builder.build(); + MaxBorderNeeded = BorderNeeded.values().stream().mapToInt(Integer::intValue).max().getAsInt(); } public BatchGenerationEnvironment(IDhServerLevel serverlevel) @@ -219,12 +260,17 @@ public final class BatchGenerationEnvironment extends AbstractBatchGenerationEnv if (generator.getClass().toString().equals("class com.terraforged.mod.chunk.TFChunkGenerator")) { EVENT_LOGGER.info("TerraForge Chunk Generator detected: ["+generator.getClass()+"], Distant Generation will try its best to support it."); - EVENT_LOGGER.info("If it does crash, turn Distant Generation off or set it to to ["+EDhApiWorldGenerationStep.EMPTY+"]."); + EVENT_LOGGER.info("If it does crash, turn Distant Generation off or set it to to ["+EDhApiDistantGeneratorMode.PRE_EXISTING_ONLY+"]."); + } + else if (generator.getClass().toString().equals("class net.dries007.tfc.world.TFCChunkGenerator")) + { + EVENT_LOGGER.info("TerraFirmaCraft Chunk Generator detected: ["+generator.getClass()+"], Distant Generation will try its best to support it."); + EVENT_LOGGER.info("If it does crash, turn Distant Generation off or set it to to ["+ EDhApiDistantGeneratorMode.PRE_EXISTING_ONLY +"]."); } else { EVENT_LOGGER.warn("Unknown Chunk Generator detected: ["+generator.getClass()+"], Distant Generation May Fail!"); - EVENT_LOGGER.warn("If it does crash, disable Distant Generation or set the Generation Mode to ["+EDhApiWorldGenerationStep.EMPTY+"]."); + EVENT_LOGGER.warn("If it does crash, disable Distant Generation or set the Generation Mode to ["+EDhApiDistantGeneratorMode.PRE_EXISTING_ONLY+"]."); } } @@ -357,20 +403,29 @@ public final class BatchGenerationEnvironment extends AbstractBatchGenerationEnv } } } - + + private static ArrayGridList GetCutoutFrom(ArrayGridList total, int border) { + return new ArrayGridList<>(total, border, total.gridSize - border); + } + + private static ArrayGridList GetCutoutFrom(ArrayGridList total, EDhApiWorldGenerationStep step) { + return GetCutoutFrom(total, MaxBorderNeeded - BorderNeeded.get(step)); + } + public void generateLodFromList(GenerationEvent genEvent) throws InterruptedException { EVENT_LOGGER.debug("Lod Generate Event: "+genEvent.minPos); - ArrayGridList referencedChunks; - ArrayGridList genChunks; + ArrayGridList totalChunks; + ArrayGridList finalGenChunks; LightedWorldGenRegion region; WorldGenLevelLightEngine lightEngine; LightGetterAdaptor adaptor; - - int refSize = genEvent.size+2; // +2 for the border referenced chunks - int refPosX = genEvent.minPos.x - 1; // -1 for the border referenced chunks - int refPosZ = genEvent.minPos.z - 1; // -1 for the border referenced chunks + + int borderSize = MaxBorderNeeded; + int refSize = genEvent.size + borderSize * 2; + int refPosX = genEvent.minPos.x - borderSize; + int refPosZ = genEvent.minPos.z - borderSize; try { @@ -399,17 +454,15 @@ public final class BatchGenerationEnvironment extends AbstractBatchGenerationEnv } return target; }; - - referencedChunks = new ArrayGridList<>(refSize, (x,z) -> generator.generate(x + refPosX,z + refPosZ)); + + totalChunks = new ArrayGridList<>(refSize, (x,z) -> generator.generate(x + refPosX,z + refPosZ)); genEvent.refreshTimeout(); - region = new LightedWorldGenRegion(params.level, lightEngine, referencedChunks, + region = new LightedWorldGenRegion(params.level, lightEngine, totalChunks, ChunkStatus.STRUCTURE_STARTS, refSize/2, generator); adaptor.setRegion(region); genEvent.threadedParam.makeStructFeat(region, params); - genChunks = new ArrayGridList<>(referencedChunks, RANGE_TO_RANGE_EMPTY_EXTENSION, - referencedChunks.gridSize - RANGE_TO_RANGE_EMPTY_EXTENSION); - this.generateDirect(genEvent, genChunks, genEvent.targetGenerationStep, region); + this.generateDirect(genEvent, totalChunks, borderSize, genEvent.targetGenerationStep, region); genEvent.timer.nextEvent("cleanup"); } catch (StepStructureStart.StructStartCorruptedException f) @@ -417,12 +470,13 @@ public final class BatchGenerationEnvironment extends AbstractBatchGenerationEnv genEvent.threadedParam.markAsInvalid(); throw (RuntimeException)f.getCause(); } - - for (int offsetY = 0; offsetY < genChunks.gridSize; offsetY++) + + finalGenChunks = GetCutoutFrom(totalChunks, borderSize); + for (int offsetY = 0; offsetY < finalGenChunks.gridSize; offsetY++) { - for (int offsetX = 0; offsetX < genChunks.gridSize; offsetX++) + for (int offsetX = 0; offsetX < finalGenChunks.gridSize; offsetX++) { - ChunkAccess target = genChunks.get(offsetX, offsetY); + ChunkAccess target = finalGenChunks.get(offsetX, offsetY); ChunkWrapper wrappedChunk = new ChunkWrapper(target, region, null); if (target instanceof LevelChunk) { ((LevelChunk) target).loaded = true; @@ -472,7 +526,7 @@ public final class BatchGenerationEnvironment extends AbstractBatchGenerationEnv } } - public void generateDirect(GenerationEvent genEvent, ArrayGridList chunksToGenerate, + public void generateDirect(GenerationEvent genEvent, ArrayGridList chunksToGenerate, int border, EDhApiWorldGenerationStep step, LightedWorldGenRegion region) throws InterruptedException { if (Thread.interrupted()) @@ -500,7 +554,7 @@ public final class BatchGenerationEnvironment extends AbstractBatchGenerationEnv genEvent.timer.nextEvent("structStart"); throwIfThreadInterrupted(); - stepStructureStart.generateGroup(genEvent.threadedParam, region, chunksToGenerate); + stepStructureStart.generateGroup(genEvent.threadedParam, region, GetCutoutFrom(chunksToGenerate, EDhApiWorldGenerationStep.STRUCTURE_START)); genEvent.refreshTimeout(); if (step == EDhApiWorldGenerationStep.STRUCTURE_START) { @@ -509,7 +563,7 @@ public final class BatchGenerationEnvironment extends AbstractBatchGenerationEnv genEvent.timer.nextEvent("structRef"); throwIfThreadInterrupted(); - stepStructureReference.generateGroup(genEvent.threadedParam, region, chunksToGenerate); + stepStructureReference.generateGroup(genEvent.threadedParam, region, GetCutoutFrom(chunksToGenerate, EDhApiWorldGenerationStep.STRUCTURE_REFERENCE)); genEvent.refreshTimeout(); if (step == EDhApiWorldGenerationStep.STRUCTURE_REFERENCE) { @@ -518,7 +572,7 @@ public final class BatchGenerationEnvironment extends AbstractBatchGenerationEnv genEvent.timer.nextEvent("biome"); throwIfThreadInterrupted(); - stepBiomes.generateGroup(genEvent.threadedParam, region, chunksToGenerate); + stepBiomes.generateGroup(genEvent.threadedParam, region, GetCutoutFrom(chunksToGenerate, EDhApiWorldGenerationStep.BIOMES)); genEvent.refreshTimeout(); if (step == EDhApiWorldGenerationStep.BIOMES) { @@ -527,7 +581,7 @@ public final class BatchGenerationEnvironment extends AbstractBatchGenerationEnv genEvent.timer.nextEvent("noise"); throwIfThreadInterrupted(); - stepNoise.generateGroup(genEvent.threadedParam, region, chunksToGenerate); + stepNoise.generateGroup(genEvent.threadedParam, region, GetCutoutFrom(chunksToGenerate, EDhApiWorldGenerationStep.NOISE)); genEvent.refreshTimeout(); if (step == EDhApiWorldGenerationStep.NOISE) { @@ -536,7 +590,7 @@ public final class BatchGenerationEnvironment extends AbstractBatchGenerationEnv genEvent.timer.nextEvent("surface"); throwIfThreadInterrupted(); - stepSurface.generateGroup(genEvent.threadedParam, region, chunksToGenerate); + stepSurface.generateGroup(genEvent.threadedParam, region, GetCutoutFrom(chunksToGenerate, EDhApiWorldGenerationStep.SURFACE)); genEvent.refreshTimeout(); if (step == EDhApiWorldGenerationStep.SURFACE) { @@ -552,7 +606,7 @@ public final class BatchGenerationEnvironment extends AbstractBatchGenerationEnv genEvent.timer.nextEvent("feature"); throwIfThreadInterrupted(); - stepFeatures.generateGroup(genEvent.threadedParam, region, chunksToGenerate); + stepFeatures.generateGroup(genEvent.threadedParam, region, GetCutoutFrom(chunksToGenerate, EDhApiWorldGenerationStep.FEATURES)); genEvent.refreshTimeout(); } finally @@ -566,7 +620,7 @@ public final class BatchGenerationEnvironment extends AbstractBatchGenerationEnv if (!Thread.interrupted()) { - this.stepLight.generateGroup(region.getLightEngine(), chunksToGenerate); + this.stepLight.generateGroup(region.getLightEngine(), GetCutoutFrom(chunksToGenerate, EDhApiWorldGenerationStep.LIGHT)); } } else diff --git a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/mimicObject/LightedWorldGenRegion.java b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/mimicObject/LightedWorldGenRegion.java index 315ac4e42..9e8bcf453 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/mimicObject/LightedWorldGenRegion.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/mimicObject/LightedWorldGenRegion.java @@ -208,7 +208,7 @@ public class LightedWorldGenRegion extends WorldGenRegion } // Use this instead of super.hasChunk() to bypass C2ME concurrency checks - private boolean superHasChunk(int x, int z) { + public boolean superHasChunk(int x, int z) { int k = x - firstPos.x; int l = z - firstPos.z; return l >= 0 && l < size && k >= 0 && k < size; diff --git a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/step/StepFeatures.java b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/step/StepFeatures.java index 7c1dd8857..fc5566c84 100644 --- a/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/step/StepFeatures.java +++ b/common/src/main/java/com/seibel/distanthorizons/common/wrappers/worldGeneration/step/StepFeatures.java @@ -66,10 +66,12 @@ public final class StepFeatures { worldGenRegion.setOverrideCenter(chunk.getPos()); environment.params.generator.applyBiomeDecoration(worldGenRegion, tParams.structFeat); Heightmap.primeHeightmaps(chunk, STATUS.heightmapsAfter()); + BatchGenerationEnvironment.clearDistantGenerationMixinData(); #else environment.params.generator.applyBiomeDecoration(worldGenRegion, chunk, tParams.structFeat.forWorldGenRegion(worldGenRegion)); Heightmap.primeHeightmaps(chunk, STATUS.heightmapsAfter()); + BatchGenerationEnvironment.clearDistantGenerationMixinData(); #endif } catch (ReportedException e) { e.printStackTrace(); diff --git a/coreSubProjects b/coreSubProjects index 10b916118..ea4000602 160000 --- a/coreSubProjects +++ b/coreSubProjects @@ -1 +1 @@ -Subproject commit 10b9161188deb2e676d611d6d38bbbbfad79990e +Subproject commit ea400060236c06ef8d878abd7535ad871c855058 diff --git a/fabric/src/main/java/com/seibel/distanthorizons/fabric/mixins/client/MixinGameRenderer.java b/fabric/src/main/java/com/seibel/distanthorizons/fabric/mixins/client/MixinGameRenderer.java index 54356697f..c2bc8da9d 100644 --- a/fabric/src/main/java/com/seibel/distanthorizons/fabric/mixins/client/MixinGameRenderer.java +++ b/fabric/src/main/java/com/seibel/distanthorizons/fabric/mixins/client/MixinGameRenderer.java @@ -1,5 +1,6 @@ package com.seibel.distanthorizons.fabric.mixins.client; +import com.seibel.distanthorizons.common.wrappers.DependencySetupDoneCheck; import com.seibel.distanthorizons.core.api.internal.ClientApi; import net.minecraft.client.renderer.GameRenderer; import org.apache.logging.log4j.LogManager; @@ -18,14 +19,22 @@ public class MixinGameRenderer #if POST_MC_1_17_1 @Inject(method = "shutdownShaders", at = @At("HEAD")) public void onShutdownShaders(CallbackInfo ci) { - LOGGER.info("Shutting down renderer"); + LOGGER.info("Shutting down renderer (fabric)"); + if (!DependencySetupDoneCheck.isDone) { + LOGGER.warn("Dependency setup is not done yet, skipping renderer this shutdown event!"); + return; + } ClientApi.INSTANCE.rendererShutdownEvent(); } // FIXME: This I think will dup multiple renderStartupEvent calls... - @Inject(method = {"reloadShaders", "preloadUiShader", "preloadShader"}, at = @At("TAIL")) + @Inject(method = {"reloadShaders", "preloadUiShader"}, at = @At("TAIL")) public void onStartupShaders(CallbackInfo ci) { - LOGGER.info("Starting up renderer"); + LOGGER.info("Starting up renderer (fabric)"); + if (!DependencySetupDoneCheck.isDone) { + LOGGER.warn("Dependency setup is not done yet, skipping renderer this startup event!"); + return; + } ClientApi.INSTANCE.rendererStartupEvent(); } #else diff --git a/forge/build.gradle b/forge/build.gradle index e7bf2df9b..4bd1293e4 100644 --- a/forge/build.gradle +++ b/forge/build.gradle @@ -85,6 +85,8 @@ dependencies { addMod("curse.maven:TerraForged-363820:${rootProject.terraforged_version}", rootProject.enable_terraforged) + addMod("curse.maven:TerraFirmaCraft-302973:4616004", rootProject.enable_terrafirmacraft) + // if (System.getProperty("idea.sync.active") != "true") { // annotationProcessor "org.spongepowered:mixin:0.8.4:processor" // } diff --git a/forge/src/main/java/com/seibel/distanthorizons/forge/mixins/client/MixinGameRenderer.java b/forge/src/main/java/com/seibel/distanthorizons/forge/mixins/client/MixinGameRenderer.java index 98e1ac33e..1c39a9204 100644 --- a/forge/src/main/java/com/seibel/distanthorizons/forge/mixins/client/MixinGameRenderer.java +++ b/forge/src/main/java/com/seibel/distanthorizons/forge/mixins/client/MixinGameRenderer.java @@ -1,5 +1,6 @@ package com.seibel.distanthorizons.forge.mixins.client; +import com.seibel.distanthorizons.common.wrappers.DependencySetupDoneCheck; import com.seibel.distanthorizons.core.api.internal.ClientApi; import net.minecraft.client.renderer.GameRenderer; import org.apache.logging.log4j.LogManager; @@ -18,14 +19,22 @@ public class MixinGameRenderer #if POST_MC_1_17_1 @Inject(method = "shutdownShaders", at = @At("HEAD")) public void onShutdownShaders(CallbackInfo ci) { - LOGGER.info("Shutting down renderer"); + LOGGER.info("Shutting down renderer (forge)"); + if (!DependencySetupDoneCheck.isDone) { + LOGGER.warn("Dependency setup is not done yet, skipping renderer this shutdown event!"); + return; + } ClientApi.INSTANCE.rendererShutdownEvent(); } // FIXME: This I think will dup multiple renderStartupEvent calls... - @Inject(method = {"reloadShaders", "preloadUiShader", "preloadShader"}, at = @At("TAIL")) + @Inject(method = {"reloadShaders", "preloadUiShader"}, at = @At("TAIL")) public void onStartupShaders(CallbackInfo ci) { - LOGGER.info("Starting up renderer"); + LOGGER.info("Starting up renderer (forge)"); + if (!DependencySetupDoneCheck.isDone) { + LOGGER.warn("Dependency setup is not done yet, skipping renderer this startup event!"); + return; + } ClientApi.INSTANCE.rendererStartupEvent(); } #else diff --git a/versionProperties/1.18.2.properties b/versionProperties/1.18.2.properties index 38daea648..86f293243 100644 --- a/versionProperties/1.18.2.properties +++ b/versionProperties/1.18.2.properties @@ -54,3 +54,4 @@ forge_version=40.2.9 # 2 = Can be referenced in code and runs in client enable_starlight_forge=0 enable_terraforged=0 + enable_terrafirmacraft=0 \ No newline at end of file From 79513b6c525f47b4522bd22ad35c052f064ed527 Mon Sep 17 00:00:00 2001 From: TomTheFurry Date: Sun, 2 Jul 2023 18:33:28 +0800 Subject: [PATCH 2/2] Fix build issues --- versionProperties/1.16.5.properties | 1 + versionProperties/1.17.1.properties | 1 + versionProperties/1.19.2.properties | 1 + versionProperties/1.19.4.properties | 1 + versionProperties/1.20.1.properties | 1 + 5 files changed, 5 insertions(+) diff --git a/versionProperties/1.16.5.properties b/versionProperties/1.16.5.properties index 946897212..159d07d0a 100644 --- a/versionProperties/1.16.5.properties +++ b/versionProperties/1.16.5.properties @@ -45,3 +45,4 @@ forge_version=36.2.39 # 2 = Can be referenced in code and runs in client enable_starlight_forge=0 enable_terraforged=2 + enable_terrafirmacraft=0 diff --git a/versionProperties/1.17.1.properties b/versionProperties/1.17.1.properties index ddc76d7b7..42f12deae 100644 --- a/versionProperties/1.17.1.properties +++ b/versionProperties/1.17.1.properties @@ -45,3 +45,4 @@ forge_version=37.1.1 # 2 = Can be referenced in code and runs in client enable_starlight_forge=0 enable_terraforged=0 + enable_terrafirmacraft=0 diff --git a/versionProperties/1.19.2.properties b/versionProperties/1.19.2.properties index 136714073..103e1129a 100644 --- a/versionProperties/1.19.2.properties +++ b/versionProperties/1.19.2.properties @@ -45,3 +45,4 @@ forge_version=43.2.14 # 2 = Can be referenced in code and runs in client enable_starlight_forge=0 enable_terraforged=0 + enable_terrafirmacraft=0 diff --git a/versionProperties/1.19.4.properties b/versionProperties/1.19.4.properties index 0b9afab72..349c47c62 100644 --- a/versionProperties/1.19.4.properties +++ b/versionProperties/1.19.4.properties @@ -45,3 +45,4 @@ forge_version=45.1.0 # 2 = Can be referenced in code and runs in client enable_starlight_forge=0 enable_terraforged=0 + enable_terrafirmacraft=0 diff --git a/versionProperties/1.20.1.properties b/versionProperties/1.20.1.properties index 1d0955bd9..c55c542a0 100644 --- a/versionProperties/1.20.1.properties +++ b/versionProperties/1.20.1.properties @@ -45,3 +45,4 @@ forge_version=47.0.1 # 2 = Can be referenced in code and runs in client enable_starlight_forge=0 enable_terraforged=0 + enable_terrafirmacraft=0