-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad01a06
commit 0a4e1ae
Showing
8 changed files
with
264 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/net/pistonmaster/serverwrecker/ServerWreckerLauncher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* ServerWrecker | ||
* | ||
* Copyright (C) 2023 ServerWrecker | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
*/ | ||
package net.pistonmaster.serverwrecker; | ||
|
||
import net.pistonmaster.serverwrecker.util.SWContextClassLoader; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* This class only changes the classLoader for the rest of the program. | ||
* This is so we can merge plugin and server classes. | ||
*/ | ||
public class ServerWreckerLauncher { | ||
private static final SWContextClassLoader SW_CONTEXT_CLASS_LOADER = new SWContextClassLoader(); | ||
|
||
public static void main(String[] args) { | ||
Thread.currentThread().setContextClassLoader(SW_CONTEXT_CLASS_LOADER); | ||
|
||
try { | ||
SW_CONTEXT_CLASS_LOADER.loadClass("net.pistonmaster.serverwrecker.ServerWreckerBootstrap") | ||
.getDeclaredMethod("bootstrap", String[].class, List.class) | ||
.invoke(null, args, SW_CONTEXT_CLASS_LOADER.getCheckedClassLoaders()); | ||
} catch (ReflectiveOperationException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/main/java/net/pistonmaster/serverwrecker/util/CustomClassProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* ServerWrecker | ||
* | ||
* Copyright (C) 2023 ServerWrecker | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
*/ | ||
package net.pistonmaster.serverwrecker.util; | ||
|
||
import com.google.common.reflect.ClassPath; | ||
import net.lenni0451.classtransform.utils.tree.IClassProvider; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.InputStream; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.function.Supplier; | ||
|
||
import static net.lenni0451.classtransform.utils.ASMUtils.slash; | ||
import static net.lenni0451.classtransform.utils.Sneaky.sneakySupply; | ||
|
||
public class CustomClassProvider implements IClassProvider { | ||
private final ClassPath[] classPaths; | ||
private final ClassLoader[] classLoaders; | ||
|
||
public CustomClassProvider(List<ClassLoader> classLoaders) { | ||
this.classLoaders = classLoaders.toArray(new ClassLoader[0]); | ||
|
||
try { | ||
this.classPaths = new ClassPath[classLoaders.size()]; | ||
for (int i = 0; i < classLoaders.size(); i++) { | ||
this.classPaths[i] = ClassPath.from(classLoaders.get(i)); | ||
} | ||
} catch (Throwable t) { | ||
throw new RuntimeException("Failed to initialize ClassPath", t); | ||
} | ||
} | ||
|
||
@Override | ||
public byte @NotNull [] getClass(@NotNull String name) throws ClassNotFoundException { | ||
for (ClassLoader classLoader : this.classLoaders) { | ||
byte[] bytes = this.getClassFromLoader(classLoader, name); | ||
if (bytes != null) return bytes; | ||
} | ||
|
||
throw new ClassNotFoundException("Class not found: " + name); | ||
} | ||
|
||
private byte[] getClassFromLoader(ClassLoader classLoader, String name) { | ||
try (InputStream is = classLoader.getResourceAsStream(slash(name) + ".class")) { | ||
Objects.requireNonNull(is, "Class input stream is null"); | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
byte[] buf = new byte[1024]; | ||
int len; | ||
while ((len = is.read(buf)) > 0) baos.write(buf, 0, len); | ||
return baos.toByteArray(); | ||
} catch (Throwable t) { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
@Nonnull | ||
public Map<String, Supplier<byte[]>> getAllClasses() { | ||
Map<String, Supplier<byte[]>> map = new HashMap<>(); | ||
for (ClassPath classPath : this.classPaths) { | ||
for (ClassPath.ClassInfo classInfo : classPath.getAllClasses()) { | ||
map.put(classInfo.getName(), sneakySupply(() -> this.getClass(classInfo.getName()))); | ||
} | ||
} | ||
return map; | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
src/main/java/net/pistonmaster/serverwrecker/util/SWContextClassLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* ServerWrecker | ||
* | ||
* Copyright (C) 2023 ServerWrecker | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation, either version 3 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public | ||
* License along with this program. If not, see | ||
* <http://www.gnu.org/licenses/gpl-3.0.html>. | ||
*/ | ||
package net.pistonmaster.serverwrecker.util; | ||
|
||
import lombok.Getter; | ||
import net.lenni0451.reflect.Methods; | ||
import net.lenni0451.reflect.exceptions.MethodInvocationException; | ||
import net.pistonmaster.serverwrecker.ServerWreckerLauncher; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.lang.reflect.Method; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
public class SWContextClassLoader extends ClassLoader { | ||
@Getter | ||
private final List<ClassLoader> checkedClassLoaders = new ArrayList<>(); | ||
private final Method findLoadedClassMethod = Objects.requireNonNull(Methods.getDeclaredMethod(ClassLoader.class, "loadClass", String.class, boolean.class)); | ||
private final ClassLoader platformClassLoader = ClassLoader.getSystemClassLoader().getParent(); | ||
|
||
public SWContextClassLoader() { | ||
this(ClassLoader.getSystemClassLoader()); | ||
} | ||
|
||
private SWContextClassLoader(ClassLoader parent) { | ||
super(parent); | ||
this.checkedClassLoaders.add(parent); | ||
} | ||
|
||
@Override | ||
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { | ||
synchronized (getClassLoadingLock(name)) { | ||
// First, check if the class has already been loaded | ||
Class<?> c = findLoadedClass(name); | ||
if (c == null) { | ||
try { | ||
return Methods.invoke(platformClassLoader, findLoadedClassMethod, name, resolve); | ||
} catch (MethodInvocationException ignored) { | ||
} | ||
|
||
byte[] classData = loadClassData(name); | ||
if (classData == null) { | ||
throw new ClassNotFoundException(); | ||
} | ||
|
||
if (name.equals(ServerWreckerLauncher.class.getName())) { | ||
return super.loadClass(name, resolve); | ||
} | ||
|
||
c = defineClass(name, classData, 0, classData.length); | ||
} | ||
|
||
if (resolve) { | ||
resolveClass(c); | ||
} | ||
|
||
return c; | ||
} | ||
} | ||
|
||
private byte[] loadClassData(String className) { | ||
var classPath = className.replace('.', '/') + ".class"; | ||
|
||
for (var classLoader : checkedClassLoaders) { | ||
try (InputStream inputStream = classLoader.getResourceAsStream(classPath)) { | ||
if (inputStream == null) { | ||
continue; | ||
} | ||
|
||
return inputStream.readAllBytes(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |