From 759aae40e51709570ead3b2e9b16bf923ff310c6 Mon Sep 17 00:00:00 2001 From: aperkovic Date: Fri, 26 Jul 2019 17:05:42 +0200 Subject: [PATCH] Optimised GLIBC fix, added autostart feature --- README.md | 5 ++++ src/factorio_server.go | 53 ++++++++++++++++++++++++++++-------------- src/main.go | 5 +++- 3 files changed, 44 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 07a1f35..985c2d6 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,11 @@ Usage of ./factorio-server-manager: Path to the glibc ld.so file (default "/opt/glibc-2.18/lib/ld-2.18.so") -glibc-lib-loc Path to the glibc lib folder (default "/opt/glibc-2.18/lib") + -autostart + Autostarts Factorio Server when FSM is starting. Default false [true/false] + (If no IP and/or port provided at startup, it will bind the factorio server to all interfaces + and set the server port to the default 34197, always loads latest save) + Example: ./factorio-server-manager --dir /home/user/.factorio --host 10.0.0.1 diff --git a/src/factorio_server.go b/src/factorio_server.go index ab63a44..63cb62c 100644 --- a/src/factorio_server.go +++ b/src/factorio_server.go @@ -42,6 +42,26 @@ func randomPort() int { // Returns random port to use for rcon connection return rand.Intn(45000-40000) + 40000 } +func autostart() { + + var err error + if FactorioServ.BindIP == "" { + FactorioServ.BindIP = "0.0.0.0" + + } + if FactorioServ.Port == 0 { + FactorioServ.Port = 34197 + } + FactorioServ.Savefile = "Load Latest" + + err = FactorioServ.Run() + + if err != nil { + log.Printf("Error starting Factorio server: %+v", err) + return + } + +} func initFactorio() (f *FactorioServer, err error) { f = new(FactorioServer) @@ -133,6 +153,10 @@ func initFactorio() (f *FactorioServer, err error) { f.BaseModVersion = modInfo.Version + if config.autostart == "true" { + go autostart() + } + return } @@ -147,28 +171,21 @@ func (f *FactorioServer) Run() error { } args := []string{} + + //The factorio server refenences its executable-path, since we execute the ld.so file and pass the factorio binary as a parameter + //the game would use the path to the ld.so file as it's executable path and crash, to prevent this the parameter "--executable-path" is added if config.glibcCustom == "true" { log.Println("Custom glibc selected, glibc.so location:", config.glibcLocation, " lib location:", config.glibcLibLoc) - args = []string{ - "--library-path", config.glibcLibLoc, - config.FactorioBinary, - "--bind", (f.BindIP), - "--port", strconv.Itoa(f.Port), - "--server-settings", filepath.Join(config.FactorioConfigDir, "server-settings.json"), - "--rcon-port", strconv.Itoa(config.FactorioRconPort), - "--rcon-password", config.FactorioRconPass, - "--executable-path", config.FactorioBinary, - } - } else { - args = []string{ - "--bind", (f.BindIP), - "--port", strconv.Itoa(f.Port), - "--server-settings", filepath.Join(config.FactorioConfigDir, "server-settings.json"), - "--rcon-port", strconv.Itoa(config.FactorioRconPort), - "--rcon-password", config.FactorioRconPass, - } + args = append(args, "--library-path", config.glibcLibLoc, config.FactorioBinary, "--executable-path", config.FactorioBinary) } + args = append(args, + "--bind", (f.BindIP), + "--port", strconv.Itoa(f.Port), + "--server-settings", filepath.Join(config.FactorioConfigDir, "server-settings.json"), + "--rcon-port", strconv.Itoa(config.FactorioRconPort), + "--rcon-password", config.FactorioRconPass) + if f.Savefile == "Load Latest" { args = append(args, "--start-server-load-latest") } else { diff --git a/src/main.go b/src/main.go index 9fe73b8..a141e84 100644 --- a/src/main.go +++ b/src/main.go @@ -37,6 +37,7 @@ type Config struct { glibcCustom string glibcLocation string glibcLibLoc string + autostart string } var ( @@ -78,8 +79,10 @@ func parseFlags() { glibcCustom := flag.String("glibc-custom", "false", "By default false, if custom glibc is required set this to true and add glibc-loc and glibc-lib-loc parameters") glibcLocation := flag.String("glibc-loc", "/opt/glibc-2.18/lib/ld-2.18.so", "Location glibc ld.so file if needed (ex. /opt/glibc-2.18/lib/ld-2.18.so)") glibcLibLoc := flag.String("glibc-lib-loc", "/opt/glibc-2.18/lib", "Location of glibc lib folder (ex. /opt/glibc-2.18/lib)") + autostart := flag.String("autostart", "false", "Autostart factorio server on bootup of FSM, default false [true/false]") flag.Parse() + config.autostart = *autostart config.glibcCustom = *glibcCustom config.glibcLocation = *glibcLocation config.glibcLibLoc = *glibcLibLoc @@ -129,7 +132,7 @@ func main() { // Initialize HTTP router router := NewRouter() - log.Printf("Starting server on: %s:%s", config.ServerIP, config.ServerPort) log.Fatal(http.ListenAndServe(config.ServerIP+":"+config.ServerPort, router)) + }