-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimenting with an in memory launcher
- Loading branch information
1 parent
3972adb
commit 9203c54
Showing
8 changed files
with
124 additions
and
20 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
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
72 changes: 72 additions & 0 deletions
72
headlessmc-launcher/src/main/java/me/earth/headlessmc/launcher/launch/InMemoryLauncher.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,72 @@ | ||
package me.earth.headlessmc.launcher.launch; | ||
|
||
import lombok.CustomLog; | ||
import lombok.RequiredArgsConstructor; | ||
import me.earth.headlessmc.api.config.HasConfig; | ||
import me.earth.headlessmc.launcher.Main; | ||
import me.earth.headlessmc.launcher.auth.AuthException; | ||
import me.earth.headlessmc.launcher.files.FileManager; | ||
import me.earth.headlessmc.launcher.os.OS; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@CustomLog | ||
@RequiredArgsConstructor | ||
public class InMemoryLauncher { | ||
private final FileManager files; | ||
private final HasConfig config; | ||
private final OS os; | ||
private final LaunchOptions options; | ||
private final Command command; | ||
|
||
public void launch() throws IOException, LaunchException, AuthException { | ||
String mainClass = command.getActualMainClass(new ArrayList<>()); | ||
URL[] classpathUrls = new URL[command.getClasspath().size()]; | ||
for (int i = 0; i < command.getClasspath().size(); i++) { | ||
log.info(command.getClasspath().get(i)); | ||
classpathUrls[i] = Paths.get(command.getClasspath().get(i)).toUri().toURL(); | ||
} | ||
|
||
List<String> actualCommand = command.build(); | ||
List<String> gameArgs = new ArrayList<>(); | ||
boolean hasPassedMainClass = false; | ||
for (String arg : actualCommand) { | ||
if (arg.startsWith("-D")) { | ||
String[] split = arg.split("-D|="); | ||
if (split.length > 2) { | ||
log.info("SystemProperty: " + split[1] + " : " + split[2]); | ||
System.setProperty(split[1], split[2]); | ||
} | ||
} | ||
|
||
if (hasPassedMainClass) { | ||
gameArgs.add(arg); | ||
} | ||
|
||
if (arg.equals(mainClass)) { | ||
hasPassedMainClass = true; | ||
} | ||
} | ||
|
||
try (URLClassLoader urlClassLoader = new URLClassLoader(classpathUrls)) { | ||
try { | ||
Class<?> mainClassClass = Class.forName(mainClass, false, urlClassLoader); | ||
Method main = mainClassClass.getDeclaredMethod("main", String[].class); | ||
main.setAccessible(true); | ||
main.invoke(null, (Object) gameArgs.toArray(new String[0])); | ||
} catch (InvocationTargetException e) { | ||
e.printStackTrace(); | ||
} catch (Exception e) { | ||
throw new LaunchException("Failed to launch game", 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
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