Skip to content

Commit f611017

Browse files
committed
Add OptiFine transformer hook
1 parent aaa2f28 commit f611017

File tree

6 files changed

+157
-1
lines changed

6 files changed

+157
-1
lines changed

README.MD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ A library for 1.7.10 with lots of useful stuff, licensed under the LGPLv3 licens
99
| [dependencies](src/main/java/com/falsepattern/lib/dependencies) | Runtime dependency loader which uses Maven (DEPRECATED, see [The new data-driven dependency format](src/main/resources/DEPENDENCIES.md) |
1010
| [mapping](src/main/java/com/falsepattern/lib/mapping) | Notch-SRG-MCP name mapping helper code |
1111
| [mixin](src/main/java/com/falsepattern/lib/mixin) | Mixin loader plugin boilerplate code |
12+
| [optifine](src/main/java/com/falsepattern/lib/optifine) | Tools for messing with OptiFine |
1213
| [text](src/main/java/com/falsepattern/lib/text) | Better Chat and GUI text processing |
1314
| [toasts](src/main/java/com/falsepattern/lib/toasts) | The toast system from newer versions, with some extras |
1415
| [util](src/main/java/com/falsepattern/lib/util) | Additional utilities that do not fit the above categories, see below for more information |

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ apiPackage =
1919
# Packages separated by semicolons
2020
# ExampleValue: apiPackages = foo;bar;baz
2121
# ... foo;bar;baz -> com.myname.mymodid.foo;com.myname.mymodid.bar;com.myname.mymodid.baz
22-
apiPackages = asm;compat;config;dependencies;mapping;mixin;text;toasts;util
22+
apiPackages = asm;compat;config;dependencies;mapping;mixin;optifine;text;toasts;util
2323

2424
# Same as apiPackages, but does not pull in inner packages
2525
apiPackagesNoRecurse = .

src/main/java/com/falsepattern/lib/internal/asm/FPTransformer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.falsepattern.lib.internal.asm.transformers.GasStationValidatorTransformer;
3030
import com.falsepattern.lib.internal.asm.transformers.IMixinPluginTransformer;
3131
import com.falsepattern.lib.internal.asm.transformers.ITypeDiscovererTransformer;
32+
import com.falsepattern.lib.internal.impl.optifine.OptiFineTransformerHooksImpl;
3233
import lombok.Getter;
3334
import lombok.experimental.Accessors;
3435
import org.apache.logging.log4j.LogManager;
@@ -48,6 +49,10 @@ public class FPTransformer implements SmartTransformer {
4849
@Getter
4950
private final Logger logger = LOG;
5051

52+
static {
53+
OptiFineTransformerHooksImpl.init();
54+
}
55+
5156
public FPTransformer() {
5257
transformers = Arrays.asList(new IMixinPluginTransformer(),
5358
new ITypeDiscovererTransformer(),
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (C) 2022-2023 FalsePattern
3+
* All Rights Reserved
4+
*
5+
* The above copyright notice and this permission notice
6+
* shall be included in all copies or substantial portions of the Software.
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Lesser General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*/
21+
22+
package com.falsepattern.lib.internal.impl.optifine;
23+
24+
import com.falsepattern.lib.internal.Share;
25+
import lombok.val;
26+
27+
import net.minecraft.launchwrapper.IClassTransformer;
28+
import net.minecraft.launchwrapper.Launch;
29+
import net.minecraft.launchwrapper.LaunchClassLoader;
30+
31+
import java.lang.reflect.Field;
32+
import java.util.HashMap;
33+
import java.util.HashSet;
34+
import java.util.List;
35+
import java.util.Map;
36+
import java.util.Set;
37+
38+
public class OptiFineTransformerHooksImpl {
39+
private static final Set<String> disabledPatches = new HashSet<>();
40+
public static void init() {
41+
Field tField;
42+
try {
43+
tField = LaunchClassLoader.class.getDeclaredField("transformers");
44+
} catch (NoSuchFieldException e) {
45+
Share.LOG.error("Could not retrieve transformers field", e);
46+
return;
47+
}
48+
tField.setAccessible(true);
49+
List<IClassTransformer> transformers;
50+
try {
51+
transformers = (List<IClassTransformer>)tField.get(Launch.classLoader);
52+
} catch (IllegalAccessException e) {
53+
Share.LOG.error("Could not retrieve transformers list", e);
54+
return;
55+
}
56+
for (int i = 0; i < transformers.size(); i++) {
57+
val transformer = transformers.get(i);
58+
System.out.println(transformer.getClass().getName());
59+
if (transformer.getClass().getName().equals("optifine.OptiFineClassTransformer")) {
60+
Share.LOG.info("Attaching OptiFine ASM transformer hooks");
61+
transformers.set(i, new WrappedOptiFineClassTransformer(transformer));
62+
}
63+
}
64+
}
65+
66+
public static void disableOptiFinePatch(String patchName) {
67+
disabledPatches.add(patchName);
68+
}
69+
70+
public static boolean isDisabled(String className) {
71+
return disabledPatches.contains(className);
72+
}
73+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (C) 2022-2023 FalsePattern
3+
* All Rights Reserved
4+
*
5+
* The above copyright notice and this permission notice
6+
* shall be included in all copies or substantial portions of the Software.
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Lesser General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*/
21+
22+
package com.falsepattern.lib.internal.impl.optifine;
23+
24+
import lombok.RequiredArgsConstructor;
25+
26+
import net.minecraft.launchwrapper.IClassTransformer;
27+
28+
@RequiredArgsConstructor
29+
public class WrappedOptiFineClassTransformer implements IClassTransformer {
30+
private final IClassTransformer optiFineTransformer;
31+
@Override
32+
public byte[] transform(String name, String transformedName, byte[] basicClass) {
33+
if (OptiFineTransformerHooksImpl.isDisabled(name)) {
34+
return basicClass;
35+
}
36+
return optiFineTransformer.transform(name, transformedName, basicClass);
37+
}
38+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright (C) 2022-2023 FalsePattern
3+
* All Rights Reserved
4+
*
5+
* The above copyright notice and this permission notice
6+
* shall be included in all copies or substantial portions of the Software.
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the GNU Lesser General Public License as published by
10+
* the Free Software Foundation, either version 3 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public License
19+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
20+
*/
21+
22+
package com.falsepattern.lib.optifine;
23+
24+
import com.falsepattern.lib.StableAPI;
25+
import com.falsepattern.lib.internal.impl.optifine.OptiFineTransformerHooksImpl;
26+
27+
/**
28+
* A utility for manipulating OptiFine's patches.
29+
* If you have a mod that injects code that would otherwise conflict with OptiFine, and you have provably tested that
30+
* removing certain OptiFine patches from the jar does NOT break anything, you may use this class to disable them
31+
* without having to do jar file edits.
32+
*/
33+
@StableAPI(since = "1.0.0")
34+
public class OptiFineTransformerHooks {
35+
@StableAPI.Expose
36+
public static void disableOptiFinePatch(String patchName) {
37+
OptiFineTransformerHooksImpl.disableOptiFinePatch(patchName);
38+
}
39+
}

0 commit comments

Comments
 (0)