Skip to content

Commit b671bc5

Browse files
committed
add option --start-server-load-latest when starting Factorio server
1 parent d2b084f commit b671bc5

File tree

2 files changed

+35
-18
lines changed

2 files changed

+35
-18
lines changed

src/factorio_server.go

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ func initFactorio() (f *FactorioServer, err error) {
9595

9696
func (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
}

src/handlers.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ func ListSaves(w http.ResponseWriter, r *http.Request) {
329329

330330
w.Header().Set("Content-Type", "application/json;charset=UTF-8")
331331

332-
resp.Data, err = listSaves(config.FactorioSavesDir)
332+
savesList, err := listSaves(config.FactorioSavesDir)
333333
if err != nil {
334334
resp.Success = false
335335
resp.Data = fmt.Sprintf("Error listing save files: %s", err)
@@ -339,6 +339,11 @@ func ListSaves(w http.ResponseWriter, r *http.Request) {
339339
return
340340
}
341341

342+
loadLatest := Save{Name: "Load Latest"}
343+
savesList = append(savesList, loadLatest)
344+
345+
resp.Data = savesList
346+
342347
resp.Success = true
343348

344349
if err := json.NewEncoder(w).Encode(resp); err != nil {
@@ -645,7 +650,6 @@ func StopServer(w http.ResponseWriter, r *http.Request) {
645650
}
646651
return
647652
}
648-
err = FactorioServ.Rcon.Close()
649653
if err != nil {
650654
log.Printf("Error closing rcon connection: %s", err)
651655
resp.Data = fmt.Sprintf("Error in stop server handler: %s", err)

0 commit comments

Comments
 (0)