Skip to content

Commit e6dfca5

Browse files
committed
Unpick ChatColors for dev.
1 parent 10f6f40 commit e6dfca5

File tree

1 file changed

+58
-17
lines changed

1 file changed

+58
-17
lines changed

patching/src/main/java/com/fox2code/foxloader/patching/dev/DevelopmentSourceConstantData.java

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,18 @@ public class DevelopmentSourceConstantData {
3939
private static final int STATUS_DISABLED = -1;
4040
private static final int STATUS_DEFAULT = 0;
4141
private static final int STATUS_DISPLAY_FIRST = 1;
42+
private static final int PUBLIC_STATIC_FINAL = Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_FINAL;
43+
private static final String CoreConstants = "net/minecraft/common/CoreConstants";
44+
private static final String ChatColors = "net/minecraft/common/util/ChatColors";
4245
public final String internalVersion, version, displayVersion;
4346
private final ConstantCheck[] regular, displayFirst;
47+
private final ArrayList<ConstantCheck> chatColorsConstantChecks;
48+
private final HashSet<String> constants;
4449

45-
private DevelopmentSourceConstantData(ClassNode classNode) {
46-
this.internalVersion = TransformerUtils.getFieldStringData(classNode, "INTERNAL_VERSION");
47-
this.version = TransformerUtils.getFieldStringData(classNode, "VERSION");
48-
this.displayVersion = TransformerUtils.getFieldStringData(classNode, "DISPLAY_VERSION");
50+
private DevelopmentSourceConstantData(ClassNode coreConstantsClassNode, ClassNode chatColorsClassNode) {
51+
this.internalVersion = TransformerUtils.getFieldStringData(coreConstantsClassNode, "INTERNAL_VERSION");
52+
this.version = TransformerUtils.getFieldStringData(coreConstantsClassNode, "VERSION");
53+
this.displayVersion = TransformerUtils.getFieldStringData(coreConstantsClassNode, "DISPLAY_VERSION");
4954
CoreConstantCheck internalVersionCheck = new CoreConstantCheck(this.internalVersion, "INTERNAL_VERSION");
5055
CoreConstantCheck versionCheck = new CoreConstantCheck(this.version, "VERSION");
5156
CoreConstantCheck displayVersionCheck = new CoreConstantCheck(this.displayVersion, "DISPLAY_VERSION");
@@ -56,38 +61,57 @@ private DevelopmentSourceConstantData(ClassNode classNode) {
5661
this.displayFirst = this.regular = new ConstantCheck[]{
5762
displayVersionCheck, versionCheck, internalVersionCheck};
5863
}
64+
this.chatColorsConstantChecks = new ArrayList<>();
65+
this.constants = new HashSet<>();
66+
this.constants.add(this.internalVersion);
67+
this.constants.add(this.version);
68+
this.constants.add(this.displayVersion);
69+
70+
for (FieldNode fieldNode : chatColorsClassNode.fields) {
71+
if ((fieldNode.access & PUBLIC_STATIC_FINAL) == PUBLIC_STATIC_FINAL &&
72+
fieldNode.value != null && fieldNode.desc.equals("Ljava/lang/String;")) {
73+
String value = (String) fieldNode.value;
74+
if (value.length() != 2) continue;
75+
if (this.constants.add(value)) {
76+
this.chatColorsConstantChecks.add(
77+
new ColorConstantCheck(value, fieldNode.name));
78+
}
79+
}
80+
}
5981
}
6082

6183
public static DevelopmentSourceConstantData fromPatchedJar(File patchedJar) throws IOException {
6284
try (JarFile jarFile = new JarFile(patchedJar)) {
63-
JarEntry jarEntry = jarFile.getJarEntry("net/minecraft/common/CoreConstants.class");
85+
JarEntry coreConstantsJarEntry = jarFile.getJarEntry(CoreConstants + ".class");
6486
ClassNode coreConstantsClassNode = new ClassNode();
65-
try (InputStream inputStream = jarFile.getInputStream(jarEntry)) {
87+
try (InputStream inputStream = jarFile.getInputStream(coreConstantsJarEntry)) {
6688
new ClassReader(inputStream).accept(coreConstantsClassNode, ClassReader.SKIP_FRAMES);
6789
}
68-
return new DevelopmentSourceConstantData(coreConstantsClassNode);
90+
JarEntry chatColorsJarEntry = jarFile.getJarEntry(ChatColors + ".class");
91+
ClassNode chatColorsClassNode = new ClassNode();
92+
try (InputStream inputStream = jarFile.getInputStream(chatColorsJarEntry)) {
93+
new ClassReader(inputStream).accept(chatColorsClassNode, ClassReader.SKIP_FRAMES);
94+
}
95+
return new DevelopmentSourceConstantData(coreConstantsClassNode, chatColorsClassNode);
6996
}
7097
}
7198

7299
public List<ConstantCheck> getClassConstantChecks(ClassNode classNode) {
73-
if ("net/minecraft/common/CoreConstants".equals(classNode.name)) {
100+
if (CoreConstants.equals(classNode.name) || ChatColors.equals(classNode.name)) {
74101
return Collections.emptyList();
75102
}
76103
ArrayList<ConstantCheck> constantChecks = null;
77-
HashSet<String> usedConstant = null;
104+
HashSet<String> usedConstants = null;
78105
for (FieldNode fieldNode : classNode.fields) {
79106
if (((fieldNode.access & (Opcodes.ACC_STATIC | Opcodes.ACC_FINAL)) ==
80107
(Opcodes.ACC_STATIC | Opcodes.ACC_FINAL)) && fieldNode.value != null &&
81108
"Ljava/lang/String;".equals(fieldNode.desc)) {
82109
String value = (String) fieldNode.value;
83110
if (value.length() <= 1) continue;
84-
if (usedConstant == null) {
85-
usedConstant = new HashSet<>();
86-
usedConstant.add(this.internalVersion);
87-
usedConstant.add(this.version);
88-
usedConstant.add(this.displayVersion);
111+
if (usedConstants == null) {
112+
usedConstants = new HashSet<>(this.constants);
89113
}
90-
if (usedConstant.add(value)) {
114+
if (usedConstants.add(value)) {
91115
if (constantChecks == null) constantChecks = new ArrayList<>();
92116
constantChecks.add(new ClassConstantCheck(value, classNode.name, fieldNode.name));
93117
} else {
@@ -101,7 +125,7 @@ public List<ConstantCheck> getClassConstantChecks(ClassNode classNode) {
101125

102126
public int methodStatus(ClassNode classNode, MethodNode methodNode) {
103127
switch (classNode.name) {
104-
case "net/minecraft/common/CoreConstants":
128+
case CoreConstants:
105129
return STATUS_DISABLED;
106130
case "net/minecraft/client/Minecraft":
107131
return "startGame".equals(methodNode.name) ? STATUS_DISPLAY_FIRST : STATUS_DEFAULT;
@@ -127,6 +151,9 @@ public void patchStringConstant(IdentityHashMap<AbstractInsnNode, InsnList> cons
127151
for (ConstantCheck constantCheck : classConstantCheck) {
128152
constantCheck.applyCheck(list);
129153
}
154+
for (ConstantCheck constantCheck : this.chatColorsConstantChecks) {
155+
constantCheck.applyCheck(list);
156+
}
130157
if (list.size() != 1 || list.get(0).getOpcode() != Opcodes.LDC) {
131158
constantPatching.put(ldcInsnNode,
132159
TransformerUtils.compileStringAppendChain(list));
@@ -186,7 +213,21 @@ private CoreConstantCheck(String value, String fieldName) {
186213

187214
public FieldInsnNode makeFieldInsnNode() {
188215
return new FieldInsnNode(Opcodes.GETSTATIC,
189-
"net/minecraft/common/CoreConstants", this.fieldName, "Ljava/lang/String;");
216+
CoreConstants, this.fieldName, "Ljava/lang/String;");
217+
}
218+
}
219+
220+
private static class ColorConstantCheck extends ConstantCheck {
221+
private final String fieldName;
222+
223+
private ColorConstantCheck(String value, String fieldName) {
224+
super(value);
225+
this.fieldName = fieldName;
226+
}
227+
228+
public FieldInsnNode makeFieldInsnNode() {
229+
return new FieldInsnNode(Opcodes.GETSTATIC,
230+
ChatColors, this.fieldName, "Ljava/lang/String;");
190231
}
191232
}
192233

0 commit comments

Comments
 (0)