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

This commit is contained in:
embeddedt 2023-12-27 19:06:49 -05:00
commit 6f0d6e473f
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
2 changed files with 34 additions and 2 deletions

View File

@ -21,6 +21,7 @@ import org.embeddedt.modernfix.util.ForwardingInclDefaultsMap;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.function.BiFunction;
/**
* Stores a list of all known default block/item models in the game, and provides a namespaced version
@ -28,7 +29,11 @@ import java.util.*;
*/
public class ModelBakeEventHelper {
// TODO: make into config option
private static final Set<String> INCOMPATIBLE_MODS = ImmutableSet.of("industrialforegoing", "vampirism", "elevatorid");
private static final Set<String> INCOMPATIBLE_MODS = ImmutableSet.of(
"industrialforegoing",
"mekanism",
"vampirism",
"elevatorid");
private final Map<ResourceLocation, BakedModel> modelRegistry;
private final Set<ResourceLocation> topLevelModelLocations;
private final MutableGraph<String> dependencyGraph;
@ -79,7 +84,7 @@ public class ModelBakeEventHelper {
private void logWarning() {
if(!WARNED_MOD_IDS.add(modId))
return;
ModernFix.LOGGER.warn("Mod '{}' is accessing Map#keySet/entrySet/values on the model registry map inside its event handler." +
ModernFix.LOGGER.warn("Mod '{}' is accessing Map#keySet/entrySet/values/replaceAll on the model registry map inside its event handler." +
" This probably won't work as expected with dynamic resources on. Prefer using Map#get/put and constructing ModelResourceLocations another way.", modId);
}
@ -100,6 +105,12 @@ public class ModelBakeEventHelper {
logWarning();
return super.values();
}
@Override
public void replaceAll(BiFunction<? super ResourceLocation, ? super BakedModel, ? extends BakedModel> function) {
logWarning();
super.replaceAll(function);
}
};
}
@ -139,6 +150,19 @@ public class ModelBakeEventHelper {
public boolean containsKey(@Nullable Object key) {
return ourModelLocations.contains(key) || super.containsKey(key);
}
@Override
public void replaceAll(BiFunction<? super ResourceLocation, ? super BakedModel, ? extends BakedModel> function) {
ModernFix.LOGGER.warn("Mod '{}' is calling replaceAll on the model registry. This requires temporarily loading every model for that mod, which is slow.", modId);
List<ResourceLocation> locations = new ArrayList<>(keySet());
for(ResourceLocation location : locations) {
BakedModel existing = get(location);
BakedModel replacement = function.apply(location, existing);
if(replacement != existing) {
put(location, replacement);
}
}
}
};
}
}

View File

@ -1,5 +1,6 @@
package org.embeddedt.modernfix.forge.mixin.perf.dynamic_resources;
import com.google.common.base.Stopwatch;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.client.ForgeHooksClient;
@ -9,6 +10,7 @@ import net.minecraftforge.fml.ModContainer;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.ModLoader;
import net.minecraftforge.fml.util.ObfuscationReflectionHelper;
import org.embeddedt.modernfix.ModernFix;
import org.embeddedt.modernfix.forge.dynresources.ModelBakeEventHelper;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
@ -16,6 +18,7 @@ import org.spongepowered.asm.mixin.injection.Redirect;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.TimeUnit;
@Mixin(ForgeHooksClient.class)
public class ForgeHooksClientMixin {
@ -32,11 +35,16 @@ public class ForgeHooksClientMixin {
ModList.get().forEachModContainer((id, mc) -> {
Map<ResourceLocation, BakedModel> newRegistry = helper.wrapRegistry(id);
ModelBakeEvent postedEvent = new ModelBakeEvent(bakeEvent.getModelManager(), newRegistry, bakeEvent.getModelLoader());
Stopwatch timer = Stopwatch.createStarted();
try {
acceptEv.invoke(mc, postedEvent);
} catch(ReflectiveOperationException e) {
e.printStackTrace();
}
timer.stop();
if(timer.elapsed(TimeUnit.SECONDS) >= 1) {
ModernFix.LOGGER.warn("Mod '{}' took {} in the model bake event", id, timer);
}
});
}
}