Skip to content

Commit

Permalink
Registry Fixes (FabricMC#7)
Browse files Browse the repository at this point in the history
* Regsync Fixes

* Regsync Fixes
  • Loading branch information
shartte authored Oct 20, 2022
1 parent 4d6f4ea commit 1a5b596
Show file tree
Hide file tree
Showing 20 changed files with 112 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@

import org.spongepowered.asm.mixin.MixinEnvironment;

import net.minecraft.test.GameTest;
import net.minecraft.test.TestContext;

//import net.fabricmc.fabric.api.gametest.v1.FabricGameTest;

public class FabricApiBaseGameTest {
// TODO 22w42a
//@GameTest(templateName = FabricGameTest.EMPTY_STRUCTURE)
Expand Down
3 changes: 2 additions & 1 deletion fabric-biome-api-v1/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ loom {

testDependencies(project, [
':fabric-api-base',
':fabric-resource-loader-v0'
':fabric-resource-loader-v0',
':fabric-registry-sync-v0'
])
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.RegistryKey;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.level.LevelProperties;

import net.fabricmc.fabric.api.biome.v1.BiomeModificationContext;
import net.fabricmc.fabric.api.biome.v1.BiomeSelectionContext;
Expand Down Expand Up @@ -103,7 +102,7 @@ private List<ModifierRecord> getSortedModifiers() {
}

@SuppressWarnings("ConstantConditions")
public void finalizeWorldGen(DynamicRegistryManager impl, LevelProperties levelProperties) {
public void finalizeWorldGen(DynamicRegistryManager impl) {
Stopwatch sw = Stopwatch.createStarted();

// Now that we apply biome modifications inside the MinecraftServer constructor, we should only ever do
Expand Down Expand Up @@ -134,7 +133,7 @@ public void finalizeWorldGen(DynamicRegistryManager impl, LevelProperties levelP

// Make a copy of the biome to allow selection contexts to see it unmodified,
// But do so only once it's known anything wants to modify the biome at all
BiomeSelectionContext context = new BiomeSelectionContextImpl(impl, levelProperties, key, biome);
BiomeSelectionContext context = new BiomeSelectionContextImpl(impl, key, biome);
BiomeModificationContextImpl modificationContext = null;

for (ModifierRecord modifier : sortedModifiers) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,18 @@
import net.minecraft.world.gen.feature.ConfiguredFeature;
import net.minecraft.world.gen.feature.PlacedFeature;
import net.minecraft.world.gen.structure.Structure;
import net.minecraft.world.level.LevelProperties;

import net.fabricmc.fabric.api.biome.v1.BiomeSelectionContext;

@ApiStatus.Internal
public class BiomeSelectionContextImpl implements BiomeSelectionContext {
private final DynamicRegistryManager dynamicRegistries;
private final LevelProperties levelProperties;
private final RegistryKey<Biome> key;
private final Biome biome;
private final RegistryEntry<Biome> entry;

public BiomeSelectionContextImpl(DynamicRegistryManager dynamicRegistries, LevelProperties levelProperties, RegistryKey<Biome> key, Biome biome) {
public BiomeSelectionContextImpl(DynamicRegistryManager dynamicRegistries, RegistryKey<Biome> key, Biome biome) {
this.dynamicRegistries = dynamicRegistries;
this.levelProperties = levelProperties;
this.key = key;
this.biome = biome;
this.entry = dynamicRegistries.get(Registry.BIOME_KEY).getEntry(this.key).orElseThrow();
Expand Down Expand Up @@ -96,13 +93,13 @@ public Optional<RegistryKey<Structure>> getStructureKey(Structure structure) {

@Override
public boolean canGenerateIn(RegistryKey<DimensionOptions> dimensionKey) {
DimensionOptions dimension = levelProperties.getGeneratorOptions().getDimensions().get(dimensionKey);
DimensionOptions dimension = dynamicRegistries.get(Registry.DIMENSION_KEY).get(dimensionKey);

if (dimension == null) {
return false;
}

return dimension.getChunkGenerator().getBiomeSource().getBiomes().stream().anyMatch(entry -> entry.value() == biome);
return dimension.chunkGenerator().getBiomeSource().getBiomes().stream().anyMatch(entry -> entry.value() == biome);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package net.fabricmc.fabric.mixin.biome;

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;
Expand All @@ -27,25 +26,24 @@
import net.minecraft.server.WorldGenerationProgressListener;
import net.minecraft.util.registry.DynamicRegistryManager;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.SaveProperties;
import net.minecraft.world.dimension.DimensionOptions;

import net.fabricmc.fabric.impl.biome.NetherBiomeData;

@Mixin(MinecraftServer.class)
public class MinecraftServerMixin {
@Shadow
@Final
protected SaveProperties saveProperties;

@Shadow
@Final
private DynamicRegistryManager.Immutable registryManager;
private DynamicRegistryManager.Immutable getRegistryManager() {
throw new AssertionError();
}

@Inject(method = "createWorlds", at = @At("HEAD"))
private void addNetherBiomes(WorldGenerationProgressListener worldGenerationProgressListener, CallbackInfo ci) {
// This is the last point where we can safely modify worldgen related things
// plus, this is server-side only, and DRM is easily accessible
// please blame Mojang for using dynamic registry
this.saveProperties.getGeneratorOptions().getDimensions().stream().forEach(dimensionOptions -> NetherBiomeData.modifyBiomeSource(this.registryManager.get(Registry.BIOME_KEY), dimensionOptions.getChunkGenerator().getBiomeSource()));
Registry<DimensionOptions> registry = getRegistryManager().get(Registry.DIMENSION_KEY);

registry.stream().forEach(dimensionOptions -> NetherBiomeData.modifyBiomeSource(getRegistryManager().get(Registry.BIOME_KEY), dimensionOptions.chunkGenerator().getBiomeSource()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ private void finalizeWorldGen(CallbackInfo ci) {
throw new RuntimeException("Incompatible SaveProperties passed to MinecraftServer: " + saveProperties);
}

BiomeModificationImpl.INSTANCE.finalizeWorldGen(getRegistryManager(), levelProperties);
BiomeModificationImpl.INSTANCE.finalizeWorldGen(getRegistryManager());
}
}
4 changes: 4 additions & 0 deletions fabric-registry-sync-v0/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
archivesBaseName = "fabric-registry-sync-v0"
version = getSubprojectVersion(project)

loom {
accessWidenerPath = file("src/main/resources/fabric-registry-sync-v0.accesswidener")
}

moduleDependencies(project, [
'fabric-api-base',
'fabric-networking-api-v1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static <T, R extends MutableRegistry<T>> FabricRegistryBuilder<T, R> from
* @return An instance of FabricRegistryBuilder
*/
public static <T> FabricRegistryBuilder<T, SimpleRegistry<T>> createSimple(Class<T> type, Identifier registryId) {
return from(new SimpleRegistry<T>(RegistryKey.ofRegistry(registryId), Lifecycle.stable(), null));
return from(new SimpleRegistry<T>(RegistryKey.ofRegistry(registryId), Lifecycle.stable(), false));
}

/**
Expand All @@ -76,7 +76,7 @@ public static <T> FabricRegistryBuilder<T, SimpleRegistry<T>> createSimple(Class
* @return An instance of FabricRegistryBuilder
*/
public static <T> FabricRegistryBuilder<T, DefaultedRegistry<T>> createDefaulted(Class<T> type, Identifier registryId, Identifier defaultId) {
return from(new DefaultedRegistry<T>(defaultId.toString(), RegistryKey.ofRegistry(registryId), Lifecycle.stable(), null));
return from(new DefaultedRegistry<T>(defaultId.toString(), RegistryKey.ofRegistry(registryId), Lifecycle.stable(), false));
}

private final R registry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import net.minecraft.fluid.Fluids;
import net.minecraft.item.Items;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.SimpleRegistry;
import net.minecraft.world.biome.BiomeKeys;

import net.fabricmc.fabric.impl.registry.sync.RegistrySyncManager;
Expand Down Expand Up @@ -64,6 +65,12 @@ private static void initialize(CallbackInfo info) {

@Redirect(method = "initialize", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/registry/Registry;freezeRegistries()V"))
private static void skipFreeze() {
// Don't freeze
Registry.freezeRegistries();

((SimpleRegistry<?>) Registry.REGISTRIES).frozen = false;

for (Registry<?> registry : Registry.REGISTRIES) {
((SimpleRegistry<?>) registry).frozen = false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.mixin.registry.sync;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.util.registry.BuiltinRegistries;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.SimpleRegistry;

@Mixin(BuiltinRegistries.class)
public class BuiltinRegistriesMixin {
@Inject(method = "<clinit>", at = @At("TAIL"))
private static void unfreezeBultinRegistries(CallbackInfo ci) {
((SimpleRegistry<?>) BuiltinRegistries.REGISTRIES).frozen = false;

for (Registry<?> registry : BuiltinRegistries.REGISTRIES) {
((SimpleRegistry<?>) registry).frozen = false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.server.MinecraftServer;
import net.minecraft.util.registry.BuiltinRegistries;
import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.SimpleRegistry;

import net.fabricmc.api.EnvType;
import net.fabricmc.fabric.impl.registry.sync.trackers.vanilla.BlockInitTracker;
Expand All @@ -41,6 +43,11 @@ private void beforeSetupServer(CallbackInfo info) {
if (FabricLoader.getInstance().getEnvironmentType() == EnvType.SERVER) {
// Freeze the registries on the server
FABRIC_LOGGER.debug("Freezing registries");
BuiltinRegistries.REGISTRIES.freeze();
for (Registry<?> registry : BuiltinRegistries.REGISTRIES) {
((SimpleRegistry<?>) registry).freeze();
}

Registry.freezeRegistries();
BlockInitTracker.postFreeze();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.util.registry.Registry;
import net.minecraft.util.registry.SimpleRegistry;

import net.fabricmc.fabric.api.event.registry.RegistryAttribute;
import net.fabricmc.fabric.api.event.registry.RegistryAttributeHolder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,14 @@ private <V extends T> void add(RegistryKey<Registry<T>> registryKey, V entry, Li
}

@Inject(method = "set", at = @At("RETURN"))
private <V extends T> void set(int rawId, RegistryKey<Registry<T>> registryKey, V entry, Lifecycle lifecycle, CallbackInfoReturnable<V> info) {
private <V extends T> void set(int rawId, RegistryKey<Registry<T>> registryKey, V entry, Lifecycle lifecycle, CallbackInfoReturnable<RegistryEntry<T>> info) {
// We need to restore the 1.19 behavior of binding the value to references immediately.
// Unfrozen registries cannot be interacted with otherwise, because the references would throw when
// trying to access their values.
if (info.getReturnValue() instanceof RegistryEntry.Reference<T> reference) {
reference.method_45918(entry);

}
onChange(registryKey);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.fabricmc.fabric.mixin.registry.sync;

import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessWidener v2 named

accessible field net/minecraft/util/registry/SimpleRegistry frozen Z
accessible method net/minecraft/util/registry/RegistryEntry$Reference method_45918 (Ljava/lang/Object;)V
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"DebugChunkGeneratorAccessor",
"RegistryAccessor",
"BootstrapMixin",
"BuiltinRegistriesMixin",
"ChunkSerializerMixin",
"DynamicRegistryManagerMixin",
"IdListMixin",
Expand Down
1 change: 1 addition & 0 deletions fabric-registry-sync-v0/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"net.fabricmc.fabric.impl.registry.sync.FabricRegistryClientInit"
]
},
"accessWidener": "fabric-registry-sync-v0.accesswidener",
"custom": {
"fabric-api:module-lifecycle": "stable"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,13 @@ private void testBuiltInRegistrySync() {

// Force-Initialize the dynamic registry manager, doing this in a Mod initializer would cause
// further registrations into BuiltInRegistries to _NOT_ propagate into DynamicRegistryManager.BUILTIN
checkFeature(DynamicRegistryManager.createAndLoad(), f1Id);
checkFeature(DynamicRegistryManager.of(Registry.REGISTRIES), f1Id);

ConfiguredFeature<DefaultFeatureConfig, ?> cf2 = new ConfiguredFeature<>(Feature.DESERT_WELL, DefaultFeatureConfig.INSTANCE);
Identifier f2Id = new Identifier("registry_sync", "f2");
Registry.register(BuiltinRegistries.CONFIGURED_FEATURE, f2Id, cf2);

DynamicRegistryManager impl2 = DynamicRegistryManager.createAndLoad();
DynamicRegistryManager impl2 = DynamicRegistryManager.of(Registry.REGISTRIES);
checkFeature(impl2, f1Id);
checkFeature(impl2, f2Id);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
accessWidener v2 named
accessible class net/minecraft/resource/NamespaceResourceManager$class_7682
accessible field net/minecraft/resource/NamespaceResourceManager type Lnet/minecraft/resource/ResourceType;
accessible method net/minecraft/resource/NamespaceResourceManager getMetadataPath (Lnet/minecraft/util/Identifier;)Lnet/minecraft/util/Identifier;
accessible method net/minecraft/resource/NamespaceResourceManager loadMetadata (Lnet/minecraft/resource/InputSupplier;)Lnet/minecraft/resource/metadata/ResourceMetadata;
Expand Down
4 changes: 2 additions & 2 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ rootProject.name = "fabric-api"
include 'fabric-api-base'

//include 'fabric-api-lookup-api-v1'
//include 'fabric-biome-api-v1'
include 'fabric-biome-api-v1'
//include 'fabric-blockrenderlayer-v1'
include 'fabric-command-api-v2'
//include 'fabric-content-registries-v0'
include 'fabric-crash-report-info-v1'
include 'fabric-data-generation-api-v1'
//include 'fabric-data-generation-api-v1'
//include 'fabric-dimensions-v1'
//include 'fabric-entity-events-v1'
//include 'fabric-events-interaction-v0'
Expand Down

0 comments on commit 1a5b596

Please sign in to comment.