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

This commit is contained in:
embeddedt 2023-05-10 18:07:50 -04:00
commit dffb89390e
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
4 changed files with 61 additions and 14 deletions

View File

@ -0,0 +1,27 @@
package org.embeddedt.modernfix.common.mixin.perf.resourcepacks;
import net.minecraft.server.packs.PackResources;
import net.minecraft.server.packs.resources.ReloadableResourceManager;
import net.minecraft.util.Unit;
import org.embeddedt.modernfix.ModernFix;
import org.embeddedt.modernfix.resources.ICachingResourcePack;
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.CallbackInfoReturnable;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
@Mixin(ReloadableResourceManager.class)
public class ReloadableResourceManagerMixin {
@Inject(method = "createReload", at = @At("HEAD"))
private void invalidateResourceCaches(Executor backgroundExecutor, Executor gameExecutor, CompletableFuture<Unit> waitingFor, List<PackResources> resourcePacks, CallbackInfoReturnable<?> cir) {
ModernFix.LOGGER.info("Invalidating pack caches");
for(PackResources pack : resourcePacks) {
if(pack instanceof ICachingResourcePack)
((ICachingResourcePack)pack).invalidateCache();
}
}
}

View File

@ -0,0 +1,5 @@
package org.embeddedt.modernfix.resources;
public interface ICachingResourcePack {
void invalidateCache();
}

View File

@ -1,10 +1,11 @@
package org.embeddedt.modernfix.fabric.mixin.perf.fabric_resourcepacks; package org.embeddedt.modernfix.fabric.mixin.perf.resourcepacks;
import net.fabricmc.fabric.impl.resource.loader.ModNioResourcePack; import net.fabricmc.fabric.impl.resource.loader.ModNioResourcePack;
import net.fabricmc.loader.api.metadata.ModMetadata; import net.fabricmc.loader.api.metadata.ModMetadata;
import net.minecraft.server.packs.PackType; import net.minecraft.server.packs.PackType;
import org.embeddedt.modernfix.ModernFix; import org.embeddedt.modernfix.ModernFix;
import org.embeddedt.modernfix.annotation.RequiresMod; import org.embeddedt.modernfix.annotation.RequiresMod;
import org.embeddedt.modernfix.resources.ICachingResourcePack;
import org.embeddedt.modernfix.resources.PackResourcesCacheEngine; import org.embeddedt.modernfix.resources.PackResourcesCacheEngine;
import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
@ -21,7 +22,7 @@ import java.util.Set;
@Mixin(ModNioResourcePack.class) @Mixin(ModNioResourcePack.class)
@RequiresMod("fabric-resource-loader-v0") @RequiresMod("fabric-resource-loader-v0")
public abstract class ModNioResourcePackMixin { public abstract class ModNioResourcePackMixin implements ICachingResourcePack {
@Shadow public abstract Set<String> getNamespaces(PackType type); @Shadow public abstract Set<String> getNamespaces(PackType type);
@Shadow @Final private List<Path> basePaths; @Shadow @Final private List<Path> basePaths;
@ -30,6 +31,12 @@ public abstract class ModNioResourcePackMixin {
@Inject(method = "<init>", at = @At("RETURN")) @Inject(method = "<init>", at = @At("RETURN"))
private void cacheResources(CallbackInfo ci) { private void cacheResources(CallbackInfo ci) {
invalidateCache();
}
@Override
public void invalidateCache() {
this.cacheEngine = null;
if(this.basePaths.size() == 1) { if(this.basePaths.size() == 1) {
Path basePath = this.basePaths.get(0); Path basePath = this.basePaths.get(0);
this.cacheEngine = new PackResourcesCacheEngine(this::getNamespaces, (type, namespace) -> { this.cacheEngine = new PackResourcesCacheEngine(this::getNamespaces, (type, namespace) -> {

View File

@ -3,6 +3,7 @@ package org.embeddedt.modernfix.forge.mixin.perf.resourcepacks;
import net.minecraft.server.packs.PackType; import net.minecraft.server.packs.PackType;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.resource.PathResourcePack; import net.minecraftforge.resource.PathResourcePack;
import org.embeddedt.modernfix.resources.ICachingResourcePack;
import org.embeddedt.modernfix.resources.PackResourcesCacheEngine; import org.embeddedt.modernfix.resources.PackResourcesCacheEngine;
import org.embeddedt.modernfix.util.PackTypeHelper; import org.embeddedt.modernfix.util.PackTypeHelper;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
@ -17,7 +18,7 @@ import java.util.*;
import java.util.function.Predicate; import java.util.function.Predicate;
@Mixin(PathResourcePack.class) @Mixin(PathResourcePack.class)
public abstract class ModFileResourcePackMixin { public abstract class ModFileResourcePackMixin implements ICachingResourcePack {
@Shadow public abstract Set<String> getNamespaces(PackType type); @Shadow public abstract Set<String> getNamespaces(PackType type);
@Shadow protected abstract Path resolve(String... paths); @Shadow protected abstract Path resolve(String... paths);
@ -29,18 +30,26 @@ public abstract class ModFileResourcePackMixin {
this.cacheEngine = null; this.cacheEngine = null;
} }
private void generateResourceCache() { private PackResourcesCacheEngine generateResourceCache() {
synchronized (this) { synchronized (this) {
if(this.cacheEngine != null) PackResourcesCacheEngine engine = this.cacheEngine;
return; if(engine != null)
this.cacheEngine = new PackResourcesCacheEngine(this::getNamespaces, (type, namespace) -> this.resolve(type.getDirectory(), namespace)); return engine;
this.cacheEngine = engine = new PackResourcesCacheEngine(this::getNamespaces, (type, namespace) -> this.resolve(type.getDirectory(), namespace));
return engine;
} }
} }
@Override
public void invalidateCache() {
this.cacheEngine = null;
}
@Inject(method = "getNamespaces", at = @At("HEAD"), cancellable = true) @Inject(method = "getNamespaces", at = @At("HEAD"), cancellable = true)
private void useCacheForNamespaces(PackType type, CallbackInfoReturnable<Set<String>> cir) { private void useCacheForNamespaces(PackType type, CallbackInfoReturnable<Set<String>> cir) {
if(cacheEngine != null) { PackResourcesCacheEngine engine = cacheEngine;
Set<String> namespaces = cacheEngine.getNamespaces(type); if(engine != null) {
Set<String> namespaces = engine.getNamespaces(type);
if(namespaces != null) if(namespaces != null)
cir.setReturnValue(namespaces); cir.setReturnValue(namespaces);
} }
@ -48,9 +57,9 @@ public abstract class ModFileResourcePackMixin {
@Inject(method = "hasResource(Ljava/lang/String;)Z", at = @At(value = "HEAD"), cancellable = true) @Inject(method = "hasResource(Ljava/lang/String;)Z", at = @At(value = "HEAD"), cancellable = true)
private void useCacheForExistence(String path, CallbackInfoReturnable<Boolean> cir) { private void useCacheForExistence(String path, CallbackInfoReturnable<Boolean> cir) {
this.generateResourceCache(); PackResourcesCacheEngine engine = this.generateResourceCache();
if(cacheEngine != null) if(engine != null)
cir.setReturnValue(this.cacheEngine.hasResource(path)); cir.setReturnValue(engine.hasResource(path));
} }
/** /**
@ -62,7 +71,6 @@ public abstract class ModFileResourcePackMixin {
{ {
if(!PackTypeHelper.isVanillaPackType(type) || this.cacheEngine == null) if(!PackTypeHelper.isVanillaPackType(type) || this.cacheEngine == null)
return; return;
this.generateResourceCache(); cir.setReturnValue(this.generateResourceCache().getResources(type, resourceNamespace, pathIn, maxDepth, filter));
cir.setReturnValue(this.cacheEngine.getResources(type, resourceNamespace, pathIn, maxDepth, filter));
} }
} }