-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize folder resource packs & remove use of getCanonicalPath
Relatd: #21
- Loading branch information
Showing
6 changed files
with
190 additions
and
32 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
src/main/java/org/embeddedt/vintagefix/mixin/resourcepacks/MixinDirectoryResourcePack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.embeddedt.vintagefix.mixin.resourcepacks; | ||
|
||
import com.teamacronymcoders.base.util.files.DirectoryResourcePack; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.util.ResourceLocation; | ||
import org.embeddedt.vintagefix.annotation.ClientOnlyMixin; | ||
import org.embeddedt.vintagefix.annotation.LateMixin; | ||
import org.embeddedt.vintagefix.util.FolderPackCache; | ||
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.CallbackInfo; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
import java.io.*; | ||
|
||
@Mixin(DirectoryResourcePack.class) | ||
@LateMixin | ||
@ClientOnlyMixin | ||
public class MixinDirectoryResourcePack { | ||
private FolderPackCache vfix_cache; | ||
|
||
@Inject(method = "<init>", at = @At("RETURN")) | ||
private void afterConstruct(CallbackInfo ci) { | ||
vfix_cache = new FolderPackCache(new File(Minecraft.getMinecraft().gameDir, "resources")); | ||
} | ||
|
||
@Inject(method = "hasResourceName", at = @At("HEAD"), cancellable = true) | ||
private void useFastCheck(String name, CallbackInfoReturnable<Boolean> cir) { | ||
cir.setReturnValue(vfix_cache.hasPath(name.replace("assets/", ""))); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/org/embeddedt/vintagefix/mixin/resourcepacks/MixinFolderResourcePack.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package org.embeddedt.vintagefix.mixin.resourcepacks; | ||
|
||
import net.minecraft.client.resources.FolderResourcePack; | ||
import org.embeddedt.vintagefix.annotation.ClientOnlyMixin; | ||
import org.embeddedt.vintagefix.util.FolderPackCache; | ||
import org.embeddedt.vintagefix.util.Util; | ||
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.Redirect; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
@Mixin(FolderResourcePack.class) | ||
@ClientOnlyMixin | ||
public class MixinFolderResourcePack { | ||
private FolderPackCache vfix_cache; | ||
|
||
@Inject(method = "<init>*", at = @At("RETURN")) | ||
private void afterConstructor(File folder, CallbackInfo ci) { | ||
vfix_cache = new FolderPackCache(folder); | ||
} | ||
|
||
@Redirect(method = "validatePath", at = @At(value = "INVOKE", target = "Ljava/io/File;getCanonicalPath()Ljava/lang/String;")) | ||
private static String getCanonicalPathFast(File file) throws IOException { | ||
return Util.getCanonicalPathFast(file); | ||
} | ||
|
||
@Redirect(method = "getFile", | ||
at = @At(value = "INVOKE", target = "Ljava/io/File;isFile()Z", remap = false)) | ||
public boolean redirectIsFile(File file, String name) { | ||
return vfix_cache.hasPath(name); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/org/embeddedt/vintagefix/mixin/resourcepacks/MixinNormalResourceLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package org.embeddedt.vintagefix.mixin.resourcepacks; | ||
|
||
import lumien.resourceloader.loader.NormalResourceLoader; | ||
import lumien.resourceloader.loader.OverridingResourceLoader; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.util.ResourceLocation; | ||
import org.embeddedt.vintagefix.annotation.ClientOnlyMixin; | ||
import org.embeddedt.vintagefix.annotation.LateMixin; | ||
import org.embeddedt.vintagefix.util.FolderPackCache; | ||
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.CallbackInfo; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
import java.io.*; | ||
|
||
@Mixin({ NormalResourceLoader.class, OverridingResourceLoader.class }) | ||
@LateMixin | ||
@ClientOnlyMixin | ||
public class MixinNormalResourceLoader { | ||
private FolderPackCache vfix_cache; | ||
|
||
private String ourFolderName; | ||
|
||
@Inject(method = "<init>", at = @At("RETURN")) | ||
private void afterConstruct(CallbackInfo ci) { | ||
ourFolderName = ((Object)this instanceof OverridingResourceLoader) ? "oresources" : "resources"; | ||
vfix_cache = new FolderPackCache(new File(Minecraft.getMinecraft().gameDir, ourFolderName)); | ||
} | ||
|
||
@Inject(method = "resourceExists", at = @At("HEAD"), cancellable = true) | ||
private void useFastCheck(ResourceLocation rl, CallbackInfoReturnable<Boolean> cir) { | ||
cir.setReturnValue(vfix_cache.hasPath(rl.getNamespace() + "/" + rl.getPath())); | ||
} | ||
|
||
@Inject(method = "getInputStream", at = @At("HEAD"), cancellable = true) | ||
private void getFastInputStream(ResourceLocation rl, CallbackInfoReturnable<InputStream> cir) throws IOException { | ||
try { | ||
cir.setReturnValue(new FileInputStream(new File(new File(Minecraft.getMinecraft().gameDir, ourFolderName + "/" + rl.getNamespace()), rl.getPath()))); | ||
} catch(FileNotFoundException e) { | ||
cir.setReturnValue(null); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/org/embeddedt/vintagefix/util/FolderPackCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.embeddedt.vintagefix.util; | ||
|
||
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet; | ||
|
||
import java.io.File; | ||
|
||
public class FolderPackCache { | ||
private final ObjectOpenHashSet<CachedResourcePath> cachedPaths = new ObjectOpenHashSet<>(); | ||
public FolderPackCache(File folderPath) { | ||
explore(folderPath, ""); | ||
cachedPaths.trim(); | ||
} | ||
|
||
private void explore(File folder, String path) { | ||
File[] theFiles = folder.listFiles(); | ||
if(theFiles == null) | ||
return; | ||
for(File f : theFiles) { | ||
String myPath = (path.isEmpty() ? "" : path + "/") + f.getName(); | ||
CachedResourcePath cPath = new CachedResourcePath(myPath, true); | ||
cachedPaths.add(cPath); | ||
if(f.isDirectory()) { | ||
explore(f, myPath); | ||
} | ||
} | ||
} | ||
|
||
public boolean hasPath(String p) { | ||
return cachedPaths.contains(new CachedResourcePath(p, false)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters