33import java .io .IOException ;
44import java .io .InputStreamReader ;
55import java .io .Reader ;
6+ import java .net .URISyntaxException ;
7+ import java .nio .charset .StandardCharsets ;
68import java .nio .file .FileSystems ;
79import java .nio .file .Files ;
810import 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 );
0 commit comments