Workaround for mods instantiating PathResourcePack incorrectly

This commit is contained in:
embeddedt 2023-07-31 11:14:38 -04:00
parent e3051d6427
commit 4fe235cdbd
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
2 changed files with 38 additions and 0 deletions

View File

@ -0,0 +1,21 @@
package org.embeddedt.modernfix.forge.load;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.forgespi.language.IModFileInfo;
import net.minecraftforge.forgespi.locating.IModFile;
import java.nio.file.Path;
import java.util.IdentityHashMap;
public class ModResourcePackPathFixer {
private static final IdentityHashMap<Path, IModFile> modFileByPath = new IdentityHashMap<>();
public static synchronized IModFile getModFileByRootPath(Path path) {
if(modFileByPath.size() == 0) {
for(IModFileInfo info : ModList.get().getModFiles()) {
modFileByPath.put(info.getFile().getFilePath(), info.getFile());
}
}
return modFileByPath.get(path);
}
}

View File

@ -2,7 +2,10 @@ package org.embeddedt.modernfix.forge.mixin.perf.resourcepacks;
import net.minecraft.server.packs.PackType;
import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.forgespi.locating.IModFile;
import net.minecraftforge.resource.PathResourcePack;
import org.embeddedt.modernfix.ModernFix;
import org.embeddedt.modernfix.forge.load.ModResourcePackPathFixer;
import org.embeddedt.modernfix.resources.ICachingResourcePack;
import org.embeddedt.modernfix.resources.PackResourcesCacheEngine;
import org.embeddedt.modernfix.util.PackTypeHelper;
@ -25,12 +28,26 @@ public abstract class ModFileResourcePackMixin implements ICachingResourcePack {
private PackResourcesCacheEngine cacheEngine;
private IModFile mfix$resolveFileOverride;
@Inject(method = "<init>", at = @At("TAIL"))
private void cacheResources(String packName, Path source, CallbackInfo ci) {
// handle buggy mods instantiating at the root path, but only if they didn't override at all
// (otherwise they may have handled resolve() already)
if(((Object)this).getClass() == PathResourcePack.class)
this.mfix$resolveFileOverride = ModResourcePackPathFixer.getModFileByRootPath(source);
if(this.mfix$resolveFileOverride != null)
ModernFix.LOGGER.warn("PathResourcePack base class instantiated with root path of mod file {}. This probably means a mod should be calling ResourcePackLoader.createPackForMod instead. Applying workaround.", mfix$resolveFileOverride.getFileName());
invalidateCache();
PackResourcesCacheEngine.track(this);
}
@Inject(method = "resolve", at = @At("HEAD"), cancellable = true, remap = false)
private void resolveViaModFile(String[] paths, CallbackInfoReturnable<Path> cir) {
if(this.mfix$resolveFileOverride != null)
cir.setReturnValue(this.mfix$resolveFileOverride.findResource(paths));
}
private PackResourcesCacheEngine generateResourceCache() {
synchronized (this) {
PackResourcesCacheEngine engine = this.cacheEngine;