initial commit (v1.0.0)
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
package com.ccr4ft3r.lightspeed;
|
||||
|
||||
import com.ccr4ft3r.lightspeed.cache.GlobalCache;
|
||||
import net.minecraftforge.fml.ModList;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
|
||||
@Mod(ModConstants.MOD_ID)
|
||||
public class Main {
|
||||
|
||||
public Main() {
|
||||
updateCacheFlags();
|
||||
}
|
||||
|
||||
private void updateCacheFlags() {
|
||||
if (ModList.get().isLoaded(ModConstants.SOPHISTICATED_STORAGE_ID) && ModList.get().isLoaded(ModConstants.JSON_THINGS_ID))
|
||||
GlobalCache.shouldCacheWalkedPaths = false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.ccr4ft3r.lightspeed;
|
||||
|
||||
public class ModConstants {
|
||||
|
||||
public static final String MOD_ID = "lightspeed";
|
||||
public static final String JSON_THINGS_ID = "jsonthings";
|
||||
public static final String SOPHISTICATED_STORAGE_ID = "sophisticatedstorage";
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.ccr4ft3r.lightspeed.cache;
|
||||
|
||||
import com.ccr4ft3r.lightspeed.interfaces.ICache;
|
||||
import com.ccr4ft3r.lightspeed.util.CacheUtil;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.google.common.collect.Sets;
|
||||
import com.mojang.bridge.game.PackType;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import static com.ccr4ft3r.lightspeed.util.CacheUtil.*;
|
||||
|
||||
public class GlobalCache {
|
||||
|
||||
public static Boolean isEnabled = true;
|
||||
public static Boolean shouldCacheWalkedPaths = true;
|
||||
public static final Map<CharSequence, List<String>> SPLITTED_STRINGS_BY_SEQUENCE = Maps.newConcurrentMap();
|
||||
public static final Map<String, String> CANONICAL_PATH_PER_FILE = Maps.newConcurrentMap();
|
||||
private static final Set<ICache> CACHES = Sets.newConcurrentHashSet();
|
||||
public static final Map<String, Map<String, Boolean>> PERSISTED_EXISTENCES_BY_MOD = Maps.newConcurrentMap();
|
||||
public static final Map<String, Map<PackType, Set<String>>> PERSISTED_NAMESPACES_BY_MOD = Maps.newConcurrentMap();
|
||||
public static final ExecutorService EXECUTOR = Executors.newSingleThreadExecutor();
|
||||
|
||||
public static void add(ICache cache) {
|
||||
CACHES.add(cache);
|
||||
}
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
public static void disablePersistAndClear() {
|
||||
isEnabled = false;
|
||||
CacheUtil.getCacheFiles(HAS_RESOURCE_CACHE_DIR).forEach(File::delete);
|
||||
CacheUtil.getCacheFiles(NAMESPACE_CACHE_DIR).forEach(File::delete);
|
||||
CACHES.forEach(ICache::persistAndClearCache);
|
||||
SPLITTED_STRINGS_BY_SEQUENCE.clear();
|
||||
CANONICAL_PATH_PER_FILE.clear();
|
||||
CACHES.clear();
|
||||
PERSISTED_EXISTENCES_BY_MOD.clear();
|
||||
PERSISTED_NAMESPACES_BY_MOD.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.ccr4ft3r.lightspeed.events;
|
||||
|
||||
import com.ccr4ft3r.lightspeed.ModConstants;
|
||||
import com.ccr4ft3r.lightspeed.cache.GlobalCache;
|
||||
import com.mojang.logging.LogUtils;
|
||||
import net.minecraft.client.gui.screens.TitleScreen;
|
||||
import net.minecraftforge.api.distmarker.Dist;
|
||||
import net.minecraftforge.client.event.ScreenEvent;
|
||||
import net.minecraftforge.eventbus.api.SubscribeEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import net.minecraftforge.internal.BrandingControl;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Mod.EventBusSubscriber(modid = ModConstants.MOD_ID, value = Dist.CLIENT)
|
||||
public class TitleScreenInjector {
|
||||
|
||||
private static boolean launchComplete = false;
|
||||
|
||||
@SuppressWarnings({"InstantiationOfUtilityClass", "unchecked"})
|
||||
@SubscribeEvent
|
||||
public static void onScreenInit(ScreenEvent.InitScreenEvent event) {
|
||||
if (!(event.getScreen() instanceof TitleScreen) || launchComplete)
|
||||
return;
|
||||
launchComplete = true;
|
||||
try {
|
||||
long secondsToStart = ManagementFactory.getRuntimeMXBean().getUptime() / 1000;
|
||||
LogUtils.getLogger().info("Lightspeed: Launch took {}s", secondsToStart);
|
||||
BrandingControl brandingControl = new BrandingControl();
|
||||
|
||||
Field f = BrandingControl.class.getDeclaredField("brandings");
|
||||
f.setAccessible(true);
|
||||
Method computeBranding = BrandingControl.class.getDeclaredMethod("computeBranding");
|
||||
computeBranding.setAccessible(true);
|
||||
computeBranding.invoke(null);
|
||||
|
||||
List<String> brandings = new ArrayList<>((List<String>) f.get(brandingControl));
|
||||
List<String> newBrandings = new ArrayList<>(brandings);
|
||||
f.set(brandingControl, newBrandings);
|
||||
newBrandings.add("Lightspeed: Launch took " + secondsToStart + "s");
|
||||
} catch (NoSuchFieldException | NoSuchMethodException | IllegalAccessException |
|
||||
InvocationTargetException e) {
|
||||
LogUtils.getLogger().error("Cannot add launch time to title screen", e);
|
||||
}
|
||||
GlobalCache.EXECUTOR.execute(GlobalCache::disablePersistAndClear);
|
||||
GlobalCache.EXECUTOR.shutdown();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.ccr4ft3r.lightspeed.interfaces;
|
||||
|
||||
public interface ICache {
|
||||
void persistAndClearCache();
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.ccr4ft3r.lightspeed.interfaces;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public interface IPackResources extends ICache {
|
||||
|
||||
Map<String, Boolean> getExistenceByResource();
|
||||
|
||||
void setExistenceByResource(Map<String, Boolean> existenceByResource);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.ccr4ft3r.lightspeed.interfaces;
|
||||
|
||||
import net.minecraft.server.packs.PackResources;
|
||||
import net.minecraftforge.forgespi.locating.IModFile;
|
||||
|
||||
public interface IPathResourcePack extends PackResources, IPackResources {
|
||||
void setModFile(IModFile modFile);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.ccr4ft3r.lightspeed.mixin;
|
||||
|
||||
import com.ccr4ft3r.lightspeed.cache.GlobalCache;
|
||||
import com.ccr4ft3r.lightspeed.util.CacheUtil;
|
||||
import net.minecraft.client.main.Main;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
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 java.io.File;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.ccr4ft3r.lightspeed.util.CacheUtil.*;
|
||||
|
||||
@Mixin(Main.class)
|
||||
public class MinecraftMainMixin {
|
||||
|
||||
@Inject(method = "main", at = @At(value = "INVOKE", target = "Lnet/minecraft/SharedConstants;tryDetectVersion()V", shift = At.Shift.AFTER))
|
||||
private static void mainTryDetecVersionInjected(String[] p_129642_, CallbackInfo ci) {
|
||||
GlobalCache.EXECUTOR.execute(() -> {
|
||||
loadPersistedCaches(HAS_RESOURCE_CACHE_DIR, GlobalCache.PERSISTED_EXISTENCES_BY_MOD);
|
||||
loadPersistedCaches(NAMESPACE_CACHE_DIR, GlobalCache.PERSISTED_NAMESPACES_BY_MOD);
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
private static <K, V> void loadPersistedCaches(File dir, Map<String, Map<K, V>> targetMap) {
|
||||
dir.mkdirs();
|
||||
CacheUtil.getCacheFiles(dir).forEach(file -> {
|
||||
String id = FilenameUtils.getBaseName(file.getName());
|
||||
targetMap.put(id, CacheUtil.load(file));
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.ccr4ft3r.lightspeed.mixin.misc;
|
||||
|
||||
import com.ccr4ft3r.lightspeed.cache.GlobalCache;
|
||||
import com.google.common.base.Splitter;
|
||||
import net.minecraft.client.renderer.block.model.multipart.KeyValueCondition;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Redirect;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static com.ccr4ft3r.lightspeed.cache.GlobalCache.*;
|
||||
|
||||
@Mixin(value = KeyValueCondition.class, priority = Integer.MAX_VALUE)
|
||||
public abstract class KeyValueConditionMixin {
|
||||
|
||||
@Redirect(method = "getPredicate", at = @At(value = "INVOKE", target = "Lcom/google/common/base/Splitter;splitToList(Ljava/lang/CharSequence;)Ljava/util/List;"))
|
||||
public List<String> getPredicateSplitToListRedirected(Splitter instance, CharSequence sequence) {
|
||||
if (!GlobalCache.isEnabled)
|
||||
return instance.splitToList(sequence);
|
||||
return SPLITTED_STRINGS_BY_SEQUENCE.computeIfAbsent(sequence, (k) -> instance.splitToList(sequence));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.ccr4ft3r.lightspeed.mixin.misc;
|
||||
|
||||
import com.ccr4ft3r.lightspeed.cache.GlobalCache;
|
||||
import net.minecraft.client.resources.model.Material;
|
||||
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(Material.class)
|
||||
public abstract class MaterialMixin {
|
||||
|
||||
private Integer hash;
|
||||
|
||||
@Inject(method = "hashCode", at = @At("HEAD"), cancellable = true)
|
||||
public void hashCodeHeadInjected(CallbackInfoReturnable<Integer> cir) {
|
||||
if (GlobalCache.isEnabled && hash != null)
|
||||
cir.setReturnValue(hash);
|
||||
}
|
||||
|
||||
@Inject(method = "hashCode", at = @At("RETURN"))
|
||||
public void hashCodeReturnInjected(CallbackInfoReturnable<Integer> cir) {
|
||||
if (GlobalCache.isEnabled && hash == null)
|
||||
hash = cir.getReturnValue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.ccr4ft3r.lightspeed.mixin.misc;
|
||||
|
||||
import com.ccr4ft3r.lightspeed.cache.GlobalCache;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
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(ResourceLocation.class)
|
||||
public abstract class ResourceLocationMixin {
|
||||
|
||||
private Integer hash;
|
||||
|
||||
@Inject(method = "hashCode", at = @At("HEAD"), cancellable = true)
|
||||
public void hashCodeHeadInjected(CallbackInfoReturnable<Integer> cir) {
|
||||
if (GlobalCache.isEnabled && hash != null)
|
||||
cir.setReturnValue(hash);
|
||||
}
|
||||
|
||||
@Inject(method = "hashCode", at = @At("RETURN"))
|
||||
public void hashCodeReturnInjected(CallbackInfoReturnable<Integer> cir) {
|
||||
if (GlobalCache.isEnabled && hash == null)
|
||||
hash = cir.getReturnValue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.ccr4ft3r.lightspeed.mixin.misc;
|
||||
|
||||
import com.ccr4ft3r.lightspeed.cache.GlobalCache;
|
||||
import com.ccr4ft3r.lightspeed.interfaces.IPathResourcePack;
|
||||
import net.minecraftforge.forgespi.language.IModFileInfo;
|
||||
import net.minecraftforge.resource.PathResourcePack;
|
||||
import net.minecraftforge.resource.ResourcePackLoader;
|
||||
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(ResourcePackLoader.class)
|
||||
public abstract class ResourcePackLoaderMixin {
|
||||
|
||||
@Inject(method = "createPackForMod", at = @At("HEAD"), remap = false, cancellable = true)
|
||||
private static void createPackForModHeadInjected(IModFileInfo mf, CallbackInfoReturnable<PathResourcePack> cir) {
|
||||
if (!GlobalCache.isEnabled)
|
||||
return;
|
||||
PathResourcePack resourcePack = new PathResourcePack(mf.getFile().getFileName(), mf.getFile().getFilePath());
|
||||
((IPathResourcePack) resourcePack).setModFile(mf.getFile());
|
||||
cir.setReturnValue(resourcePack);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package com.ccr4ft3r.lightspeed.mixin.misc;
|
||||
|
||||
import com.ccr4ft3r.lightspeed.cache.GlobalCache;
|
||||
import com.ccr4ft3r.lightspeed.interfaces.ICache;
|
||||
import com.google.common.collect.MapMaker;
|
||||
import net.minecraft.client.renderer.block.model.MultiVariant;
|
||||
import net.minecraft.client.renderer.block.model.multipart.Condition;
|
||||
import net.minecraft.client.renderer.block.model.multipart.Selector;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.block.state.StateDefinition;
|
||||
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.util.Map;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
@Mixin(Selector.class)
|
||||
public class SelectorMixin implements ICache {
|
||||
|
||||
private final Map<StateDefinition<Block, BlockState>, Predicate<BlockState>> predicatePerDefinition = new MapMaker().weakKeys().makeMap();
|
||||
|
||||
@Inject(method = "<init>", at = @At("RETURN"))
|
||||
public void initReturnInjected(Condition p_112018_, MultiVariant p_112019_, CallbackInfo ci) {
|
||||
if (GlobalCache.isEnabled)
|
||||
GlobalCache.add(this);
|
||||
}
|
||||
|
||||
@Inject(method = "getPredicate", at = @At("HEAD"), cancellable = true)
|
||||
public void getPredicateHeadInjected(StateDefinition<Block, BlockState> p_112022_, CallbackInfoReturnable<Predicate<BlockState>> cir) {
|
||||
if (!GlobalCache.isEnabled)
|
||||
return;
|
||||
Predicate<BlockState> predicate = predicatePerDefinition.get(p_112022_);
|
||||
if (predicate != null)
|
||||
cir.setReturnValue(predicate);
|
||||
}
|
||||
|
||||
@Inject(method = "getPredicate", at = @At("RETURN"))
|
||||
public void getPredicateReturnInjected(StateDefinition<Block, BlockState> p_112022_, CallbackInfoReturnable<Predicate<BlockState>> cir) {
|
||||
if (GlobalCache.isEnabled)
|
||||
predicatePerDefinition.put(p_112022_, cir.getReturnValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void persistAndClearCache() {
|
||||
predicatePerDefinition.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.ccr4ft3r.lightspeed.mixin.misc;
|
||||
|
||||
import com.ccr4ft3r.lightspeed.cache.GlobalCache;
|
||||
import com.ccr4ft3r.lightspeed.interfaces.ICache;
|
||||
import net.minecraft.world.level.block.state.StateDefinition;
|
||||
import net.minecraft.world.level.block.state.properties.Property;
|
||||
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.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
@SuppressWarnings({"rawtypes", "unchecked"})
|
||||
@Mixin(StateDefinition.class)
|
||||
public class StateDefinitonMixin implements ICache {
|
||||
|
||||
private Map<String, Property<?>> propsByName;
|
||||
|
||||
@Inject(method = "<init>", at = @At("RETURN"))
|
||||
public void initReturnInjected(Function p_61052_, Object p_61053_, StateDefinition.Factory p_61054_, Map p_61055_, CallbackInfo ci) {
|
||||
if (GlobalCache.isEnabled)
|
||||
GlobalCache.add(this);
|
||||
}
|
||||
|
||||
@Inject(method = "<init>", at = @At("RETURN"))
|
||||
private void initHeadInjected(Function p_61052_, Object p_61053_, StateDefinition.Factory p_61054_, Map p_61055_, CallbackInfo ci) {
|
||||
if (GlobalCache.isEnabled)
|
||||
propsByName = p_61055_;
|
||||
}
|
||||
|
||||
@Inject(method = "getProperty", at = @At("HEAD"), cancellable = true)
|
||||
public void getPropertyHeadInjected(String p_61082_, CallbackInfoReturnable<Property<?>> cir) {
|
||||
if (GlobalCache.isEnabled)
|
||||
cir.setReturnValue(propsByName.get(p_61082_));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void persistAndClearCache() {
|
||||
propsByName.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.ccr4ft3r.lightspeed.mixin.model;
|
||||
|
||||
import com.ccr4ft3r.lightspeed.cache.GlobalCache;
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
import net.minecraft.client.renderer.block.model.BlockModel;
|
||||
import net.minecraft.client.resources.model.Material;
|
||||
import net.minecraft.client.resources.model.UnbakedModel;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
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.Collection;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
@Mixin(BlockModel.class)
|
||||
public abstract class BlockModelMixin {
|
||||
|
||||
private Collection<Material> materials;
|
||||
|
||||
@Inject(method = "getMaterials", at = @At("HEAD"), cancellable = true)
|
||||
public void getMaterialsHeadInjected(Function<ResourceLocation, UnbakedModel> p_111855_, Set<Pair<String, String>> p_111856_, CallbackInfoReturnable<Collection<Material>> cir) {
|
||||
if (GlobalCache.isEnabled && materials != null)
|
||||
cir.setReturnValue(materials);
|
||||
}
|
||||
|
||||
@Inject(method = "getMaterials", at = @At("RETURN"))
|
||||
public void getMaterialsReturnInjected(Function<ResourceLocation, UnbakedModel> p_111855_, Set<Pair<String, String>> p_111856_, CallbackInfoReturnable<Collection<Material>> cir) {
|
||||
if (GlobalCache.isEnabled && materials == null)
|
||||
materials = cir.getReturnValue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.ccr4ft3r.lightspeed.mixin.model;
|
||||
|
||||
import com.ccr4ft3r.lightspeed.cache.GlobalCache;
|
||||
import com.mojang.math.Transformation;
|
||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||
import net.minecraft.client.resources.model.BakedModel;
|
||||
import net.minecraft.client.resources.model.Material;
|
||||
import net.minecraft.client.resources.model.ModelBakery;
|
||||
import net.minecraft.client.resources.model.ModelState;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import org.apache.commons.lang3.tuple.Triple;
|
||||
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.CallbackInfoReturnable;
|
||||
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
@Mixin(ModelBakery.class)
|
||||
public abstract class ModelBakeryMixin {
|
||||
|
||||
@Shadow
|
||||
@Final
|
||||
private Map<Triple<ResourceLocation, Transformation, Boolean>, BakedModel> bakedCache;
|
||||
|
||||
@Inject(method = "bake(Lnet/minecraft/resources/ResourceLocation;Lnet/minecraft/client/resources/model/ModelState;Ljava/util/function/Function;)Lnet/minecraft/client/resources/model/BakedModel;", at = @At(value = "INVOKE", target = "Ljava/util/Map;containsKey(Ljava/lang/Object;)Z", shift = At.Shift.BEFORE), cancellable = true, remap = false, locals = LocalCapture.CAPTURE_FAILSOFT)
|
||||
public void bakeContainsKeyInjected(ResourceLocation p_119350_, ModelState p_119351_, Function<Material, TextureAtlasSprite> sprites, CallbackInfoReturnable<BakedModel> cir, Triple<ResourceLocation, Transformation, Boolean> triple) {
|
||||
if (!GlobalCache.isEnabled) {
|
||||
return;
|
||||
}
|
||||
BakedModel bakedModel = this.bakedCache.get(triple);
|
||||
if (bakedModel != null)
|
||||
cir.setReturnValue(bakedModel);
|
||||
}
|
||||
|
||||
@SuppressWarnings("SuspiciousMethodCalls")
|
||||
@Redirect(method = "bake(Lnet/minecraft/resources/ResourceLocation;Lnet/minecraft/client/resources/model/ModelState;Ljava/util/function/Function;)Lnet/minecraft/client/resources/model/BakedModel;", at = @At(value = "INVOKE", target = "Ljava/util/Map;containsKey(Ljava/lang/Object;)Z"), remap = false)
|
||||
public boolean bakeContainsKeyRedirected(Map<Triple<ResourceLocation, Transformation, Boolean>, BakedModel> instance, Object o) {
|
||||
if (!GlobalCache.isEnabled) {
|
||||
return instance.containsKey(o);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.ccr4ft3r.lightspeed.mixin.model;
|
||||
|
||||
import com.ccr4ft3r.lightspeed.cache.GlobalCache;
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
import net.minecraft.client.renderer.block.model.multipart.MultiPart;
|
||||
import net.minecraft.client.renderer.block.model.multipart.Selector;
|
||||
import net.minecraft.client.resources.model.Material;
|
||||
import net.minecraft.client.resources.model.UnbakedModel;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
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.CallbackInfoReturnable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static com.ccr4ft3r.lightspeed.util.UnbakedModelHelper.*;
|
||||
|
||||
@Mixin(MultiPart.class)
|
||||
public abstract class MultiPartMixin implements UnbakedModel {
|
||||
|
||||
private Collection<Material> materials;
|
||||
private Collection<ResourceLocation> dependencies;
|
||||
|
||||
@Redirect(method = "getMaterials", at = @At(value = "INVOKE", target = "Ljava/util/stream/Stream;flatMap(Ljava/util/function/Function;)Ljava/util/stream/Stream;"))
|
||||
public <T extends Selector, R extends Material> Stream<Material> getMaterialsFlatMapRedirected(Stream<Selector> instance, Function<? super T, ? extends Stream<? extends R>> function, Function<ResourceLocation, UnbakedModel> p_111976_, Set<Pair<String, String>> p_111977_) {
|
||||
if (!GlobalCache.isEnabled)
|
||||
return instance.flatMap((p_111981_) -> p_111981_.getVariant().getMaterials(p_111976_, p_111977_).stream());
|
||||
return instance.flatMap(selector -> getMaterialsStream(selector.getVariant(), p_111976_, p_111977_));
|
||||
}
|
||||
|
||||
@Inject(method = "getMaterials", at = @At("HEAD"), cancellable = true)
|
||||
public void getMaterialsHeadInjected(Function<ResourceLocation, UnbakedModel> p_111855_, Set<Pair<String, String>> p_111856_, CallbackInfoReturnable<Collection<Material>> cir) {
|
||||
if (GlobalCache.isEnabled && materials != null)
|
||||
cir.setReturnValue(materials);
|
||||
}
|
||||
|
||||
@Inject(method = "getMaterials", at = @At("RETURN"))
|
||||
public void getMaterialsReturnInjected(Function<ResourceLocation, UnbakedModel> p_111855_, Set<Pair<String, String>> p_111856_, CallbackInfoReturnable<Collection<Material>> cir) {
|
||||
if (GlobalCache.isEnabled && materials == null)
|
||||
materials = cir.getReturnValue();
|
||||
}
|
||||
|
||||
@Inject(method = "getDependencies", at = @At("HEAD"), cancellable = true)
|
||||
public void getDependenciesHeadInjected(CallbackInfoReturnable<Collection<ResourceLocation>> cir) {
|
||||
if (GlobalCache.isEnabled && dependencies != null)
|
||||
cir.setReturnValue(dependencies);
|
||||
}
|
||||
|
||||
@Inject(method = "getDependencies", at = @At("RETURN"))
|
||||
public void getDependenciesReturnInjected(CallbackInfoReturnable<Collection<ResourceLocation>> cir) {
|
||||
if (GlobalCache.isEnabled && dependencies == null)
|
||||
dependencies = cir.getReturnValue();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.ccr4ft3r.lightspeed.mixin.model;
|
||||
|
||||
import com.ccr4ft3r.lightspeed.cache.GlobalCache;
|
||||
import com.ccr4ft3r.lightspeed.util.WeightedBuilder;
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
import net.minecraft.client.renderer.block.model.MultiVariant;
|
||||
import net.minecraft.client.renderer.block.model.Variant;
|
||||
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
||||
import net.minecraft.client.resources.model.*;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
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.callback.CallbackInfoReturnable;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
|
||||
@Mixin(MultiVariant.class)
|
||||
public abstract class MultivariantMixin implements UnbakedModel {
|
||||
|
||||
@Shadow
|
||||
public abstract List<Variant> getVariants();
|
||||
|
||||
private Collection<Material> materials;
|
||||
private Collection<ResourceLocation> dependencies;
|
||||
|
||||
@Inject(method = "getMaterials", at = @At("HEAD"), cancellable = true)
|
||||
public void getMaterialsHeadInjected(Function<ResourceLocation, UnbakedModel> p_111855_, Set<Pair<String, String>> p_111856_, CallbackInfoReturnable<Collection<Material>> cir) {
|
||||
if (GlobalCache.isEnabled && materials != null)
|
||||
cir.setReturnValue(materials);
|
||||
}
|
||||
|
||||
@Inject(method = "getMaterials", at = @At("RETURN"))
|
||||
public void getMaterialsReturnInjected(Function<ResourceLocation, UnbakedModel> p_111855_, Set<Pair<String, String>> p_111856_, CallbackInfoReturnable<Collection<Material>> cir) {
|
||||
if (GlobalCache.isEnabled && materials == null)
|
||||
materials = cir.getReturnValue();
|
||||
}
|
||||
|
||||
@Inject(method = "getDependencies", at = @At("HEAD"), cancellable = true)
|
||||
public void getDependenciesHeadInjected(CallbackInfoReturnable<Collection<ResourceLocation>> cir) {
|
||||
if (GlobalCache.isEnabled && dependencies != null)
|
||||
cir.setReturnValue(dependencies);
|
||||
}
|
||||
|
||||
@Inject(method = "getDependencies", at = @At("RETURN"))
|
||||
public void getDependenciesReturnInjected(CallbackInfoReturnable<Collection<ResourceLocation>> cir) {
|
||||
if (GlobalCache.isEnabled && dependencies == null)
|
||||
dependencies = cir.getReturnValue();
|
||||
}
|
||||
|
||||
@Inject(method = "bake", at = @At("HEAD"), cancellable = true)
|
||||
public void bakeHeadInjected(ModelBakery p_111850_, Function<Material, TextureAtlasSprite> p_111851_, ModelState p_111852_, ResourceLocation p_111853_, CallbackInfoReturnable<BakedModel> cir) {
|
||||
if (!GlobalCache.isEnabled) {
|
||||
return;
|
||||
}
|
||||
if (this.getVariants().isEmpty()) {
|
||||
cir.setReturnValue(null);
|
||||
} else {
|
||||
WeightedBuilder weightedBuilder = new WeightedBuilder();
|
||||
//noinspection CodeBlock2Expr
|
||||
this.getVariants().parallelStream().forEach(variant -> {
|
||||
weightedBuilder.add(p_111850_.bake(variant.getModelLocation(), variant, p_111851_), variant.getWeight());
|
||||
});
|
||||
cir.setReturnValue(weightedBuilder.build());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.ccr4ft3r.lightspeed.mixin.resources;
|
||||
|
||||
import com.ccr4ft3r.lightspeed.cache.GlobalCache;
|
||||
import com.ccr4ft3r.lightspeed.interfaces.IPackResources;
|
||||
import com.google.common.collect.Maps;
|
||||
import net.minecraft.server.packs.AbstractPackResources;
|
||||
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 java.io.File;
|
||||
import java.util.Map;
|
||||
|
||||
@Mixin(AbstractPackResources.class)
|
||||
public abstract class AbstractPackResourcesMixin implements IPackResources {
|
||||
|
||||
@Shadow
|
||||
protected abstract boolean hasResource(String p_10229_);
|
||||
|
||||
private Map<String, Boolean> existenceByResource = Maps.newConcurrentMap();
|
||||
|
||||
@Inject(method = "<init>", at = @At("RETURN"))
|
||||
public void initReturnInjected(File p_10207_, CallbackInfo ci) {
|
||||
if (GlobalCache.isEnabled)
|
||||
GlobalCache.add(this);
|
||||
}
|
||||
|
||||
@Redirect(method = "hasResource(Lnet/minecraft/server/packs/PackType;Lnet/minecraft/resources/ResourceLocation;)Z",
|
||||
at = @At(value = "INVOKE", target = "Lnet/minecraft/server/packs/AbstractPackResources;hasResource(Ljava/lang/String;)Z"))
|
||||
public boolean hasResourceHasResourceRedirected(AbstractPackResources instance, String s) {
|
||||
if (!GlobalCache.isEnabled)
|
||||
return hasResource(s);
|
||||
Boolean exists = existenceByResource.get(s);
|
||||
if (exists == null) {
|
||||
existenceByResource.put(s, exists = hasResource(s));
|
||||
}
|
||||
return exists;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void persistAndClearCache() {
|
||||
existenceByResource.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setExistenceByResource(Map<String, Boolean> existenceByResource) {
|
||||
this.existenceByResource = existenceByResource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<String, Boolean> getExistenceByResource() {
|
||||
return existenceByResource;
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package com.ccr4ft3r.lightspeed.mixin.resources;
|
||||
|
||||
import com.ccr4ft3r.lightspeed.cache.GlobalCache;
|
||||
import com.ccr4ft3r.lightspeed.interfaces.IPackResources;
|
||||
import com.ccr4ft3r.lightspeed.util.CacheUtil;
|
||||
import com.google.common.collect.Maps;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.client.resources.AssetIndex;
|
||||
import net.minecraft.client.resources.DefaultClientPackResources;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.packs.PackType;
|
||||
import net.minecraft.server.packs.metadata.pack.PackMetadataSection;
|
||||
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.File;
|
||||
import java.util.Map;
|
||||
|
||||
import static com.ccr4ft3r.lightspeed.cache.GlobalCache.*;
|
||||
import static com.ccr4ft3r.lightspeed.util.CacheUtil.*;
|
||||
|
||||
@Mixin(DefaultClientPackResources.class)
|
||||
public abstract class DefaultClientPackResourcesMixin implements IPackResources {
|
||||
|
||||
private Map<String, Boolean> existenceByClientResource = Maps.newConcurrentMap();
|
||||
private Map<String, Boolean> existenceByServerResource = Maps.newConcurrentMap();
|
||||
|
||||
private String id;
|
||||
|
||||
@Inject(method = "<init>", at = @At("RETURN"))
|
||||
public void initReturnInjected(PackMetadataSection p_174827_, AssetIndex p_174828_, CallbackInfo ci) {
|
||||
if (!GlobalCache.isEnabled)
|
||||
return;
|
||||
GlobalCache.add(this);
|
||||
id = Minecraft.getInstance().getLaunchedVersion();
|
||||
existenceByClientResource = PERSISTED_EXISTENCES_BY_MOD.computeIfAbsent(
|
||||
id + "-client", i -> Maps.newConcurrentMap());
|
||||
existenceByServerResource = PERSISTED_EXISTENCES_BY_MOD.computeIfAbsent(
|
||||
id + "-server", i -> Maps.newConcurrentMap());
|
||||
}
|
||||
|
||||
@Inject(method = "hasResource", at = @At("HEAD"), cancellable = true)
|
||||
public void hasResourceHeadInjected(PackType p_10355_, ResourceLocation p_10356_, CallbackInfoReturnable<Boolean> cir) {
|
||||
if (!GlobalCache.isEnabled)
|
||||
return;
|
||||
Boolean exists = exists(p_10355_, p_10356_.toString());
|
||||
if (exists != null)
|
||||
cir.setReturnValue(exists);
|
||||
}
|
||||
|
||||
@Inject(method = "hasResource", at = @At("RETURN"))
|
||||
public void hasResourceReturnInjected(PackType p_10355_, ResourceLocation p_10356_, CallbackInfoReturnable<Boolean> cir) {
|
||||
if (!GlobalCache.isEnabled)
|
||||
return;
|
||||
cacheExists(p_10355_, p_10356_.toString(), cir.getReturnValue());
|
||||
}
|
||||
|
||||
public Boolean exists(PackType packType, String resourceName) {
|
||||
if (packType == PackType.CLIENT_RESOURCES)
|
||||
return existenceByClientResource.get(resourceName);
|
||||
return existenceByServerResource.get(resourceName);
|
||||
}
|
||||
|
||||
public void cacheExists(PackType packType, String resourceName, boolean exists) {
|
||||
if (packType == PackType.CLIENT_RESOURCES)
|
||||
existenceByClientResource.put(resourceName, exists);
|
||||
else existenceByServerResource.put(resourceName, exists);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void persistAndClearCache() {
|
||||
CacheUtil.persist(existenceByClientResource, new File(HAS_RESOURCE_CACHE_DIR.getPath(), id + "-client.ser"));
|
||||
CacheUtil.persist(existenceByServerResource, new File(HAS_RESOURCE_CACHE_DIR.getPath(), id + "-server.ser"));
|
||||
existenceByClientResource.clear();
|
||||
existenceByServerResource.clear();
|
||||
getExistenceByResource().clear();
|
||||
}
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
package com.ccr4ft3r.lightspeed.mixin.resources;
|
||||
|
||||
import com.ccr4ft3r.lightspeed.cache.GlobalCache;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.packs.PackResources;
|
||||
import net.minecraft.server.packs.PackType;
|
||||
import net.minecraftforge.resource.DelegatingResourcePack;
|
||||
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.CallbackInfoReturnable;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Mixin(DelegatingResourcePack.class)
|
||||
public abstract class DelegatingResourcePackMixin {
|
||||
|
||||
@Shadow
|
||||
protected abstract List<PackResources> getCandidatePacks(PackType type, ResourceLocation location);
|
||||
|
||||
@Inject(method = "hasResource(Lnet/minecraft/server/packs/PackType;Lnet/minecraft/resources/ResourceLocation;)Z", at = @At(value = "HEAD"), cancellable = true)
|
||||
public void hasResourceHeadInjected(PackType type, ResourceLocation location, CallbackInfoReturnable<Boolean> cir) {
|
||||
if (!GlobalCache.isEnabled)
|
||||
return;
|
||||
cir.setReturnValue(getCandidatePacks(type, location).parallelStream().anyMatch(p -> p.hasResource(type, location)));
|
||||
}
|
||||
|
||||
@Redirect(method = "getResources", at = @At(value = "INVOKE", target = "Ljava/util/List;stream()Ljava/util/stream/Stream;"))
|
||||
public <R> Stream<R> getResourcesStreamRedirected(List<R> instance) {
|
||||
return !GlobalCache.isEnabled ? instance.stream() : instance.parallelStream();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.ccr4ft3r.lightspeed.mixin.resources;
|
||||
|
||||
import com.ccr4ft3r.lightspeed.cache.GlobalCache;
|
||||
import com.ccr4ft3r.lightspeed.interfaces.IPackResources;
|
||||
import com.google.common.collect.Maps;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.packs.FilePackResources;
|
||||
import net.minecraft.server.packs.PackType;
|
||||
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 org.spongepowered.asm.mixin.injection.callback.LocalCapture;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.zip.ZipEntry;
|
||||
import java.util.zip.ZipFile;
|
||||
|
||||
@Mixin(FilePackResources.class)
|
||||
public abstract class FilePackResourcesMixin implements IPackResources {
|
||||
|
||||
private final Map<PackType, List<ZipEntry>> entriesByPackType = Maps.newConcurrentMap();
|
||||
|
||||
@Inject(method = "<init>", at = @At("RETURN"))
|
||||
public void initReturnInjected(File p_10236_, CallbackInfo ci) {
|
||||
if (GlobalCache.isEnabled)
|
||||
GlobalCache.add(this);
|
||||
}
|
||||
|
||||
@Inject(method = "getResources", at = @At(value = "INVOKE",target = "Ljava/util/zip/ZipFile;entries()Ljava/util/Enumeration;",shift = At.Shift.BEFORE), cancellable = true,locals = LocalCapture.CAPTURE_FAILSOFT)
|
||||
public void getResourcesHeadInjected(PackType packType, String pathIn, String pathIn2, int maxDepth, Predicate<String> filter, CallbackInfoReturnable<Collection<ResourceLocation>> cir, ZipFile zip) {
|
||||
if (!GlobalCache.isEnabled)
|
||||
return;
|
||||
String base = packType.getDirectory() + "/" + pathIn + "/";
|
||||
String path = base + pathIn2 + "/";
|
||||
List<ZipEntry> entries;
|
||||
if ((entries = entriesByPackType.get(packType)) == null) {
|
||||
entries = zip.stream().filter(e -> !e.isDirectory()).filter(
|
||||
e -> !e.getName().endsWith(".mcmeta") && e.getName().startsWith(path)
|
||||
).collect(Collectors.toList());
|
||||
entriesByPackType.put(packType, entries);
|
||||
}
|
||||
cir.setReturnValue(entries.stream().map(e -> {
|
||||
String locPath = e.getName().substring(base.length());
|
||||
String[] splitted = locPath.split("/");
|
||||
if (splitted.length >= maxDepth + 1 && filter.test(splitted[splitted.length - 1])) {
|
||||
return new ResourceLocation(pathIn, locPath);
|
||||
}
|
||||
return null;
|
||||
}).filter(Objects::nonNull).collect(Collectors.toSet()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void persistAndClearCache() {
|
||||
entriesByPackType.clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.ccr4ft3r.lightspeed.mixin.resources;
|
||||
|
||||
import com.ccr4ft3r.lightspeed.cache.GlobalCache;
|
||||
import net.minecraft.server.packs.FolderPackResources;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
import org.spongepowered.asm.mixin.injection.Redirect;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import static com.ccr4ft3r.lightspeed.cache.GlobalCache.*;
|
||||
|
||||
@Mixin(FolderPackResources.class)
|
||||
public abstract class FolderPackResourcesMixin {
|
||||
|
||||
@Redirect(method = "validatePath", at = @At(value = "INVOKE", target = "Ljava/io/File;getCanonicalPath()Ljava/lang/String;"))
|
||||
private static String validatePathGetCanonicalPathRedirected(File instance) throws IOException {
|
||||
if (!GlobalCache.isEnabled)
|
||||
return instance.getCanonicalPath();
|
||||
String canoncialPath = CANONICAL_PATH_PER_FILE.get(instance.getPath());
|
||||
if (canoncialPath == null) {
|
||||
CANONICAL_PATH_PER_FILE.put(instance.getPath(), canoncialPath = instance.getCanonicalPath());
|
||||
}
|
||||
return canoncialPath;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
package com.ccr4ft3r.lightspeed.mixin.resources;
|
||||
|
||||
import com.ccr4ft3r.lightspeed.cache.GlobalCache;
|
||||
import com.ccr4ft3r.lightspeed.interfaces.IPackResources;
|
||||
import com.ccr4ft3r.lightspeed.interfaces.IPathResourcePack;
|
||||
import com.ccr4ft3r.lightspeed.util.CacheUtil;
|
||||
import com.google.common.collect.Maps;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.packs.PackType;
|
||||
import net.minecraftforge.forgespi.locating.IModFile;
|
||||
import net.minecraftforge.resource.PathResourcePack;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
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 org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
|
||||
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileSystem;
|
||||
import java.nio.file.FileVisitOption;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.*;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static com.ccr4ft3r.lightspeed.util.CacheUtil.*;
|
||||
|
||||
@Mixin(value = PathResourcePack.class)
|
||||
public abstract class PathResourcePackMixin implements IPathResourcePack, IPackResources {
|
||||
|
||||
private final Map<String, Path> resolvedPathByResource = Maps.newConcurrentMap();
|
||||
private final Map<PackType, Set<String>> namespacesByPackType = Maps.newConcurrentMap();
|
||||
private final Map<String, Path> inputPathByPath = Maps.newConcurrentMap();
|
||||
private final Map<PackType, Map<String, List<Path>>> filePathsByRootByPackType = initPathsMap();
|
||||
private IModFile modFile;
|
||||
private String id;
|
||||
|
||||
@Inject(method = "<init>", at = @At("RETURN"))
|
||||
public void initReturnInjected(String packName, Path source, CallbackInfo ci) {
|
||||
if (GlobalCache.isEnabled)
|
||||
GlobalCache.add(this);
|
||||
}
|
||||
|
||||
private static Map<PackType, Map<String, List<Path>>> initPathsMap() {
|
||||
Map<PackType, Map<String, List<Path>>> map = Maps.newConcurrentMap();
|
||||
for (PackType packType : PackType.values()) {
|
||||
map.put(packType, Maps.newConcurrentMap());
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@Inject(method = "resolve", at = @At("HEAD"), cancellable = true, remap = false)
|
||||
public void resolveHeadInjected(String[] paths, CallbackInfoReturnable<Path> cir) {
|
||||
if (modFile == null) {
|
||||
if (!GlobalCache.isEnabled)
|
||||
return;
|
||||
Path resolved = getResolvedPath(paths);
|
||||
if (resolved != null)
|
||||
cir.setReturnValue(resolved);
|
||||
return;
|
||||
}
|
||||
if (!GlobalCache.isEnabled) {
|
||||
cir.setReturnValue(modFile.findResource(paths));
|
||||
return;
|
||||
}
|
||||
Path path = getResolvedPath(paths);
|
||||
if (path == null)
|
||||
resolvedPathByResource.put(Arrays.toString(paths), path = modFile.findResource(paths));
|
||||
|
||||
cir.setReturnValue(path);
|
||||
}
|
||||
|
||||
@Inject(method = "resolve", at = @At("RETURN"), remap = false)
|
||||
public void resolveReturnInjected(String[] paths, CallbackInfoReturnable<Path> cir) {
|
||||
if (!GlobalCache.isEnabled || modFile != null)
|
||||
return;
|
||||
resolvedPathByResource.put(Arrays.toString(paths), cir.getReturnValue());
|
||||
}
|
||||
|
||||
@Inject(method = "getNamespaces", at = @At("HEAD"), cancellable = true)
|
||||
public void getNamespacesHeadInjected(PackType type, CallbackInfoReturnable<Set<String>> cir) {
|
||||
if (!GlobalCache.isEnabled)
|
||||
return;
|
||||
Set<String> namespaces = getCachedNamespaces(type);
|
||||
if (namespaces != null)
|
||||
cir.setReturnValue(namespaces);
|
||||
}
|
||||
|
||||
@Inject(method = "getNamespaces", at = @At("RETURN"))
|
||||
public void getNamespacesReturnInjected(PackType type, CallbackInfoReturnable<Set<String>> cir) {
|
||||
if (!GlobalCache.isEnabled)
|
||||
return;
|
||||
cacheNamespaces(type, cir.getReturnValue());
|
||||
}
|
||||
|
||||
@Inject(method = "hasResource(Ljava/lang/String;)Z", at = @At("HEAD"), cancellable = true)
|
||||
public void hasResourceHeadInjected(String name, CallbackInfoReturnable<Boolean> cir) {
|
||||
if (!GlobalCache.isEnabled)
|
||||
return;
|
||||
Boolean exists = exists(name);
|
||||
if (exists != null)
|
||||
cir.setReturnValue(exists);
|
||||
}
|
||||
|
||||
@Inject(method = "hasResource(Ljava/lang/String;)Z", at = @At("RETURN"))
|
||||
public void hasResourceReturnInjected(String name, CallbackInfoReturnable<Boolean> cir) {
|
||||
if (!GlobalCache.isEnabled)
|
||||
return;
|
||||
cacheExists(name, cir.getReturnValue());
|
||||
}
|
||||
|
||||
@Inject(method = "getResources", at = @At(value = "INVOKE", target = "Ljava/nio/file/Path;getFileSystem()Ljava/nio/file/FileSystem;"), locals = LocalCapture.CAPTURE_FAILSOFT, cancellable = true)
|
||||
public synchronized void getResourcesInjected(PackType type, String resourceNamespace, String pathIn, int maxDepth, Predicate<String> filter, CallbackInfoReturnable<Collection<ResourceLocation>> cir, Path root) {
|
||||
if (!GlobalCache.isEnabled)
|
||||
return;
|
||||
String resource = root.toString();
|
||||
Boolean exists = exists(resource);
|
||||
if (exists == null) {
|
||||
cacheExists(resource, exists = Files.exists(root));
|
||||
}
|
||||
if (!exists)
|
||||
cir.setReturnValue(Collections.emptyList());
|
||||
}
|
||||
|
||||
@Redirect(method = "getResources", at = @At(value = "INVOKE", target = "Ljava/nio/file/Files;walk(Ljava/nio/file/Path;[Ljava/nio/file/FileVisitOption;)Ljava/util/stream/Stream;"))
|
||||
public synchronized Stream<Path> getResourcesWalkInjected(Path start, FileVisitOption[] options, PackType type, String resourceNamespace) throws IOException {
|
||||
if (!GlobalCache.isEnabled || !GlobalCache.shouldCacheWalkedPaths)
|
||||
return Files.walk(start);
|
||||
List<Path> paths = getFilePaths(type, resourceNamespace);
|
||||
if (paths == null) {
|
||||
try (Stream<Path> pathStream = Files.walk(start)) {
|
||||
paths = pathStream.collect(Collectors.toList());
|
||||
} finally {
|
||||
cacheFilePaths(type, resourceNamespace, paths == null ? Collections.emptyList() : paths);
|
||||
}
|
||||
}
|
||||
return paths.parallelStream();
|
||||
}
|
||||
|
||||
@Redirect(method = "getResources", at = @At(value = "INVOKE", target = "Ljava/nio/file/FileSystem;getPath(Ljava/lang/String;[Ljava/lang/String;)Ljava/nio/file/Path;"))
|
||||
public Path getResourcesGetPathRedirected(FileSystem instance, String s, String[] strings) {
|
||||
if (!GlobalCache.isEnabled)
|
||||
return instance.getPath(s);
|
||||
Path inputPath = inputPathByPath.get(s);
|
||||
if (inputPath == null) {
|
||||
inputPathByPath.put(s, inputPath = instance.getPath(s));
|
||||
}
|
||||
return inputPath;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void persistAndClearCache() {
|
||||
if (modFile != null) {
|
||||
CacheUtil.persist(getExistenceByResource(), new File(HAS_RESOURCE_CACHE_DIR.getPath(), id + ".ser"));
|
||||
CacheUtil.persist(namespacesByPackType, new File(NAMESPACE_CACHE_DIR.getPath(), id + ".ser"));
|
||||
}
|
||||
getExistenceByResource().clear();
|
||||
resolvedPathByResource.clear();
|
||||
namespacesByPackType.clear();
|
||||
filePathsByRootByPackType.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setModFile(IModFile modFile) {
|
||||
this.modFile = modFile;
|
||||
this.id = modFile.getModFileInfo().moduleName() + modFile.getModFileInfo().versionString()
|
||||
+ "-" + FilenameUtils.getBaseName(modFile.getFilePath().toString()).replaceAll("[^a-zA-Z0-9.-]", "");
|
||||
setExistenceByResource(GlobalCache.PERSISTED_EXISTENCES_BY_MOD.computeIfAbsent(
|
||||
id, i -> Maps.newConcurrentMap()));
|
||||
}
|
||||
|
||||
public Path getResolvedPath(String... paths) {
|
||||
return resolvedPathByResource.get(Arrays.toString(paths));
|
||||
}
|
||||
|
||||
public Boolean exists(String resourceName) {
|
||||
return getExistenceByResource().get(resourceName);
|
||||
}
|
||||
|
||||
public void cacheExists(String resourceName, boolean exists) {
|
||||
getExistenceByResource().put(resourceName, exists);
|
||||
}
|
||||
|
||||
public void cacheFilePaths(PackType packType, String resourceNamespace, List<Path> filePaths) {
|
||||
getFilePathsMap(packType).putIfAbsent(resourceNamespace, filePaths);
|
||||
}
|
||||
|
||||
public List<Path> getFilePaths(PackType packType, String resourceNamespace) {
|
||||
return getFilePathsMap(packType).get(resourceNamespace);
|
||||
}
|
||||
|
||||
private Map<String, List<Path>> getFilePathsMap(PackType packType) {
|
||||
return filePathsByRootByPackType.get(packType);
|
||||
}
|
||||
|
||||
public void cacheNamespaces(PackType packType, Set<String> namespaces) {
|
||||
namespacesByPackType.put(packType, namespaces);
|
||||
}
|
||||
|
||||
public Set<String> getCachedNamespaces(PackType packType) {
|
||||
return namespacesByPackType.get(packType);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.ccr4ft3r.lightspeed.mixin.resources;
|
||||
|
||||
import com.ccr4ft3r.lightspeed.cache.GlobalCache;
|
||||
import com.ccr4ft3r.lightspeed.interfaces.IPackResources;
|
||||
import com.google.common.collect.Maps;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.server.packs.PackType;
|
||||
import net.minecraft.server.packs.metadata.pack.PackMetadataSection;
|
||||
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.util.Map;
|
||||
|
||||
@Mixin(net.minecraft.server.packs.VanillaPackResources.class)
|
||||
public abstract class VanillaPackResourcesMixin implements IPackResources {
|
||||
|
||||
private final Map<String, Boolean> existencePerClientResource = Maps.newConcurrentMap();
|
||||
private final Map<String, Boolean> existencePerServerResource = Maps.newConcurrentMap();
|
||||
|
||||
@Inject(method = "<init>", at = @At("RETURN"))
|
||||
public void initHeadInjected(PackMetadataSection p_143761_, String[] p_143762_, CallbackInfo ci) {
|
||||
if (GlobalCache.isEnabled)
|
||||
GlobalCache.add(this);
|
||||
}
|
||||
|
||||
@Inject(method = "hasResource", at = @At("HEAD"), cancellable = true)
|
||||
public void hasResourceHeadInjected(PackType p_10355_, ResourceLocation p_10356_, CallbackInfoReturnable<Boolean> cir) {
|
||||
if (!GlobalCache.isEnabled)
|
||||
return;
|
||||
Boolean exists = exists(p_10355_, p_10356_.toString());
|
||||
if (exists != null)
|
||||
cir.setReturnValue(exists);
|
||||
}
|
||||
|
||||
@Inject(method = "hasResource", at = @At("RETURN"))
|
||||
public void hasResourceReturnInjected(PackType p_10355_, ResourceLocation p_10356_, CallbackInfoReturnable<Boolean> cir) {
|
||||
if (!GlobalCache.isEnabled)
|
||||
return;
|
||||
cacheExists(p_10355_, p_10356_.toString(), cir.getReturnValue());
|
||||
}
|
||||
|
||||
public Boolean exists(PackType packType, String resourceName) {
|
||||
if (packType == PackType.CLIENT_RESOURCES)
|
||||
return existencePerClientResource.get(resourceName);
|
||||
return existencePerServerResource.get(resourceName);
|
||||
}
|
||||
|
||||
public void cacheExists(PackType packType, String resourceName, boolean exists) {
|
||||
if (packType == PackType.CLIENT_RESOURCES)
|
||||
existencePerClientResource.put(resourceName, exists);
|
||||
else existencePerServerResource.put(resourceName, exists);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void persistAndClearCache() {
|
||||
existencePerClientResource.clear();
|
||||
existencePerServerResource.clear();
|
||||
getExistenceByResource().clear();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.ccr4ft3r.lightspeed.util;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.mojang.logging.LogUtils;
|
||||
import net.minecraft.SharedConstants;
|
||||
import net.minecraftforge.fml.loading.FMLPaths;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Arrays;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class CacheUtil {
|
||||
|
||||
public static final File CACHE_DIR = Paths.get(FMLPaths.GAMEDIR.get().toString(), "lightspeed-cache",
|
||||
SharedConstants.getCurrentVersion().getId()).toFile();
|
||||
public static final File HAS_RESOURCE_CACHE_DIR = new File(CACHE_DIR, "hasResource");
|
||||
public static final File NAMESPACE_CACHE_DIR = new File(CACHE_DIR, "namespaces");
|
||||
|
||||
public static Stream<File> getCacheFiles(File dir) {
|
||||
File[] caches = dir.listFiles((dir1, name) -> name.toLowerCase().endsWith(".ser"));
|
||||
if (caches == null)
|
||||
return Stream.empty();
|
||||
return Arrays.stream(caches)
|
||||
.filter(file -> !file.isDirectory());
|
||||
}
|
||||
|
||||
public static void persist(Map<?, ?> toPersist, File file) {
|
||||
try {
|
||||
FileOutputStream fos = new FileOutputStream(file);
|
||||
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(fos));
|
||||
oos.writeObject(toPersist);
|
||||
oos.flush();
|
||||
oos.close();
|
||||
fos.close();
|
||||
} catch (Exception e) {
|
||||
LogUtils.getLogger().error("Cannot create cache file: {}", file, e);
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static <K, V> Map<K, V> load(File file) {
|
||||
try {
|
||||
FileInputStream fis = new FileInputStream(file);
|
||||
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(fis));
|
||||
ConcurrentHashMap<K, V> loaded = (ConcurrentHashMap<K, V>) ois.readObject();
|
||||
ois.close();
|
||||
fis.close();
|
||||
return loaded;
|
||||
} catch (Exception e) {
|
||||
LogUtils.getLogger().error("Cannot load cache file: {}", file.getName(), e);
|
||||
}
|
||||
return Maps.newConcurrentMap();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.ccr4ft3r.lightspeed.util;
|
||||
|
||||
import com.mojang.datafixers.util.Pair;
|
||||
import net.minecraft.client.renderer.block.model.MultiVariant;
|
||||
import net.minecraft.client.renderer.block.model.Variant;
|
||||
import net.minecraft.client.renderer.block.model.multipart.MultiPart;
|
||||
import net.minecraft.client.resources.model.Material;
|
||||
import net.minecraft.client.resources.model.UnbakedModel;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class UnbakedModelHelper {
|
||||
|
||||
public static Stream<Material> getMaterialsStream(MultiPart multiPart, Function<ResourceLocation, UnbakedModel> p_111976_, Set<Pair<String, String>> p_111977_) {
|
||||
return multiPart.getSelectors().stream().flatMap((p_111981_) -> getMaterialsStream(p_111981_.getVariant(), p_111976_, p_111977_));
|
||||
}
|
||||
|
||||
public static Stream<Material> getMaterialsStream(MultiVariant multiVariant, Function<ResourceLocation, UnbakedModel> p_111855_, Set<Pair<String, String>> p_111856_) {
|
||||
return multiVariant.getVariants().stream().map(Variant::getModelLocation).distinct().flatMap((p_111860_) -> {
|
||||
UnbakedModel unbakedModel = p_111855_.apply(p_111860_);
|
||||
if (unbakedModel instanceof MultiPart multiPart)
|
||||
return getMaterialsStream(multiPart, p_111855_, p_111856_);
|
||||
if (unbakedModel instanceof MultiVariant multiVariant1)
|
||||
return getMaterialsStream(multiVariant1, p_111855_, p_111856_);
|
||||
return unbakedModel.getMaterials(p_111855_, p_111856_).stream();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.ccr4ft3r.lightspeed.util;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import net.minecraft.client.resources.model.BakedModel;
|
||||
import net.minecraft.client.resources.model.WeightedBakedModel;
|
||||
import net.minecraft.util.random.WeightedEntry;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Set;
|
||||
|
||||
public class WeightedBuilder {
|
||||
|
||||
private final Set<WeightedEntry.Wrapper<BakedModel>> list = Sets.newConcurrentHashSet();
|
||||
|
||||
public void add(@Nullable BakedModel p_119560_, int p_119561_) {
|
||||
if (p_119560_ != null)
|
||||
list.add(WeightedEntry.wrap(p_119560_, p_119561_));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public BakedModel build() {
|
||||
if (list.isEmpty()) {
|
||||
return null;
|
||||
} else {
|
||||
return list.size() == 1 ? list.iterator().next().getData() : new WeightedBakedModel(new ArrayList<>(list));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user