#32 Update the utility class

This commit is contained in:
ME1312 2019-01-14 18:28:18 -05:00
parent df159f72fe
commit 622ef32b4e
No known key found for this signature in database
GPG Key ID: FEFFE2F698E88FA8
36 changed files with 395 additions and 261 deletions

View File

@ -1,6 +1,7 @@
package net.ME1312.SubServers.Bungee.Host;
import net.ME1312.SubServers.Bungee.Library.Compatibility.JNA;
import net.ME1312.SubServers.Bungee.Library.Util;
import java.io.File;
import java.lang.reflect.Field;
@ -44,10 +45,7 @@ public class Executable {
} catch (Throwable ex) {
try {
if (process.getClass().getName().equals("java.lang.Win32Process") || process.getClass().getName().equals("java.lang.ProcessImpl")) {
Field f = process.getClass().getDeclaredField("handle");
f.setAccessible(true);
long handle = f.getLong(process);
f.setAccessible(false);
long handle = Util.reflect(process.getClass().getDeclaredField("handle"), process);
ClassLoader jna = JNA.get();
Class<?> pc = jna.loadClass("com.sun.jna.Pointer"),
@ -58,10 +56,7 @@ public class Executable {
ntc.getMethod("setPointer", pc).invoke(nt, pc.getMethod("createConstant", long.class).invoke(null, handle));
return ((Number) k32c.getMethod("GetProcessId", ntc).invoke(k32, nt)).longValue();
} else if (process.getClass().getName().equals("java.lang.UNIXProcess")) {
Field f = process.getClass().getDeclaredField("pid");
f.setAccessible(true);
Object response = f.get(process);
f.setAccessible(false);
Object response = Util.reflect(process.getClass().getDeclaredField("pid"), process);
if (response instanceof Number)
return ((Number) response).longValue();

View File

@ -239,10 +239,7 @@ public class ExternalSubServer extends SubServerContainer {
break;
case "group":
if (value.isList()) {
Field f = ServerContainer.class.getDeclaredField("groups");
f.setAccessible(true);
f.set(this, value.asStringList());
f.setAccessible(false);
Util.reflect(ServerContainer.class.getDeclaredField("groups"), this, value.asStringList());
if (this.host.plugin.config.get().getSection("Servers").getKeys().contains(getName())) {
this.host.plugin.config.get().getSection("Servers").getSection(getName()).set("Group", value.asStringList());
this.host.plugin.config.save();
@ -280,10 +277,7 @@ public class ExternalSubServer extends SubServerContainer {
break;
case "motd":
if (value.isString()) {
Field f = BungeeServerInfo.class.getDeclaredField("motd");
f.setAccessible(true);
f.set(this, value.asColoredString('&'));
f.setAccessible(false);
Util.reflect(BungeeServerInfo.class.getDeclaredField("motd"), this, value.asColoredString('&'));
if (this.host.plugin.config.get().getSection("Servers").getKeys().contains(getName())) {
this.host.plugin.config.get().getSection("Servers").getSection(getName()).set("Motd", value.asString());
this.host.plugin.config.save();
@ -383,10 +377,7 @@ public class ExternalSubServer extends SubServerContainer {
break;
case "restricted":
if (value.isBoolean()) {
Field f = BungeeServerInfo.class.getDeclaredField("restricted");
f.setAccessible(true);
f.set(this, value.asBoolean());
f.setAccessible(false);
Util.reflect(BungeeServerInfo.class.getDeclaredField("restricted"), this, value.asBoolean());
if (this.host.plugin.config.get().getSection("Servers").getKeys().contains(getName())) {
this.host.plugin.config.get().getSection("Servers").getSection(getName()).set("Restricted", isRestricted());
this.host.plugin.config.save();
@ -396,10 +387,7 @@ public class ExternalSubServer extends SubServerContainer {
break;
case "hidden":
if (value.isBoolean()) {
Field f = ServerContainer.class.getDeclaredField("hidden");
f.setAccessible(true);
f.set(this, value.asBoolean());
f.setAccessible(false);
Util.reflect(ServerContainer.class.getDeclaredField("hidden"), this, value.asBoolean());
if (this.host.plugin.config.get().getSection("Servers").getKeys().contains(getName())) {
this.host.plugin.config.get().getSection("Servers").getSection(getName()).set("Hidden", isHidden());
this.host.plugin.config.save();

View File

@ -314,10 +314,7 @@ public class InternalSubServer extends SubServerContainer {
break;
case "group":
if (value.isList()) {
Field f = ServerContainer.class.getDeclaredField("groups");
f.setAccessible(true);
f.set(this, value.asStringList());
f.setAccessible(false);
Util.reflect(ServerContainer.class.getDeclaredField("groups"), this, value.asStringList());
if (this.host.plugin.config.get().getSection("Servers").getKeys().contains(getName())) {
this.host.plugin.config.get().getSection("Servers").getSection(getName()).set("Group", value.asStringList());
this.host.plugin.config.save();
@ -353,10 +350,7 @@ public class InternalSubServer extends SubServerContainer {
break;
case "motd":
if (value.isString()) {
Field f = BungeeServerInfo.class.getDeclaredField("motd");
f.setAccessible(true);
f.set(this, value.asColoredString('&'));
f.setAccessible(false);
Util.reflect(BungeeServerInfo.class.getDeclaredField("motd"), this, value.asColoredString('&'));
if (this.host.plugin.config.get().getSection("Servers").getKeys().contains(getName())) {
this.host.plugin.config.get().getSection("Servers").getSection(getName()).set("Motd", value.asString());
this.host.plugin.config.save();
@ -455,10 +449,7 @@ public class InternalSubServer extends SubServerContainer {
break;
case "restricted":
if (value.isBoolean()) {
Field f = BungeeServerInfo.class.getDeclaredField("restricted");
f.setAccessible(true);
f.set(this, value.asBoolean());
f.setAccessible(false);
Util.reflect(BungeeServerInfo.class.getDeclaredField("restricted"), this, value.asBoolean());
if (this.host.plugin.config.get().getSection("Servers").getKeys().contains(getName())) {
this.host.plugin.config.get().getSection("Servers").getSection(getName()).set("Restricted", isRestricted());
this.host.plugin.config.save();
@ -468,10 +459,7 @@ public class InternalSubServer extends SubServerContainer {
break;
case "hidden":
if (value.isBoolean()) {
Field f = ServerContainer.class.getDeclaredField("hidden");
f.setAccessible(true);
f.set(this, value.asBoolean());
f.setAccessible(false);
Util.reflect(ServerContainer.class.getDeclaredField("hidden"), this, value.asBoolean());
if (this.host.plugin.config.get().getSection("Servers").getKeys().contains(getName())) {
this.host.plugin.config.get().getSection("Servers").getSection(getName()).set("Hidden", isHidden());
this.host.plugin.config.save();

View File

@ -133,10 +133,7 @@ public class ServerContainer extends BungeeServerInfo implements Server {
if (Util.isNull(value)) throw new NullPointerException();
SubAPI.getInstance().getInternals().getPluginManager().callEvent(new SubEditServerEvent(null, this, new NamedContainer<String, Object>("motd", value), false));
try {
Field f = BungeeServerInfo.class.getDeclaredField("motd");
f.setAccessible(true);
f.set(this, value);
f.setAccessible(false);
Util.reflect(BungeeServerInfo.class.getDeclaredField("motd"), this, value);
} catch (Exception e) {
e.printStackTrace();
}
@ -147,10 +144,7 @@ public class ServerContainer extends BungeeServerInfo implements Server {
if (Util.isNull(value)) throw new NullPointerException();
SubAPI.getInstance().getInternals().getPluginManager().callEvent(new SubEditServerEvent(null, this, new NamedContainer<String, Object>("restricted", value), false));
try {
Field f = BungeeServerInfo.class.getDeclaredField("restricted");
f.setAccessible(true);
f.set(this, value);
f.setAccessible(false);
Util.reflect(BungeeServerInfo.class.getDeclaredField("restricted"), this, value);
} catch (Exception e) {
e.printStackTrace();
}

View File

@ -1,6 +1,11 @@
package net.ME1312.SubServers.Bungee.Library;
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
@ -131,6 +136,78 @@ public final class Util {
}
}
/**
* Get a Field's value using Reflection
*
* @param field Field to grab
* @param instance Object Instance (Null for static fields)
* @param <R> Return Type
* @return Field Value
* @throws IllegalAccessException
*/
@SuppressWarnings("unchecked")
public static <R> R reflect(Field field, Object instance) throws IllegalAccessException {
R value;
field.setAccessible(true);
value = (R) field.get(instance);
field.setAccessible(false);
return value;
}
/**
* Set a Field's value using Reflection
*
* @param field Field to write to
* @param instance Object Instance (Null for static fields)
* @param value Value to write
* @throws IllegalAccessException
*/
public static void reflect(Field field, Object instance, Object value) throws IllegalAccessException {
field.setAccessible(true);
field.set(instance, value);
field.setAccessible(false);
}
/**
* Call a method using Reflection
*
* @param method Method to call
* @param instance Object Instance (Null for static methods)
* @param arguments Method Arguments
* @param <R> Return Type
* @return Returned Value
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
@SuppressWarnings("unchecked")
public static <R> R reflect(Method method, Object instance, Object... arguments) throws InvocationTargetException, IllegalAccessException {
R value;
method.setAccessible(true);
value = (R) method.invoke(instance, arguments);
method.setAccessible(false);
return value;
}
/**
* Construct an object using Reflection
*
* @param constructor Constructor to use
* @param arguments Constructor Arguments
* @param <R> Return Type
* @return New Instance
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws InstantiationException
*/
@SuppressWarnings("unchecked")
public static <R> R reflect(Constructor<?> constructor, Object... arguments) throws InvocationTargetException, IllegalAccessException, InstantiationException {
R value;
constructor.setAccessible(true);
value = (R) constructor.newInstance(arguments);
constructor.setAccessible(false);
return value;
}
/**
* Get a variable from a method which may throw an exception
*
@ -170,11 +247,13 @@ public final class Util {
public static void deleteDirectory(File folder) {
File[] files = folder.listFiles();
if(files!=null) {
for(File f: files) {
if(f.isDirectory()) {
for(File f : files) {
if(f.isDirectory() && !Files.isSymbolicLink(f.toPath())) {
deleteDirectory(f);
} else {
f.delete();
} else try {
Files.delete(f.toPath());
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -3,6 +3,7 @@ package net.ME1312.SubServers.Bungee.Network.Packet;
import net.ME1312.SubServers.Bungee.Host.External.ExternalSubServer;
import net.ME1312.SubServers.Bungee.Host.SubServer;
import net.ME1312.SubServers.Bungee.Library.Config.YAMLSection;
import net.ME1312.SubServers.Bungee.Library.Util;
import net.ME1312.SubServers.Bungee.Library.Version.Version;
import net.ME1312.SubServers.Bungee.Network.Client;
import net.ME1312.SubServers.Bungee.Network.PacketIn;
@ -91,16 +92,10 @@ public class PacketExUpdateServer implements PacketIn, PacketOut {
ExternalSubServer server = (ExternalSubServer) plugin.api.getSubServer(data.getRawString("server"));
switch (data.getInt("type")) {
case 1:
Method falsestart = ExternalSubServer.class.getDeclaredMethod("falsestart");
falsestart.setAccessible(true);
falsestart.invoke(server);
falsestart.setAccessible(false);
Util.reflect(ExternalSubServer.class.getDeclaredMethod("falsestart"), server);
break;
case 2:
Method stopped = ExternalSubServer.class.getDeclaredMethod("stopped", Boolean.class);
stopped.setAccessible(true);
stopped.invoke(server, data.getList("args").get(1).asBoolean());
stopped.setAccessible(false);
Util.reflect(ExternalSubServer.class.getDeclaredMethod("stopped", Boolean.class), server, data.getList("args").get(1).asBoolean());
break;
}
} catch (Exception e) {

View File

@ -26,10 +26,7 @@ public class PacketInExLogMessage implements PacketIn {
public void execute(Client client, YAMLSection data) {
try {
if (data.contains("h") && data.contains("m") && data.getRawString("m").length() != 0 && loggers.keySet().contains(UUID.fromString(data.getRawString("h")))) {
Method m = ExternalSubLogger.class.getDeclaredMethod("log", String.class);
m.setAccessible(true);
m.invoke(loggers.get(UUID.fromString(data.getRawString("h"))), data.getRawString("m"));
m.setAccessible(false);
Util.reflect(ExternalSubLogger.class.getDeclaredMethod("log", String.class), loggers.get(UUID.fromString(data.getRawString("h"))), data.getRawString("m"));
}
} catch (Exception e) {
e.printStackTrace();

View File

@ -2,6 +2,7 @@ package net.ME1312.SubServers.Bungee.Network.Packet;
import net.ME1312.SubServers.Bungee.Host.External.ExternalHost;
import net.ME1312.SubServers.Bungee.Library.Config.YAMLSection;
import net.ME1312.SubServers.Bungee.Library.Util;
import net.ME1312.SubServers.Bungee.Library.Version.Version;
import net.ME1312.SubServers.Bungee.Network.Client;
import net.ME1312.SubServers.Bungee.Network.PacketIn;
@ -27,10 +28,7 @@ public class PacketInExRequestQueue implements PacketIn {
public void execute(Client client, YAMLSection data) {
if (client.getHandler() != null && client.getHandler() instanceof ExternalHost && plugin.config.get().getSection("Hosts").getKeys().contains(((ExternalHost) client.getHandler()).getName())) {
try {
Method requeue = ExternalHost.class.getDeclaredMethod("requeue");
requeue.setAccessible(true);
requeue.invoke(client.getHandler());
requeue.setAccessible(false);
Util.reflect(ExternalHost.class.getDeclaredMethod("requeue"), client.getHandler());
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}

View File

@ -1,6 +1,11 @@
package net.ME1312.SubServers.Client.Bukkit.Library;
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.util.*;
/**
@ -159,6 +164,78 @@ public final class Util {
}
}
/**
* Get a Field's value using Reflection
*
* @param field Field to grab
* @param instance Object Instance (Null for static fields)
* @param <R> Return Type
* @return Field Value
* @throws IllegalAccessException
*/
@SuppressWarnings("unchecked")
public static <R> R reflect(Field field, Object instance) throws IllegalAccessException {
R value;
field.setAccessible(true);
value = (R) field.get(instance);
field.setAccessible(false);
return value;
}
/**
* Set a Field's value using Reflection
*
* @param field Field to write to
* @param instance Object Instance (Null for static fields)
* @param value Value to write
* @throws IllegalAccessException
*/
public static void reflect(Field field, Object instance, Object value) throws IllegalAccessException {
field.setAccessible(true);
field.set(instance, value);
field.setAccessible(false);
}
/**
* Call a method using Reflection
*
* @param method Method to call
* @param instance Object Instance (Null for static methods)
* @param arguments Method Arguments
* @param <R> Return Type
* @return Returned Value
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
@SuppressWarnings("unchecked")
public static <R> R reflect(Method method, Object instance, Object... arguments) throws InvocationTargetException, IllegalAccessException {
R value;
method.setAccessible(true);
value = (R) method.invoke(instance, arguments);
method.setAccessible(false);
return value;
}
/**
* Construct an object using Reflection
*
* @param constructor Constructor to use
* @param arguments Constructor Arguments
* @param <R> Return Type
* @return New Instance
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws InstantiationException
*/
@SuppressWarnings("unchecked")
public static <R> R reflect(Constructor<?> constructor, Object... arguments) throws InvocationTargetException, IllegalAccessException, InstantiationException {
R value;
constructor.setAccessible(true);
value = (R) constructor.newInstance(arguments);
constructor.setAccessible(false);
return value;
}
/**
* Delete a Directory
*
@ -167,11 +244,13 @@ public final class Util {
public static void deleteDirectory(File folder) {
File[] files = folder.listFiles();
if(files!=null) {
for(File f: files) {
if(f.isDirectory()) {
for(File f : files) {
if(f.isDirectory() && !Files.isSymbolicLink(f.toPath())) {
deleteDirectory(f);
} else {
f.delete();
} else try {
Files.delete(f.toPath());
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -32,12 +32,7 @@ public final class PacketAuthorization implements PacketIn, PacketOut {
public void execute(YAMLSection data) {
try {
if (data.getInt("r") == 0) {
try {
Method m = SubDataClient.class.getDeclaredMethod("sendPacket", NamedContainer.class);
m.setAccessible(true);
m.invoke(plugin.subdata, new NamedContainer<String, PacketOut>(null, new PacketLinkServer(plugin)));
m.setAccessible(false);
} catch (Exception e) {}
Util.isException(() -> Util.reflect(SubDataClient.class.getDeclaredMethod("sendPacket", NamedContainer.class), plugin.subdata, new NamedContainer<String, PacketOut>(null, new PacketLinkServer(plugin))));
} else {
Bukkit.getLogger().info("SubServers > Could not authorize SubData connection: " + data.getRawString("m"));
plugin.subdata.destroy(0);

View File

@ -41,10 +41,7 @@ public class PacketDownloadLang implements PacketIn, PacketOut {
@Override
public void execute(YAMLSection data) {
try {
Field f = SubPlugin.class.getDeclaredField("lang");
f.setAccessible(true);
f.set(plugin, new NamedContainer<>(Calendar.getInstance().getTime().getTime(), data.getSection("Lang").get()));
f.setAccessible(false);
Util.reflect(SubPlugin.class.getDeclaredField("lang"), plugin, new NamedContainer<>(Calendar.getInstance().getTime().getTime(), data.getSection("Lang").get()));
Bukkit.getLogger().info("SubData > Lang Settings Downloaded");
} catch (IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();

View File

@ -41,15 +41,9 @@ public class PacketLinkServer implements PacketIn, PacketOut {
if (data.getInt("r") == 0) {
try {
if (data.contains("n")) {
Field f = SubDataClient.class.getDeclaredField("name");
f.setAccessible(true);
f.set(plugin.subdata, data.getRawString("n"));
f.setAccessible(false);
Util.reflect(SubDataClient.class.getDeclaredField("name"), plugin.subdata, data.getRawString("n"));
}
Method m = SubDataClient.class.getDeclaredMethod("init");
m.setAccessible(true);
m.invoke(plugin.subdata);
m.setAccessible(false);
Util.reflect(SubDataClient.class.getDeclaredMethod("init"), plugin.subdata);
} catch (Exception e) {}
} else {
try {

View File

@ -1,6 +1,11 @@
package net.ME1312.SubServers.Client.Sponge.Library;
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.util.*;
/**
@ -128,6 +133,78 @@ public final class Util {
}
}
/**
* Get a Field's value using Reflection
*
* @param field Field to grab
* @param instance Object Instance (Null for static fields)
* @param <R> Return Type
* @return Field Value
* @throws IllegalAccessException
*/
@SuppressWarnings("unchecked")
public static <R> R reflect(Field field, Object instance) throws IllegalAccessException {
R value;
field.setAccessible(true);
value = (R) field.get(instance);
field.setAccessible(false);
return value;
}
/**
* Set a Field's value using Reflection
*
* @param field Field to write to
* @param instance Object Instance (Null for static fields)
* @param value Value to write
* @throws IllegalAccessException
*/
public static void reflect(Field field, Object instance, Object value) throws IllegalAccessException {
field.setAccessible(true);
field.set(instance, value);
field.setAccessible(false);
}
/**
* Call a method using Reflection
*
* @param method Method to call
* @param instance Object Instance (Null for static methods)
* @param arguments Method Arguments
* @param <R> Return Type
* @return Returned Value
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
@SuppressWarnings("unchecked")
public static <R> R reflect(Method method, Object instance, Object... arguments) throws InvocationTargetException, IllegalAccessException {
R value;
method.setAccessible(true);
value = (R) method.invoke(instance, arguments);
method.setAccessible(false);
return value;
}
/**
* Construct an object using Reflection
*
* @param constructor Constructor to use
* @param arguments Constructor Arguments
* @param <R> Return Type
* @return New Instance
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws InstantiationException
*/
@SuppressWarnings("unchecked")
public static <R> R reflect(Constructor<?> constructor, Object... arguments) throws InvocationTargetException, IllegalAccessException, InstantiationException {
R value;
constructor.setAccessible(true);
value = (R) constructor.newInstance(arguments);
constructor.setAccessible(false);
return value;
}
/**
* Get a variable from a method which may throw an exception
*
@ -167,11 +244,13 @@ public final class Util {
public static void deleteDirectory(File folder) {
File[] files = folder.listFiles();
if(files!=null) {
for(File f: files) {
if(f.isDirectory()) {
for(File f : files) {
if(f.isDirectory() && !Files.isSymbolicLink(f.toPath())) {
deleteDirectory(f);
} else {
f.delete();
} else try {
Files.delete(f.toPath());
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -21,12 +21,7 @@ public final class PacketAuthorization implements PacketIn, PacketOut {
public PacketAuthorization(SubPlugin plugin) {
if (Util.isNull(plugin)) throw new NullPointerException();
this.plugin = plugin;
try {
Field f = SubDataClient.class.getDeclaredField("log");
f.setAccessible(true);
this.log = (Logger) f.get(null);
f.setAccessible(false);
} catch (IllegalAccessException | NoSuchFieldException e) {}
Util.isException(() -> Util.reflect(SubDataClient.class.getDeclaredField("log"), null));
}
@Override
@ -40,12 +35,7 @@ public final class PacketAuthorization implements PacketIn, PacketOut {
public void execute(YAMLSection data) {
try {
if (data.getInt("r") == 0) {
try {
Method m = SubDataClient.class.getDeclaredMethod("sendPacket", NamedContainer.class);
m.setAccessible(true);
m.invoke(plugin.subdata, new NamedContainer<String, PacketOut>(null, new PacketLinkServer(plugin)));
m.setAccessible(false);
} catch (Exception e) {}
Util.isException(() -> Util.reflect(SubDataClient.class.getDeclaredMethod("sendPacket", NamedContainer.class), plugin.subdata, new NamedContainer<String, PacketOut>(null, new PacketLinkServer(plugin))));
} else {
log.info("Could not authorize SubData connection: " + data.getRawString("m"));
plugin.subdata.destroy(0);

View File

@ -28,12 +28,7 @@ public class PacketDownloadLang implements PacketIn, PacketOut {
public PacketDownloadLang(SubPlugin plugin) {
if (Util.isNull(plugin)) throw new NullPointerException();
this.plugin = plugin;
try {
Field f = SubDataClient.class.getDeclaredField("log");
f.setAccessible(true);
this.log = (Logger) f.get(null);
f.setAccessible(false);
} catch (IllegalAccessException | NoSuchFieldException e) {}
Util.isException(() -> Util.reflect(SubDataClient.class.getDeclaredField("log"), null));
}
/**
@ -49,10 +44,7 @@ public class PacketDownloadLang implements PacketIn, PacketOut {
@Override
public void execute(YAMLSection data) {
try {
Field f = SubPlugin.class.getDeclaredField("lang");
f.setAccessible(true);
f.set(plugin, new NamedContainer<>(Calendar.getInstance().getTime().getTime(), data.getSection("Lang").get()));
f.setAccessible(false);
Util.reflect(SubPlugin.class.getDeclaredField("lang"), plugin, new NamedContainer<>(Calendar.getInstance().getTime().getTime(), data.getSection("Lang").get()));
log.info("Lang Settings Downloaded");
} catch (IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();

View File

@ -1,6 +1,7 @@
package net.ME1312.SubServers.Client.Sponge.Network.Packet;
import net.ME1312.SubServers.Client.Sponge.Library.Config.YAMLSection;
import net.ME1312.SubServers.Client.Sponge.Library.Util;
import net.ME1312.SubServers.Client.Sponge.Library.Version.Version;
import net.ME1312.SubServers.Client.Sponge.Network.PacketIn;
import net.ME1312.SubServers.Client.Sponge.Network.SubDataClient;
@ -23,12 +24,7 @@ public class PacketInReload implements PacketIn {
*/
public PacketInReload(SubPlugin plugin) {
this.plugin = plugin;
try {
Field f = SubDataClient.class.getDeclaredField("log");
f.setAccessible(true);
this.log = (Logger) f.get(null);
f.setAccessible(false);
} catch (IllegalAccessException | NoSuchFieldException e) {}
Util.isException(() -> Util.reflect(SubDataClient.class.getDeclaredField("log"), null));
}
@Override

View File

@ -1,6 +1,7 @@
package net.ME1312.SubServers.Client.Sponge.Network.Packet;
import net.ME1312.SubServers.Client.Sponge.Library.Config.YAMLSection;
import net.ME1312.SubServers.Client.Sponge.Library.Util;
import net.ME1312.SubServers.Client.Sponge.Library.Version.Version;
import net.ME1312.SubServers.Client.Sponge.Network.PacketIn;
import net.ME1312.SubServers.Client.Sponge.Network.SubDataClient;
@ -19,12 +20,7 @@ public class PacketInReset implements PacketIn {
* New PacketInReset
*/
public PacketInReset() {
try {
Field f = SubDataClient.class.getDeclaredField("log");
f.setAccessible(true);
this.log = (Logger) f.get(null);
f.setAccessible(false);
} catch (IllegalAccessException | NoSuchFieldException e) {}
Util.isException(() -> Util.reflect(SubDataClient.class.getDeclaredField("log"), null));
}
@Override

View File

@ -27,12 +27,7 @@ public class PacketLinkServer implements PacketIn, PacketOut {
public PacketLinkServer(SubPlugin plugin) {
if (Util.isNull(plugin)) throw new NullPointerException();
this.plugin = plugin;
try {
Field f = SubDataClient.class.getDeclaredField("log");
f.setAccessible(true);
this.log = (Logger) f.get(null);
f.setAccessible(false);
} catch (IllegalAccessException | NoSuchFieldException e) {}
Util.isException(() -> Util.reflect(SubDataClient.class.getDeclaredField("log"), null));
}
@Override
@ -48,15 +43,9 @@ public class PacketLinkServer implements PacketIn, PacketOut {
if (data.getInt("r") == 0) {
try {
if (data.contains("n")) {
Field f = SubDataClient.class.getDeclaredField("name");
f.setAccessible(true);
f.set(plugin.subdata, data.getRawString("n"));
f.setAccessible(false);
Util.reflect(SubDataClient.class.getDeclaredField("name"), plugin.subdata, data.getRawString("n"));
}
Method m = SubDataClient.class.getDeclaredMethod("init");
m.setAccessible(true);
m.invoke(plugin.subdata);
m.setAccessible(false);
Util.reflect(SubDataClient.class.getDeclaredMethod("init"), plugin.subdata);
} catch (Exception e) {}
} else {
try {

View File

@ -20,7 +20,7 @@
<dependency>
<groupId>net.ME1312.Galaxi</groupId>
<artifactId>GalaxiEngine</artifactId>
<version>19w02f</version>
<version>19w03a</version>
<scope>compile</scope>
</dependency>
<dependency>

View File

@ -104,11 +104,8 @@ public final class ExHost {
if (manifest.getMainAttributes().getValue("Implementation-Version") != null && manifest.getMainAttributes().getValue("Implementation-Version").length() > 0)
galaxibuild = new Version(manifest.getMainAttributes().getValue("Implementation-Version"));
} catch (Exception e) {} try {
Field f = Version.class.getDeclaredField("type");
f.setAccessible(true);
if (f.get(subservers) != VersionType.SNAPSHOT && ExHost.class.getPackage().getSpecificationTitle() != null)
if (Util.reflect(Version.class.getDeclaredField("type"), subservers) != VersionType.SNAPSHOT && ExHost.class.getPackage().getSpecificationTitle() != null)
subserversbuild = new Version(ExHost.class.getPackage().getSpecificationTitle());
f.setAccessible(false);
} catch (Exception e) {}
System.out.println("");

View File

@ -1,5 +1,6 @@
package net.ME1312.SubServers.Host.Executable;
import net.ME1312.Galaxi.Library.Util;
import net.ME1312.SubServers.Host.Library.Compatibility.JNA;
import java.io.File;
@ -44,10 +45,7 @@ public class Executable {
} catch (Throwable ex) {
try {
if (process.getClass().getName().equals("java.lang.Win32Process") || process.getClass().getName().equals("java.lang.ProcessImpl")) {
Field f = process.getClass().getDeclaredField("handle");
f.setAccessible(true);
long handle = f.getLong(process);
f.setAccessible(false);
long handle = Util.reflect(process.getClass().getDeclaredField("handle"), process);
ClassLoader jna = JNA.get();
Class<?> pc = jna.loadClass("com.sun.jna.Pointer"),
@ -58,10 +56,7 @@ public class Executable {
ntc.getMethod("setPointer", pc).invoke(nt, pc.getMethod("createConstant", long.class).invoke(null, handle));
return ((Number) k32c.getMethod("GetProcessId", ntc).invoke(k32, nt)).longValue();
} else if (process.getClass().getName().equals("java.lang.UNIXProcess")) {
Field f = process.getClass().getDeclaredField("pid");
f.setAccessible(true);
Object response = f.get(process);
f.setAccessible(false);
Object response = Util.reflect(process.getClass().getDeclaredField("pid"), process);
if (response instanceof Number)
return ((Number) response).longValue();

View File

@ -29,12 +29,7 @@ public final class PacketAuthorization implements PacketIn, PacketOut {
public PacketAuthorization(ExHost host) {
if (Util.isNull(host)) throw new NullPointerException();
this.host = host;
try {
Field f = SubDataClient.class.getDeclaredField("log");
f.setAccessible(true);
this.log = (Logger) f.get(null);
f.setAccessible(false);
} catch (IllegalAccessException | NoSuchFieldException e) {}
Util.isException(() -> this.log = Util.reflect(SubDataClient.class.getDeclaredField("log"), null));
}
@Override
@ -48,12 +43,7 @@ public final class PacketAuthorization implements PacketIn, PacketOut {
public void execute(YAMLSection data) {
try {
if (data.getInt("r") == 0) {
try {
Method m = SubDataClient.class.getDeclaredMethod("sendPacket", NamedContainer.class);
m.setAccessible(true);
m.invoke(host.subdata, new NamedContainer<String, PacketOut>(null, new PacketLinkExHost(host)));
m.setAccessible(false);
} catch (Exception e) {}
Util.isException(() -> Util.reflect(SubDataClient.class.getDeclaredMethod("sendPacket", NamedContainer.class), host.subdata, new NamedContainer<String, PacketOut>(null, new PacketLinkExHost(host))));
} else {
log.info.println("Could not authorize SubData connection: " + data.getRawString("m"));
host.subdata.destroy(0);

View File

@ -22,12 +22,7 @@ public class PacketDownloadLang implements PacketIn, PacketOut {
public PacketDownloadLang(ExHost host) {
if (Util.isNull(host)) throw new NullPointerException();
this.host = host;
try {
Field f = SubDataClient.class.getDeclaredField("log");
f.setAccessible(true);
this.log = (Logger) f.get(null);
f.setAccessible(false);
} catch (IllegalAccessException | NoSuchFieldException e) {}
Util.isException(() -> this.log = Util.reflect(SubDataClient.class.getDeclaredField("log"), null));
}
@Override
@ -38,10 +33,7 @@ public class PacketDownloadLang implements PacketIn, PacketOut {
@Override
public void execute(YAMLSection data) {
try {
Field f = ExHost.class.getDeclaredField("lang");
f.setAccessible(true);
f.set(host, new NamedContainer<>(Calendar.getInstance().getTime().getTime(), data.getSection("Lang").get()));
f.setAccessible(false);
Util.reflect(ExHost.class.getDeclaredField("lang"), host, new NamedContainer<>(Calendar.getInstance().getTime().getTime(), data.getSection("Lang").get()));
log.info.println("Lang Settings Downloaded");
} catch (IllegalAccessException | NoSuchFieldException e) {
log.error.println(e);

View File

@ -32,12 +32,7 @@ public class PacketExAddServer implements PacketIn, PacketOut {
public PacketExAddServer(ExHost host) {
if (Util.isNull(host)) throw new NullPointerException();
this.host = host;
try {
Field f = SubDataClient.class.getDeclaredField("log");
f.setAccessible(true);
this.log = (Logger) f.get(null);
f.setAccessible(false);
} catch (IllegalAccessException | NoSuchFieldException e) {}
Util.isException(() -> this.log = Util.reflect(SubDataClient.class.getDeclaredField("log"), null));
}
/**

View File

@ -29,12 +29,7 @@ public class PacketExConfigureHost implements PacketIn, PacketOut {
*/
public PacketExConfigureHost(ExHost host) {
this.host = host;
try {
Field f = SubDataClient.class.getDeclaredField("log");
f.setAccessible(true);
this.log = (Logger) f.get(null);
f.setAccessible(false);
} catch (IllegalAccessException | NoSuchFieldException e) {}
Util.isException(() -> this.log = Util.reflect(SubDataClient.class.getDeclaredField("log"), null));
}
@Override

View File

@ -36,12 +36,7 @@ public class PacketExDeleteServer implements PacketIn, PacketOut {
public PacketExDeleteServer(ExHost host) {
if (Util.isNull(host)) throw new NullPointerException();
this.host = host;
try {
Field f = SubDataClient.class.getDeclaredField("log");
f.setAccessible(true);
this.log = (Logger) f.get(null);
f.setAccessible(false);
} catch (IllegalAccessException | NoSuchFieldException e) {}
Util.isException(() -> this.log = Util.reflect(SubDataClient.class.getDeclaredField("log"), null));
}
/**

View File

@ -30,12 +30,7 @@ public class PacketExRemoveServer implements PacketIn, PacketOut {
public PacketExRemoveServer(ExHost host) {
if (Util.isNull(host)) throw new NullPointerException();
this.host = host;
try {
Field f = SubDataClient.class.getDeclaredField("log");
f.setAccessible(true);
this.log = (Logger) f.get(null);
f.setAccessible(false);
} catch (IllegalAccessException | NoSuchFieldException e) {}
Util.isException(() -> this.log = Util.reflect(SubDataClient.class.getDeclaredField("log"), null));
}
/**

View File

@ -2,6 +2,7 @@ package net.ME1312.SubServers.Host.Network.Packet;
import net.ME1312.Galaxi.Library.Config.YAMLSection;
import net.ME1312.Galaxi.Library.Log.Logger;
import net.ME1312.Galaxi.Library.Util;
import net.ME1312.Galaxi.Library.Version.Version;
import net.ME1312.SubServers.Host.ExHost;
import net.ME1312.SubServers.Host.Network.PacketIn;
@ -19,12 +20,7 @@ public class PacketInReload implements PacketIn {
public PacketInReload(ExHost host) {
this.host = host;
try {
Field f = SubDataClient.class.getDeclaredField("log");
f.setAccessible(true);
this.log = (Logger) f.get(null);
f.setAccessible(false);
} catch (IllegalAccessException | NoSuchFieldException e) {}
Util.isException(() -> this.log = Util.reflect(SubDataClient.class.getDeclaredField("log"), null));
}
@Override

View File

@ -28,12 +28,7 @@ public class PacketLinkExHost implements PacketIn, PacketOut {
public PacketLinkExHost(ExHost host) {
if (Util.isNull(host)) throw new NullPointerException();
this.host = host;
try {
Field f = SubDataClient.class.getDeclaredField("log");
f.setAccessible(true);
this.log = (Logger) f.get(null);
f.setAccessible(false);
} catch (IllegalAccessException | NoSuchFieldException e) {}
Util.isException(() -> this.log = Util.reflect(SubDataClient.class.getDeclaredField("log"), null));
}
@Override
@ -46,12 +41,7 @@ public class PacketLinkExHost implements PacketIn, PacketOut {
@Override
public void execute(YAMLSection data) {
if (data.getInt("r") == 0) {
try {
Method m = SubDataClient.class.getDeclaredMethod("init");
m.setAccessible(true);
m.invoke(host.subdata);
m.setAccessible(false);
} catch (Exception e) {}
Util.isException(() -> Util.reflect(SubDataClient.class.getDeclaredMethod("init"), host.subdata));
} else {
log.info.println("Could not link name with host: " + data.getRawString("m"));
GalaxiEngine.getInstance().stop();

View File

@ -53,10 +53,7 @@ public class SubCommand {
String last = (args.length > 0)?args[args.length - 1].toLowerCase():"";
TreeMap<String, Command> commands;
try {
Field f = PluginManager.class.getDeclaredField("commands");
f.setAccessible(true);
commands = (TreeMap<String, Command>) f.get(GalaxiEngine.getInstance().getPluginManager());
f.setAccessible(false);
commands = Util.reflect(PluginManager.class.getDeclaredField("commands"), GalaxiEngine.getInstance().getPluginManager());
} catch (Exception e) {
SubAPI.getInstance().getAppInfo().getLogger().error.println(e);
commands = new TreeMap<String, Command>();

View File

@ -1,6 +1,11 @@
package net.ME1312.SubServers.Sync.Library;
import java.io.*;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.util.*;
/**
@ -128,6 +133,78 @@ public final class Util {
}
}
/**
* Get a Field's value using Reflection
*
* @param field Field to grab
* @param instance Object Instance (Null for static fields)
* @param <R> Return Type
* @return Field Value
* @throws IllegalAccessException
*/
@SuppressWarnings("unchecked")
public static <R> R reflect(Field field, Object instance) throws IllegalAccessException {
R value;
field.setAccessible(true);
value = (R) field.get(instance);
field.setAccessible(false);
return value;
}
/**
* Set a Field's value using Reflection
*
* @param field Field to write to
* @param instance Object Instance (Null for static fields)
* @param value Value to write
* @throws IllegalAccessException
*/
public static void reflect(Field field, Object instance, Object value) throws IllegalAccessException {
field.setAccessible(true);
field.set(instance, value);
field.setAccessible(false);
}
/**
* Call a method using Reflection
*
* @param method Method to call
* @param instance Object Instance (Null for static methods)
* @param arguments Method Arguments
* @param <R> Return Type
* @return Returned Value
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
@SuppressWarnings("unchecked")
public static <R> R reflect(Method method, Object instance, Object... arguments) throws InvocationTargetException, IllegalAccessException {
R value;
method.setAccessible(true);
value = (R) method.invoke(instance, arguments);
method.setAccessible(false);
return value;
}
/**
* Construct an object using Reflection
*
* @param constructor Constructor to use
* @param arguments Constructor Arguments
* @param <R> Return Type
* @return New Instance
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws InstantiationException
*/
@SuppressWarnings("unchecked")
public static <R> R reflect(Constructor<?> constructor, Object... arguments) throws InvocationTargetException, IllegalAccessException, InstantiationException {
R value;
constructor.setAccessible(true);
value = (R) constructor.newInstance(arguments);
constructor.setAccessible(false);
return value;
}
/**
* Get a variable from a method which may throw an exception
*
@ -167,11 +244,13 @@ public final class Util {
public static void deleteDirectory(File folder) {
File[] files = folder.listFiles();
if(files!=null) {
for(File f: files) {
if(f.isDirectory()) {
for(File f : files) {
if(f.isDirectory() && !Files.isSymbolicLink(f.toPath())) {
deleteDirectory(f);
} else {
f.delete();
} else try {
Files.delete(f.toPath());
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@ -30,12 +30,7 @@ public final class PacketAuthorization implements PacketIn, PacketOut {
public void execute(YAMLSection data) {
try {
if (data.getInt("r") == 0) {
try {
Method m = SubDataClient.class.getDeclaredMethod("init");
m.setAccessible(true);
m.invoke(plugin.subdata);
m.setAccessible(false);
} catch (Exception e) {}
Util.isException(() -> Util.reflect(SubDataClient.class.getDeclaredMethod("init"), plugin.subdata));
} else {
System.out.println("SubServers > Could not authorize SubData connection: " + data.getRawString("m"));
plugin.subdata.destroy(0);

View File

@ -40,10 +40,7 @@ public class PacketDownloadLang implements PacketIn, PacketOut {
@Override
public void execute(YAMLSection data) {
try {
Field f = SubPlugin.class.getDeclaredField("lang");
f.setAccessible(true);
f.set(plugin, new NamedContainer<>(Calendar.getInstance().getTime().getTime(), data.getSection("Lang").get()));
f.setAccessible(false);
Util.reflect(SubPlugin.class.getDeclaredField("lang"), plugin, new NamedContainer<>(Calendar.getInstance().getTime().getTime(), data.getSection("Lang").get()));
System.out.println("SubData > Lang Settings Downloaded");
} catch (IllegalAccessException | NoSuchFieldException e) {
e.printStackTrace();

View File

@ -36,12 +36,7 @@ public class PacketLinkProxy implements PacketIn, PacketOut {
@Override
public void execute(YAMLSection data) {
if (data.getInt("r") == 0) {
if (data.contains("n")) try {
Field f = SubDataClient.class.getDeclaredField("name");
f.setAccessible(true);
f.set(plugin.subdata, data.getRawString("n"));
f.setAccessible(false);
} catch (Exception e) {}
if (data.contains("n")) Util.isException(() -> Util.reflect(SubDataClient.class.getDeclaredField("name"), data.getRawString("n")));
} else {
try {
if (data.getInt("r") == 2) {

View File

@ -100,13 +100,13 @@ public final class SubDataClient {
try {
LinkedList<ListenerInfo> listeners = new LinkedList<ListenerInfo>(plugin.getConfig().getListeners());
for (int i = 0; i < platform.getSection("bungee").getSectionList("listeners").size(); i++) if (i < listeners.size()) {
if (plugin.config.get().getSection("Sync", new YAMLSection()).getBoolean("Forced-Hosts", true)) updateField(ListenerInfo.class.getDeclaredField("forcedHosts"), listeners.get(i), platform.getSection("bungee").getSectionList("listeners").get(i).getSection("forced-hosts").get());
if (plugin.config.get().getSection("Sync", new YAMLSection()).getBoolean("Motd", false)) updateField(ListenerInfo.class.getDeclaredField("motd"), listeners.get(i), platform.getSection("bungee").getSectionList("listeners").get(i).getRawString("motd"));
if (plugin.config.get().getSection("Sync", new YAMLSection()).getBoolean("Player-Limit", false)) updateField(ListenerInfo.class.getDeclaredField("maxPlayers"), listeners.get(i), platform.getSection("bungee").getSectionList("listeners").get(i).getInt("player-limit"));
if (plugin.config.get().getSection("Sync", new YAMLSection()).getBoolean("Server-Priorities", true)) updateField(ListenerInfo.class.getDeclaredField("serverPriority"), listeners.get(i), platform.getSection("bungee").getSectionList("listeners").get(i).getRawStringList("priorities"));
if (plugin.config.get().getSection("Sync", new YAMLSection()).getBoolean("Forced-Hosts", true)) Util.reflect(ListenerInfo.class.getDeclaredField("forcedHosts"), listeners.get(i), platform.getSection("bungee").getSectionList("listeners").get(i).getSection("forced-hosts").get());
if (plugin.config.get().getSection("Sync", new YAMLSection()).getBoolean("Motd", false)) Util.reflect(ListenerInfo.class.getDeclaredField("motd"), listeners.get(i), platform.getSection("bungee").getSectionList("listeners").get(i).getRawString("motd"));
if (plugin.config.get().getSection("Sync", new YAMLSection()).getBoolean("Player-Limit", false)) Util.reflect(ListenerInfo.class.getDeclaredField("maxPlayers"), listeners.get(i), platform.getSection("bungee").getSectionList("listeners").get(i).getInt("player-limit"));
if (plugin.config.get().getSection("Sync", new YAMLSection()).getBoolean("Server-Priorities", true)) Util.reflect(ListenerInfo.class.getDeclaredField("serverPriority"), listeners.get(i), platform.getSection("bungee").getSectionList("listeners").get(i).getRawStringList("priorities"));
}
if (plugin.config.get().getSection("Sync", new YAMLSection()).getBoolean("Disabled-Commands", false)) updateField(Configuration.class.getDeclaredField("disabledCommands"), plugin.getConfig(), platform.getSection("bungee").getRawStringList("disabled-cmds"));
if (plugin.config.get().getSection("Sync", new YAMLSection()).getBoolean("Player-Limit", false)) updateField(Configuration.class.getDeclaredField("playerLimit"), plugin.getConfig(), platform.getSection("bungee").getInt("player-limit"));
if (plugin.config.get().getSection("Sync", new YAMLSection()).getBoolean("Disabled-Commands", false)) Util.reflect(Configuration.class.getDeclaredField("disabledCommands"), plugin.getConfig(), platform.getSection("bungee").getRawStringList("disabled-cmds"));
if (plugin.config.get().getSection("Sync", new YAMLSection()).getBoolean("Player-Limit", false)) Util.reflect(Configuration.class.getDeclaredField("playerLimit"), plugin.getConfig(), platform.getSection("bungee").getInt("player-limit"));
} catch (Exception e) {
System.out.println("SubServers > Problem syncing BungeeCord configuration options");
e.printStackTrace();
@ -122,10 +122,6 @@ public final class SubDataClient {
socket.rename(true);
plugin.getPluginManager().callEvent(new SubNetworkConnectEvent(this));
}
private void updateField(Field field, Object instance, Object value) throws IllegalAccessException {
field.setAccessible(true);
field.set(instance, value);
}
static {
addCipher("AES", new AES(128));

View File

@ -128,10 +128,7 @@ public class ServerContainer extends BungeeServerInfo {
public void setMotd(String value) {
if (Util.isNull(value)) throw new NullPointerException();
try {
Field f = BungeeServerInfo.class.getDeclaredField("motd");
f.setAccessible(true);
f.set(this, value);
f.setAccessible(false);
Util.reflect(BungeeServerInfo.class.getDeclaredField("motd"), this, value);
} catch (Exception e) {
e.printStackTrace();
}
@ -145,10 +142,7 @@ public class ServerContainer extends BungeeServerInfo {
public void setRestricted(boolean value) {
if (Util.isNull(value)) throw new NullPointerException();
try {
Field f = BungeeServerInfo.class.getDeclaredField("restricted");
f.setAccessible(true);
f.set(this, value);
f.setAccessible(false);
Util.reflect(BungeeServerInfo.class.getDeclaredField("restricted"), this, value);
} catch (Exception e) {
e.printStackTrace();
}