1
0
mirror of https://github.com/lucko/LuckPerms.git synced 2025-09-02 19:02:33 +02:00

Sponge use jar-in-jar loader

This commit is contained in:
lucko
2022-02-17 00:22:46 +00:00
committed by GitHub
parent 5c8b0a604e
commit b235234633
7 changed files with 145 additions and 88 deletions

View File

@@ -23,6 +23,9 @@ include (
'fabric', 'fabric',
'nukkit', 'nukkit',
'nukkit:loader', 'nukkit:loader',
'sponge', 'sponge:sponge-service', 'sponge:sponge-service-api8', 'sponge',
'sponge:loader',
'sponge:sponge-service',
'sponge:sponge-service-api8',
'velocity' 'velocity'
) )

View File

@@ -10,6 +10,7 @@ dependencies {
implementation project(':common') implementation project(':common')
implementation project(':sponge:sponge-service') implementation project(':sponge:sponge-service')
implementation project(':sponge:sponge-service-api8') implementation project(':sponge:sponge-service-api8')
compileOnly project(':common:loader-utils')
compileOnly('org.spongepowered:spongeapi:8.0.0') { compileOnly('org.spongepowered:spongeapi:8.0.0') {
exclude(module: 'configurate-core') exclude(module: 'configurate-core')
@@ -26,10 +27,9 @@ processResources {
} }
shadowJar { shadowJar {
archiveFileName = "LuckPerms-Sponge-${project.ext.fullVersion}.jar" archiveFileName = 'luckperms-sponge.jarinjar'
dependencies { dependencies {
include(dependency('net.luckperms:.*'))
include(dependency('me.lucko.luckperms:.*')) include(dependency('me.lucko.luckperms:.*'))
} }

View File

@@ -0,0 +1,32 @@
plugins {
id 'com.github.johnrengelman.shadow'
}
repositories {
maven { url 'https://repo.spongepowered.org/repository/maven-public/' }
}
dependencies {
compileOnly 'org.spongepowered:spongeapi:8.0.0'
implementation project(':api')
implementation project(':common:loader-utils')
}
processResources {
filesMatching('META-INF/sponge_plugins.json') {
expand 'pluginVersion': project.ext.fullVersion
}
}
shadowJar {
archiveFileName = "LuckPerms-Sponge-${project.ext.fullVersion}.jar"
from {
project(':sponge').tasks.shadowJar.archiveFile
}
}
artifacts {
archives shadowJar
}

View File

@@ -0,0 +1,75 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* 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.
*/
package me.lucko.luckperms.sponge.loader;
import com.google.inject.Inject;
import com.google.inject.Injector;
import me.lucko.luckperms.common.loader.JarInJarClassLoader;
import me.lucko.luckperms.common.loader.LoaderBootstrap;
import org.spongepowered.api.Server;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.Order;
import org.spongepowered.api.event.lifecycle.ConstructPluginEvent;
import org.spongepowered.api.event.lifecycle.StoppingEngineEvent;
import org.spongepowered.plugin.builtin.jvm.Plugin;
import java.util.function.Supplier;
@Plugin("luckperms")
public class SpongeLoaderPlugin implements Supplier<Injector> {
private static final String JAR_NAME = "luckperms-sponge.jarinjar";
private static final String BOOTSTRAP_CLASS = "me.lucko.luckperms.sponge.LPSpongeBootstrap";
private final LoaderBootstrap plugin;
private final Injector injector;
@Inject
public SpongeLoaderPlugin(Injector injector) {
this.injector = injector;
JarInJarClassLoader loader = new JarInJarClassLoader(getClass().getClassLoader(), JAR_NAME);
this.plugin = loader.instantiatePlugin(BOOTSTRAP_CLASS, Supplier.class, this);
}
@Override
public Injector get() {
return this.injector;
}
@Listener(order = Order.FIRST)
public void onEnable(ConstructPluginEvent event) {
this.plugin.onLoad();
this.plugin.onEnable();
}
@Listener
public void onDisable(StoppingEngineEvent<Server> event) {
this.plugin.onDisable();
}
}

View File

@@ -9,7 +9,7 @@
"id": "luckperms", "id": "luckperms",
"name": "LuckPerms", "name": "LuckPerms",
"version": "${pluginVersion}", "version": "${pluginVersion}",
"entrypoint": "me.lucko.luckperms.sponge.LPSpongeBootstrap", "entrypoint": "me.lucko.luckperms.sponge.loader.SpongeLoaderPlugin",
"description": "A permissions plugin", "description": "A permissions plugin",
"links": { "links": {
"homepage": "https://luckperms.net" "homepage": "https://luckperms.net"

View File

@@ -26,9 +26,13 @@
package me.lucko.luckperms.sponge; package me.lucko.luckperms.sponge;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Injector;
import me.lucko.luckperms.common.loader.LoaderBootstrap;
import me.lucko.luckperms.common.plugin.bootstrap.BootstrappedWithLoader;
import me.lucko.luckperms.common.plugin.bootstrap.LuckPermsBootstrap; import me.lucko.luckperms.common.plugin.bootstrap.LuckPermsBootstrap;
import me.lucko.luckperms.common.plugin.classpath.ClassPathAppender; import me.lucko.luckperms.common.plugin.classpath.ClassPathAppender;
import me.lucko.luckperms.common.plugin.classpath.JarInJarClassPathAppender;
import me.lucko.luckperms.common.plugin.logging.Log4jPluginLogger; import me.lucko.luckperms.common.plugin.logging.Log4jPluginLogger;
import me.lucko.luckperms.common.plugin.logging.PluginLogger; import me.lucko.luckperms.common.plugin.logging.PluginLogger;
import me.lucko.luckperms.common.util.MoreFiles; import me.lucko.luckperms.common.util.MoreFiles;
@@ -42,13 +46,8 @@ import org.spongepowered.api.Server;
import org.spongepowered.api.config.ConfigDir; import org.spongepowered.api.config.ConfigDir;
import org.spongepowered.api.entity.living.player.Player; import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.living.player.server.ServerPlayer; import org.spongepowered.api.entity.living.player.server.ServerPlayer;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.Order;
import org.spongepowered.api.event.lifecycle.ConstructPluginEvent;
import org.spongepowered.api.event.lifecycle.StoppingEngineEvent;
import org.spongepowered.api.profile.GameProfile; import org.spongepowered.api.profile.GameProfile;
import org.spongepowered.plugin.PluginContainer; import org.spongepowered.plugin.PluginContainer;
import org.spongepowered.plugin.builtin.jvm.Plugin;
import org.spongepowered.plugin.metadata.PluginMetadata; import org.spongepowered.plugin.metadata.PluginMetadata;
import java.io.IOException; import java.io.IOException;
@@ -62,12 +61,13 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.function.Supplier;
/** /**
* Bootstrap plugin for LuckPerms running on Sponge. * Bootstrap plugin for LuckPerms running on Sponge.
*/ */
@Plugin("luckperms") public class LPSpongeBootstrap implements LuckPermsBootstrap, LoaderBootstrap, BootstrappedWithLoader {
public class LPSpongeBootstrap implements LuckPermsBootstrap { private final Object loader;
/** /**
* The plugin logger * The plugin logger
@@ -111,22 +111,31 @@ public class LPSpongeBootstrap implements LuckPermsBootstrap {
/** /**
* Injected configuration directory for the plugin * Injected configuration directory for the plugin
*/ */
private final Path configDirectory;
@Inject @Inject
public LPSpongeBootstrap(Logger logger, Game game, PluginContainer pluginContainer, @ConfigDir(sharedRoot = false) Path configDirectory) { @ConfigDir(sharedRoot = false)
this.logger = new Log4jPluginLogger(logger); private Path configDirectory;
this.game = game;
this.pluginContainer = pluginContainer; public LPSpongeBootstrap(Supplier<Injector> loader) {
this.configDirectory = configDirectory; this.loader = loader;
Injector injector = loader.get();
this.logger = new Log4jPluginLogger(injector.getInstance(Logger.class));
this.game = injector.getInstance(Game.class);
this.pluginContainer = injector.getInstance(PluginContainer.class);
injector.injectMembers(this);
this.schedulerAdapter = new SpongeSchedulerAdapter(this.game, this.pluginContainer); this.schedulerAdapter = new SpongeSchedulerAdapter(this.game, this.pluginContainer);
this.classPathAppender = new SpongeClassPathAppender(this); this.classPathAppender = new JarInJarClassPathAppender(getClass().getClassLoader());
this.plugin = new LPSpongePlugin(this); this.plugin = new LPSpongePlugin(this);
} }
// provide adapters // provide adapters
@Override
public Object getLoader() {
return this.loader;
}
@Override @Override
public PluginLogger getPluginLogger() { public PluginLogger getPluginLogger() {
return this.logger; return this.logger;
@@ -143,15 +152,18 @@ public class LPSpongeBootstrap implements LuckPermsBootstrap {
} }
// lifecycle // lifecycle
@Listener(order = Order.FIRST)
public void onEnable(ConstructPluginEvent event) { @Override
this.startTime = Instant.now(); public void onLoad() {
try { try {
this.plugin.load(); this.plugin.load();
} finally { } finally {
this.loadLatch.countDown(); this.loadLatch.countDown();
} }
}
public void onEnable() {
this.startTime = Instant.now();
try { try {
this.plugin.enable(); this.plugin.enable();
} finally { } finally {
@@ -159,8 +171,7 @@ public class LPSpongeBootstrap implements LuckPermsBootstrap {
} }
} }
@Listener public void onDisable() {
public void onDisable(StoppingEngineEvent<Server> event) {
this.plugin.disable(); this.plugin.disable();
} }

View File

@@ -1,64 +0,0 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* 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.
*/
package me.lucko.luckperms.sponge;
import me.lucko.luckperms.common.plugin.bootstrap.LuckPermsBootstrap;
import me.lucko.luckperms.common.plugin.classpath.ReflectionClassPathAppender;
import java.lang.reflect.Field;
import java.net.URLClassLoader;
public class SpongeClassPathAppender extends ReflectionClassPathAppender {
private static URLClassLoader extractClassLoaderFromBootstrap(LuckPermsBootstrap bootstrap) {
ClassLoader classLoader = bootstrap.getClass().getClassLoader();
// try to cast directly to URLClassLoader in case things change in the future
if (classLoader instanceof URLClassLoader) {
return (URLClassLoader) classLoader;
}
Class<? extends ClassLoader> classLoaderClass = classLoader.getClass();
if (!classLoaderClass.getName().equals("cpw.mods.modlauncher.TransformingClassLoader")) {
throw new IllegalStateException("ClassLoader is not instance of TransformingClassLoader: " + classLoaderClass.getName());
}
try {
Field delegatedClassLoaderField = classLoaderClass.getDeclaredField("delegatedClassLoader");
delegatedClassLoaderField.setAccessible(true);
Object delegatedClassLoader = delegatedClassLoaderField.get(classLoader);
return (URLClassLoader) delegatedClassLoader;
} catch (ReflectiveOperationException e) {
throw new RuntimeException(e);
}
}
public SpongeClassPathAppender(LuckPermsBootstrap bootstrap) throws IllegalStateException {
super(extractClassLoaderFromBootstrap(bootstrap));
}
}