Update LibsPremium check

This commit is contained in:
libraryaddict 2021-12-06 11:37:44 +13:00
parent 8f398d798a
commit dbe9073f33
2 changed files with 51 additions and 71 deletions

View File

@ -114,34 +114,14 @@ public class LibsPremium {
try (URLClassLoader cl = new URLClassLoader(new URL[]{file.toURI().toURL()})) { try (URLClassLoader cl = new URLClassLoader(new URL[]{file.toURI().toURL()})) {
Class c = cl.loadClass(LibsPremium.class.getName()); Class c = cl.loadClass(LibsPremium.class.getName());
boolean oldJarFile = true;
try {
// Error thrown if method doesn't exist
c.getMethod("getUserID");
// Method exists, is not older file
oldJarFile = false;
}
catch (Exception ignored) {
}
// Fetch the plugin.yml from the jar file // Fetch the plugin.yml from the jar file
YamlConfiguration config = ReflectionManager.getPluginYAML(file); YamlConfiguration config = ReflectionManager.getPluginYAMLEx(file);
// No checks for null config as the correct error will be thrown on access // No checks for null config as the correct error will be thrown on access
Boolean premium; String userId = (String) c.getMethod("getUserID").invoke(null);
String userId = null; String downloadId = (String) c.getMethod("getDownloadID").invoke(null);
String downloadId = null; String resourceId = (String) c.getMethod("getResourceID").invoke(null);
String resourceId = null; Boolean premium = isPremium(resourceId, userId);
if (oldJarFile) {
premium = (Boolean) c.getMethod("isPremium").invoke(null);
} else {
userId = (String) c.getMethod("getUserID").invoke(null);
resourceId = (String) c.getMethod("getResourceID").invoke(null);
downloadId = (String) c.getMethod("getDownloadID").invoke(null);
premium = isPremium(resourceId, userId);
}
String pluginBuildDate = "??/??/????"; String pluginBuildDate = "??/??/????";
@ -164,8 +144,7 @@ public class LibsPremium {
String pluginVersion = config.getString("version"); String pluginVersion = config.getString("version");
return new PluginInformation(file.length(), userId, resourceId, downloadId, premium, pluginVersion, return new PluginInformation(file.length(), userId, resourceId, downloadId, premium, pluginVersion, pluginBuildNumber, pluginBuildDate);
pluginBuildNumber, pluginBuildDate);
} }
} }
@ -173,15 +152,18 @@ public class LibsPremium {
File[] files = new File("plugins/LibsDisguises/").listFiles(); File[] files = new File("plugins/LibsDisguises/").listFiles();
boolean foundJar = false; boolean foundJar = false;
if (files == null) if (files == null) {
return; return;
}
for (File file : files) { for (File file : files) {
if (!file.isFile()) if (!file.isFile()) {
continue; continue;
}
if (!file.getName().endsWith(".jar")) if (!file.getName().endsWith(".jar")) {
continue; continue;
}
foundJar = true; foundJar = true;
@ -189,13 +171,11 @@ public class LibsPremium {
try { try {
plugin = getInformation(file); plugin = getInformation(file);
} } catch (ClassNotFoundException | NoClassDefFoundError ex) {
catch (ClassNotFoundException ex) { DisguiseUtilities.getLogger().warning("Found an unrecognized jar in the LibsDisguises folder (" + file.getName() +
DisguiseUtilities.getLogger() ") - It may need replacing with a newer jar from SpigotMC");
.warning("Found an unrecognized jar in the LibsDisguises folder (" + file.getName() + ")");
continue; continue;
} } catch (Exception ex) {
catch (Exception ex) {
DisguiseUtilities.getLogger().warning("Error while trying to handle the file " + file.getName()); DisguiseUtilities.getLogger().warning("Error while trying to handle the file " + file.getName());
ex.printStackTrace(); ex.printStackTrace();
continue; continue;
@ -203,16 +183,13 @@ public class LibsPremium {
// Format into a string // Format into a string
// v5.2.6, build #40, created 16/02/2019 // v5.2.6, build #40, created 16/02/2019
String fileInfo = String.format("v%s, build %s, created %s", plugin.getVersion(), plugin.getBuildNumber(), String fileInfo = String.format("v%s, build %s, created %s", plugin.getVersion(), plugin.getBuildNumber(), plugin.getBuildDate());
plugin.getBuildDate());
if (plugin.isPremium()) { if (plugin.isPremium()) {
if (!isValidVersion(version, plugin.getVersion()) || plugin.getUserID() == null || if (!isValidVersion(version, plugin.getVersion()) || plugin.getUserID() == null || plugin.getDownloadID() == null ||
plugin.getDownloadID() == null || plugin.getUserID().equals("666666")) { plugin.getUserID().equals("666666")) {
DisguiseUtilities.getLogger().warning( DisguiseUtilities.getLogger().warning("You have an old Lib's Disguises jar (" + file.getName() + " " + fileInfo +
"You have an old Lib's Disguises jar (" + file.getName() + " " + fileInfo + ") in the LibsDisguises folder! For security purposes, please replace this with a" + " new " +
") in the LibsDisguises folder! For security purposes, please replace this with a" +
" new " +
"version from SpigotMC - https://www.spigotmc.org/resources/libs-disguises.32453/"); "version from SpigotMC - https://www.spigotmc.org/resources/libs-disguises.32453/");
continue; continue;
} }
@ -234,12 +211,10 @@ public class LibsPremium {
} else { } else {
// You have a non-premium Lib's Disguises jar (LibsDisguises.jar v5.2.6, build #40, created // You have a non-premium Lib's Disguises jar (LibsDisguises.jar v5.2.6, build #40, created
// 16/02/2019) in the LibsDisguises folder! // 16/02/2019) in the LibsDisguises folder!
DisguiseUtilities.getLogger().warning( DisguiseUtilities.getLogger()
"You have a non-premium Lib's Disguises jar (" + file.getName() + " " + fileInfo + .warning("You have a non-premium Lib's Disguises jar (" + file.getName() + " " + fileInfo + ") in the LibsDisguises folder!");
") in the LibsDisguises folder!"); DisguiseUtilities.getLogger()
DisguiseUtilities.getLogger().warning( .warning("Please place the premium jar downloaded from https://www.spigotmc" + ".org/resources/libs-disguises.32453/ " + "in here!");
"Please place the premium jar downloaded from https://www.spigotmc" +
".org/resources/libs-disguises.32453/ " + "in here!");
} }
} }
@ -251,8 +226,7 @@ public class LibsPremium {
thisPluginIsPaidFor = true; thisPluginIsPaidFor = true;
} else { } else {
DisguiseUtilities.getLogger().warning( DisguiseUtilities.getLogger().warning("If you own the plugin, place the premium jar downloaded from https://www.spigotmc" +
"If you own the plugin, place the premium jar downloaded from https://www.spigotmc" +
".org/resources/libs-disguises.32453/ in plugins/LibsDisguises/"); ".org/resources/libs-disguises.32453/ in plugins/LibsDisguises/");
} }
} }
@ -281,25 +255,21 @@ public class LibsPremium {
try { try {
pluginInformation = getInformation(file); pluginInformation = getInformation(file);
} } catch (Exception e) {
catch (Exception e) {
String pluginBuildDate = "??/??/????"; String pluginBuildDate = "??/??/????";
YamlConfiguration config = new YamlConfiguration(); YamlConfiguration config = new YamlConfiguration();
try { try {
try (InputStream stream = LibsDisguises.getInstance().getResource("plugin.yml")) { try (InputStream stream = LibsDisguises.getInstance().getResource("plugin.yml")) {
config.loadFromString( config.loadFromString(new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)).lines().collect(Collectors.joining("\n")));
new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8)).lines()
.collect(Collectors.joining("\n")));
} }
// If plugin.yml contains a build-date // If plugin.yml contains a build-date
if (config.contains("build-date")) { if (config.contains("build-date")) {
pluginBuildDate = config.getString("build-date"); pluginBuildDate = config.getString("build-date");
} }
} } catch (Exception ex) {
catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
@ -309,9 +279,8 @@ public class LibsPremium {
buildNo = "#" + buildNo; buildNo = "#" + buildNo;
} }
pluginInformation = new PluginInformation(LibsDisguises.getInstance().getFile().length(), getUserID(), pluginInformation = new PluginInformation(LibsDisguises.getInstance().getFile().length(), getUserID(), getResourceID(), getDownloadID(),
getResourceID(), getDownloadID(), isPremium(getResourceID(), getUserID()), version, buildNo, isPremium(getResourceID(), getUserID()), version, buildNo, pluginBuildDate);
pluginBuildDate);
} }
if (!isPremium() || !LibsDisguises.getInstance().isReleaseBuild()) { if (!isPremium() || !LibsDisguises.getInstance().isReleaseBuild()) {
@ -349,7 +318,7 @@ public class LibsPremium {
foundBetter = true; foundBetter = true;
break; break;
} catch (ClassNotFoundException e) { } catch (ClassNotFoundException | NoClassDefFoundError e) {
DisguiseUtilities.getLogger().info("Ew, error about invalid Libs Disguises jar. Deleting " + f.getName()); DisguiseUtilities.getLogger().info("Ew, error about invalid Libs Disguises jar. Deleting " + f.getName());
f.delete(); f.delete();
} catch (Throwable e) { } catch (Throwable e) {
@ -362,17 +331,14 @@ public class LibsPremium {
FileUtil.copy(f, new File(LibsDisguises.getInstance().getDataFolder(), f.getName())); FileUtil.copy(f, new File(LibsDisguises.getInstance().getDataFolder(), f.getName()));
DisguiseUtilities.getLogger().info("Copied " + f.getName() + DisguiseUtilities.getLogger().info("Copied " + f.getName() + " to the plugin folder! You can use dev builds with premium enabled!");
" to the plugin folder! You can use dev builds with premium enabled!");
} }
} }
if (isPremium()) { if (isPremium()) {
boolean prem = boolean prem = getPaidInformation() == null ? getPluginInformation().isLegit() : getPaidInformation().isLegit();
getPaidInformation() == null ? getPluginInformation().isLegit() : getPaidInformation().isLegit();
DisguiseUtilities.getLogger() DisguiseUtilities.getLogger().info("Premium enabled, thank you for supporting Lib's Disguises!" + (!prem ? "!" : ""));
.info("Premium enabled, thank you for supporting Lib's Disguises!" + (!prem ? "!" : ""));
} }
} }
} }

View File

@ -429,6 +429,20 @@ public class ReflectionManager {
return null; return null;
} }
public static YamlConfiguration getPluginYAMLEx(File file) throws Exception {
String s = getResourceAsString(file, "plugin.yml");
if (s == null) {
return null;
}
YamlConfiguration config = new YamlConfiguration();
config.loadFromString(getResourceAsString(file, "plugin.yml"));
return config;
}
public static int getNewEntityId() { public static int getNewEntityId() {
return getNewEntityId(true); return getNewEntityId(true);
} }