From 6757437193b7ad0c72ffc9bf47e1fb7614f459f7 Mon Sep 17 00:00:00 2001 From: ImDaBigBoss <67973871+ImDaBigBoss@users.noreply.github.com> Date: Mon, 10 Jan 2022 20:01:36 +0100 Subject: [PATCH] Moved the extension into geyser-api --- .../geyser/api}/extension/Extension.java | 17 ++-- .../api/extension/ExtensionDescription.java | 36 ++++++++ .../geyser/api/extension/ExtensionLoader.java | 41 +++++++++ .../geyser/api/extension/ExtensionLogger.java | 90 +++++++++++++++++++ .../api}/extension/GeyserExtension.java | 43 +++++---- .../InvalidDescriptionException.java | 2 +- .../exception/InvalidExtensionException.java | 2 +- .../java/org/geysermc/geyser/GeyserImpl.java | 8 +- ...r.java => GeyserExtensionClassLoader.java} | 18 ++-- ...n.java => GeyserExtensionDescription.java} | 22 +++-- ...Loader.java => GeyserExtensionLoader.java} | 51 ++++++----- .../extension/GeyserExtensionLogger.java | 88 ++++++++++++++++++ ...nager.java => GeyserExtensionManager.java} | 65 +++++++------- 13 files changed, 382 insertions(+), 101 deletions(-) rename {core/src/main/java/org/geysermc/geyser => api/geyser/src/main/java/org/geysermc/geyser/api}/extension/Extension.java (83%) create mode 100644 api/geyser/src/main/java/org/geysermc/geyser/api/extension/ExtensionDescription.java create mode 100644 api/geyser/src/main/java/org/geysermc/geyser/api/extension/ExtensionLoader.java create mode 100644 api/geyser/src/main/java/org/geysermc/geyser/api/extension/ExtensionLogger.java rename {core/src/main/java/org/geysermc/geyser => api/geyser/src/main/java/org/geysermc/geyser/api}/extension/GeyserExtension.java (81%) rename {core/src/main/java/org/geysermc/geyser => api/geyser/src/main/java/org/geysermc/geyser/api}/extension/exception/InvalidDescriptionException.java (96%) rename {core/src/main/java/org/geysermc/geyser => api/geyser/src/main/java/org/geysermc/geyser/api}/extension/exception/InvalidExtensionException.java (96%) rename core/src/main/java/org/geysermc/geyser/extension/{ExtensionClassLoader.java => GeyserExtensionClassLoader.java} (80%) rename core/src/main/java/org/geysermc/geyser/extension/{ExtensionDescription.java => GeyserExtensionDescription.java} (85%) rename core/src/main/java/org/geysermc/geyser/extension/{ExtensionLoader.java => GeyserExtensionLoader.java} (74%) create mode 100644 core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLogger.java rename core/src/main/java/org/geysermc/geyser/extension/{ExtensionManager.java => GeyserExtensionManager.java} (79%) diff --git a/core/src/main/java/org/geysermc/geyser/extension/Extension.java b/api/geyser/src/main/java/org/geysermc/geyser/api/extension/Extension.java similarity index 83% rename from core/src/main/java/org/geysermc/geyser/extension/Extension.java rename to api/geyser/src/main/java/org/geysermc/geyser/api/extension/Extension.java index a911e56ad..8a820d8ac 100644 --- a/core/src/main/java/org/geysermc/geyser/extension/Extension.java +++ b/api/geyser/src/main/java/org/geysermc/geyser/api/extension/Extension.java @@ -23,9 +23,9 @@ * @link https://github.com/GeyserMC/Geyser */ -package org.geysermc.geyser.extension; +package org.geysermc.geyser.api.extension; -import org.geysermc.geyser.GeyserImpl; +import org.geysermc.api.GeyserApiBase; import java.io.File; import java.io.InputStream; @@ -37,14 +37,15 @@ public interface Extension { boolean isEnabled(); boolean isDisabled(); - File getDataFolder(); - ExtensionDescription getDescription(); - String getName(); + File dataFolder(); + ExtensionDescription description(); + String name(); InputStream getResource(String filename); void saveResource(String filename, boolean replace); - GeyserImpl getGeyser(); - ClassLoader getClassLoader(); - ExtensionLoader getExtensionLoader(); + ClassLoader classLoader(); + ExtensionLoader extensionLoader(); + ExtensionLogger logger(); + GeyserApiBase geyserApi(); } diff --git a/api/geyser/src/main/java/org/geysermc/geyser/api/extension/ExtensionDescription.java b/api/geyser/src/main/java/org/geysermc/geyser/api/extension/ExtensionDescription.java new file mode 100644 index 000000000..0f752e72d --- /dev/null +++ b/api/geyser/src/main/java/org/geysermc/geyser/api/extension/ExtensionDescription.java @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2019-2022 GeyserMC. http://geysermc.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @author GeyserMC + * @link https://github.com/GeyserMC/Geyser + */ + +package org.geysermc.geyser.api.extension; + +import java.util.*; + +public interface ExtensionDescription { + String name(); + String main(); + List ApiVersions(); + String version(); + List authors(); +} diff --git a/api/geyser/src/main/java/org/geysermc/geyser/api/extension/ExtensionLoader.java b/api/geyser/src/main/java/org/geysermc/geyser/api/extension/ExtensionLoader.java new file mode 100644 index 000000000..c558957eb --- /dev/null +++ b/api/geyser/src/main/java/org/geysermc/geyser/api/extension/ExtensionLoader.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2019-2022 GeyserMC. http://geysermc.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @author GeyserMC + * @link https://github.com/GeyserMC/Geyser + */ + +package org.geysermc.geyser.api.extension; + +import org.geysermc.geyser.api.extension.exception.InvalidDescriptionException; +import org.geysermc.geyser.api.extension.exception.InvalidExtensionException; + +import java.io.File; +import java.util.regex.Pattern; + +public interface ExtensionLoader { + GeyserExtension loadExtension(File file) throws InvalidExtensionException; + ExtensionDescription extensionDescription(File file) throws InvalidDescriptionException; + Pattern[] extensionFilters(); + Class classByName(final String name) throws ClassNotFoundException; + void enableExtension(Extension extension); + void disableExtension(Extension extension); +} diff --git a/api/geyser/src/main/java/org/geysermc/geyser/api/extension/ExtensionLogger.java b/api/geyser/src/main/java/org/geysermc/geyser/api/extension/ExtensionLogger.java new file mode 100644 index 000000000..6b5d86153 --- /dev/null +++ b/api/geyser/src/main/java/org/geysermc/geyser/api/extension/ExtensionLogger.java @@ -0,0 +1,90 @@ +/* + * Copyright (c) 2019-2022 GeyserMC. http://geysermc.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @author GeyserMC + * @link https://github.com/GeyserMC/Geyser + */ + +package org.geysermc.geyser.api.extension; + +public interface ExtensionLogger { + /** + * Get the logger prefix + * @return the logger prefix + */ + String prefix(); + + /** + * Logs a severe message to console + * + * @param message the message to log + */ + void severe(String message); + + /** + * Logs a severe message and an exception to console + * + * @param message the message to log + * @param error the error to throw + */ + void severe(String message, Throwable error); + + /** + * Logs an error message to console + * + * @param message the message to log + */ + void error(String message); + + /** + * Logs an error message and an exception to console + * + * @param message the message to log + * @param error the error to throw + */ + void error(String message, Throwable error); + + /** + * Logs a warning message to console + * + * @param message the message to log + */ + void warning(String message); + + /** + * Logs an info message to console + * + * @param message the message to log + */ + void info(String message); + + /** + * Logs a debug message to console + * + * @param message the message to log + */ + void debug(String message); + + /** + * If debug is enabled for this logger + */ + boolean isDebug(); +} diff --git a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtension.java b/api/geyser/src/main/java/org/geysermc/geyser/api/extension/GeyserExtension.java similarity index 81% rename from core/src/main/java/org/geysermc/geyser/extension/GeyserExtension.java rename to api/geyser/src/main/java/org/geysermc/geyser/api/extension/GeyserExtension.java index c6d7b6f10..3ed66b444 100644 --- a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtension.java +++ b/api/geyser/src/main/java/org/geysermc/geyser/api/extension/GeyserExtension.java @@ -23,9 +23,9 @@ * @link https://github.com/GeyserMC/Geyser */ -package org.geysermc.geyser.extension; +package org.geysermc.geyser.api.extension; -import org.geysermc.geyser.GeyserImpl; +import org.geysermc.api.GeyserApiBase; import java.io.*; import java.net.URL; import java.net.URLConnection; @@ -36,9 +36,10 @@ public class GeyserExtension implements Extension { private File file = null; private File dataFolder = null; private ClassLoader classLoader = null; - private GeyserImpl geyser = null; private ExtensionLoader loader; + private ExtensionLogger logger; private ExtensionDescription description = null; + private GeyserApiBase api = null; @Override public void onLoad() { @@ -74,29 +75,30 @@ public class GeyserExtension implements Extension { } @Override - public File getDataFolder() { + public File dataFolder() { return this.dataFolder; } @Override - public ExtensionDescription getDescription() { + public ExtensionDescription description() { return this.description; } @Override - public String getName() { - return this.description.getName(); + public String name() { + return this.description.name(); } - public void init(GeyserImpl geyser, ExtensionDescription description, File dataFolder, File file, ExtensionLoader loader) { + public void init(GeyserApiBase api, ExtensionLogger logger, ExtensionLoader loader, ExtensionDescription description, File dataFolder, File file) { if (!this.initialized) { this.initialized = true; this.file = file; this.dataFolder = dataFolder; this.classLoader = this.getClass().getClassLoader(); - this.geyser = geyser; this.loader = loader; + this.logger = logger; this.description = description; + this.api = api; } } @@ -152,25 +154,30 @@ public class GeyserExtension implements Extension { out.close(); in.close(); } else { - this.geyser.getLogger().warning("Could not save " + outFile.getName() + " to " + outFile + " because " + outFile.getName() + " already exists."); + this.logger.warning("Could not save " + outFile.getName() + " to " + outFile + " because " + outFile.getName() + " already exists."); } } catch (IOException ex) { - this.geyser.getLogger().severe("Could not save " + outFile.getName() + " to " + outFile, ex); + this.logger.severe("Could not save " + outFile.getName() + " to " + outFile, ex); } } @Override - public GeyserImpl getGeyser() { - return this.geyser; - } - - @Override - public ClassLoader getClassLoader() { + public ClassLoader classLoader() { return this.classLoader; } @Override - public ExtensionLoader getExtensionLoader() { + public ExtensionLoader extensionLoader() { return this.loader; } + + @Override + public ExtensionLogger logger() { + return this.logger; + } + + @Override + public GeyserApiBase geyserApi() { + return this.api; + } } diff --git a/core/src/main/java/org/geysermc/geyser/extension/exception/InvalidDescriptionException.java b/api/geyser/src/main/java/org/geysermc/geyser/api/extension/exception/InvalidDescriptionException.java similarity index 96% rename from core/src/main/java/org/geysermc/geyser/extension/exception/InvalidDescriptionException.java rename to api/geyser/src/main/java/org/geysermc/geyser/api/extension/exception/InvalidDescriptionException.java index 0764dc1d1..ca2fabad9 100644 --- a/core/src/main/java/org/geysermc/geyser/extension/exception/InvalidDescriptionException.java +++ b/api/geyser/src/main/java/org/geysermc/geyser/api/extension/exception/InvalidDescriptionException.java @@ -23,7 +23,7 @@ * @link https://github.com/GeyserMC/Geyser */ -package org.geysermc.geyser.extension.exception; +package org.geysermc.geyser.api.extension.exception; public class InvalidDescriptionException extends Exception { public InvalidDescriptionException(Throwable cause) { diff --git a/core/src/main/java/org/geysermc/geyser/extension/exception/InvalidExtensionException.java b/api/geyser/src/main/java/org/geysermc/geyser/api/extension/exception/InvalidExtensionException.java similarity index 96% rename from core/src/main/java/org/geysermc/geyser/extension/exception/InvalidExtensionException.java rename to api/geyser/src/main/java/org/geysermc/geyser/api/extension/exception/InvalidExtensionException.java index f29f5d280..1053e6d50 100644 --- a/core/src/main/java/org/geysermc/geyser/extension/exception/InvalidExtensionException.java +++ b/api/geyser/src/main/java/org/geysermc/geyser/api/extension/exception/InvalidExtensionException.java @@ -23,7 +23,7 @@ * @link https://github.com/GeyserMC/Geyser */ -package org.geysermc.geyser.extension.exception; +package org.geysermc.geyser.api.extension.exception; public class InvalidExtensionException extends Exception { public InvalidExtensionException(Throwable cause) { diff --git a/core/src/main/java/org/geysermc/geyser/GeyserImpl.java b/core/src/main/java/org/geysermc/geyser/GeyserImpl.java index 82a5fd354..7e61b3af7 100644 --- a/core/src/main/java/org/geysermc/geyser/GeyserImpl.java +++ b/core/src/main/java/org/geysermc/geyser/GeyserImpl.java @@ -52,7 +52,7 @@ import org.geysermc.geyser.api.GeyserApi; import org.geysermc.geyser.command.CommandManager; import org.geysermc.geyser.configuration.GeyserConfiguration; import org.geysermc.geyser.entity.EntityDefinitions; -import org.geysermc.geyser.extension.ExtensionManager; +import org.geysermc.geyser.extension.GeyserExtensionManager; import org.geysermc.geyser.level.WorldManager; import org.geysermc.geyser.network.ConnectorServerEventHandler; import org.geysermc.geyser.pack.ResourcePack; @@ -155,6 +155,8 @@ public class GeyserImpl implements GeyserApi { MessageTranslator.init(); MinecraftLocale.init(); + GeyserExtensionManager.init(); + start(); GeyserConfiguration config = bootstrap.getGeyserConfig(); @@ -198,8 +200,6 @@ public class GeyserImpl implements GeyserApi { ResourcePack.loadPacks(); - ExtensionManager.init(); - if (platformType != PlatformType.STANDALONE && config.getRemote().getAddress().equals("auto")) { // Set the remote address to localhost since that is where we are always connecting try { @@ -460,7 +460,7 @@ public class GeyserImpl implements GeyserApi { ResourcePack.PACKS.clear(); - ExtensionManager.getExtensionManager().disableExtensions(); + GeyserExtensionManager.getExtensionManager().disableExtensions(); bootstrap.getGeyserLogger().info(GeyserLocale.getLocaleStringLog("geyser.core.shutdown.done")); } diff --git a/core/src/main/java/org/geysermc/geyser/extension/ExtensionClassLoader.java b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionClassLoader.java similarity index 80% rename from core/src/main/java/org/geysermc/geyser/extension/ExtensionClassLoader.java rename to core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionClassLoader.java index 3261adc01..a1ccc063e 100644 --- a/core/src/main/java/org/geysermc/geyser/extension/ExtensionClassLoader.java +++ b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionClassLoader.java @@ -25,7 +25,9 @@ package org.geysermc.geyser.extension; -import org.geysermc.geyser.extension.exception.InvalidExtensionException; +import org.geysermc.geyser.api.extension.ExtensionDescription; +import org.geysermc.geyser.api.extension.GeyserExtension; +import org.geysermc.geyser.api.extension.exception.InvalidExtensionException; import java.io.File; import java.net.MalformedURLException; import java.net.URL; @@ -34,28 +36,28 @@ import java.util.HashMap; import java.util.Map; import java.util.Set; -public class ExtensionClassLoader extends URLClassLoader { - private ExtensionLoader loader; +public class GeyserExtensionClassLoader extends URLClassLoader { + private GeyserExtensionLoader loader; private Map classes = new HashMap<>(); public GeyserExtension extension; - public ExtensionClassLoader(ExtensionLoader loader, ClassLoader parent, ExtensionDescription description, File file) throws InvalidExtensionException, MalformedURLException { + public GeyserExtensionClassLoader(GeyserExtensionLoader loader, ClassLoader parent, ExtensionDescription description, File file) throws InvalidExtensionException, MalformedURLException { super(new URL[] { file.toURI().toURL() }, parent); this.loader = loader; try { Class jarClass; try { - jarClass = Class.forName(description.getMain(), true, this); + jarClass = Class.forName(description.main(), true, this); } catch (ClassNotFoundException ex) { - throw new InvalidExtensionException("Class " + description.getMain() + " not found, extension cannot be loaded", ex); + throw new InvalidExtensionException("Class " + description.main() + " not found, extension cannot be loaded", ex); } Class extensionClass; try { extensionClass = jarClass.asSubclass(GeyserExtension.class); } catch (ClassCastException ex) { - throw new InvalidExtensionException("Main class " + description.getMain() + " should extends GeyserExtension, but extends " + jarClass.getSuperclass().getSimpleName(), ex); + throw new InvalidExtensionException("Main class " + description.main() + " should extends GeyserExtension, but extends " + jarClass.getSuperclass().getSimpleName(), ex); } extension = extensionClass.newInstance(); @@ -78,7 +80,7 @@ public class ExtensionClassLoader extends URLClassLoader { Class result = classes.get(name); if(result == null) { if(checkGlobal) { - result = loader.getClassByName(name); + result = loader.classByName(name); } if(result == null) { result = super.findClass(name); diff --git a/core/src/main/java/org/geysermc/geyser/extension/ExtensionDescription.java b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionDescription.java similarity index 85% rename from core/src/main/java/org/geysermc/geyser/extension/ExtensionDescription.java rename to core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionDescription.java index 9f714dc11..ee215f2e4 100644 --- a/core/src/main/java/org/geysermc/geyser/extension/ExtensionDescription.java +++ b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionDescription.java @@ -25,20 +25,19 @@ package org.geysermc.geyser.extension; -import org.geysermc.geyser.extension.exception.InvalidDescriptionException; +import org.geysermc.geyser.api.extension.exception.InvalidDescriptionException; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; - import java.util.*; -public class ExtensionDescription { +public class GeyserExtensionDescription implements org.geysermc.geyser.api.extension.ExtensionDescription { private String name; private String main; private List api; private String version; private final List authors = new ArrayList<>(); - public ExtensionDescription(String yamlString) throws InvalidDescriptionException { + public GeyserExtensionDescription(String yamlString) throws InvalidDescriptionException { DumperOptions dumperOptions = new DumperOptions(); dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml yaml = new Yaml(dumperOptions); @@ -72,23 +71,28 @@ public class ExtensionDescription { } } - public String getName() { + @Override + public String name() { return this.name; } - public String getMain() { + @Override + public String main() { return this.main; } - public List getAPIVersions() { + @Override + public List ApiVersions() { return api; } - public String getVersion() { + @Override + public String version() { return this.version; } - public List getAuthors() { + @Override + public List authors() { return this.authors; } } diff --git a/core/src/main/java/org/geysermc/geyser/extension/ExtensionLoader.java b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java similarity index 74% rename from core/src/main/java/org/geysermc/geyser/extension/ExtensionLoader.java rename to core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java index 15795c2c5..9b741fb2d 100644 --- a/core/src/main/java/org/geysermc/geyser/extension/ExtensionLoader.java +++ b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLoader.java @@ -25,9 +25,13 @@ package org.geysermc.geyser.extension; +import org.geysermc.api.Geyser; import org.geysermc.geyser.GeyserImpl; -import org.geysermc.geyser.extension.exception.InvalidDescriptionException; -import org.geysermc.geyser.extension.exception.InvalidExtensionException; +import org.geysermc.geyser.api.extension.Extension; +import org.geysermc.geyser.api.extension.ExtensionLoader; +import org.geysermc.geyser.api.extension.GeyserExtension; +import org.geysermc.geyser.api.extension.exception.InvalidDescriptionException; +import org.geysermc.geyser.api.extension.exception.InvalidExtensionException; import java.io.*; import java.util.HashMap; import java.util.Map; @@ -35,10 +39,11 @@ import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.regex.Pattern; -public class ExtensionLoader { +public class GeyserExtensionLoader implements ExtensionLoader { private final Map classes = new HashMap<>(); - private final Map classLoaders = new HashMap<>(); + private final Map classLoaders = new HashMap<>(); + @Override public GeyserExtension loadExtension(File file) throws InvalidExtensionException { if (file == null) { throw new InvalidExtensionException("File is null"); @@ -48,37 +53,39 @@ public class ExtensionLoader { throw new InvalidExtensionException(new FileNotFoundException(file.getPath()) + " does not exist"); } - final ExtensionDescription description; + final GeyserExtensionDescription description; try { - description = getExtensionDescription(file); + description = extensionDescription(file); } catch (InvalidDescriptionException e) { throw new InvalidExtensionException(e); } final File parentFile = file.getParentFile(); - final File dataFolder = new File(parentFile, description.getName()); + final File dataFolder = new File(parentFile, description.name()); if (dataFolder.exists() && !dataFolder.isDirectory()) { - throw new InvalidExtensionException("The folder " + dataFolder.getPath() + " is not a directory and is the data folder for the extension " + description.getName() + "!"); + throw new InvalidExtensionException("The folder " + dataFolder.getPath() + " is not a directory and is the data folder for the extension " + description.name() + "!"); } - final ExtensionClassLoader loader; + final GeyserExtensionClassLoader loader; try { - loader = new ExtensionClassLoader(this, getClass().getClassLoader(), description, file); + loader = new GeyserExtensionClassLoader(this, getClass().getClassLoader(), description, file); } catch (Throwable e) { throw new InvalidExtensionException(e); } - classLoaders.put(description.getName(), loader); + classLoaders.put(description.name(), loader); setup(loader.extension, description, dataFolder, file); return loader.extension; } - private void setup(GeyserExtension extension, ExtensionDescription description, File dataFolder, File file) { - extension.init(GeyserImpl.getInstance(), description, dataFolder, file, this); + private void setup(GeyserExtension extension, GeyserExtensionDescription description, File dataFolder, File file) { + GeyserExtensionLogger logger = new GeyserExtensionLogger(GeyserImpl.getInstance().getLogger(), description.name()); + extension.init(Geyser.api(), logger, this, description, dataFolder, file); extension.onLoad(); } - public ExtensionDescription getExtensionDescription(File file) throws InvalidDescriptionException { + @Override + public GeyserExtensionDescription extensionDescription(File file) throws InvalidDescriptionException { JarFile jarFile = null; InputStream stream = null; @@ -105,7 +112,7 @@ public class ExtensionLoader { temp = bufferedReader.readLine(); } - return new ExtensionDescription(builder.toString()); + return new GeyserExtensionDescription(builder.toString()); } catch (IOException e) { throw new InvalidDescriptionException(e); } finally { @@ -124,14 +131,16 @@ public class ExtensionLoader { } } - public Pattern[] getExtensionFilters() { + @Override + public Pattern[] extensionFilters() { return new Pattern[] { Pattern.compile("^.+\\.jar$") }; } - public Class getClassByName(final String name) throws ClassNotFoundException{ + @Override + public Class classByName(final String name) throws ClassNotFoundException{ Class clazz = classes.get(name); try { - for(ExtensionClassLoader loader : classLoaders.values()) { + for(GeyserExtensionClassLoader loader : classLoaders.values()) { try { clazz = loader.findClass(name,false); } catch(NullPointerException e) { @@ -153,19 +162,21 @@ public class ExtensionLoader { Class clazz = classes.remove(name); } + @Override public void enableExtension(Extension extension) { if (extension instanceof GeyserExtension) { if(!extension.isEnabled()) { - GeyserImpl.getInstance().getLogger().info("Enabled extension " + extension.getDescription().getName()); + GeyserImpl.getInstance().getLogger().info("Enabled extension " + extension.description().name()); ((GeyserExtension) extension).setEnabled(true); } } } + @Override public void disableExtension(Extension extension) { if (extension instanceof GeyserExtension) { if(extension.isEnabled()) { - GeyserImpl.getInstance().getLogger().info("Disabled extension " + extension.getDescription().getName()); + GeyserImpl.getInstance().getLogger().info("Disabled extension " + extension.description().name()); ((GeyserExtension) extension).setEnabled(false); } } diff --git a/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLogger.java b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLogger.java new file mode 100644 index 000000000..225d6cda3 --- /dev/null +++ b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionLogger.java @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2019-2022 GeyserMC. http://geysermc.org + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + * @author GeyserMC + * @link https://github.com/GeyserMC/Geyser + */ + +package org.geysermc.geyser.extension; + +import org.geysermc.geyser.GeyserLogger; +import org.geysermc.geyser.api.extension.ExtensionLogger; + +public class GeyserExtensionLogger implements ExtensionLogger { + private GeyserLogger logger; + private String loggerPrefix; + + public GeyserExtensionLogger(GeyserLogger logger, String prefix) { + this.logger = logger; + this.loggerPrefix = prefix; + } + + @Override + public String prefix() { + return this.loggerPrefix; + } + + private String addPrefix(String message) { + return "[" + this.loggerPrefix + "] " + message; + } + + @Override + public void severe(String message) { + this.logger.severe(this.addPrefix(message)); + } + + @Override + public void severe(String message, Throwable error) { + this.logger.severe(this.addPrefix(message), error); + } + + @Override + public void error(String message) { + this.logger.error(this.addPrefix(message)); + } + + @Override + public void error(String message, Throwable error) { + this.logger.error(this.addPrefix(message), error); + } + + @Override + public void warning(String message) { + this.logger.warning(this.addPrefix(message)); + } + + @Override + public void info(String message) { + this.logger.info(this.addPrefix(message)); + } + + @Override + public void debug(String message) { + this.logger.debug(this.addPrefix(message)); + } + + @Override + public boolean isDebug() { + return this.logger.isDebug(); + } +} diff --git a/core/src/main/java/org/geysermc/geyser/extension/ExtensionManager.java b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionManager.java similarity index 79% rename from core/src/main/java/org/geysermc/geyser/extension/ExtensionManager.java rename to core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionManager.java index ad757d499..c06feba1e 100644 --- a/core/src/main/java/org/geysermc/geyser/extension/ExtensionManager.java +++ b/core/src/main/java/org/geysermc/geyser/extension/GeyserExtensionManager.java @@ -26,35 +26,36 @@ package org.geysermc.geyser.extension; import org.geysermc.geyser.GeyserImpl; - +import org.geysermc.geyser.api.extension.Extension; +import org.geysermc.geyser.api.extension.ExtensionDescription; +import org.geysermc.geyser.api.extension.GeyserExtension; import java.io.File; -import java.io.FilenameFilter; import java.lang.reflect.Constructor; import java.util.*; import java.util.regex.Pattern; -public class ExtensionManager { - private static ExtensionManager extensionManager = null; +public class GeyserExtensionManager { + private static GeyserExtensionManager geyserExtensionManager = null; protected Map extensions = new LinkedHashMap<>(); - protected Map fileAssociations = new HashMap<>(); + protected Map fileAssociations = new HashMap<>(); public static void init() { GeyserImpl.getInstance().getLogger().info("Loading extensions..."); - extensionManager = new ExtensionManager(); - extensionManager.registerInterface(ExtensionLoader.class); - extensionManager.loadExtensions(new File("extensions")); - GeyserImpl.getInstance().getLogger().info("Loaded " + extensionManager.extensions.size() + " extensions."); + geyserExtensionManager = new GeyserExtensionManager(); + geyserExtensionManager.registerInterface(GeyserExtensionLoader.class); + geyserExtensionManager.loadExtensions(new File("extensions")); + GeyserImpl.getInstance().getLogger().info("Loaded " + geyserExtensionManager.extensions.size() + " extensions."); - for (Extension extension : extensionManager.getExtensions().values()) { + for (Extension extension : geyserExtensionManager.getExtensions().values()) { if (!extension.isEnabled()) { - extensionManager.enableExtension(extension); + geyserExtensionManager.enableExtension(extension); } } } - public static ExtensionManager getExtensionManager() { - return extensionManager; + public static GeyserExtensionManager getExtensionManager() { + return geyserExtensionManager; } public Extension getExtension(String name) { @@ -68,11 +69,11 @@ public class ExtensionManager { return this.extensions; } - public void registerInterface(Class loader) { - ExtensionLoader instance; + public void registerInterface(Class loader) { + GeyserExtensionLoader instance; - if (ExtensionLoader.class.isAssignableFrom(loader)) { - Constructor constructor; + if (GeyserExtensionLoader.class.isAssignableFrom(loader)) { + Constructor constructor; try { constructor = loader.getConstructor(); @@ -88,7 +89,7 @@ public class ExtensionManager { throw new IllegalArgumentException("Class " + loader.getName() + " does not implement interface ExtensionLoader"); } - Pattern[] patterns = instance.getExtensionFilters(); + Pattern[] patterns = instance.extensionFilters(); synchronized (this) { for (Pattern pattern : patterns) { @@ -97,17 +98,17 @@ public class ExtensionManager { } } - public GeyserExtension loadExtension(File file, Map loaders) { - for (ExtensionLoader loader : (loaders == null ? this.fileAssociations : loaders).values()) { - for (Pattern pattern : loader.getExtensionFilters()) { + public GeyserExtension loadExtension(File file, Map loaders) { + for (GeyserExtensionLoader loader : (loaders == null ? this.fileAssociations : loaders).values()) { + for (Pattern pattern : loader.extensionFilters()) { if (pattern.matcher(file.getName()).matches()) { try { - ExtensionDescription description = loader.getExtensionDescription(file); + ExtensionDescription description = loader.extensionDescription(file); if (description != null) { GeyserExtension extension = loader.loadExtension(file); if (extension != null) { - this.extensions.put(extension.getDescription().getName(), extension); + this.extensions.put(extension.description().name(), extension); return extension; } @@ -143,9 +144,9 @@ public class ExtensionManager { Map extensions = new LinkedHashMap<>(); Map loadedExtensions = new LinkedHashMap<>(); - for (final ExtensionLoader loader : this.fileAssociations.values()) { + for (final GeyserExtensionLoader loader : this.fileAssociations.values()) { for (File file : dictionary.listFiles((dir, name) -> { - for (Pattern pattern : loader.getExtensionFilters()) { + for (Pattern pattern : loader.extensionFilters()) { if (pattern.matcher(name).matches()) { return true; } @@ -157,9 +158,9 @@ public class ExtensionManager { } try { - ExtensionDescription description = loader.getExtensionDescription(file); + ExtensionDescription description = loader.extensionDescription(file); if (description != null) { - String name = description.getName(); + String name = description.name(); if (extensions.containsKey(name) || this.getExtension(name) != null) { GeyserImpl.getInstance().getLogger().warning("Found duplicate extension '" + name + "', ignoring '" + file.getName() + "'"); @@ -168,7 +169,7 @@ public class ExtensionManager { boolean compatible = false; - for (String version : description.getAPIVersions()) { + for (String version : description.ApiVersions()) { try { //Check the format: majorVersion.minorVersion.patch if (!Pattern.matches("^[0-9]+\\.[0-9]+\\.[0-9]+$", version)) { @@ -217,9 +218,9 @@ public class ExtensionManager { public void enableExtension(Extension extension) { if (!extension.isEnabled()) { try { - extension.getExtensionLoader().enableExtension(extension); + extension.extensionLoader().enableExtension(extension); } catch (Exception e) { - GeyserImpl.getInstance().getLogger().error("Error enabling extension " + extension.getName() + ": ", e); + GeyserImpl.getInstance().getLogger().error("Error enabling extension " + extension.name() + ": ", e); this.disableExtension(extension); } } @@ -228,9 +229,9 @@ public class ExtensionManager { public void disableExtension(Extension extension) { if (extension.isEnabled()) { try { - extension.getExtensionLoader().disableExtension(extension); + extension.extensionLoader().disableExtension(extension); } catch (Exception e) { - GeyserImpl.getInstance().getLogger().error("Error disabling extension " + extension.getName() + ": ", e); + GeyserImpl.getInstance().getLogger().error("Error disabling extension " + extension.name() + ": ", e); } } }