mirror of
https://github.com/PurpurMC/Purpur.git
synced 2026-02-20 18:07:43 +01:00
reorder spark loader logic to fix a few issues (#1326)
This commit is contained in:
committed by
GitHub
parent
6319570c63
commit
b84222981c
@@ -19,10 +19,10 @@ index 89bf48fd581ee6580b91e2eb31dd532cb622df5e..e35da199be67e04c34df6bc09afd8d81
|
|||||||
// This will be the end of me...
|
// This will be the end of me...
|
||||||
diff --git a/src/main/java/io/papermc/paper/plugin/provider/source/SparkProviderSource.java b/src/main/java/io/papermc/paper/plugin/provider/source/SparkProviderSource.java
|
diff --git a/src/main/java/io/papermc/paper/plugin/provider/source/SparkProviderSource.java b/src/main/java/io/papermc/paper/plugin/provider/source/SparkProviderSource.java
|
||||||
new file mode 100644
|
new file mode 100644
|
||||||
index 0000000000000000000000000000000000000000..2d6fa262a7f1a298069e74266cb62fed0136833c
|
index 0000000000000000000000000000000000000000..a7d1ae53eac94bc2dcf8bc78ef1da0d3b8554736
|
||||||
--- /dev/null
|
--- /dev/null
|
||||||
+++ b/src/main/java/io/papermc/paper/plugin/provider/source/SparkProviderSource.java
|
+++ b/src/main/java/io/papermc/paper/plugin/provider/source/SparkProviderSource.java
|
||||||
@@ -0,0 +1,82 @@
|
@@ -0,0 +1,102 @@
|
||||||
+package io.papermc.paper.plugin.provider.source;
|
+package io.papermc.paper.plugin.provider.source;
|
||||||
+
|
+
|
||||||
+import com.mojang.logging.LogUtils;
|
+import com.mojang.logging.LogUtils;
|
||||||
@@ -55,44 +55,64 @@ index 0000000000000000000000000000000000000000..2d6fa262a7f1a298069e74266cb62fed
|
|||||||
+
|
+
|
||||||
+ @Override
|
+ @Override
|
||||||
+ public void registerProviders(EntrypointHandler entrypointHandler, Path context) throws Exception {
|
+ public void registerProviders(EntrypointHandler entrypointHandler, Path context) throws Exception {
|
||||||
+ if (!Boolean.getBoolean("Purpur.IReallyDontWantSpark")) {
|
+ // first, check if user doesn't want spark at all
|
||||||
+ try {
|
+ if (Boolean.getBoolean("Purpur.IReallyDontWantSpark")) {
|
||||||
+ File file = context.toFile();
|
+ return; // boo!
|
||||||
+ file.getParentFile().mkdirs();
|
+ }
|
||||||
+
|
+
|
||||||
+ boolean shouldDownload = true;
|
+ // second, check if user has their own spark
|
||||||
+ if (file.exists()) {
|
+ if (hasSpark()) {
|
||||||
+ String fileSha1 = String.format("%040x", new BigInteger(1, MessageDigest.getInstance("SHA-1").digest(Files.readAllBytes(file.toPath()))));
|
+ LOGGER.info("Purpur: Using user-provided spark plugin instead of our own.");
|
||||||
+ String sparkSha1;
|
+ return; // let's hope it's at least the modern version :3
|
||||||
+ URLConnection urlConnection = new URL("https://sparkapi.lucko.me/download/bukkit/sha1").openConnection();
|
+ }
|
||||||
+ urlConnection.setReadTimeout(5000);
|
|
||||||
+ urlConnection.setConnectTimeout(5000);
|
|
||||||
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()))) {
|
|
||||||
+ sparkSha1 = reader.lines().collect(Collectors.joining(""));
|
|
||||||
+ }
|
|
||||||
+
|
+
|
||||||
+ if (fileSha1.equals(sparkSha1)) {
|
+ // you can't have errors in your code if you wrap the entire codebase in a try/catch block
|
||||||
+ shouldDownload = false;
|
+ try {
|
||||||
+ }
|
+
|
||||||
|
+ // make sure the directory exists where we want to keep spark
|
||||||
|
+ File file = context.toFile();
|
||||||
|
+ file.getParentFile().mkdirs();
|
||||||
|
+
|
||||||
|
+ boolean shouldDownload;
|
||||||
|
+
|
||||||
|
+ // check if our spark exists
|
||||||
|
+ if (!file.exists()) {
|
||||||
|
+ // it does not, so let's download it
|
||||||
|
+ shouldDownload = true;
|
||||||
|
+ } else {
|
||||||
|
+ // we have a spark file, let's see if it's up-to-date by comparing shas
|
||||||
|
+ String fileSha1 = String.format("%040x", new BigInteger(1, MessageDigest.getInstance("SHA-1").digest(Files.readAllBytes(file.toPath()))));
|
||||||
|
+ String sparkSha1;
|
||||||
|
+
|
||||||
|
+ // luck has a nifty endpoint containing the sha of the newest version
|
||||||
|
+ URLConnection urlConnection = new URL("https://sparkapi.lucko.me/download/bukkit/sha1").openConnection();
|
||||||
|
+
|
||||||
|
+ // set a reasonable timeout to prevent servers without internet from hanging for 60+ seconds on startup
|
||||||
|
+ urlConnection.setReadTimeout(5000);
|
||||||
|
+ urlConnection.setConnectTimeout(5000);
|
||||||
|
+
|
||||||
|
+ // read it
|
||||||
|
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()))) {
|
||||||
|
+ sparkSha1 = reader.lines().collect(Collectors.joining(""));
|
||||||
+ }
|
+ }
|
||||||
+
|
+
|
||||||
+ if (shouldDownload) {
|
+ // compare; we only download a new spark if the shas don't match
|
||||||
+ URLConnection urlConnection = new URL("https://sparkapi.lucko.me/download/bukkit").openConnection();
|
+ shouldDownload = !fileSha1.equals(sparkSha1);
|
||||||
+ urlConnection.setReadTimeout(5000);
|
|
||||||
+ urlConnection.setConnectTimeout(5000);
|
|
||||||
+ Files.copy(urlConnection.getInputStream(), file.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
|
||||||
+ }
|
|
||||||
+
|
|
||||||
+ if (hasSpark()) {
|
|
||||||
+ LOGGER.info("Purpur: Using user-provided spark plugin instead of our own.");
|
|
||||||
+ } else {
|
|
||||||
+ super.registerProviders(entrypointHandler, context);
|
|
||||||
+ }
|
|
||||||
+
|
|
||||||
+ } catch (Exception e) {
|
|
||||||
+ LOGGER.error("Purpur: Failed to download and install spark plugin");
|
|
||||||
+ e.printStackTrace();
|
|
||||||
+ }
|
+ }
|
||||||
|
+
|
||||||
|
+ // ok, finally we can download spark if we need it
|
||||||
|
+ if (shouldDownload) {
|
||||||
|
+ URLConnection urlConnection = new URL("https://sparkapi.lucko.me/download/bukkit").openConnection();
|
||||||
|
+ urlConnection.setReadTimeout(5000);
|
||||||
|
+ urlConnection.setConnectTimeout(5000);
|
||||||
|
+ Files.copy(urlConnection.getInputStream(), file.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||||
|
+ }
|
||||||
|
+
|
||||||
|
+ // register the spark, newly downloaded or existing
|
||||||
|
+ super.registerProviders(entrypointHandler, context);
|
||||||
|
+
|
||||||
|
+ } catch (Throwable e) {
|
||||||
|
+ LOGGER.error("Purpur: Failed to download and install spark plugin", e);
|
||||||
+ }
|
+ }
|
||||||
+ }
|
+ }
|
||||||
+
|
+
|
||||||
|
|||||||
Reference in New Issue
Block a user