create registry sanity check

This commit is contained in:
dfsek
2022-06-20 23:25:03 -07:00
parent a0225d6ece
commit e1656bac20
3 changed files with 73 additions and 38 deletions
@@ -7,6 +7,8 @@ import java.io.IOException;
import java.nio.file.FileVisitOption; import java.nio.file.FileVisitOption;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import java.util.jar.JarFile; import java.util.jar.JarFile;
import java.util.stream.Stream; import java.util.stream.Stream;
@@ -23,13 +25,12 @@ public final class AwfulForgeHacks {
* needed. * needed.
* *
* <code> * <code>
* Class.class.getProtectionDomain() * Class.class.getProtectionDomain()
* .getCodeSource() * .getCodeSource()
* .getLocation() * .getLocation()
* .toURI() * .toURI()
* .getPath() * .getPath()
* </code> * </code>
*
*/ */
public static JarFile getTerraJar() throws IOException { public static JarFile getTerraJar() throws IOException {
LOGGER.info("Scanning for Terra JAR..."); LOGGER.info("Scanning for Terra JAR...");
@@ -59,22 +60,48 @@ public final class AwfulForgeHacks {
try(JarFile jar = getTerraJar()) { try(JarFile jar = getTerraJar()) {
jar.stream() jar.stream()
.forEach(jarEntry -> { .forEach(jarEntry -> {
if(jarEntry.getName().startsWith("com/dfsek/terra/forge/mixin")) { if(jarEntry.getName().startsWith("com/dfsek/terra/forge/mixin")) {
return; return;
} }
if(jarEntry.getName().endsWith(".class")) { if(jarEntry.getName().endsWith(".class")) {
String name = jarEntry.getName().replace('/', '.'); String name = jarEntry.getName().replace('/', '.');
name = name.substring(0, name.length() - 6); name = name.substring(0, name.length() - 6);
try { try {
Class.forName(name); Class.forName(name);
} catch(ClassNotFoundException | NoClassDefFoundError e) { } catch(ClassNotFoundException | NoClassDefFoundError e) {
LOGGER.warn("Failed to load class {}: {}", name, e); LOGGER.warn("Failed to load class {}: {}", name, e);
} }
} }
}); });
} catch(IOException e) { } catch(IOException e) {
throw new IllegalStateException("Could not load all Terra classes", e); throw new IllegalStateException("Could not load all Terra classes", e);
} }
} }
public enum RegistryStep {
BLOCK,
BIOME,
WORLD_TYPE,
DONE;
}
public static class RegistrySanityCheck {
private final AtomicReference<RegistryStep> step = new AtomicReference<>(RegistryStep.BLOCK);
public <T> void progress(RegistryStep expected, Runnable action) {
step.getAndUpdate(s -> {
if(s != expected) {
LOGGER.error("Registry sanity check failed, expected to find {}, found {}", expected, step);
}
action.run();
RegistryStep[] registrySteps = RegistryStep.values();
if(s.ordinal() < registrySteps.length - 1) {
return registrySteps[s.ordinal() + 1];
}
return s;
});
}
}
} }
@@ -17,6 +17,9 @@
package com.dfsek.terra.forge; package com.dfsek.terra.forge;
import com.dfsek.terra.forge.AwfulForgeHacks.RegistrySanityCheck;
import com.dfsek.terra.forge.AwfulForgeHacks.RegistryStep;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry; import net.minecraft.util.registry.Registry;
import net.minecraftforge.eventbus.api.EventPriority; import net.minecraftforge.eventbus.api.EventPriority;
@@ -25,9 +28,7 @@ import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber; import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus; import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.ForgeRegistries.Keys; import net.minecraftforge.registries.ForgeRegistries.Keys;
import net.minecraftforge.registries.RegisterEvent; import net.minecraftforge.registries.RegisterEvent;
import org.slf4j.Logger; import org.slf4j.Logger;
@@ -36,9 +37,14 @@ import org.slf4j.LoggerFactory;
import com.dfsek.terra.forge.data.Codecs; import com.dfsek.terra.forge.data.Codecs;
import com.dfsek.terra.forge.util.LifecycleUtil; import com.dfsek.terra.forge.util.LifecycleUtil;
import java.util.concurrent.atomic.AtomicReference;
@Mod("terra") @Mod("terra")
@EventBusSubscriber(bus = Bus.MOD) @EventBusSubscriber(bus = Bus.MOD)
public class ForgeEntryPoint { public class ForgeEntryPoint {
private final RegistrySanityCheck sanityCheck = new RegistrySanityCheck();
static { static {
AwfulForgeHacks.loadAllTerraClasses(); AwfulForgeHacks.loadAllTerraClasses();
TERRA_PLUGIN = new PlatformImpl(); TERRA_PLUGIN = new PlatformImpl();
@@ -61,12 +67,12 @@ public class ForgeEntryPoint {
@SubscribeEvent(priority = EventPriority.LOWEST) @SubscribeEvent(priority = EventPriority.LOWEST)
public void registerBiomes(RegisterEvent event) { public void registerBiomes(RegisterEvent event) {
event.register(Keys.BIOMES, helper -> { event.register(Keys.BLOCKS, helper -> sanityCheck.progress(RegistryStep.BLOCK, () -> logger.debug("Block registration detected.")));
logger.info("Loading Terra data..."); event.register(Keys.BIOMES, helper -> sanityCheck.progress(RegistryStep.BIOME, LifecycleUtil::initialize));
LifecycleUtil.initialize(); event.register(Registry.WORLD_PRESET_KEY, helper -> sanityCheck.progress(RegistryStep.WORLD_TYPE, () -> LifecycleUtil.registerWorldTypes(helper)));
});
event.register(Registry.CHUNK_GENERATOR_KEY, helper -> helper.register(new Identifier("terra:terra"), Codecs.FABRIC_CHUNK_GENERATOR_WRAPPER)); event.register(Registry.CHUNK_GENERATOR_KEY, helper -> helper.register(new Identifier("terra:terra"), Codecs.FABRIC_CHUNK_GENERATOR_WRAPPER));
event.register(Registry.BIOME_SOURCE_KEY, helper -> helper.register(new Identifier("terra:terra"), Codecs.TERRA_BIOME_SOURCE)); event.register(Registry.BIOME_SOURCE_KEY, helper -> helper.register(new Identifier("terra:terra"), Codecs.TERRA_BIOME_SOURCE));
event.register(Keys.BLOCKS, helper -> logger.debug("Block registration detected."));
} }
} }
@@ -21,6 +21,7 @@ import net.minecraft.world.gen.WorldPreset;
import net.minecraft.world.gen.chunk.ChunkGenerator; import net.minecraft.world.gen.chunk.ChunkGenerator;
import net.minecraft.world.gen.chunk.ChunkGeneratorSettings; import net.minecraft.world.gen.chunk.ChunkGeneratorSettings;
import net.minecraft.world.gen.chunk.NoiseChunkGenerator; import net.minecraft.world.gen.chunk.NoiseChunkGenerator;
import net.minecraftforge.registries.RegisterEvent.RegisterHelper;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -38,8 +39,9 @@ public class LifecycleUtil {
ForgeEntryPoint.getPlatform().getEventManager().callEvent( ForgeEntryPoint.getPlatform().getEventManager().callEvent(
new PlatformInitializationEvent()); new PlatformInitializationEvent());
BiomeUtil.registerBiomes(); BiomeUtil.registerBiomes();
}
public static void registerWorldTypes(RegisterHelper<WorldPreset> helper) {
LOGGER.info("Registering Terra world types..."); LOGGER.info("Registering Terra world types...");
Registry<DimensionType> dimensionTypeRegistry = BuiltinRegistries.DIMENSION_TYPE; Registry<DimensionType> dimensionTypeRegistry = BuiltinRegistries.DIMENSION_TYPE;
@@ -88,7 +90,7 @@ public class LifecycleUtil {
DimensionOptions.END, endDimensionOptions DimensionOptions.END, endDimensionOptions
) )
); );
BuiltinRegistries.add(BuiltinRegistries.WORLD_PRESET, generatorID, preset); helper.register(generatorID, preset);
LOGGER.info("Registered world type \"{}\"", generatorID); LOGGER.info("Registered world type \"{}\"", generatorID);
} }
); );