Skip to content

Commit

Permalink
wip: refactor: add loader modules per platform
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotcorreia committed Dec 27, 2023
1 parent 4536a88 commit d906f8d
Show file tree
Hide file tree
Showing 17 changed files with 244 additions and 170 deletions.
34 changes: 19 additions & 15 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ allprojects {

subprojects {
dependencies {
implementation 'org.jetbrains:annotations:23.0.0'
compileOnly 'org.jetbrains:annotations:23.0.0'

compileOnly 'org.projectlombok:lombok:1.18.22'
annotationProcessor 'org.projectlombok:lombok:1.18.22'
Expand All @@ -57,25 +57,29 @@ subprojects {
}

dependencies {
implementation project(':core:triton-core-loader')
implementation project(':triton-spigot:triton-spigot-loader')
implementation project(':triton-velocity:triton-velocity-loader')

implementation project(path: ":core")
implementation project(path: ":triton-bungeecord")
implementation project(path: ":triton-spigot")
implementation project(path: ":triton-velocity")
compileOnly project(':core')
compileOnly project(':triton-bungeecord')
compileOnly project(':triton-spigot')
compileOnly project(':triton-velocity')
}

shadowJar {
getArchiveBaseName().set(rootProject.name)
relocate 'org.bstats', 'com.rexcantor64.triton.metrics'
//relocate 'org.slf4j', 'com.rexcantor64.shaded.slf4j'
relocate 'com.tananaev.jsonpatch', 'com.rexcantor64.shaded.jsonpatch'
relocate 'com.zaxxer.hikari', 'com.rexcantor64.shaded.hikari'

relocate 'net.kyori.adventure.text.minimessage', 'com.rexcantor64.triton.lib.adventure.minimessage'
relocate 'net.kyori.adventure.text.serializer.gson', 'com.rexcantor64.triton.lib.adventure.serializer.gson'
relocate 'net.kyori.adventure.text.serializer.legacy', 'com.rexcantor64.triton.lib.adventure.serializer.legacy'
relocate 'net.kyori.adventure.text.serializer.plain', 'com.rexcantor64.triton.lib.adventure.serializer.plain'
relocate 'net.kyori.adventure.text.serializer.bungeecord', 'com.rexcantor64.triton.lib.adventure.serializer.bungeecord'
from {
project(':core').tasks.shadowJar.archiveFile
}
from {
project(':triton-spigot').tasks.shadowJar.archiveFile
}
from {
project(':triton-velocity').tasks.shadowJar.archiveFile
}

relocate 'net.byteflux.libby', 'com.rexcantor64.triton.lib.libby'
relocate 'org.objectweb.asm', 'com.rexcantor64.triton.lib.objectweb.asm'
relocate 'me.lucko.jarrelocator', 'com.rexcantor64.triton.lib.jarrelocator'
}
11 changes: 11 additions & 0 deletions core/loader/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plugins {
id 'java-library'
}

group 'com.rexcantor64.triton'

dependencies {
api 'org.ow2.asm:asm-commons:9.2'
api 'org.ow2.asm:asm:9.2'
api 'me.lucko:jar-relocator:1.7'
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public class JarInJarClassLoader extends URLClassLoader {
* @param jarResourcePaths one or more paths to the jar-in-jar resources within the loader jar
* @throws LoadingException if something unexpectedly bad happens
*/
public JarInJarClassLoader(ClassLoader loaderClassLoader, String... jarResourcePaths) throws LoadingException {
public JarInJarClassLoader(ClassLoader loaderClassLoader, List<Relocation> relocations, String... jarResourcePaths) throws LoadingException {
super(
Arrays.stream(jarResourcePaths)
.map(path -> extractJar(loaderClassLoader, path))
.map(path -> extractJar(loaderClassLoader, path, relocations))
.toArray(URL[]::new),
loaderClassLoader);
}
Expand Down Expand Up @@ -75,6 +75,19 @@ public void deleteJarResource() {
* @return the instantiated bootstrap plugin
*/
public <T> LoaderBootstrap instantiatePlugin(String bootstrapClass, Class<T> loaderPluginType, T loaderPlugin) throws LoadingException {
return this.instantiatePlugin(bootstrapClass, new Class[]{loaderPluginType}, new Object[]{loaderPlugin});
}

/**
* Creates a new plugin instance.
*
* @param bootstrapClass the name of the bootstrap plugin class
* @param loaderPluginType the type of the loader plugin, the only parameter of the bootstrap
* plugin constructor
* @param loaderPlugin the loader plugin instance
* @return the instantiated bootstrap plugin
*/
public LoaderBootstrap instantiatePlugin(String bootstrapClass, Class<?>[] loaderPluginType, Object[] loaderPlugin) throws LoadingException {
Class<? extends LoaderBootstrap> plugin;
try {
plugin = loadClass(bootstrapClass).asSubclass(LoaderBootstrap.class);
Expand Down Expand Up @@ -104,7 +117,7 @@ public <T> LoaderBootstrap instantiatePlugin(String bootstrapClass, Class<T> loa
* @param jarResourcePath the inner jar resource path
* @return a URL to the extracted file
*/
private static URL extractJar(ClassLoader loaderClassLoader, String jarResourcePath) throws LoadingException {
private static URL extractJar(ClassLoader loaderClassLoader, String jarResourcePath, List<Relocation> relocations) throws LoadingException {
// get the jar-in-jar resource
URL jarInJar = loaderClassLoader.getResource(jarResourcePath);
if (jarInJar == null) {
Expand Down Expand Up @@ -133,16 +146,20 @@ private static URL extractJar(ClassLoader loaderClassLoader, String jarResourceP
throw new LoadingException("Unable to copy jar-in-jar to temporary path", e);
}

try {
List<Relocation> relocations = new LinkedList<>();
relocations.add(new Relocation("net/kyori/adventure", "com/rexcantor64/triton/lib/adventure"));
new JarRelocator(path.toFile(), pathRelocated.toFile(), relocations).run();
} catch (IOException e) {
throw new LoadingException("Unable to apply relocations to jar", e);
if (!relocations.isEmpty()) {
try {
new JarRelocator(path.toFile(), pathRelocated.toFile(), relocations).run();
} catch (IOException e) {
throw new LoadingException("Unable to apply relocations to jar", e);
}
}

try {
return pathRelocated.toUri().toURL();
if (relocations.isEmpty()) {
return path.toUri().toURL();
} else {
return pathRelocated.toUri().toURL();
}
} catch (MalformedURLException e) {
throw new LoadingException("Unable to get URL from path", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
*/
public interface LoaderBootstrap {

void onLoad();

default void onEnable() {}

default void onDisable() {}
Expand Down

This file was deleted.

9 changes: 9 additions & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,12 @@ include 'triton-bungeecord'
include 'triton-spigot'
include 'triton-velocity'
include 'loader'
include 'core:loader'
findProject(':core:loader')?.name = 'triton-core-loader'
include 'triton-bungeecord:loader'
findProject(':triton-bungeecord:loader')?.name = 'triton-bungeecord-loader'
include 'triton-spigot:loader'
findProject(':triton-spigot:loader')?.name = 'triton-spigot-loader'
include 'triton-velocity:loader'
findProject(':triton-velocity:loader')?.name = 'triton-velocity-loader'

19 changes: 19 additions & 0 deletions triton-bungeecord/loader/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
plugins {
id 'java'
}

group = 'org.example'
version = '4.0.0-SNAPSHOT'

repositories {
mavenCentral()
}

dependencies {
testImplementation platform('org.junit:junit-bom:5.9.1')
testImplementation 'org.junit.jupiter:junit-jupiter'
}

test {
useJUnitPlatform()
}
7 changes: 7 additions & 0 deletions triton-bungeecord/loader/src/main/java/org/example/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.example;

public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
74 changes: 5 additions & 69 deletions triton-spigot/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
plugins {
id 'java'
id 'net.minecrell.plugin-yml.bukkit' version '0.5.2'
id 'com.github.johnrengelman.shadow' version '7.1.0'
}

Expand All @@ -10,15 +9,14 @@ String pluginAuthor = project.property("pluginAuthor")
String pluginApiVersion = project.property("pluginApiVersion")
String pluginName = project.property("pluginName")

apply plugin: 'net.minecrell.plugin-yml.bukkit'
group 'com.rexcantor64.triton'

dependencies {
compileOnly project(path: ":loader")
compileOnly project(path: ":api")
compileOnly project(path: ":core")
implementation project(path: ":spigot-legacy")
implementation project(path: ":v1_13")
compileOnly project(':core:triton-core-loader')
compileOnly project(':api')
compileOnly project(':core')
implementation project(':spigot-legacy')
implementation project(':v1_13')

compileOnly 'org.spigotmc:spigot-api:1.20.1-R0.1-SNAPSHOT'

Expand All @@ -37,72 +35,10 @@ dependencies {
implementation 'org.bstats:bstats-bukkit:3.0.0'
}

bukkit {
name = pluginName
version = project.version
description = pluginDescription
website = pluginWebsite
author = pluginAuthor
main = 'com.rexcantor64.triton.spigot.plugin.SpigotPlugin'
apiVersion = pluginApiVersion

depend = ['ProtocolLib']
softDepend = ['PlaceholderAPI']

commands {
twin {
description = 'Upload and download your config to/from the Triton Web INterface.'
usage = '/twin [code]'
}
}

permissions {
'triton.getflag' {
setDefault('OP')
}
'triton.setlanguage' {
setDefault('TRUE')
}
'triton.setlanguage.others' {
setDefault('OP')
}
'triton.openselector' {
setDefault('TRUE')
}
'triton.reload' {
setDefault('OP')
}
'triton.help' {
setDefault('TRUE')
}
'triton.sign' {
setDefault('OP')
}
'triton.database' {
setDefault('OP')
}
'triton.info' {
setDefault('TRUE')
}
'triton.loglevel' {
setDefault('OP')
}
'twin.upload' {
setDefault('OP')
}
'twin.download' {
setDefault('OP')
}
}
}

shadowJar {
archiveFileName = 'triton-spigot.jarinjar'

relocate 'org.bstats', 'com.rexcantor64.triton.metrics'
//relocate 'org.slf4j', 'com.rexcantor64.shaded.slf4j'
relocate 'com.tananaev.jsonpatch', 'com.rexcantor64.shaded.jsonpatch'
relocate 'com.zaxxer.hikari', 'com.rexcantor64.shaded.hikari'

relocate 'net.kyori.adventure.text.minimessage', 'com.rexcantor64.triton.lib.adventure.minimessage'
relocate 'net.kyori.adventure.text.serializer.gson', 'com.rexcantor64.triton.lib.adventure.serializer.gson'
Expand Down
21 changes: 1 addition & 20 deletions loader/build.gradle → triton-spigot/loader/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
plugins {
id 'java'
id 'net.minecrell.plugin-yml.bukkit' version '0.5.2'
id 'com.github.johnrengelman.shadow' version '7.1.0'
}

String pluginDescription = project.property("pluginDescription")
Expand All @@ -14,11 +13,7 @@ apply plugin: 'net.minecrell.plugin-yml.bukkit'
group 'com.rexcantor64.triton'

dependencies {
implementation project(path: ":api")
implementation 'org.ow2.asm:asm-commons:9.2'
implementation 'org.ow2.asm:asm:9.2'
implementation 'me.lucko:jar-relocator:1.7'

compileOnly project(':core:triton-core-loader')
compileOnly 'org.spigotmc:spigot-api:1.20.1-R0.1-SNAPSHOT'
}

Expand Down Expand Up @@ -80,17 +75,3 @@ bukkit {
}
}
}

shadowJar {
getArchiveBaseName().set(rootProject.name)

from {
project(':triton-spigot').tasks.shadowJar.archiveFile
}
from {
project(':core').tasks.shadowJar.archiveFile
}

relocate 'org.objectweb.asm', 'com.rexcantor64.triton.lib.objectweb.asm'
relocate 'me.lucko.jarrelocator', 'com.rexcantor64.triton.lib.jarrelocator'
}
Loading

0 comments on commit d906f8d

Please sign in to comment.