1- use std:: collections:: HashMap ;
1+ use std:: { collections:: HashMap , fs , path :: Path } ;
22
3- use crate :: cache;
4- use anyhow:: { Context , Result , bail} ;
5- use netstat2:: { AddressFamilyFlags , ProtocolFlags , ProtocolSocketInfo , iterate_sockets_info} ;
6- use sysinfo:: System ;
3+ use anyhow:: { Result , bail} ;
74
85fn command_url ( port : u16 , command : Option < String > ) -> String {
96 format ! (
@@ -12,97 +9,36 @@ fn command_url(port: u16, command: Option<String>) -> String {
129 )
1310}
1411
15- pub fn find_port ( ) -> Option < u16 > {
16- const CACHE : & str = "editor-port" ;
12+ #[ must_use]
13+ pub fn find_port ( game_root : & Path ) -> Option < u16 > {
14+ let editor_port = game_root. join ( ".internal" ) . join ( "editor.port" ) ;
1715
18- if let Some ( port) = cache:: get ( CACHE )
19- && let Some ( port) = port. parse :: < u16 > ( ) . ok ( )
20- && is_editor_port ( port)
21- {
22- return Some ( port) ;
16+ // editor port file doesnt exist? Editor isn't open
17+ if !editor_port. exists ( ) {
18+ return None ;
2319 }
2420
25- tracing :: debug! ( "Looking for editor port..." ) ;
21+ let content = fs :: read_to_string ( editor_port ) . ok ( ) ? ;
2622
27- let sys = System :: new_all ( ) ;
23+ let port : u16 = content . parse ( ) . ok ( ) ? ;
2824
29- for ( pid, proc) in sys. processes ( ) {
30- if !proc
31- . name ( )
32- . to_ascii_lowercase ( )
33- . to_str ( )
34- . unwrap_or_default ( )
35- . contains ( "java" )
36- {
37- continue ;
38- }
39-
40- let ports = ports_for_pid ( pid. as_u32 ( ) ) ;
41-
42- if ports. is_err ( ) {
43- continue ;
44- }
45-
46- let ports = ports. unwrap ( ) ;
47- if ports. is_empty ( ) {
48- continue ;
49- }
50-
51- tracing:: debug!( "Pid {} has ports: {:?}" , pid. as_u32( ) , ports) ;
52-
53- for port in ports {
54- if is_editor_port ( port) {
55- tracing:: debug!( "Found editor at {port}" ) ;
56-
57- if let Err ( err) = cache:: set ( CACHE , & port. to_string ( ) ) {
58- tracing:: error!( "Could not cache editor key because: {err:?}" ) ;
59- }
60-
61- return Some ( port) ;
62- }
63- }
64- }
65-
66- None
67- }
68-
69- fn ports_for_pid ( pid : u32 ) -> Result < Vec < u16 > > {
70- let mut ports = Vec :: new ( ) ;
71-
72- let af_flags = AddressFamilyFlags :: IPV4 | AddressFamilyFlags :: IPV6 ;
73- let proto_flags = ProtocolFlags :: TCP | ProtocolFlags :: UDP ;
74-
75- for socket in iterate_sockets_info ( af_flags, proto_flags) ? {
76- let socket = socket?;
77-
78- if socket. associated_pids . contains ( & pid) {
79- match & socket. protocol_socket_info {
80- ProtocolSocketInfo :: Tcp ( tcp) => {
81- ports. push ( tcp. local_port ) ;
82- }
83- ProtocolSocketInfo :: Udp ( udp) => {
84- ports. push ( udp. local_port ) ;
85- }
86- }
87- }
25+ if is_editor_port ( port) {
26+ Some ( port)
27+ } else {
28+ None
8829 }
89-
90- Ok ( ports)
9130}
9231
32+ #[ must_use]
9333pub fn is_editor_port ( port : u16 ) -> bool {
9434 reqwest:: blocking:: Client :: new ( )
9535 . head ( command_url ( port, None ) )
9636 . send ( )
9737 . is_ok_and ( |r| r. status ( ) . is_success ( ) )
9838}
9939
100- pub fn list_commands ( port : Option < u16 > ) -> Result < HashMap < String , String > > {
101- let url = command_url (
102- port. or_else ( find_port)
103- . context ( "could not determine editor port" ) ?,
104- None ,
105- ) ;
40+ pub fn list_commands ( port : u16 ) -> Result < HashMap < String , String > > {
41+ let url = command_url ( port, None ) ;
10642
10743 let res = reqwest:: blocking:: get ( url) ?;
10844
@@ -115,12 +51,8 @@ pub fn list_commands(port: Option<u16>) -> Result<HashMap<String, String>> {
11551 serde_json:: from_str ( & content) . map_err ( anyhow:: Error :: from)
11652}
11753
118- pub fn send_command ( port : Option < u16 > , cmd : & str ) -> Result < ( ) > {
119- let url = command_url (
120- port. or_else ( find_port)
121- . context ( "could not determine editor port" ) ?,
122- Some ( cmd. to_string ( ) ) ,
123- ) ;
54+ pub fn send_command ( port : u16 , cmd : & str ) -> Result < ( ) > {
55+ let url = command_url ( port, Some ( cmd. to_string ( ) ) ) ;
12456
12557 let res = reqwest:: blocking:: Client :: new ( ) . post ( url) . send ( ) ?;
12658
0 commit comments