Skip to content

Commit e296660

Browse files
committed
Allow using custom commands for launching the process.
Allows using Minecraft servers for example
1 parent e72e573 commit e296660

19 files changed

Lines changed: 444 additions & 118 deletions

src/agent/java/com/mcmoddev/relauncher/agent/ProcessConnectorServer.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,15 @@ public HashMap<String, Object> getProcessInfoProfiling() throws RemoteException
7474
} catch (UnknownHostException e) {
7575
map.put("host", "unknown");
7676
}
77-
final String jar = System.getProperty(Properties.JAR_PATH);
78-
map.put("appJar", jar);
79-
try {
80-
map.put("appClass", new JarFile(jar).getManifest().getMainAttributes().getValue("Main-Class"));
81-
} catch (Exception e) {
82-
map.put("appClass", "unknown");
77+
78+
final String jar = System.getProperty(Properties.JAR_PATH, null);
79+
if (jar != null) {
80+
map.put("appJar", jar);
81+
try {
82+
map.put("appClass", new JarFile(jar).getManifest().getMainAttributes().getValue("Main-Class"));
83+
} catch (Exception e) {
84+
map.put("appClass", "unknown");
85+
}
8386
}
8487

8588
map.put("jvmInputArguments", runtimeBean.getInputArguments() == null ? List.of() : new ArrayList<>(runtimeBean.getInputArguments()));
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.mcmoddev.relauncher.api;
2+
3+
import org.jetbrains.annotations.NotNull;
4+
import org.jetbrains.annotations.Nullable;
5+
6+
import java.io.InputStream;
7+
import java.nio.file.Path;
8+
import java.util.Objects;
9+
import java.util.Optional;
10+
11+
public interface BaseProcessManager {
12+
/**
13+
* Starts a process.
14+
*/
15+
void startProcess();
16+
17+
/**
18+
* Tries to start the process, after the launcher was started.
19+
*/
20+
void tryFirstStart();
21+
22+
/**
23+
* Clears the currently running process.
24+
*/
25+
void clearProcess();
26+
27+
/**
28+
* @return the currently running process, or else null
29+
*/
30+
@Nullable
31+
ProcessInfo getProcess();
32+
33+
/**
34+
* Kills the currently running process.
35+
* @param forcibly if the process should be forcibly destroyed
36+
*/
37+
default void destroy(boolean forcibly) {
38+
if (getProcess() != null) {
39+
if (forcibly) getProcess().process().destroyForcibly();
40+
else getProcess().process().destroy();
41+
}
42+
}
43+
44+
45+
/**
46+
* Gets the agent jar as an {@link InputStream InputStream}.
47+
*
48+
* @return the agent
49+
* @apiNote it is recommended the agent is stored as a resource
50+
*/
51+
@NotNull
52+
default InputStream getAgentResource() {
53+
var agent = getClass().getResourceAsStream("/relauncher-agent.jar");
54+
if (agent == null) {
55+
// If we can't find it as a .jar, try a .zip
56+
agent = getClass().getResourceAsStream("/relauncher-agent.zip");
57+
}
58+
return Objects.requireNonNull(agent);
59+
}
60+
61+
/**
62+
* Gets the path of the agent to be installed on the process.
63+
*
64+
* @return the path of the agent
65+
*/
66+
@NotNull
67+
Path getAgentPath();
68+
69+
/**
70+
* @return an optional which may contain the version of the current process
71+
*/
72+
Optional<String> getProcessVersion();
73+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.mcmoddev.relauncher.api;
2+
3+
import java.util.List;
4+
5+
/**
6+
* The interface used for managing custom user scripts.
7+
*/
8+
public interface CustomScriptManager extends BaseProcessManager {
9+
10+
/**
11+
* @return the script
12+
*/
13+
List<String> getScript();
14+
15+
}

src/api/java/com/mcmoddev/relauncher/api/JarUpdater.java

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,12 @@
2020
*/
2121
package com.mcmoddev.relauncher.api;
2222

23-
import org.jetbrains.annotations.NotNull;
24-
import org.jetbrains.annotations.Nullable;
25-
26-
import java.io.InputStream;
2723
import java.nio.file.Path;
28-
import java.util.Objects;
29-
import java.util.Optional;
3024

3125
/**
3226
* An updater which manages and updates a jar.
3327
*/
34-
public interface JarUpdater extends Runnable {
35-
36-
/**
37-
* Starts a process.
38-
*/
39-
void startProcess();
28+
public interface JarUpdater extends Runnable, BaseProcessManager {
4029

4130
/**
4231
* Kills the currently running process and updates it.
@@ -45,16 +34,6 @@ public interface JarUpdater extends Runnable {
4534
*/
4635
void killAndUpdate(final Release release) throws Exception;
4736

48-
/**
49-
* Tries to start the jar, after the launcher was started.
50-
*/
51-
void tryFirstStart();
52-
53-
/**
54-
* Clears the currently running process.
55-
*/
56-
void clearProcess();
57-
5837
/**
5938
* @return the update checker
6039
*/
@@ -65,46 +44,9 @@ public interface JarUpdater extends Runnable {
6544
*/
6645
Path getJarPath();
6746

68-
/**
69-
* @return an optional which may contain the version of the current jar
70-
*/
71-
default Optional<String> getJarVersion() {
72-
return Optional.empty();
73-
}
74-
75-
/**
76-
* @return the currently running process, or else null
77-
*/
78-
@Nullable
79-
ProcessInfo getProcess();
80-
8147
/**
8248
* Runs this updater.
8349
*/
8450
@Override
8551
void run();
86-
87-
/**
88-
* Gets the agent jar as an {@link InputStream InputStream}.
89-
*
90-
* @return the agent
91-
* @apiNote it is recommended the agent is stored as a resource
92-
*/
93-
@NotNull
94-
default InputStream getAgentResource() {
95-
var agent = getClass().getResourceAsStream("/relauncher-agent.jar");
96-
if (agent == null) {
97-
// If we can't find it as a .jar, try a .zip
98-
agent = getClass().getResourceAsStream("/relauncher-agent.zip");
99-
}
100-
return Objects.requireNonNull(agent);
101-
}
102-
103-
/**
104-
* Gets the path of the agent to be installed on the process.
105-
*
106-
* @return the path of the agent
107-
*/
108-
@NotNull
109-
Path getAgentPath();
11052
}

src/api/java/com/mcmoddev/relauncher/api/LauncherConfig.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,26 @@ default boolean allowSelfUpdate() {
5858
return true;
5959
}
6060

61+
/**
62+
* @return gets the mode the launcher will run in
63+
*/
64+
default LauncherMode getLauncherMode() {
65+
return LauncherMode.JAR;
66+
}
67+
6168
record CheckingRate(long amount, TimeUnit unit) {}
69+
70+
enum LauncherMode {
71+
/**
72+
* The jar mode means that the launcher will manage a one-jar process. <br>
73+
* This option supports updating.
74+
*/
75+
JAR,
76+
/**
77+
* The custom script mode means that the launcher will manage a custom java script. <br.
78+
* Useful for Minecraft servers. <br>
79+
* This option doesn't currently support updating.
80+
*/
81+
CUSTOM_SCRIPT
82+
}
6283
}

src/api/java/com/mcmoddev/relauncher/api/LauncherFactory.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,7 @@
2424
import org.jetbrains.annotations.Nullable;
2525

2626
import java.io.IOException;
27-
import java.net.URI;
28-
import java.net.http.HttpClient;
29-
import java.net.http.HttpRequest;
30-
import java.net.http.HttpResponse;
3127
import java.nio.file.Path;
32-
import java.util.concurrent.ForkJoinPool;
3328

3429
/**
3530
* A factory for ReLauncher. <br>
@@ -69,16 +64,20 @@ default Path getConfigPath(Path launcherDirectory) {
6964
@NotNull
7065
JarUpdater createUpdater(T config);
7166

67+
default CustomScriptManager createScriptManager(T config) {
68+
throw new UnsupportedOperationException();
69+
}
70+
7271
/**
7372
* Creates a {@link DiscordIntegration}. <br>
7473
* The launcher only calls this method if Discord Integration is enabled in the config.
7574
*
7675
* @param config the launcher config
77-
* @param updater the {@link JarUpdater jar updater}
76+
* @param updater the {@link BaseProcessManager process manager}
7877
* @return the integration instance. Can be {@code null}.
7978
*/
8079
@Nullable
81-
DiscordIntegration createDiscordIntegration(T config, JarUpdater updater);
80+
DiscordIntegration createDiscordIntegration(T config, BaseProcessManager manager);
8281

8382
/**
8483
* @return the GitHub repository of the launcher

src/launcher/java/com/mcmoddev/relauncher/Config.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,20 @@ public class Config implements LauncherConfig {
4141
public static final Config DEFAULT = new Config();
4242

4343
@Required
44+
@Setting("mode")
45+
@Comment("""
46+
The mode this launcher runs in:
47+
- JAR => manages a jar, supports updating
48+
- CUSTOM_SCRIPT => manages a custom java launch script, does NOT support updating""")
49+
public LauncherMode mode = LauncherMode.JAR;
50+
4451
@Setting("jar_path")
45-
@Comment("The path of the jar to launch.")
52+
@Comment("Only if the mode is JAR, the path of the jar to launch.")
4653
public String jarPath = "file.jar";
4754

4855
@Required
4956
@Setting("jvm_args")
50-
@Comment("The arguments to start the jar with.")
57+
@Comment("The arguments to start the process with.")
5158
public List<String> jvmArgs = new ArrayList<>();
5259

5360
@Required
@@ -60,6 +67,12 @@ public class Config implements LauncherConfig {
6067
@Comment("Information about the github repo for update checking.")
6168
public GitHub gitHub = new GitHub();
6269

70+
@Setting("custom_script")
71+
@Comment("""
72+
Only if the mode is CUSTOM_SCRIPT, the arguments of the script to run.
73+
Example for running a Minecraft server: ["@user_jvm_args.txt", "@libraries/net/minecraftforge/forge/1.19-41.0.17/unix_args.txt"]""")
74+
public List<String> customScript = List.of();
75+
6376
@ConfigSerializable
6477
public static final class GitHub {
6578
@Required

src/launcher/java/com/mcmoddev/relauncher/DefaultJarUpdater.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public Path getJarPath() {
216216
}
217217

218218
@Override
219-
public Optional<String> getJarVersion() {
219+
public Optional<String> getProcessVersion() {
220220
if (!Files.exists(jarPath)) {
221221
if (process == null) {
222222
return Optional.empty();

src/launcher/java/com/mcmoddev/relauncher/DefaultLauncherFactory.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
*/
2121
package com.mcmoddev.relauncher;
2222

23+
import com.mcmoddev.relauncher.api.BaseProcessManager;
24+
import com.mcmoddev.relauncher.api.CustomScriptManager;
2325
import com.mcmoddev.relauncher.api.DiscordIntegration;
2426
import com.mcmoddev.relauncher.api.JarUpdater;
2527
import com.mcmoddev.relauncher.api.LauncherFactory;
@@ -61,7 +63,15 @@ public class DefaultLauncherFactory implements LauncherFactory<Config> {
6163
}
6264

6365
@Override
64-
public @Nullable DiscordIntegration createDiscordIntegration(final Config config, final JarUpdater updater) {
66+
public CustomScriptManager createScriptManager(final Config config) {
67+
return new DefaultScriptManager(
68+
config.customScript, config.jvmArgs,
69+
config.discord.loggingWebhook
70+
);
71+
}
72+
73+
@Override
74+
public @Nullable DiscordIntegration createDiscordIntegration(final Config config, final BaseProcessManager updater) {
6575
return new DefaultDiscordIntegration(Path.of(""), config.discord, () -> updater);
6676
}
6777

0 commit comments

Comments
 (0)