-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Deal with old malformed UUID's being invalid after Java 8
- Loading branch information
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.cleanroommc.loader.javafix; | ||
|
||
import net.minecraftforge.fml.common.FMLLog; | ||
|
||
import java.util.UUID; | ||
|
||
public class UUIDFix { | ||
|
||
public static UUID fromString(String uuid) | ||
{ | ||
try | ||
{ | ||
return UUID.fromString(uuid); | ||
} | ||
catch (IllegalArgumentException e) | ||
{ | ||
if ("UUID string too large".equals(e.getMessage())) | ||
{ | ||
return oldFromString(uuid); | ||
} | ||
throw e; | ||
} | ||
} | ||
|
||
private static UUID oldFromString(String uuid) | ||
{ | ||
String[] components = uuid.split("-"); | ||
if (components.length != 5) | ||
{ | ||
throw new IllegalArgumentException("Invalid UUID string: " + uuid); | ||
} | ||
FMLLog.log.error("UUID [{}] is being processed with the approach from Java 8 for compatibility's sake. This UUID is malformed!", uuid); | ||
for (int i = 0; i < 5; i++) | ||
{ | ||
components[i] = "0x" + components[i]; | ||
} | ||
long mostSigBits = Long.decode(components[0]); | ||
mostSigBits <<= 16; | ||
mostSigBits |= Long.decode(components[1]); | ||
mostSigBits <<= 16; | ||
mostSigBits |= Long.decode(components[2]); | ||
|
||
long leastSigBits = Long.decode(components[3]); | ||
leastSigBits <<= 48; | ||
leastSigBits |= Long.decode(components[4]); | ||
|
||
return new UUID(mostSigBits, leastSigBits); | ||
} | ||
|
||
private UUIDFix() { } | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
src/main/java/net/minecraftforge/fml/common/asm/transformers/MalformedUUIDTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package net.minecraftforge.fml.common.asm.transformers; | ||
|
||
import net.minecraft.launchwrapper.IClassTransformer; | ||
import org.objectweb.asm.ClassReader; | ||
import org.objectweb.asm.ClassWriter; | ||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.tree.*; | ||
|
||
public class MalformedUUIDTransformer implements IClassTransformer | ||
{ | ||
|
||
@Override | ||
public byte[] transform(String s, String s1, byte[] bytes) | ||
{ | ||
if (bytes == null) | ||
{ | ||
return null; | ||
} | ||
ClassNode classNode = new ClassNode(); | ||
ClassReader classReader = new ClassReader(bytes); | ||
classReader.accept(classNode, 0); | ||
boolean modified = false; | ||
if (classNode.methods != null) | ||
{ | ||
for (MethodNode methodNode : classNode.methods) | ||
{ | ||
InsnList instructions = methodNode.instructions; | ||
if (instructions != null) | ||
{ | ||
for (AbstractInsnNode insnNode : instructions) | ||
{ | ||
if (insnNode.getOpcode() == Opcodes.INVOKESTATIC && insnNode instanceof MethodInsnNode methodInsnNode) | ||
{ | ||
if ("java/util/UUID".equals(methodInsnNode.owner) && "fromString".equalsIgnoreCase(methodInsnNode.name)) | ||
{ | ||
methodInsnNode.owner = "com/cleanroommc/loader/javafix/UUIDFix"; | ||
modified = true; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
if (modified) | ||
{ | ||
ClassWriter classWriter = new ClassWriter(0); | ||
|
||
classNode.accept(classWriter); | ||
return classWriter.toByteArray(); | ||
} | ||
return bytes; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters