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

This commit is contained in:
embeddedt 2023-05-11 10:39:33 -04:00
commit 2de8f02814
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
5 changed files with 75 additions and 18 deletions

View File

@ -0,0 +1,18 @@
package org.embeddedt.modernfix.common.mixin.perf.resourcepacks;
import net.minecraft.server.packs.resources.ReloadableResourceManager;
import org.embeddedt.modernfix.ModernFix;
import org.embeddedt.modernfix.resources.PackResourcesCacheEngine;
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;
@Mixin(ReloadableResourceManager.class)
public class ReloadableResourceManagerMixin {
@Inject(method = "createReload", at = @At("HEAD"))
private void invalidateResourceCaches(CallbackInfoReturnable<?> cir) {
ModernFix.LOGGER.info("Invalidating pack caches");
PackResourcesCacheEngine.invalidate();
}
}

View File

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

View File

@ -154,4 +154,20 @@ public class PackResourcesCacheEngine {
}
return resources;
}
private static final WeakHashMap<ICachingResourcePack, Boolean> cachingPacks = new WeakHashMap<>();
public static void track(ICachingResourcePack pack) {
synchronized (cachingPacks) {
cachingPacks.put(pack, Boolean.TRUE);
}
}
public static void invalidate() {
synchronized (cachingPacks) {
cachingPacks.keySet().forEach(pack -> {
if(pack != null)
pack.invalidateCache();
});
}
}
}

View File

@ -1,4 +1,4 @@
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.loader.api.metadata.ModMetadata;
@ -6,6 +6,7 @@ import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackType;
import org.embeddedt.modernfix.ModernFix;
import org.embeddedt.modernfix.annotation.RequiresMod;
import org.embeddedt.modernfix.resources.ICachingResourcePack;
import org.embeddedt.modernfix.resources.PackResourcesCacheEngine;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
@ -23,7 +24,7 @@ import java.util.Set;
@Mixin(ModNioResourcePack.class)
@RequiresMod("fabric-resource-loader-v0")
public abstract class ModNioResourcePackMixin {
public abstract class ModNioResourcePackMixin implements ICachingResourcePack {
@Shadow public abstract Set<String> getNamespaces(PackType type);
@Shadow @Final private List<Path> basePaths;
@ -32,6 +33,13 @@ public abstract class ModNioResourcePackMixin {
@Inject(method = "<init>", at = @At("RETURN"))
private void cacheResources(CallbackInfo ci) {
invalidateCache();
PackResourcesCacheEngine.track(this);
}
@Override
public void invalidateCache() {
this.cacheEngine = null;
if(this.basePaths.size() == 1) {
Path basePath = this.basePaths.get(0);
this.cacheEngine = new PackResourcesCacheEngine(this::getNamespaces, (type, namespace) -> {

View File

@ -1,8 +1,9 @@
package org.embeddedt.modernfix.forge.mixin.perf.resourcepacks;
import net.minecraft.server.packs.PackType;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.packs.PackType;
import net.minecraftforge.resource.PathPackResources;
import org.embeddedt.modernfix.resources.ICachingResourcePack;
import org.embeddedt.modernfix.resources.PackResourcesCacheEngine;
import org.embeddedt.modernfix.util.PackTypeHelper;
import org.jetbrains.annotations.NotNull;
@ -13,8 +14,9 @@ import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
import java.nio.file.*;
import java.util.*;
import java.nio.file.Path;
import java.util.Collection;
import java.util.Set;
import java.util.function.Predicate;
/**
@ -23,7 +25,7 @@ import java.util.function.Predicate;
* and 1.18.
*/
@Mixin(PathPackResources.class)
public abstract class ModFileResourcePackMixin {
public abstract class ModFileResourcePackMixin implements ICachingResourcePack {
@Shadow protected abstract Path resolve(String... paths);
@Shadow @NotNull
@ -33,21 +35,30 @@ public abstract class ModFileResourcePackMixin {
@Inject(method = "<init>", at = @At("TAIL"))
private void cacheResources(String packName, Path source, CallbackInfo ci) {
this.cacheEngine = null;
invalidateCache();
PackResourcesCacheEngine.track(this);
}
private void generateResourceCache() {
private PackResourcesCacheEngine generateResourceCache() {
synchronized (this) {
if(this.cacheEngine != null)
return;
this.cacheEngine = new PackResourcesCacheEngine(this::getNamespacesFromDisk, (type, namespace) -> this.resolve(type.getDirectory(), namespace));
PackResourcesCacheEngine engine = this.cacheEngine;
if(engine != null)
return engine;
this.cacheEngine = engine = new PackResourcesCacheEngine(this::getNamespacesFromDisk, (type, namespace) -> this.resolve(type.getDirectory(), namespace));
return engine;
}
}
@Override
public void invalidateCache() {
this.cacheEngine = null;
}
@Inject(method = "getNamespaces", at = @At("HEAD"), cancellable = true)
private void useCacheForNamespaces(PackType type, CallbackInfoReturnable<Set<String>> cir) {
if(cacheEngine != null) {
Set<String> namespaces = cacheEngine.getNamespaces(type);
PackResourcesCacheEngine engine = cacheEngine;
if(engine != null) {
Set<String> namespaces = engine.getNamespaces(type);
if(namespaces != null)
cir.setReturnValue(namespaces);
}
@ -55,9 +66,9 @@ public abstract class ModFileResourcePackMixin {
@Inject(method = "hasResource(Ljava/lang/String;)Z", at = @At(value = "HEAD"), cancellable = true)
private void useCacheForExistence(String path, CallbackInfoReturnable<Boolean> cir) {
this.generateResourceCache();
if(cacheEngine != null)
cir.setReturnValue(this.cacheEngine.hasResource(path));
PackResourcesCacheEngine engine = this.generateResourceCache();
if(engine != null)
cir.setReturnValue(engine.hasResource(path));
}
/**
@ -69,7 +80,6 @@ public abstract class ModFileResourcePackMixin {
{
if(!PackTypeHelper.isVanillaPackType(type) || this.cacheEngine == null)
return;
this.generateResourceCache();
cir.setReturnValue(this.cacheEngine.getResources(type, resourceNamespace, pathIn, Integer.MAX_VALUE, filter));
cir.setReturnValue(this.generateResourceCache().getResources(type, resourceNamespace, pathIn, Integer.MAX_VALUE, filter));
}
}