Add resource caching to Fabric

This commit is contained in:
embeddedt 2023-05-02 14:44:48 -04:00
parent a1246358ba
commit f274dc1f5f
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
2 changed files with 57 additions and 0 deletions

View File

@ -32,6 +32,7 @@ dependencies {
modIncludeImplementation(fabricApi.module("fabric-api-base", rootProject.fabric_api_version)) { exclude group: 'net.fabricmc', module: 'fabric-loader' }
modIncludeImplementation(fabricApi.module("fabric-lifecycle-events-v1", rootProject.fabric_api_version)) { exclude group: 'net.fabricmc', module: 'fabric-loader' }
modIncludeImplementation(fabricApi.module("fabric-screen-api-v1", 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' }
// Remove the next line if you don't want to depend on the API
// modApi "me.shedaniel:architectury-fabric:${rootProject.architectury_version}"

View File

@ -0,0 +1,56 @@
package org.embeddedt.modernfix.fabric.mixin.perf.fabric_resourcepacks;
import net.fabricmc.fabric.impl.resource.loader.ModNioResourcePack;
import net.minecraft.server.packs.PackType;
import org.embeddedt.modernfix.annotation.RequiresMod;
import org.embeddedt.modernfix.resources.PackResourcesCacheEngine;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
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.CallbackInfoReturnable;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.util.Set;
@Mixin(ModNioResourcePack.class)
@RequiresMod("fabric-resource-loader-v0")
public abstract class ModNioResourcePackMixin {
@Shadow public abstract Set<String> getNamespaces(PackType type);
@Shadow @Final private Path basePath;
private PackResourcesCacheEngine cacheEngine;
@Inject(method = "<init>", at = @At("RETURN"))
private void cacheResources(CallbackInfo ci) {
this.cacheEngine = new PackResourcesCacheEngine(this::getNamespaces, (type, namespace) -> {
return basePath.resolve(type.getDirectory()).resolve(namespace);
});
}
// this check wastes CPU time, it is checked later anyway
@Redirect(method = "getPath", at = @At(value = "INVOKE", target = "Ljava/nio/file/Files;exists(Ljava/nio/file/Path;[Ljava/nio/file/LinkOption;)Z"), remap = false)
private boolean checkExists(Path p, LinkOption[] opts) {
return true;
}
// TODO might be redundant
@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);
if(namespaces != null)
cir.setReturnValue(namespaces);
}
}
@Inject(method = "hasResource", at = @At(value = "HEAD"), cancellable = true)
private void useCacheForExistence(String path, CallbackInfoReturnable<Boolean> cir) {
if(cacheEngine != null)
cir.setReturnValue(this.cacheEngine.hasResource(path));
}
}