aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/makamys/neodymium/Compat.java
blob: 452a187ef088eb0272acd379311abfef1181b1d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package makamys.neodymium;

import com.falsepattern.triangulator.api.ToggleableTessellator;
import cpw.mods.fml.common.Loader;
import makamys.neodymium.config.Config;
import makamys.neodymium.util.virtualjar.IVirtualJar;
import makamys.neodymium.util.virtualjar.VirtualJar;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.settings.GameSettings;
import org.lwjgl.opengl.GLContext;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;

import static makamys.neodymium.Constants.LOGGER;

public class Compat {
    
    private static boolean isGL33Supported;
    
    private static boolean wasAdvancedOpenGLEnabled;
    
    private static int notEnoughVRAMAmountMB = -1;

    private static boolean IS_RPLE_PRESENT;

    private static boolean IS_FALSE_TWEAKS_PRESENT;
    
    private static boolean IS_HODGEPODGE_SPEEDUP_ANIMATIONS_ENABLED;

    private static boolean isShadersEnabled;
    
    public static void init() {
        isGL33Supported = GLContext.getCapabilities().OpenGL33;
        
        if (Loader.isModLoaded("triangulator")) {
            disableTriangulator();
        }

        if (Loader.isModLoaded("rple")) {
            IS_RPLE_PRESENT = true;
        }

        if (Loader.isModLoaded("falsetweaks")) {
            IS_FALSE_TWEAKS_PRESENT = true;
        }
        
        IS_HODGEPODGE_SPEEDUP_ANIMATIONS_ENABLED = checkIfHodgepodgeSpeedupAnimationsIsEnabled();
    }

    private static boolean checkIfHodgepodgeSpeedupAnimationsIsEnabled() {
        boolean result = false;
        if (Loader.isModLoaded("hodgepodge")) {
            try {
                Class<?> CommonCls = Class.forName("com.mitchej123.hodgepodge.Common");
                Object config = CommonCls.getField("config").get(null);
                Class<?> configCls = config.getClass();
                boolean speedupAnimations = (Boolean)configCls.getField("speedupAnimations").get(config);
                LOGGER.debug("Hodgepodge's speedupAnimations is set to " + speedupAnimations);
                result = speedupAnimations;
            } catch(Exception e) {
                LOGGER.warn("Failed to determine if Hodgepodge's speedupAnimations is enabled, assuming false");
            }
        } else {
            LOGGER.debug("Hodgepodge is missing, treating speedupAnimations as false");
        }
        LOGGER.debug("Compat fix will " + (result ? "" : "not") + " be enabled");
        return result;
    }

    public static boolean isRPLEModPresent() {
        return IS_RPLE_PRESENT;
    }

    public static boolean isFalseTweaksModPresent() {
        return IS_FALSE_TWEAKS_PRESENT;
    }
    
    public static boolean isHodgepodgeSpeedupAnimationsEnabled() {
        return IS_HODGEPODGE_SPEEDUP_ANIMATIONS_ENABLED;
    }

    public static boolean isOptiFineShadersEnabled() {
        return isShadersEnabled;
    }

    public static void updateOptiFineShadersState() {
        try {
            Class<?> shaders = Class.forName("shadersmod.client.Shaders");
            try {
                String shaderPack = (String)shaders.getMethod("getShaderPackName").invoke(null);
                if(shaderPack != null) {
                    isShadersEnabled = true;
                    return;
                }
            } catch(Exception e) {
                LOGGER.warn("Failed to get shader pack name");
                e.printStackTrace();
            }
        } catch (ClassNotFoundException e) {

        }
        isShadersEnabled = false;
    }

    private static void disableTriangulator() {
        ((ToggleableTessellator)Tessellator.instance).disableTriangulator();
    }
    
    public static void getCompatibilityWarnings(List<Warning> warns, List<Warning> criticalWarns, boolean statusCommand){
        if(Minecraft.getMinecraft().gameSettings.advancedOpengl) {
            warns.add(new Warning("Advanced OpenGL is enabled, performance may be poor." + (statusCommand ? " Click here to disable it." : "")).chatAction("neodymium disable_advanced_opengl"));
        }
        
        if(!isGL33Supported) {
            criticalWarns.add(new Warning("OpenGL 3.3 is not supported."));
        }
        if(detectedNotEnoughVRAM()) {
            criticalWarns.add(new Warning("Not enough VRAM"));
        }
    }

    public static boolean hasChanged() {
        boolean changed = false;
        
        boolean advGL = Minecraft.getMinecraft().gameSettings.advancedOpengl;
        if(advGL != wasAdvancedOpenGLEnabled) {
            changed = true;
        }
        wasAdvancedOpenGLEnabled = advGL;
        
        return changed;
    }
    
    public static void onNotEnoughVRAM(int amountMB) {
        notEnoughVRAMAmountMB = amountMB;
    }
    
    public static void reset() {
        notEnoughVRAMAmountMB = -1;
    }
    
    private static boolean detectedNotEnoughVRAM() {
        return Config.VRAMSize == notEnoughVRAMAmountMB;
    }

    public static void forceEnableOptiFineDetectionOfFastCraft() {
        if(Compat.class.getResource("/fastcraft/Tweaker.class") != null) {
            // If OptiFine is present, it's already on the class path at this point, so our virtual jar won't override it.
            LOGGER.info("FastCraft is present, applying hack to forcingly enable FastCraft's OptiFine compat");
            VirtualJar.add(new OptiFineStubVirtualJar());
        }
    }
    
    public static boolean disableAdvancedOpenGL() {
        GameSettings gameSettings = Minecraft.getMinecraft().gameSettings;
        
        if(gameSettings.advancedOpengl) {
            gameSettings.advancedOpengl = false;
            gameSettings.saveOptions();
            return true;
        }
        return false;
    }

    private static class OptiFineStubVirtualJar implements IVirtualJar {

        @Override
        public String getName() {
            return "optifine-stub";
        }

        @Override
        public InputStream getInputStream(String path) {
            if(path.equals("/optifine/OptiFineForgeTweaker.class")) {
                // Dummy file to make FastCraft think OptiFine is present.
                LOGGER.debug("Returning a dummy /optifine/OptiFineForgeTweaker.class to force FastCraft compat.");
                return new ByteArrayInputStream(new byte[0]);
            } else {
                return null;
            }
        }
        
    }
    
    public static class Warning {
        public String text;
        public String chatAction;
        
        public Warning(String text) {
            this.text = text;
        }
        
        public Warning chatAction(String command) {
            this.chatAction = command;
            return this;
        }
    }
}