Skip to content

Commit

Permalink
Fix incompatibilities with Optifine and Galacticraft
Browse files Browse the repository at this point in the history
  • Loading branch information
Runemoro committed Jan 4, 2019
1 parent ed18645 commit 708ec7e
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 31 deletions.
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,17 @@ dependencies {
def travisBuildNumber = System.getenv("TRAVIS_BUILD_NUMBER")
def versionSuffix = travisBuildNumber != null ? travisBuildNumber : "SNAPSHOT"

version "1.0.10-$versionSuffix"
version "1.1-$versionSuffix"
group "org.dimdev.vanillafix"
archivesBaseName = "VanillaFix"

sourceCompatibility = 1.8
targetCompatibility = 1.8

sourceSets {
main
}

minecraft {
version "1.12.2-14.23.4.2703"
runDir "run"
Expand Down
3 changes: 3 additions & 0 deletions src/api/java/net/optifine/render/RenderEnv.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package net.optifine.render;

public class RenderEnv {}
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,9 @@
import java.util.Set;

public class ModCompatibilityMixinPlugin implements IMixinConfigPlugin {
private boolean spongeInstalled;

@Override
public void onLoad(String mixinPackage) {
try {
spongeInstalled = Launch.classLoader.getClassBytes("org.spongepowered.mod.SpongeCoremod") != null;
} catch (IOException e) {
throw new RuntimeException(e); // Should never happen
}

}

@Override
Expand All @@ -27,16 +21,30 @@ public String getRefMapperConfig() {
}

@Override
@SuppressWarnings("RedundantIfStatement")
public boolean shouldApplyMixin(String targetClassName, String mixinClassName) {
// Sponge
if (spongeInstalled) {
if (mixinClassName.equals("org.dimdev.vanillafix.profiler.mixins.MixinWorld")) return false;
if (mixinClassName.equals("org.dimdev.vanillafix.profiler.mixins.MixinWorld")) {
return !classExists("org.spongepowered.mod.SpongeCoremod");
}

if (mixinClassName.equals("org.dimdev.vanillafix.textures.mixins.client.MixinBlockModelRenderer")) {
return !classExists("optifine.OptiFineForgeTweaker");
}

if (mixinClassName.equals("org.dimdev.vanillafix.textures.mixins.client.MixinBlockModelRendererOptifine")) {
return classExists("optifine.OptiFineForgeTweaker");
}

return true;
}

public boolean classExists(String name) {
try {
return Launch.classLoader.getClassBytes(name) != null;
} catch (IOException e) {
throw new RuntimeException(e);
}
}

@Override
public void acceptTargets(Set<String> myTargets, Set<String> otherTargets) {}

Expand Down
17 changes: 7 additions & 10 deletions src/main/java/org/dimdev/vanillafix/VanillaFixLoadingPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
import net.minecraft.launchwrapper.Launch;
import net.minecraftforge.common.ForgeVersion;
import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin;
import net.minecraftforge.fml.relauncher.libraries.LibraryManager;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dimdev.utils.SSLUtils;
Expand All @@ -17,13 +13,12 @@
import sun.misc.URLClassPath;

import javax.annotation.Nullable;
import java.io.*;
import java.lang.management.ManagementFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
Expand All @@ -38,20 +33,21 @@
public class VanillaFixLoadingPlugin implements IFMLLoadingPlugin {
private static final Logger log = LogManager.getLogger("VanillaFix");
public static LoadingConfig config;
private static final boolean deobfuscatedEnvironment = VanillaFixLoadingPlugin.class.getResource("/net/minecraft/world/World.class") != null;
// private static final boolean deobfuscatedEnvironment = VanillaFixLoadingPlugin.class.getResource("/net/minecraft/world/World.class") != null;

static {
log.info("Initializing VanillaFix");
config = new LoadingConfig(new File(Launch.minecraftHome, "config/vanillafix.cfg"));
config.improvedLaunchWrapper = false; // TODO: fix this

replaceLaunchWrapper();
// replaceLaunchWrapper();
trustIdenTrust();
initStacktraceDeobfuscator();
fixMixinClasspathOrder();
initMixin();
}

/*
private static void replaceLaunchWrapper() {
if (!config.improvedLaunchWrapper) {
log.info("LaunchWrapper replacement disabled by config");
Expand Down Expand Up @@ -136,6 +132,7 @@ private static String makeNewCommandLine(String newLibraryPath) {
return command.toString();
}
*/

private static void trustIdenTrust() {
// Trust the "IdenTrust DST Root CA X3" certificate (used by Let's Encrypt, which is used by paste.dimdev.org)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package org.dimdev.vanillafix.blockstates.mixins;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.*;
import net.minecraft.block.Block;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.util.MapPopulator;
import net.minecraft.util.math.Cartesian;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.common.property.ExtendedBlockState;
import net.minecraftforge.common.property.IUnlistedProperty;
import org.dimdev.vanillafix.blockstates.IPatchedBlockStateContainer;
import org.dimdev.vanillafix.blockstates.NumericalBlockState;
Expand All @@ -18,40 +17,74 @@
import javax.annotation.Nullable;
import java.util.*;

@SuppressWarnings({"ResultOfMethodCallIgnored", "ConstructorNotProtectedInAbstractClass"})
@Mixin(BlockStateContainer.class)
public abstract class MixinBlockStateContainer implements IPatchedBlockStateContainer {
// @formatter:off
@Shadow @Final @Mutable private Block block;
@Shadow @Final @Mutable private ImmutableSortedMap<String, IProperty<?>> properties;
@Shadow public static <T extends Comparable<T>> String validateProperty(Block block, IProperty<T> property) { return null; }
@Shadow protected abstract List<Iterable<Comparable<?>>> getAllowedValues();
@Shadow public abstract Block getBlock();
@Shadow @Final private ImmutableList<IBlockState> validStates;
// @formatter:on

@SuppressWarnings("EqualsBetweenInconvertibleTypes") // mixin
private boolean isNumerical = getClass().equals(BlockStateContainer.class) || getClass().equals(ExtendedBlockState.class);
private final ImmutableMap<IUnlistedProperty<?>, Optional<?>> unlistedProperties;
private final Map<IProperty<?>, Integer> propertyOffsets = new HashMap<>();
protected ImmutableList<IBlockState> validStatesCache;
protected ImmutableList<IBlockState> validStatesCache = null;
@SuppressWarnings({"FieldCanBeLocal", "unused"}) private final Object x; // workaround for mixin bug

@Overwrite
public MixinBlockStateContainer(Block block, IProperty<?>[] properties, ImmutableMap<IUnlistedProperty<?>, Optional<?>> unlistedProperties) {
this.block = block;
this.unlistedProperties = unlistedProperties;

// Immutable map builder won't work, some mods have duplicate properties
LinkedHashMap<String, IProperty<?>> propertyMap = new LinkedHashMap<>();
Map<String, IProperty<?>> propertyMap = new LinkedHashMap<>();
int offset = 0;

for (IProperty<?> property : properties) {
validateProperty(block, property);
propertyMap.put(property.getName(), property);

NumericalBlockState.makePropertyInfo(property);
propertyOffsets.put(property, offset);
offset += MathHelper.log2(property.getAllowedValues().size()) + 1;
if (isNumerical) {
NumericalBlockState.makePropertyInfo(property);
propertyOffsets.put(property, offset);
offset += MathHelper.log2(property.getAllowedValues().size()) + 1;
}
}

this.properties = ImmutableSortedMap.copyOf(propertyMap);

if (!isNumerical) {
Map<Map<IProperty<?>, Comparable<?>>, BlockStateContainer.StateImplementation> map2 = Maps.newLinkedHashMap();
List<BlockStateContainer.StateImplementation> validStates = Lists.newArrayList();

for (List<Comparable<?>> list : Cartesian.cartesianProduct(getAllowedValues())) {
Map<IProperty<?>, Comparable<?>> map1 = MapPopulator.createMap(this.properties.values(), list);
BlockStateContainer.StateImplementation blockstatecontainer$stateimplementation = createState(block, ImmutableMap.copyOf(map1), unlistedProperties);
map2.put(map1, blockstatecontainer$stateimplementation);
validStates.add(blockstatecontainer$stateimplementation);
}

for (BlockStateContainer.StateImplementation blockstatecontainer$stateimplementation1 : validStates) {
blockstatecontainer$stateimplementation1.buildPropertyValueTable(map2);
}

this.validStates = ImmutableList.copyOf(validStates);
}

x = new Object();
}

@Overwrite
public ImmutableList<IBlockState> getValidStates() {
if (!isNumerical) {
return validStates;
}

if (validStatesCache == null) {
ImmutableList.Builder<IBlockState> states = ImmutableList.builder();

Expand All @@ -69,6 +102,10 @@ public ImmutableList<IBlockState> getValidStates() {

@Overwrite(remap = false)
protected BlockStateContainer.StateImplementation createState(Block block, ImmutableMap<IProperty<?>, Comparable<?>> properties, @Nullable ImmutableMap<IUnlistedProperty<?>, Optional<?>> unlistedProperties) {
if (!isNumerical) {
return new BlockStateContainer.StateImplementation(block, properties);
}

return null;
}

Expand All @@ -83,6 +120,10 @@ protected IBlockState createState(ImmutableMap<IProperty<?>, Comparable<?>> prop

@Overwrite
public IBlockState getBaseState() {
if (!isNumerical) {
return validStates.get(0);
}

if (validStatesCache != null) {
return validStatesCache.get(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ protected BlockStateContainer.StateImplementation createState(Block block, Immut

@Override
protected IBlockState createState(ImmutableMap<IProperty<?>, Comparable<?>> properties, @Nullable ImmutableMap<IUnlistedProperty<?>, Optional<?>> unlistedProperties) {
if (getClass() != MixinExtendedBlockState.class) {
return createState(getBlock(), properties, unlistedProperties);
}

IBlockState normalState = super.createState(properties, unlistedProperties);
if (unlistedProperties == null || unlistedProperties.isEmpty()) {
return normalState;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.dimdev.vanillafix.textures.mixins.client;

import net.minecraft.block.state.IBlockState;
import net.minecraft.client.renderer.BlockModelRenderer;
import net.minecraft.client.renderer.BufferBuilder;
import net.minecraft.client.renderer.block.model.BakedQuad;
import net.minecraft.client.renderer.chunk.CompiledChunk;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.optifine.render.RenderEnv;
import org.dimdev.vanillafix.textures.IPatchedCompiledChunk;
import org.dimdev.vanillafix.textures.IPatchedTextureAtlasSprite;
import org.dimdev.vanillafix.textures.TemporaryStorage;
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.util.List;
import java.util.Set;

@Mixin(BlockModelRenderer.class)
public class MixinBlockModelRendererOptifine {
@Inject(method = "renderQuadsSmooth", remap = false, at = @At("HEAD"))
private void onRenderQuadsSmooth(IBlockAccess blockAccess, IBlockState state, BlockPos pos, BufferBuilder buffer, List<BakedQuad> quads, RenderEnv renderEnv, CallbackInfo ci) {
markQuads(quads);
}

@Inject(method = "renderQuadsFlat", remap = false, at = @At("HEAD"))
private void onRenderQuadsFlat(IBlockAccess blockAccess, IBlockState state, BlockPos pos, int brightness, boolean ownBrightness, BufferBuilder buffer, List<BakedQuad> quads, RenderEnv renderEnv, CallbackInfo ci) {
markQuads(quads);
}

private static void markQuads(List<BakedQuad> quads) {
CompiledChunk compiledChunk = TemporaryStorage.currentCompiledChunk.get();
if (compiledChunk != null) {
Set<TextureAtlasSprite> visibleTextures = ((IPatchedCompiledChunk) compiledChunk).getVisibleTextures();

for (BakedQuad quad : quads) {
if (quad.getSprite() != null) {
visibleTextures.add(quad.getSprite());
}
}
} else {
// Called from non-chunk render thread. Unfortunately, the best we can do
// is assume it's only going to be used once:
for (BakedQuad quad : quads) {
if (quad.getSprite() != null) {
((IPatchedTextureAtlasSprite) quad.getSprite()).markNeedsAnimationUpdate();
}
}
}
}
}
3 changes: 3 additions & 0 deletions src/main/resources/META-INF/vanillafix_at.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ public net.minecraft.client.renderer.texture.TextureAtlasSprite <init>(Ljava/lan

# dynamicresources.MixinGlStateManager
public net.minecraft.client.renderer.GlStateManager$TextureState

# blockstates.MixinBlockStateContainer
public net.minecraft.block.state.BlockStateContainer$StateImplementation <init>(Lnet/minecraft/block/Block;Lcom/google/common/collect/ImmutableMap;)V
Binary file removed src/main/resources/launchwrapper-2.0.jar_
Binary file not shown.
3 changes: 2 additions & 1 deletion src/main/resources/mixins.vanillafix.textures.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"client.MixinTextureMap",
"client.MixinRender",
"client.MixinTextureAtlasSprite",
"client.MixinForgeBlockModelRenderer"
"client.MixinForgeBlockModelRenderer",
"client.MixinBlockModelRendererOptifine"
],
"injectors": {
"maxShiftBy": 10
Expand Down

0 comments on commit 708ec7e

Please sign in to comment.