2019-01-09 22:41:37 +01:00
|
|
|
package net.ME1312.SubServers.Bungee.Library.Compatibility;
|
|
|
|
|
2021-10-24 06:14:07 +02:00
|
|
|
|
2019-01-09 22:41:37 +01:00
|
|
|
import net.ME1312.SubServers.Bungee.SubAPI;
|
|
|
|
|
2020-11-16 21:34:59 +01:00
|
|
|
import com.google.common.io.Resources;
|
2022-04-06 01:26:50 +02:00
|
|
|
import net.md_5.bungee.api.ProxyServer;
|
2020-11-16 21:34:59 +01:00
|
|
|
|
2021-10-24 06:14:07 +02:00
|
|
|
import java.io.File;
|
2019-01-14 01:52:44 +01:00
|
|
|
import java.io.FileNotFoundException;
|
2019-01-09 22:41:37 +01:00
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.net.URL;
|
|
|
|
import java.net.URLClassLoader;
|
2022-04-06 01:26:50 +02:00
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
|
|
import static java.util.logging.Level.SEVERE;
|
2019-01-09 22:41:37 +01:00
|
|
|
|
|
|
|
/**
|
2019-01-10 21:52:38 +01:00
|
|
|
* JNA Library Loader Class
|
2019-01-09 22:41:37 +01:00
|
|
|
*/
|
|
|
|
public class JNA {
|
2019-01-10 21:52:38 +01:00
|
|
|
private JNA() {}
|
2019-01-09 22:41:37 +01:00
|
|
|
private static ClassLoader JNA = null;
|
|
|
|
private static final String JNA_VERSION = "5.2.0";
|
|
|
|
private static final String JNA_DOWNLOAD = "https://oss.sonatype.org/service/local/repositories/releases/content/net/java/dev/jna/$1/" + JNA_VERSION + "/$1-" + JNA_VERSION + ".jar";
|
|
|
|
|
2019-01-10 21:52:38 +01:00
|
|
|
/**
|
|
|
|
* Get/Load JNA Library
|
|
|
|
*
|
|
|
|
* @return JNA ClassLoader
|
|
|
|
*/
|
2019-01-09 22:41:37 +01:00
|
|
|
@SuppressWarnings("deprecation")
|
|
|
|
public static ClassLoader get() {
|
|
|
|
if (JNA == null) {
|
|
|
|
boolean announced = false;
|
2022-04-06 01:26:50 +02:00
|
|
|
Logger log = ProxyServer.getInstance().getLogger();
|
2021-10-24 06:14:07 +02:00
|
|
|
File library = new File(SubAPI.getInstance().getInternals().dir, "SubServers/Cache/Libraries");
|
|
|
|
File jna = new File(library, "jna-" + JNA_VERSION + ".jar");
|
2019-01-10 21:52:38 +01:00
|
|
|
jna.getParentFile().mkdirs();
|
2019-01-09 22:41:37 +01:00
|
|
|
if (!jna.exists()) {
|
2019-01-10 21:52:38 +01:00
|
|
|
announced = true;
|
2022-04-06 01:26:50 +02:00
|
|
|
log.info(">> Downloading JNA v" + JNA_VERSION);
|
2019-01-10 21:52:38 +01:00
|
|
|
try (FileOutputStream fin = new FileOutputStream(jna)) {
|
|
|
|
Resources.copy(new URL(JNA_DOWNLOAD.replace("$1", "jna")), fin);
|
|
|
|
} catch (Throwable e) {
|
|
|
|
jna.delete();
|
|
|
|
e.printStackTrace();
|
2019-01-09 22:41:37 +01:00
|
|
|
}
|
|
|
|
}
|
2021-10-24 06:14:07 +02:00
|
|
|
File platform = new File(library, "jna-platform-" + JNA_VERSION + ".jar");
|
2019-01-10 21:52:38 +01:00
|
|
|
platform.getParentFile().mkdirs();
|
2019-01-09 22:41:37 +01:00
|
|
|
if (!platform.exists()) {
|
2022-04-06 01:26:50 +02:00
|
|
|
if (!announced) log.info(">> Downloading JNA platform v" + JNA_VERSION);
|
2019-01-10 21:52:38 +01:00
|
|
|
announced = true;
|
|
|
|
try (FileOutputStream fin = new FileOutputStream(platform)) {
|
|
|
|
Resources.copy(new URL(JNA_DOWNLOAD.replace("$1", "jna-platform")), fin);
|
|
|
|
} catch (Throwable e) {
|
|
|
|
platform.delete();
|
|
|
|
e.printStackTrace();
|
2019-01-09 22:41:37 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-14 01:52:44 +01:00
|
|
|
if (jna.exists() && platform.exists()) {
|
2022-04-06 01:26:50 +02:00
|
|
|
if (announced) log.info(">> JNA download complete");
|
2019-01-09 22:41:37 +01:00
|
|
|
try {
|
|
|
|
JNA = new URLClassLoader(new URL[]{jna.toURI().toURL(), platform.toURI().toURL()});
|
|
|
|
} catch (Throwable e) {
|
2022-04-06 01:26:50 +02:00
|
|
|
log.log(SEVERE, ">> Couldn't load JNA:", e);
|
2019-01-09 22:41:37 +01:00
|
|
|
}
|
|
|
|
} else {
|
2022-04-06 01:26:50 +02:00
|
|
|
log.log(SEVERE, ">> Couldn't load JNA:", new FileNotFoundException());
|
2019-01-09 22:41:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return JNA;
|
|
|
|
}
|
|
|
|
}
|