Like a deer in the headlights

- Freeze registries in FMLLoadCompleteEvent
- Pass registry objects into freeze callback consumers
  - Should make it more difficult to pass a callback to the wrong
    registry
This commit is contained in:
Jozufozu 2024-04-14 22:24:31 -07:00
parent 4cb2cabec8
commit dbf1977c0d
6 changed files with 40 additions and 22 deletions

View file

@ -48,6 +48,7 @@ import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLLoadCompleteEvent;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegisterEvent;
@ -104,6 +105,7 @@ public class Flywheel {
modEventBus.addListener(Flywheel::registerClientReloadListeners);
modEventBus.addListener(Flywheel::onClientSetup);
modEventBus.addListener(Flywheel::onLoadComplete);
modEventBus.addListener(BackendManagerImpl::onEndClientResourceReload);
@ -133,7 +135,9 @@ public class Flywheel {
ShaderIndices.init();
VanillaVisuals.init();
}
private static void onLoadComplete(FMLLoadCompleteEvent event) {
RegistryImpl.freezeAll();
IdRegistryImpl.freezeAll();
}

View file

@ -2,6 +2,7 @@ package com.jozufozu.flywheel.api.registry;
import java.util.Collection;
import java.util.Set;
import java.util.function.Consumer;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;
@ -31,7 +32,7 @@ public interface IdRegistry<T> extends Iterable<T> {
@Unmodifiable
Collection<T> getAll();
void addFreezeCallback(Runnable callback);
void addFreezeCallback(Consumer<IdRegistry<T>> callback);
boolean isFrozen();
}

View file

@ -1,6 +1,7 @@
package com.jozufozu.flywheel.api.registry;
import java.util.Set;
import java.util.function.Consumer;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Unmodifiable;
@ -14,7 +15,7 @@ public interface Registry<T> extends Iterable<T> {
@Unmodifiable
Set<T> getAll();
void addFreezeCallback(Runnable callback);
void addFreezeCallback(Consumer<Registry<T>> callback);
boolean isFrozen();
}

View file

@ -2,11 +2,13 @@ package com.jozufozu.flywheel.backend;
import java.util.List;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
import com.jozufozu.flywheel.api.material.CutoutShader;
import com.jozufozu.flywheel.api.material.FogShader;
import com.jozufozu.flywheel.api.material.MaterialShaders;
import com.jozufozu.flywheel.api.registry.Registry;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntMaps;
@ -17,9 +19,13 @@ import it.unimi.dsi.fastutil.objects.ObjectLists;
import net.minecraft.resources.ResourceLocation;
public final class ShaderIndices {
@Nullable
private static Index vertexShaders;
@Nullable
private static Index fragmentShaders;
@Nullable
private static Index fogShaders;
@Nullable
private static Index cutoutShaders;
private ShaderIndices() {
@ -69,14 +75,14 @@ public final class ShaderIndices {
return cutout().index(cutoutShader.source());
}
private static void initMaterialShaders() {
int amount = MaterialShaders.REGISTRY.getAll()
private static void initMaterialShaders(Registry<MaterialShaders> registry) {
int amount = registry.getAll()
.size();
var vertexShaders = new IndexBuilder(amount);
var fragmentShaders = new IndexBuilder(amount);
for (MaterialShaders shaders : MaterialShaders.REGISTRY) {
for (MaterialShaders shaders : registry) {
vertexShaders.add(shaders.vertexShader());
fragmentShaders.add(shaders.fragmentShader());
}
@ -85,26 +91,26 @@ public final class ShaderIndices {
ShaderIndices.fragmentShaders = fragmentShaders.build();
}
private static void initFogShaders() {
int amount = FogShader.REGISTRY.getAll()
private static void initFogShaders(Registry<FogShader> registry) {
int amount = registry.getAll()
.size();
var fog = new IndexBuilder(amount);
for (FogShader shaders : FogShader.REGISTRY) {
for (FogShader shaders : registry) {
fog.add(shaders.source());
}
ShaderIndices.fogShaders = fog.build();
}
private static void initCutoutShaders() {
int amount = CutoutShader.REGISTRY.getAll()
private static void initCutoutShaders(Registry<CutoutShader> registry) {
int amount = registry.getAll()
.size();
var cutout = new IndexBuilder(amount);
for (CutoutShader shaders : CutoutShader.REGISTRY) {
for (CutoutShader shaders : registry) {
cutout.add(shaders.source());
}

View file

@ -3,6 +3,7 @@ package com.jozufozu.flywheel.impl.registry;
import java.util.Collection;
import java.util.Iterator;
import java.util.Set;
import java.util.function.Consumer;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.Unmodifiable;
@ -10,12 +11,15 @@ import org.jetbrains.annotations.Unmodifiable;
import com.jozufozu.flywheel.api.registry.IdRegistry;
import it.unimi.dsi.fastutil.objects.Object2ReferenceMap;
import it.unimi.dsi.fastutil.objects.Object2ReferenceMaps;
import it.unimi.dsi.fastutil.objects.Object2ReferenceOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import it.unimi.dsi.fastutil.objects.ObjectList;
import it.unimi.dsi.fastutil.objects.ObjectLists;
import it.unimi.dsi.fastutil.objects.ObjectSet;
import it.unimi.dsi.fastutil.objects.ObjectSets;
import it.unimi.dsi.fastutil.objects.Reference2ObjectMap;
import it.unimi.dsi.fastutil.objects.Reference2ObjectMaps;
import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ReferenceCollection;
import it.unimi.dsi.fastutil.objects.ReferenceCollections;
@ -24,11 +28,11 @@ import net.minecraft.resources.ResourceLocation;
public class IdRegistryImpl<T> implements IdRegistry<T> {
private static final ObjectList<IdRegistryImpl<?>> ALL = new ObjectArrayList<>();
private final Object2ReferenceMap<ResourceLocation, T> map = new Object2ReferenceOpenHashMap<>();
private final Reference2ObjectMap<T, ResourceLocation> reverseMap = new Reference2ObjectOpenHashMap<>();
private final Object2ReferenceMap<ResourceLocation, T> map = Object2ReferenceMaps.synchronize(new Object2ReferenceOpenHashMap<>());
private final Reference2ObjectMap<T, ResourceLocation> reverseMap = Reference2ObjectMaps.synchronize(new Reference2ObjectOpenHashMap<>());
private final ObjectSet<ResourceLocation> keysView = ObjectSets.unmodifiable(map.keySet());
private final ReferenceCollection<T> valuesView = ReferenceCollections.unmodifiable(map.values());
private final ObjectList<Runnable> freezeCallbacks = new ObjectArrayList<>();
private final ObjectList<Consumer<IdRegistry<T>>> freezeCallbacks = ObjectLists.synchronize(new ObjectArrayList<>());
private boolean frozen;
public IdRegistryImpl() {
@ -99,7 +103,7 @@ public class IdRegistryImpl<T> implements IdRegistry<T> {
}
@Override
public void addFreezeCallback(Runnable callback) {
public void addFreezeCallback(Consumer<IdRegistry<T>> callback) {
if (frozen) {
throw new IllegalStateException("Cannot add freeze callback to frozen registry!");
}
@ -118,8 +122,8 @@ public class IdRegistryImpl<T> implements IdRegistry<T> {
public void freeze() {
frozen = true;
for (Runnable runnable : freezeCallbacks) {
runnable.run();
for (var callback : freezeCallbacks) {
callback.accept(this);
}
freezeCallbacks.clear();
}

View file

@ -2,6 +2,7 @@ package com.jozufozu.flywheel.impl.registry;
import java.util.Iterator;
import java.util.Set;
import java.util.function.Consumer;
import org.jetbrains.annotations.Unmodifiable;
@ -9,6 +10,7 @@ import com.jozufozu.flywheel.api.registry.Registry;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import it.unimi.dsi.fastutil.objects.ObjectList;
import it.unimi.dsi.fastutil.objects.ObjectLists;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import it.unimi.dsi.fastutil.objects.ObjectSet;
import it.unimi.dsi.fastutil.objects.ObjectSets;
@ -16,9 +18,9 @@ import it.unimi.dsi.fastutil.objects.ObjectSets;
public class RegistryImpl<T> implements Registry<T> {
private static final ObjectList<RegistryImpl<?>> ALL = new ObjectArrayList<>();
private final ObjectSet<T> set = new ObjectOpenHashSet<>();
private final ObjectSet<T> set = ObjectSets.synchronize(new ObjectOpenHashSet<>());
private final ObjectSet<T> setView = ObjectSets.unmodifiable(set);
private final ObjectList<Runnable> freezeCallbacks = new ObjectArrayList<>();
private final ObjectList<Consumer<Registry<T>>> freezeCallbacks = ObjectLists.synchronize(new ObjectArrayList<>());
private boolean frozen;
public RegistryImpl() {
@ -49,7 +51,7 @@ public class RegistryImpl<T> implements Registry<T> {
}
@Override
public void addFreezeCallback(Runnable callback) {
public void addFreezeCallback(Consumer<Registry<T>> callback) {
if (frozen) {
throw new IllegalStateException("Cannot add freeze callback to frozen registry!");
}
@ -68,8 +70,8 @@ public class RegistryImpl<T> implements Registry<T> {
public void freeze() {
frozen = true;
for (Runnable runnable : freezeCallbacks) {
runnable.run();
for (var callback : freezeCallbacks) {
callback.accept(this);
}
freezeCallbacks.clear();
}