mirror of
https://github.com/GeyserMC/Geyser.git
synced 2024-08-14 23:57:35 +00:00
Moved the extension into geyser-api
This commit is contained in:
parent
bfe4c09290
commit
6757437193
13 changed files with 382 additions and 101 deletions
|
@ -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();
|
||||
}
|
|
@ -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<String> ApiVersions();
|
||||
String version();
|
||||
List<String> authors();
|
||||
}
|
|
@ -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);
|
||||
}
|
|
@ -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();
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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) {
|
|
@ -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) {
|
|
@ -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"));
|
||||
}
|
||||
|
|
|
@ -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<String, Class> 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<? extends GeyserExtension> 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);
|
|
@ -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<String> api;
|
||||
private String version;
|
||||
private final List<String> 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<String> getAPIVersions() {
|
||||
@Override
|
||||
public List<String> ApiVersions() {
|
||||
return api;
|
||||
}
|
||||
|
||||
public String getVersion() {
|
||||
@Override
|
||||
public String version() {
|
||||
return this.version;
|
||||
}
|
||||
|
||||
public List<String> getAuthors() {
|
||||
@Override
|
||||
public List<String> authors() {
|
||||
return this.authors;
|
||||
}
|
||||
}
|
|
@ -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<String, Class> classes = new HashMap<>();
|
||||
private final Map<String, ExtensionClassLoader> classLoaders = new HashMap<>();
|
||||
private final Map<String, GeyserExtensionClassLoader> 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);
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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<String, Extension> extensions = new LinkedHashMap<>();
|
||||
protected Map<Pattern, ExtensionLoader> fileAssociations = new HashMap<>();
|
||||
protected Map<Pattern, GeyserExtensionLoader> 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<? extends ExtensionLoader> loader) {
|
||||
ExtensionLoader instance;
|
||||
public void registerInterface(Class<? extends GeyserExtensionLoader> loader) {
|
||||
GeyserExtensionLoader instance;
|
||||
|
||||
if (ExtensionLoader.class.isAssignableFrom(loader)) {
|
||||
Constructor<? extends ExtensionLoader> constructor;
|
||||
if (GeyserExtensionLoader.class.isAssignableFrom(loader)) {
|
||||
Constructor<? extends GeyserExtensionLoader> 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<Pattern, ExtensionLoader> loaders) {
|
||||
for (ExtensionLoader loader : (loaders == null ? this.fileAssociations : loaders).values()) {
|
||||
for (Pattern pattern : loader.getExtensionFilters()) {
|
||||
public GeyserExtension loadExtension(File file, Map<Pattern, GeyserExtensionLoader> 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<String, File> extensions = new LinkedHashMap<>();
|
||||
Map<String, Extension> 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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue