Merge remote-tracking branch 'origin/main' into 1.18

This commit is contained in:
embeddedt 2023-05-21 19:17:39 -04:00
commit 7517c36b36
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
18 changed files with 306 additions and 134 deletions

View File

@ -11,6 +11,8 @@ import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.util.MemoryReserve; import net.minecraft.util.MemoryReserve;
import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.Entity;
import org.embeddedt.modernfix.api.constants.IntegrationConstants;
import org.embeddedt.modernfix.api.entrypoint.ModernFixClientIntegration;
import org.embeddedt.modernfix.core.ModernFixMixinPlugin; import org.embeddedt.modernfix.core.ModernFixMixinPlugin;
import org.embeddedt.modernfix.packet.EntityIDSyncPacket; import org.embeddedt.modernfix.packet.EntityIDSyncPacket;
import org.embeddedt.modernfix.platform.ModernFixPlatformHooks; import org.embeddedt.modernfix.platform.ModernFixPlatformHooks;
@ -33,6 +35,11 @@ public class ModernFixClient {
public String brandingString = null; public String brandingString = null;
/**
* The list of loaded client integrations.
*/
public static List<ModernFixClientIntegration> CLIENT_INTEGRATIONS = new ArrayList<>();
public ModernFixClient() { public ModernFixClient() {
// clear reserve as it's not needed // clear reserve as it's not needed
MemoryReserve.release(); MemoryReserve.release();
@ -41,6 +48,13 @@ public class ModernFixClient {
} }
SearchTreeProviderRegistry.register(JEIBackedSearchTree.PROVIDER); SearchTreeProviderRegistry.register(JEIBackedSearchTree.PROVIDER);
SearchTreeProviderRegistry.register(REIBackedSearchTree.PROVIDER); SearchTreeProviderRegistry.register(REIBackedSearchTree.PROVIDER);
for(String className : ModernFixPlatformHooks.getCustomModOptions().get(IntegrationConstants.CLIENT_INTEGRATION_CLASS)) {
try {
CLIENT_INTEGRATIONS.add((ModernFixClientIntegration)Class.forName(className).getDeclaredConstructor().newInstance());
} catch(ReflectiveOperationException | ClassCastException e) {
ModernFix.LOGGER.error("Could not instantiate integration {}", className, e);
}
}
} }
public void resetWorldLoadStateMachine() { public void resetWorldLoadStateMachine() {

View File

@ -0,0 +1,8 @@
package org.embeddedt.modernfix.api.constants;
public class IntegrationConstants {
public static final String INTEGRATIONS_KEY = "modernfix:integration";
public static final String CLIENT_INTEGRATION_CLASS = "client_entrypoint";
public static final String INTEGRATION_CLASS = "entrypoint";
}

View File

@ -0,0 +1,49 @@
package org.embeddedt.modernfix.api.entrypoint;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.ModelBakery;
import net.minecraft.client.resources.model.ModelState;
import net.minecraft.client.resources.model.UnbakedModel;
import net.minecraft.resources.ResourceLocation;
/**
* Implement this interface in a mod class and add it to "modernfix:integration_v1" in your mod metadata file
* to integrate with ModernFix's features.
*/
public interface ModernFixClientIntegration {
/**
* Called when the dynamic resources status has changed during a model reload so mods know whether to run their
* normal codepath or the dynamic version.
*
* @param enabled whether dynamic resources is enabled
*/
default void onDynamicResourcesStatusChange(boolean enabled) {
}
/**
* Called to allow mods to observe the loading of an unbaked model and either make changes to it or wrap it with their
* own instance.
* @param location the ResourceLocation of the model (this may be a ModelResourceLocation)
* @param originalModel the original model
* @param bakery the model bakery - do not touch internal fields as they probably don't behave the way you expect
* with dynamic resources on
* @return the model which should actually be loaded for this resource location
*/
default UnbakedModel onUnbakedModelLoad(ResourceLocation location, UnbakedModel originalModel, ModelBakery bakery) {
return originalModel;
}
/**
* Called to allow mods to observe the loading of a baked model and either make changes to it or wrap it with their
* own instance.
* @param location the ResourceLocation of the model (this may be a ModelResourceLocation)
* @param originalModel the original model
* @param bakery the model bakery - do not touch internal fields as they probably don't behave the way you expect
* with dynamic resources on
* @return the model which should actually be loaded for this resource location
*/
default BakedModel onBakedModelLoad(ResourceLocation location, UnbakedModel baseModel, BakedModel originalModel, ModelState state, ModelBakery bakery) {
return originalModel;
}
}

View File

@ -0,0 +1,55 @@
package org.embeddedt.modernfix.api.helpers;
import com.google.common.collect.ImmutableList;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.BlockModelRotation;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.client.resources.model.ModelState;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateDefinition;
import org.embeddedt.modernfix.dynamicresources.ModelBakeryHelpers;
import org.embeddedt.modernfix.util.DynamicMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiFunction;
@SuppressWarnings("unused")
public final class ModelHelpers {
/**
* Allows converting a ModelResourceLocation back into the corresponding BlockState(s). Try to avoid calling this
* multiple times if possible.
* @param location the location of the model
* @return a list of all blockstates related to the model
*/
public static ImmutableList<BlockState> getBlockStateForLocation(ModelResourceLocation location) {
Optional<Block> blockOpt = Registry.BLOCK.getOptional(new ResourceLocation(location.getNamespace(), location.getPath()));
if(blockOpt.isPresent())
return ModelBakeryHelpers.getBlockStatesForMRL(blockOpt.get().getStateDefinition(), location);
else
return ImmutableList.of();
}
/**
* Allows converting a ModelResourceLocation back into the corresponding BlockState(s). Faster version of its
* companion function if and only if you know the corresponding Block already for some reason.
* @param definition the state definition for the Block
* @param location the location of the model
* @return a list of all blockstates related to the model
*/
public static ImmutableList<BlockState> getBlockStateForLocation(StateDefinition<Block, BlockState> definition, ModelResourceLocation location) {
return ModelBakeryHelpers.getBlockStatesForMRL(definition, location);
}
/**
* Compatibility helper for mods to use to get a map-like view of the model bakery.
* @param modelGetter the model getter function supplied by the integration class
* @return a fake map of the top-level models
*/
public static Map<ResourceLocation, BakedModel> createFakeTopLevelMap(BiFunction<ResourceLocation, ModelState, BakedModel> modelGetter) {
return new DynamicMap<>(location -> modelGetter.apply(location, BlockModelRotation.X0_Y0));
}
}

View File

@ -20,6 +20,8 @@ public class ModernFixMixinPlugin implements IMixinConfigPlugin {
public static ModernFixMixinPlugin instance; public static ModernFixMixinPlugin instance;
public ModernFixMixinPlugin() { public ModernFixMixinPlugin() {
/* invoke early to ensure it gets read on one thread */
ModernFixPlatformHooks.getCustomModOptions();
boolean firstConfig = instance == null; boolean firstConfig = instance == null;
if(firstConfig) { if(firstConfig) {
instance = this; instance = this;

View File

@ -183,8 +183,9 @@ public class ModernFixEarlyConfig {
disableIfModPresent("mixin.bugfix.paper_chunk_patches", "c2me"); disableIfModPresent("mixin.bugfix.paper_chunk_patches", "c2me");
disableIfModPresent("mixin.perf.reuse_datapacks", "tac"); disableIfModPresent("mixin.perf.reuse_datapacks", "tac");
disableIfModPresent("mixin.launch.class_search_cache", "optifine"); disableIfModPresent("mixin.launch.class_search_cache", "optifine");
disableIfModPresent("mixin.perf.faster_texture_stitching", "optifine");
disableIfModPresent("mixin.perf.datapack_reload_exceptions", "cyanide"); disableIfModPresent("mixin.perf.datapack_reload_exceptions", "cyanide");
disableIfModPresent("mixin.perf.faster_texture_loading", "stitch"); disableIfModPresent("mixin.perf.faster_texture_loading", "stitch", "optifine");
} }
private void disableIfModPresent(String configName, String... ids) { private void disableIfModPresent(String configName, String... ids) {

View File

@ -1,5 +1,6 @@
package org.embeddedt.modernfix.platform; package org.embeddedt.modernfix.platform;
import com.google.common.collect.Multimap;
import com.mojang.blaze3d.platform.NativeImage; import com.mojang.blaze3d.platform.NativeImage;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import dev.architectury.injectables.annotations.ExpectPlatform; import dev.architectury.injectables.annotations.ExpectPlatform;
@ -85,4 +86,9 @@ public class ModernFixPlatformHooks {
public static void onServerCommandRegister(Consumer<CommandDispatcher<CommandSourceStack>> handler) { public static void onServerCommandRegister(Consumer<CommandDispatcher<CommandSourceStack>> handler) {
throw new AssertionError(); throw new AssertionError();
} }
@ExpectPlatform
public static Multimap<String, String> getCustomModOptions() {
throw new AssertionError();
}
} }

View File

@ -37,6 +37,7 @@ dependencies {
modImplementation(fabricApi.module("fabric-resource-loader-v0", rootProject.fabric_api_version)) { exclude group: 'net.fabricmc', module: 'fabric-loader' } modImplementation(fabricApi.module("fabric-resource-loader-v0", rootProject.fabric_api_version)) { exclude group: 'net.fabricmc', module: 'fabric-loader' }
modImplementation(fabricApi.module("fabric-data-generation-api-v1", rootProject.fabric_api_version)) { exclude group: 'net.fabricmc', module: 'fabric-loader' } modImplementation(fabricApi.module("fabric-data-generation-api-v1", rootProject.fabric_api_version)) { exclude group: 'net.fabricmc', module: 'fabric-loader' }
modCompileOnly("com.terraformersmc:modmenu:${rootProject.modmenu_version}") { transitive false } modCompileOnly("com.terraformersmc:modmenu:${rootProject.modmenu_version}") { transitive false }
modRuntimeOnly("net.fabricmc.fabric-api:fabric-api:${rootProject.fabric_api_version}") { exclude group: 'net.fabricmc', module: 'fabric-loader' }
// Remove the next line if you don't want to depend on the API // Remove the next line if you don't want to depend on the API
// modApi "me.shedaniel:architectury-fabric:${rootProject.architectury_version}" // modApi "me.shedaniel:architectury-fabric:${rootProject.architectury_version}"

View File

@ -247,6 +247,7 @@ public abstract class ModelBakeryMixin implements IExtendedModelBakery {
"entity/chest", "entity/chest",
"item", "item",
"items", "items",
"part",
"pipe", "pipe",
"ropebridge" "ropebridge"
}; };
@ -269,6 +270,14 @@ public abstract class ModelBakeryMixin implements IExtendedModelBakery {
return materialsSet; return materialsSet;
} }
@Inject(method = "<init>", at = @At(value = "INVOKE_STRING", target = "Lnet/minecraft/util/profiling/ProfilerFiller;popPush(Ljava/lang/String;)V", args = "ldc=textures"))
private void clearDummyModels(CallbackInfo ci) {
// discard unwrapped models
Predicate<Map.Entry<ResourceLocation, UnbakedModel>> isVanillaModel = entry -> entry.getValue() instanceof BlockModel || entry.getValue() instanceof MultiVariant || entry.getValue() instanceof MultiPart;
this.unbakedCache.entrySet().removeIf(isVanillaModel);
this.topLevelModels.entrySet().removeIf(isVanillaModel);
}
@Inject(method = "uploadTextures", at = @At(value = "FIELD", target = "Lnet/minecraft/client/resources/model/ModelBakery;topLevelModels:Ljava/util/Map;", ordinal = 0), cancellable = true) @Inject(method = "uploadTextures", at = @At(value = "FIELD", target = "Lnet/minecraft/client/resources/model/ModelBakery;topLevelModels:Ljava/util/Map;", ordinal = 0), cancellable = true)
private void skipBake(TextureManager resourceManager, ProfilerFiller profiler, CallbackInfoReturnable<AtlasSet> cir) { private void skipBake(TextureManager resourceManager, ProfilerFiller profiler, CallbackInfoReturnable<AtlasSet> cir) {
profiler.pop(); profiler.pop();
@ -290,10 +299,6 @@ public abstract class ModelBakeryMixin implements IExtendedModelBakery {
return super.put(key, value); return super.put(key, value);
} }
}; };
// discard unwrapped models
Predicate<Map.Entry<ResourceLocation, UnbakedModel>> isVanillaModel = entry -> entry.getValue() instanceof BlockModel || entry.getValue() instanceof MultiVariant || entry.getValue() instanceof MultiPart;
this.unbakedCache.entrySet().removeIf(isVanillaModel);
this.topLevelModels.entrySet().removeIf(isVanillaModel);
// bake indigo models // bake indigo models
Stopwatch watch = Stopwatch.createStarted(); Stopwatch watch = Stopwatch.createStarted();
this.topLevelModels.forEach((key, value) -> { this.topLevelModels.forEach((key, value) -> {

View File

@ -1,11 +1,15 @@
package org.embeddedt.modernfix.platform.fabric; package org.embeddedt.modernfix.platform.fabric;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.mojang.blaze3d.platform.NativeImage; import com.mojang.blaze3d.platform.NativeImage;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import net.fabricmc.api.EnvType; import net.fabricmc.api.EnvType;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback; import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import net.fabricmc.loader.api.FabricLoader; import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer; import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.api.metadata.CustomValue;
import net.fabricmc.loader.api.metadata.ModMetadata;
import net.minecraft.client.renderer.texture.TextureAtlas; import net.minecraft.client.renderer.texture.TextureAtlas;
import net.minecraft.client.renderer.texture.TextureAtlasSprite; import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.CommandSourceStack;
@ -14,6 +18,7 @@ import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.packs.resources.Resource; import net.minecraft.server.packs.resources.Resource;
import net.minecraft.server.packs.resources.ResourceManager; import net.minecraft.server.packs.resources.ResourceManager;
import org.embeddedt.modernfix.ModernFixFabric; import org.embeddedt.modernfix.ModernFixFabric;
import org.embeddedt.modernfix.api.constants.IntegrationConstants;
import org.objectweb.asm.tree.*; import org.objectweb.asm.tree.*;
import java.nio.file.Path; import java.nio.file.Path;
@ -82,4 +87,26 @@ public class ModernFixPlatformHooksImpl {
public static void onServerCommandRegister(Consumer<CommandDispatcher<CommandSourceStack>> handler) { public static void onServerCommandRegister(Consumer<CommandDispatcher<CommandSourceStack>> handler) {
CommandRegistrationCallback.EVENT.register((dispatcher, arg) -> handler.accept(dispatcher)); CommandRegistrationCallback.EVENT.register((dispatcher, arg) -> handler.accept(dispatcher));
} }
private static Multimap<String, String> modOptions;
public static Multimap<String, String> getCustomModOptions() {
if(modOptions == null) {
modOptions = ArrayListMultimap.create();
for (ModContainer container : FabricLoader.getInstance().getAllMods()) {
ModMetadata meta = container.getMetadata();
if (meta.containsCustomValue(IntegrationConstants.INTEGRATIONS_KEY)) {
CustomValue integrations = meta.getCustomValue(IntegrationConstants.INTEGRATIONS_KEY);
if (integrations.getType() != CustomValue.CvType.OBJECT) {
continue;
}
for (Map.Entry<String, CustomValue> entry : integrations.getAsObject()) {
if(entry.getValue().getType() != CustomValue.CvType.STRING)
continue;
modOptions.put(entry.getKey(), entry.getValue().getAsString());
}
}
}
}
return modOptions;
}
} }

View File

@ -1,47 +0,0 @@
package org.embeddedt.modernfix.forge.dynamicresources;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.UnbakedModel;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.client.model.ForgeModelBakery;
import net.minecraftforge.eventbus.api.Event;
/**
* Fired when a model is baked dynamically. Intended to be used as a replacement for ModelBakeEvent
* if mods want to replace a model.
* <p></p>
* Note that this event can fire many times for the same resource location, as models are unloaded
* if unused/under memory pressure.
*/
public class DynamicModelBakeEvent extends Event {
private final ResourceLocation location;
private BakedModel model;
private final UnbakedModel unbakedModel;
private final ForgeModelBakery modelLoader;
public DynamicModelBakeEvent(ResourceLocation location, UnbakedModel unbakedModel, BakedModel model, ForgeModelBakery loader) {
this.location = location;
this.model = model;
this.unbakedModel = unbakedModel;
this.modelLoader = loader;
}
public ResourceLocation getLocation() {
return this.location;
}
public BakedModel getModel() {
return this.model;
}
public UnbakedModel getUnbakedModel() {
return this.unbakedModel;
}
public ForgeModelBakery getModelLoader() {
return this.modelLoader;
}
public void setModel(BakedModel model) {
this.model = model;
}
}

View File

@ -28,24 +28,22 @@ import net.minecraft.world.level.block.state.properties.Property;
import net.minecraftforge.client.ForgeHooksClient; import net.minecraftforge.client.ForgeHooksClient;
import net.minecraftforge.client.model.ForgeModelBakery; import net.minecraftforge.client.model.ForgeModelBakery;
import net.minecraftforge.client.model.ModelLoaderRegistry; import net.minecraftforge.client.model.ModelLoaderRegistry;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.resource.DelegatingResourcePack; import net.minecraftforge.resource.DelegatingResourcePack;
import net.minecraftforge.resource.PathResourcePack; import net.minecraftforge.resource.PathResourcePack;
import org.apache.commons.lang3.tuple.Triple; import org.apache.commons.lang3.tuple.Triple;
import org.embeddedt.modernfix.ModernFix; import org.embeddedt.modernfix.ModernFix;
import org.embeddedt.modernfix.ModernFixClient;
import org.embeddedt.modernfix.annotation.ClientOnlyMixin; import org.embeddedt.modernfix.annotation.ClientOnlyMixin;
import org.embeddedt.modernfix.api.entrypoint.ModernFixClientIntegration;
import org.embeddedt.modernfix.duck.IExtendedModelBakery; import org.embeddedt.modernfix.duck.IExtendedModelBakery;
import org.embeddedt.modernfix.dynamicresources.DynamicBakedModelProvider; import org.embeddedt.modernfix.dynamicresources.DynamicBakedModelProvider;
import org.embeddedt.modernfix.forge.dynamicresources.DynamicModelBakeEvent;
import org.embeddedt.modernfix.dynamicresources.ModelBakeryHelpers; import org.embeddedt.modernfix.dynamicresources.ModelBakeryHelpers;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable; import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.*;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@ -89,6 +87,8 @@ public abstract class ModelBakeryMixin implements IExtendedModelBakery {
@Shadow @Nullable public abstract BakedModel bake(ResourceLocation arg, ModelState arg2, Function<Material, TextureAtlasSprite> sprites); @Shadow @Nullable public abstract BakedModel bake(ResourceLocation arg, ModelState arg2, Function<Material, TextureAtlasSprite> sprites);
@Shadow @Final private static Logger LOGGER; @Shadow @Final private static Logger LOGGER;
@Shadow @org.jetbrains.annotations.Nullable public abstract BakedModel bake(ResourceLocation location, ModelState transform);
private Cache<Triple<ResourceLocation, Transformation, Boolean>, BakedModel> loadedBakedModels; private Cache<Triple<ResourceLocation, Transformation, Boolean>, BakedModel> loadedBakedModels;
private Cache<ResourceLocation, UnbakedModel> loadedModels; private Cache<ResourceLocation, UnbakedModel> loadedModels;
@ -216,6 +216,18 @@ public abstract class ModelBakeryMixin implements IExtendedModelBakery {
return unbakedCache.get(rl); return unbakedCache.get(rl);
} }
@ModifyVariable(method = "cacheAndQueueDependencies", at = @At("HEAD"), argsOnly = true)
private UnbakedModel fireUnbakedEvent(UnbakedModel model, ResourceLocation location) {
for(ModernFixClientIntegration integration : ModernFixClient.CLIENT_INTEGRATIONS) {
try {
model = integration.onUnbakedModelLoad(location, model, (ModelBakery)(Object)this);
} catch(RuntimeException e) {
ModernFix.LOGGER.error("Exception firing model load event for {}", location, e);
}
}
return model;
}
@Inject(method = "cacheAndQueueDependencies", at = @At("RETURN")) @Inject(method = "cacheAndQueueDependencies", at = @At("RETURN"))
private void addToSmallLoadingCache(ResourceLocation location, UnbakedModel model, CallbackInfo ci) { private void addToSmallLoadingCache(ResourceLocation location, UnbakedModel model, CallbackInfo ci) {
smallLoadingCache.put(location, model); smallLoadingCache.put(location, model);
@ -328,10 +340,15 @@ public abstract class ModelBakeryMixin implements IExtendedModelBakery {
} else } else
ibakedmodel = iunbakedmodel.bake((ModelBakery) (Object) this, textureGetter, arg2, arg); ibakedmodel = iunbakedmodel.bake((ModelBakery) (Object) this, textureGetter, arg2, arg);
} }
DynamicModelBakeEvent event = new DynamicModelBakeEvent(arg, iunbakedmodel, ibakedmodel, (ForgeModelBakery)(Object)this); for(ModernFixClientIntegration integration : ModernFixClient.CLIENT_INTEGRATIONS) {
MinecraftForge.EVENT_BUS.post(event); try {
this.bakedCache.put(triple, event.getModel()); ibakedmodel = integration.onBakedModelLoad(arg, iunbakedmodel, ibakedmodel, arg2, (ForgeModelBakery)(Object)this);
cir.setReturnValue(event.getModel()); } catch(RuntimeException e) {
ModernFix.LOGGER.error("Exception encountered firing bake event for {}", arg, e);
}
}
this.bakedCache.put(triple, ibakedmodel);
cir.setReturnValue(ibakedmodel);
} }
} }
} }

View File

@ -4,11 +4,13 @@ import appeng.core.AppEng;
import appeng.init.client.InitAutoRotatingModel; import appeng.init.client.InitAutoRotatingModel;
import net.minecraft.client.resources.model.BakedModel; import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.ModelBakery; import net.minecraft.client.resources.model.ModelBakery;
import net.minecraftforge.common.MinecraftForge;
import org.embeddedt.modernfix.annotation.ClientOnlyMixin; import org.embeddedt.modernfix.annotation.ClientOnlyMixin;
import org.embeddedt.modernfix.annotation.RequiresMod; import org.embeddedt.modernfix.annotation.RequiresMod;
import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Final;
import org.embeddedt.modernfix.forge.dynamicresources.DynamicModelBakeEvent; import net.minecraft.client.resources.model.*;
import net.minecraft.resources.ResourceLocation;
import org.embeddedt.modernfix.ModernFixClient;
import org.embeddedt.modernfix.api.entrypoint.ModernFixClientIntegration;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
@ -23,20 +25,22 @@ import java.util.function.Function;
@ClientOnlyMixin @ClientOnlyMixin
public class RegistrationMixin { public class RegistrationMixin {
@Shadow @Final private static Map<String, Function<BakedModel, BakedModel>> CUSTOMIZERS; @Shadow @Final private static Map<String, Function<BakedModel, BakedModel>> CUSTOMIZERS;
@Inject(method = "init", at = @At("TAIL"), remap = false)
private static void doRegisterDynBake(CallbackInfo ci) {
MinecraftForge.EVENT_BUS.addListener(RegistrationMixin::onDynamicModelBake);
}
private static void onDynamicModelBake(DynamicModelBakeEvent event) { @Inject(method = "init", at = @At("TAIL"), remap = false)
if (!event.getLocation().getNamespace().equals(AppEng.MOD_ID)) { private void doRegisterDynBake(CallbackInfo ci) {
return; ModernFixClient.CLIENT_INTEGRATIONS.add(new ModernFixClientIntegration() {
} @Override
BakedModel missing = event.getModelLoader().getBakedTopLevelModels().get(ModelBakery.MISSING_MODEL_LOCATION); public BakedModel onBakedModelLoad(ResourceLocation location, UnbakedModel baseModel, BakedModel originalModel, ModelState state, ModelBakery bakery) {
if(event.getModel() == missing) if(location.getNamespace().equals(AppEng.MOD_ID)) {
return; BakedModel m = bakery.bake(ModelBakery.MISSING_MODEL_LOCATION, BlockModelRotation.X0_Y0);
Function<BakedModel, BakedModel> customizerFn = CUSTOMIZERS.get(event.getLocation().getPath()); if(originalModel == m)
if(customizerFn != null) return originalModel;
event.setModel(customizerFn.apply(event.getModel())); Function<BakedModel, BakedModel> customizerFn = CUSTOMIZERS.get(location.getPath());
if(customizerFn != null)
originalModel = customizerFn.apply(originalModel);
}
return originalModel;
}
});
} }
} }

View File

@ -3,23 +3,19 @@ package org.embeddedt.modernfix.forge.mixin.perf.dynamic_resources.ctm;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import net.minecraft.client.renderer.ItemBlockRenderTypes; import net.minecraft.client.renderer.ItemBlockRenderTypes;
import net.minecraft.client.renderer.RenderType; import net.minecraft.client.renderer.RenderType;
import net.minecraft.client.resources.model.BakedModel; import net.minecraft.client.resources.model.*;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.client.resources.model.MultiPartBakedModel;
import net.minecraft.client.resources.model.WeightedBakedModel;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks; import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.IRegistryDelegate; import net.minecraftforge.registries.IRegistryDelegate;
import org.embeddedt.modernfix.ModernFix; import org.embeddedt.modernfix.ModernFix;
import org.embeddedt.modernfix.ModernFixClient;
import org.embeddedt.modernfix.annotation.ClientOnlyMixin; import org.embeddedt.modernfix.annotation.ClientOnlyMixin;
import org.embeddedt.modernfix.annotation.RequiresMod; import org.embeddedt.modernfix.annotation.RequiresMod;
import org.embeddedt.modernfix.duck.IExtendedModelBakery; import org.embeddedt.modernfix.api.entrypoint.ModernFixClientIntegration;
import org.embeddedt.modernfix.forge.dynamicresources.DynamicModelBakeEvent; import org.embeddedt.modernfix.api.helpers.ModelHelpers;
import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite; import org.spongepowered.asm.mixin.Overwrite;
@ -37,7 +33,7 @@ import java.util.function.Predicate;
@Mixin(CTMPackReloadListener.class) @Mixin(CTMPackReloadListener.class)
@RequiresMod("ctm") @RequiresMod("ctm")
@ClientOnlyMixin @ClientOnlyMixin
public abstract class CTMPackReloadListenerMixin { public abstract class CTMPackReloadListenerMixin implements ModernFixClientIntegration {
/* caches the original render checks */ /* caches the original render checks */
@Shadow @Final private static Map<IRegistryDelegate<Block>, Predicate<RenderType>> blockRenderChecks; @Shadow @Final private static Map<IRegistryDelegate<Block>, Predicate<RenderType>> blockRenderChecks;
@ -51,7 +47,7 @@ public abstract class CTMPackReloadListenerMixin {
@Inject(method = "<init>", at = @At("RETURN")) @Inject(method = "<init>", at = @At("RETURN"))
private void onInit(CallbackInfo ci) { private void onInit(CallbackInfo ci) {
MinecraftForge.EVENT_BUS.addListener(EventPriority.LOW, this::onModelBake); ModernFixClient.CLIENT_INTEGRATIONS.add(this);
} }
@Overwrite(remap = false) @Overwrite(remap = false)
@ -75,27 +71,31 @@ public abstract class CTMPackReloadListenerMixin {
return override.test(type); return override.test(type);
} }
private void onModelBake(DynamicModelBakeEvent event) { @Override
if(!(event.getModel() instanceof AbstractCTMBakedModel || event.getModel() instanceof WeightedBakedModel || event.getModel() instanceof MultiPartBakedModel)) public BakedModel onBakedModelLoad(ResourceLocation location, UnbakedModel baseModel, BakedModel originalModel, ModelState modelState, ModelBakery bakery) {
return; if(!(location instanceof ModelResourceLocation))
return originalModel;
if(!(originalModel instanceof AbstractCTMBakedModel || originalModel instanceof WeightedBakedModel || originalModel instanceof MultiPartBakedModel))
return originalModel;
/* we construct a new ResourceLocation because an MRL is coming in */ /* we construct a new ResourceLocation because an MRL is coming in */
Block block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(event.getLocation().getNamespace(), event.getLocation().getPath())); Block block = ForgeRegistries.BLOCKS.getValue(new ResourceLocation(location.getNamespace(), location.getPath()));
if(block == null || block == Blocks.AIR || renderCheckOverrides.containsKey(block.delegate)) if(block == null || block == Blocks.AIR || renderCheckOverrides.containsKey(block.delegate))
return; return originalModel;
/* find all states that match this MRL */ /* find all states that match this MRL */
ImmutableList<BlockState> allStates; ImmutableList<BlockState> allStates;
try { try {
allStates = ((IExtendedModelBakery)(Object)event.getModelLoader()).getBlockStatesForMRL(block.getStateDefinition(), (ModelResourceLocation)event.getLocation()); allStates = ModelHelpers.getBlockStateForLocation(block.getStateDefinition(), (ModelResourceLocation)location);
} catch(RuntimeException e) { } catch(RuntimeException e) {
ModernFix.LOGGER.error("Couldn't get state for MRL " + event.getLocation(), e); ModernFix.LOGGER.error("Couldn't get state for MRL " + location, e);
return; return originalModel;
} }
for(BlockState state : allStates) { for(BlockState state : allStates) {
Predicate<RenderType> newPredicate = this.getLayerCheck(state, event.getModel()); Predicate<RenderType> newPredicate = this.getLayerCheck(state, originalModel);
if(newPredicate != null) { if(newPredicate != null) {
renderCheckOverrides.put(block.delegate, newPredicate); renderCheckOverrides.put(block.delegate, newPredicate);
return; return originalModel;
} }
} }
return originalModel;
} }
} }

View File

@ -1,15 +1,13 @@
package org.embeddedt.modernfix.forge.mixin.perf.dynamic_resources.ctm; package org.embeddedt.modernfix.forge.mixin.perf.dynamic_resources.ctm;
import com.mojang.datafixers.util.Pair; import com.mojang.datafixers.util.Pair;
import net.minecraft.client.resources.model.BakedModel; import net.minecraft.client.resources.model.*;
import net.minecraft.client.resources.model.Material;
import net.minecraft.client.resources.model.UnbakedModel;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.client.model.ForgeModelBakery; import net.minecraftforge.client.model.ForgeModelBakery;
import net.minecraftforge.common.MinecraftForge; import org.embeddedt.modernfix.ModernFixClient;
import org.embeddedt.modernfix.annotation.ClientOnlyMixin; import org.embeddedt.modernfix.annotation.ClientOnlyMixin;
import org.embeddedt.modernfix.annotation.RequiresMod; import org.embeddedt.modernfix.annotation.RequiresMod;
import org.embeddedt.modernfix.forge.dynamicresources.DynamicModelBakeEvent; import org.embeddedt.modernfix.api.entrypoint.ModernFixClientIntegration;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.At;
@ -29,13 +27,13 @@ import java.util.*;
@Mixin(TextureMetadataHandler.class) @Mixin(TextureMetadataHandler.class)
@RequiresMod("ctm") @RequiresMod("ctm")
@ClientOnlyMixin @ClientOnlyMixin
public abstract class TextureMetadataHandlerMixin { public abstract class TextureMetadataHandlerMixin implements ModernFixClientIntegration {
@Shadow @Nonnull protected abstract BakedModel wrap(ResourceLocation loc, UnbakedModel model, BakedModel object, ForgeModelBakery loader) throws IOException; @Shadow @Nonnull protected abstract BakedModel wrap(ResourceLocation loc, UnbakedModel model, BakedModel object, ForgeModelBakery loader) throws IOException;
@Inject(method = "<init>", at = @At("RETURN")) @Inject(method = "<init>", at = @At("RETURN"))
private void subscribeDynamic(CallbackInfo ci) { private void subscribeDynamic(CallbackInfo ci) {
MinecraftForge.EVENT_BUS.addListener(this::onDynamicModelBake); ModernFixClient.CLIENT_INTEGRATIONS.add(this);
} }
@Redirect(method = "onModelBake", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/resources/model/BakedModel;isCustomRenderer()Z")) @Redirect(method = "onModelBake", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/resources/model/BakedModel;isCustomRenderer()Z"))
@ -43,10 +41,8 @@ public abstract class TextureMetadataHandlerMixin {
return model == null || model.isCustomRenderer(); return model == null || model.isCustomRenderer();
} }
public void onDynamicModelBake(DynamicModelBakeEvent event) { @Override
UnbakedModel rootModel = event.getUnbakedModel(); public BakedModel onBakedModelLoad(ResourceLocation rl, UnbakedModel rootModel, BakedModel baked, ModelState state, ModelBakery bakery) {
BakedModel baked = event.getModel();
ResourceLocation rl = event.getLocation();
if (!(baked instanceof AbstractCTMBakedModel) && !baked.isCustomRenderer()) { if (!(baked instanceof AbstractCTMBakedModel) && !baked.isCustomRenderer()) {
Deque<ResourceLocation> dependencies = new ArrayDeque<>(); Deque<ResourceLocation> dependencies = new ArrayDeque<>();
Set<ResourceLocation> seenModels = new HashSet<>(); Set<ResourceLocation> seenModels = new HashSet<>();
@ -59,12 +55,12 @@ public abstract class TextureMetadataHandlerMixin {
ResourceLocation dep = dependencies.pop(); ResourceLocation dep = dependencies.pop();
UnbakedModel model; UnbakedModel model;
try { try {
model = dep == rl ? rootModel : event.getModelLoader().getModel(dep); model = dep == rl ? rootModel : bakery.getModel(dep);
} catch (Exception e) { } catch (Exception e) {
continue; continue;
} }
Collection<Material> textures = model.getMaterials(event.getModelLoader()::getModel, errors); Collection<Material> textures = model.getMaterials(bakery::getModel, errors);
Collection<ResourceLocation> newDependencies = model.getDependencies(); Collection<ResourceLocation> newDependencies = model.getDependencies();
for (Material tex : textures) { for (Material tex : textures) {
IMetadataSectionCTM meta = null; IMetadataSectionCTM meta = null;
@ -86,12 +82,13 @@ public abstract class TextureMetadataHandlerMixin {
} }
if (shouldWrap) { if (shouldWrap) {
try { try {
event.setModel(wrap(rl, rootModel, baked, event.getModelLoader())); baked = wrap(rl, rootModel, baked, (ForgeModelBakery)bakery);
dependencies.clear(); dependencies.clear();
} catch (IOException e) { } catch (IOException e) {
CTM.logger.error("Could not wrap model " + rl + ". Aborting...", e); CTM.logger.error("Could not wrap model " + rl + ". Aborting...", e);
} }
} }
} }
return baked;
} }
} }

View File

@ -2,12 +2,12 @@ package org.embeddedt.modernfix.forge.mixin.perf.dynamic_resources.rs;
import com.refinedmods.refinedstorage.render.BakedModelOverrideRegistry; import com.refinedmods.refinedstorage.render.BakedModelOverrideRegistry;
import com.refinedmods.refinedstorage.setup.ClientSetup; import com.refinedmods.refinedstorage.setup.ClientSetup;
import net.minecraft.client.resources.model.ModelResourceLocation; import net.minecraft.client.resources.model.*;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.common.MinecraftForge; import org.embeddedt.modernfix.ModernFixClient;
import org.embeddedt.modernfix.annotation.ClientOnlyMixin; import org.embeddedt.modernfix.annotation.ClientOnlyMixin;
import org.embeddedt.modernfix.annotation.RequiresMod; import org.embeddedt.modernfix.annotation.RequiresMod;
import org.embeddedt.modernfix.forge.dynamicresources.DynamicModelBakeEvent; import org.embeddedt.modernfix.api.entrypoint.ModernFixClientIntegration;
import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Shadow;
@ -15,20 +15,24 @@ import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@Mixin(ClientSetup.class) @Mixin(ClientSetup.class)
@RequiresMod("refinedstorage") @RequiresMod("refinedstorage")
@ClientOnlyMixin @ClientOnlyMixin
public class ClientSetupMixin { public class ClientSetupMixin {
@Shadow @Final private static BakedModelOverrideRegistry BAKED_MODEL_OVERRIDE_REGISTRY; @Shadow @Final private static BakedModelOverrideRegistry BAKED_MODEL_OVERRIDE_REGISTRY;
@Inject(method = "onClientSetup", at = @At("RETURN"), remap = false) @Inject(method = "<init>", at = @At("RETURN"))
private static void addDynamicListener(CallbackInfo ci) { private void addDynamicListener(CallbackInfo ci) {
MinecraftForge.EVENT_BUS.addListener(ClientSetupMixin::onDynamicModelBake); ModernFixClient.CLIENT_INTEGRATIONS.add(new ModernFixClientIntegration() {
} @Override
public BakedModel onBakedModelLoad(ResourceLocation location, UnbakedModel baseModel, BakedModel originalModel, ModelState state, ModelBakery bakery) {
private static void onDynamicModelBake(DynamicModelBakeEvent event) { BakedModelOverrideRegistry.BakedModelOverrideFactory factory = BAKED_MODEL_OVERRIDE_REGISTRY.get(location instanceof ModelResourceLocation ? new ResourceLocation(location.getNamespace(), location.getPath()) : location);
BakedModelOverrideRegistry.BakedModelOverrideFactory factory = BAKED_MODEL_OVERRIDE_REGISTRY.get(event.getLocation() instanceof ModelResourceLocation ? new ResourceLocation(event.getLocation().getNamespace(), event.getLocation().getPath()) : event.getLocation()); if(factory != null)
if(factory != null) return factory.create(originalModel, bakery.getBakedTopLevelModels());
event.setModel(factory.create(event.getModel(), event.getModelLoader().getBakedTopLevelModels())); else
return originalModel;
}
});
} }
} }

View File

@ -4,12 +4,14 @@ import com.supermartijn642.core.registry.ClientRegistrationHandler;
import com.supermartijn642.core.util.Pair; import com.supermartijn642.core.util.Pair;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap; import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.client.resources.model.BakedModel; import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.ModelBakery;
import net.minecraft.client.resources.model.ModelState;
import net.minecraft.client.resources.model.UnbakedModel;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.common.MinecraftForge; import org.embeddedt.modernfix.ModernFixClient;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import org.embeddedt.modernfix.annotation.ClientOnlyMixin; import org.embeddedt.modernfix.annotation.ClientOnlyMixin;
import org.embeddedt.modernfix.annotation.RequiresMod; import org.embeddedt.modernfix.annotation.RequiresMod;
import org.embeddedt.modernfix.forge.dynamicresources.DynamicModelBakeEvent; import org.embeddedt.modernfix.api.entrypoint.ModernFixClientIntegration;
import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Shadow;
@ -48,13 +50,15 @@ public class ClientRegistrationHandlerMixin {
@Inject(method = "<init>", at = @At("RETURN")) @Inject(method = "<init>", at = @At("RETURN"))
private void registerDynBake(String modid, CallbackInfo ci) { private void registerDynBake(String modid, CallbackInfo ci) {
MinecraftForge.EVENT_BUS.addListener(this::onDynamicModelBake); ModernFixClient.CLIENT_INTEGRATIONS.add(new ModernFixClientIntegration() {
} @Override
public BakedModel onBakedModelLoad(ResourceLocation location, UnbakedModel baseModel, BakedModel originalModel, ModelState state, ModelBakery bakery) {
@SubscribeEvent Function<BakedModel, BakedModel> replacer = modelOverwritesByLocation.get(location);
public void onDynamicModelBake(DynamicModelBakeEvent event) { if(replacer != null)
Function<BakedModel, BakedModel> replacer = modelOverwritesByLocation.get(event.getLocation()); return replacer.apply(originalModel);
if(replacer != null) else
event.setModel(replacer.apply(event.getModel())); return originalModel;
}
});
} }
} }

View File

@ -1,5 +1,7 @@
package org.embeddedt.modernfix.platform.forge; package org.embeddedt.modernfix.platform.forge;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;
import com.mojang.blaze3d.platform.NativeImage; import com.mojang.blaze3d.platform.NativeImage;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import net.minecraft.client.renderer.texture.TextureAtlas; import net.minecraft.client.renderer.texture.TextureAtlas;
@ -20,7 +22,9 @@ import net.minecraftforge.fml.loading.LoadingModList;
import net.minecraftforge.fml.loading.moddiscovery.ExplodedDirectoryLocator; import net.minecraftforge.fml.loading.moddiscovery.ExplodedDirectoryLocator;
import net.minecraftforge.network.PacketDistributor; import net.minecraftforge.network.PacketDistributor;
import net.minecraftforge.server.ServerLifecycleHooks; import net.minecraftforge.server.ServerLifecycleHooks;
import net.minecraftforge.fml.loading.moddiscovery.ModInfo;
import org.embeddedt.modernfix.core.ModernFixMixinPlugin; import org.embeddedt.modernfix.core.ModernFixMixinPlugin;
import org.embeddedt.modernfix.api.constants.IntegrationConstants;
import org.embeddedt.modernfix.forge.classloading.FastAccessTransformerList; import org.embeddedt.modernfix.forge.classloading.FastAccessTransformerList;
import org.embeddedt.modernfix.forge.packet.PacketHandler; import org.embeddedt.modernfix.forge.packet.PacketHandler;
import org.embeddedt.modernfix.util.DummyList; import org.embeddedt.modernfix.util.DummyList;
@ -32,6 +36,7 @@ import java.lang.invoke.MethodHandles;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Map;
import java.util.function.Consumer; import java.util.function.Consumer;
public class ModernFixPlatformHooksImpl { public class ModernFixPlatformHooksImpl {
@ -144,4 +149,24 @@ public class ModernFixPlatformHooksImpl {
handler.accept(event.getDispatcher()); handler.accept(event.getDispatcher());
}); });
} }
private static Multimap<String, String> modOptions;
public static Multimap<String, String> getCustomModOptions() {
if(modOptions == null) {
modOptions = ArrayListMultimap.create();
for (ModInfo meta : LoadingModList.get().getMods()) {
meta.getConfigElement(IntegrationConstants.INTEGRATIONS_KEY).ifPresent(optionsObj -> {
if(optionsObj instanceof Map) {
Map<Object, Object> options = (Map<Object, Object>)optionsObj;
options.forEach((key, value) -> {
if(key instanceof String && value instanceof String) {
modOptions.put((String)key, (String)value);
}
});
}
});
}
}
return modOptions;
}
} }