@@ -95,6 +95,7 @@ func initFactorio() (f *FactorioServer, err error) {
9595
9696func (f * FactorioServer ) Run () error {
9797 var err error
98+ var savefileArg string
9899
99100 data , err := json .MarshalIndent (f .Settings , "" , " " )
100101 if err != nil {
@@ -104,13 +105,18 @@ func (f *FactorioServer) Run() error {
104105 }
105106
106107 args := []string {
107- "--start-server" , filepath .Join (config .FactorioSavesDir , f .Savefile ),
108108 "--port" , strconv .Itoa (f .Port ),
109109 "--server-settings" , filepath .Join (config .FactorioConfigDir , "server-settings.json" ),
110110 "--rcon-port" , strconv .Itoa (config .FactorioRconPort ),
111111 "--rcon-pass" , config .FactorioRconPass ,
112112 }
113113
114+ if f .Savefile == "Load Latest" {
115+ args = append (args , "--start-server-load-latest" )
116+ } else {
117+ args = append (args , "--start-server" , filepath .Join (config .FactorioSavesDir , f .Savefile ))
118+ }
119+
114120 log .Println ("Starting server with command: " , config .FactorioBinary , args )
115121
116122 f .Cmd = exec .Command (config .FactorioBinary , args ... )
@@ -159,22 +165,24 @@ func (f *FactorioServer) parseRunningCommand(std io.ReadCloser) (err error) {
159165 log .Printf ("Factorio Server: %s" , stdScanner .Text ())
160166 line := strings .Fields (stdScanner .Text ())
161167 // Check if Factorio Server reports any errors if so handle it
162- if line [1 ] == "Error" {
163- err := f .checkLogError (line )
164- if err != nil {
165- log .Printf ("Error checking Factorio Server Error: %s" , err )
166- }
167- }
168- // If rcon port is opened connect to rcon
169- rconLog := "Starting RCON interface at port " + strconv .Itoa (config .FactorioRconPort )
170- // check if slice index is greater than 2 to prevent panic
171- if len (line ) > 2 {
172- // log line for opened rcon connection
173- if strings .Join (line [3 :], " " ) == rconLog {
174- log .Printf ("Rcon running on Factorio Server" )
175- err = connectRC ()
168+ if len (line ) > 0 {
169+ if line [1 ] == "Error" {
170+ err := f .checkLogError (line )
176171 if err != nil {
177- log .Printf ("Error: %s" , err )
172+ log .Printf ("Error checking Factorio Server Error: %s" , err )
173+ }
174+ }
175+ // If rcon port is opened connect to rcon
176+ rconLog := "Starting RCON interface at port " + strconv .Itoa (config .FactorioRconPort )
177+ // check if slice index is greater than 2 to prevent panic
178+ if len (line ) > 2 {
179+ // log line for opened rcon connection
180+ if strings .Join (line [3 :], " " ) == rconLog {
181+ log .Printf ("Rcon running on Factorio Server" )
182+ err = connectRC ()
183+ if err != nil {
184+ log .Printf ("Error: %s" , err )
185+ }
178186 }
179187 }
180188 }
@@ -224,5 +232,10 @@ func (f *FactorioServer) Stop() error {
224232 f .Running = false
225233 log .Printf ("Sent SIGINT to Factorio process. Factorio shutting down..." )
226234
235+ err = f .Rcon .Close ()
236+ if err != nil {
237+ log .Printf ("Error close rcon connection: %s" , err )
238+ }
239+
227240 return nil
228241}
0 commit comments