Skip to content

Commit c14f2b7

Browse files
committed
Fix server installations on windows systems
- retrieve process arguments using powershell on windows - write the relative path to the flap jar to the launch args file
1 parent d6cb0bb commit c14f2b7

3 files changed

Lines changed: 50 additions & 15 deletions

File tree

java/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ repositories {
2323
}
2424

2525
dependencies {
26-
compileOnly("net.ornithemc:flap:0.0.1")
26+
compileOnly("net.ornithemc:flap:0.0.2")
2727
compileOnly("org.apache.logging.log4j:log4j-core:2.19.0")
2828
}
2929

java/src/main/java/net/ornithemc/server_launcher/ServerLauncher.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import java.io.IOException;
44
import java.io.InputStreamReader;
55
import java.io.Reader;
6+
import java.net.URISyntaxException;
7+
import java.nio.charset.StandardCharsets;
68
import java.nio.file.FileSystems;
79
import java.nio.file.Files;
810
import java.nio.file.Path;
@@ -35,18 +37,41 @@ private static LoggerContext getLoggerCtx() {
3537
public static void main(String[] args) {
3638
try (var loggerCtx = getLoggerCtx()) {
3739
var log = loggerCtx.getLogger("ServerLauncher");
38-
var processInfo = ProcessHandle.current().info();
40+
var handle = ProcessHandle.current();
41+
var processInfo = handle.info();
3942
List<String> cmd = new ArrayList<>();
4043
cmd.add(processInfo.command().orElseThrow());
4144
var in = ServerLauncher.class.getResourceAsStream("/ornithe-args.json");
4245
var gson = new GsonBuilder().create();
4346
var arguments = new ArrayList<String>();
44-
processInfo.arguments().ifPresent(a -> Collections.addAll(arguments, a));
47+
if (System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("win")) {
48+
try {
49+
var p = new ProcessBuilder("powershell.exe", "-NoLogo", "-NoProfile", "-NonInteractive", "-Command",
50+
"(Get-CimInstance Win32_Process -Filter 'ProcessId = \"" + handle.pid() + "\"').CommandLine")
51+
.start();
52+
try (var reader = p.inputReader(StandardCharsets.UTF_8)) {
53+
var cmdLine = reader.readLine();
54+
Collections.addAll(arguments, cmdLine.substring(cmdLine.indexOf(' ')).split(" "));
55+
arguments.removeIf(String::isBlank);
56+
}
57+
} catch (IOException e) {
58+
log.error("Failed to gather windows process arguments!", e);
59+
return;
60+
}
61+
} else {
62+
var processArgs = processInfo.arguments();
63+
if (processArgs.isPresent()) {
64+
Collections.addAll(arguments, processArgs.get());
65+
} else {
66+
log.error("Failed to find process arguments, does the jvm just give up on this platform as well?");
67+
return;
68+
}
69+
}
4570
arguments.removeAll(Arrays.asList(args));
4671
var cp = new ArrayList<String>();
4772
if (in != null) {
48-
var self = ServerLauncher.class.getProtectionDomain().getCodeSource().getLocation().getPath();
4973
try (in; var reader = new InputStreamReader(in)) {
74+
var self = Path.of(ServerLauncher.class.getProtectionDomain().getCodeSource().getLocation().toURI()).toString();
5075
var ornitheArgs = gson.fromJson(reader, OrnitheArgs.class);
5176
if (arguments.contains("-jar")) {
5277
int jarIndex = arguments.indexOf("-jar");
@@ -77,6 +102,9 @@ public static void main(String[] args) {
77102
} catch (IOException e) {
78103
log.error("Failed to read ornithe launch arguments:", e);
79104
return;
105+
} catch (URISyntaxException e) {
106+
log.error("Failed to retrieve own location: ", e);
107+
return;
80108
}
81109
}
82110
try {
@@ -85,6 +113,7 @@ public static void main(String[] args) {
85113
cmd.add("-Dloader.gameJarPath=" + serverJar);
86114
} catch (IOException e) {
87115
log.error("Failed to transform server jar:", e);
116+
return;
88117
}
89118
cmd.addAll(arguments);
90119
Collections.addAll(cmd, args);

src/actions/server.rs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ async fn install_path(
7575
if !location.exists() {
7676
std::fs::create_dir_all(&location)?;
7777
}
78-
let location = location.canonicalize()?;
7978

8079
let _ = sender.send((
8180
0.1,
@@ -88,6 +87,7 @@ async fn install_path(
8887
)
8988
.into(),
9089
));
90+
let location = location.canonicalize()?;
9191

9292
let clear_paths = [location.join(".fabric"), location.join(".quilt")];
9393
for path in clear_paths {
@@ -308,7 +308,6 @@ async fn create_launch_jar(
308308
wrap_manifest_line(&format!("Main-Class: {}", launch_main_class))
309309
)?;
310310
}
311-
zip.start_file("META-INF/MANIFEST.MF", SimpleFileOptions::default())?;
312311

313312
let mut class_path = String::from("Class-Path: ");
314313
for library in library_files {
@@ -318,23 +317,26 @@ async fn create_launch_jar(
318317
}
319318
}
320319

320+
if let Some(flap_path) = flap_jar_path {
321+
if let Some(path) = flap_path.strip_prefix(install_location)?.to_str() {
322+
zip.start_file("ornithe-args.json", SimpleFileOptions::default())?;
323+
zip.write_all(&serde_json::to_vec(&json!({
324+
"flap_jar": path.replace("\\", "/"),
325+
"main_class": launch_main_class,
326+
"jvm_args": jvm_args
327+
}))?)?;
328+
}
329+
}
330+
321331
writeln!(manifest, "{}\r", wrap_manifest_line(class_path.trim_end()))?;
322332
writeln!(
323333
manifest,
324334
"{}\r",
325335
wrap_manifest_line(&format!("Minecraft-Version: {}\r", version.id))
326336
)?;
337+
zip.start_file("META-INF/MANIFEST.MF", SimpleFileOptions::default())?;
327338
zip.write_all(&manifest)?;
328339

329-
if let Some(flap_path) = flap_jar_path {
330-
zip.start_file("ornithe-args.json", SimpleFileOptions::default())?;
331-
zip.write_all(&serde_json::to_vec(&json!({
332-
"flap_jar": flap_path,
333-
"main_class": launch_main_class,
334-
"jvm_args": jvm_args
335-
}))?)?;
336-
}
337-
338340
if loader_type == &LoaderType::Fabric {
339341
zip.start_file(
340342
"fabric-server-launch.properties",
@@ -358,6 +360,10 @@ fn wrap_manifest_line(line: &str) -> String {
358360
if count == 72 {
359361
res += "\r\n ";
360362
count = 1;
363+
if char == ' ' {
364+
res += " ";
365+
count += 1;
366+
}
361367
}
362368
}
363369
res

0 commit comments

Comments
 (0)