From 0bad11aab8bf3953433f01b8f01221e4047f5dc0 Mon Sep 17 00:00:00 2001 From: Forest Ames Date: Sun, 17 Jan 2021 00:53:27 -0700 Subject: [PATCH] Handle absolute paths in settings/binary, configurable BaseMod dir --- src/api/handlers.go | 7 ++++--- src/bootstrap/config.go | 20 ++++++++++++++++++-- src/factorio/server.go | 13 +++++++------ src/main.go | 9 +++++---- 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/api/handlers.go b/src/api/handlers.go index d0c6786..d369d3a 100644 --- a/src/api/handlers.go +++ b/src/api/handlers.go @@ -4,8 +4,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/mroote/factorio-server-manager/bootstrap" - "github.com/mroote/factorio-server-manager/factorio" "io" "io/ioutil" "log" @@ -16,6 +14,9 @@ import ( "sync" "time" + "github.com/mroote/factorio-server-manager/bootstrap" + "github.com/mroote/factorio-server-manager/factorio" + "github.com/gorilla/mux" ) @@ -628,7 +629,7 @@ func UpdateServerSettings(w http.ResponseWriter, r *http.Request) { return } config := bootstrap.GetConfig() - err = ioutil.WriteFile(filepath.Join(config.FactorioConfigDir, config.SettingsFile), settings, 0644) + err = ioutil.WriteFile(config.SettingsFile, settings, 0644) if err != nil { resp = fmt.Sprintf("Failed to save server settings: %v\n", err) log.Println(resp) diff --git a/src/bootstrap/config.go b/src/bootstrap/config.go index 0dd84e8..fc1b2f0 100644 --- a/src/bootstrap/config.go +++ b/src/bootstrap/config.go @@ -3,13 +3,15 @@ package bootstrap import ( "encoding/json" "fmt" - "github.com/jessevdk/go-flags" "log" "math/rand" "os" "path/filepath" "runtime" + "strings" "time" + + "github.com/jessevdk/go-flags" ) type Flags struct { @@ -31,6 +33,7 @@ type Flags struct { type Config struct { FactorioDir string `json:"factorio_dir"` FactorioSavesDir string `json:"saves_dir"` + FactorioBaseModDir string `json:"basemod_dir"` FactorioModsDir string `json:"mods_dir"` FactorioModPackDir string `json:"mod_pack_dir"` FactorioConfigFile string `json:"config_file"` @@ -91,6 +94,14 @@ func (config *Config) loadServerConfig() { err = decoder.Decode(&config) failOnError(err, "Error decoding JSON config file.") + if !strings.HasPrefix(config.SettingsFile, "/") { + config.SettingsFile = filepath.Join(config.FactorioConfigDir, config.SettingsFile) + } + + if config.FactorioBaseModDir == "" { + config.FactorioBaseModDir = filepath.Join(config.FactorioDir, "data", "base") + } + config.FactorioRconPort = randomPort() } @@ -118,12 +129,17 @@ func mapFlags(flags Flags) Config { FactorioModPackDir: flags.ModPackDir, FactorioConfigDir: filepath.Join(flags.FactorioDir, "config"), FactorioConfigFile: filepath.Join(flags.FactorioDir, flags.FactorioConfigFile), - FactorioBinary: filepath.Join(flags.FactorioDir, flags.FactorioBinary), FactorioCredentialsFile: "./factorio.auth", FactorioAdminFile: "server-adminlist.json", MaxUploadSize: flags.FactorioMaxUpload, } + if strings.HasPrefix(flags.FactorioBinary, "/") { + config.FactorioBinary = flags.FactorioBinary + } else { + config.FactorioBinary = filepath.Join(flags.FactorioDir, flags.FactorioBinary) + } + if runtime.GOOS == "windows" { appdata := os.Getenv("APPDATA") config.FactorioLog = filepath.Join(appdata, "Factorio", "factorio-current.log") diff --git a/src/factorio/server.go b/src/factorio/server.go index 703436f..55d5234 100644 --- a/src/factorio/server.go +++ b/src/factorio/server.go @@ -3,8 +3,6 @@ package factorio import ( "bufio" "encoding/json" - "github.com/mroote/factorio-server-manager/api/websocket" - "github.com/mroote/factorio-server-manager/bootstrap" "io" "io/ioutil" "log" @@ -16,6 +14,9 @@ import ( "strings" "sync" + "github.com/mroote/factorio-server-manager/api/websocket" + "github.com/mroote/factorio-server-manager/bootstrap" + "github.com/majormjr/rcon" ) @@ -85,7 +86,7 @@ func NewFactorioServer() (err error) { return } - settingsPath := filepath.Join(config.FactorioConfigDir, config.SettingsFile) + settingsPath := config.SettingsFile var settings *os.File if _, err = os.Stat(settingsPath); os.IsNotExist(err) { @@ -165,7 +166,7 @@ func NewFactorioServer() (err error) { } //Load baseMod version - baseModInfoFile := filepath.Join(config.FactorioDir, "data", "base", "info.json") + baseModInfoFile := filepath.Join(config.FactorioBaseModDir, "info.json") bmifBa, err := ioutil.ReadFile(baseModInfoFile) if err != nil { log.Printf("couldn't open baseMods info.json: %s", err) @@ -226,7 +227,7 @@ func (server *Server) Run() error { if err != nil { log.Println("Failed to marshal FactorioServerSettings: ", err) } else { - ioutil.WriteFile(filepath.Join(config.FactorioConfigDir, config.SettingsFile), data, 0644) + ioutil.WriteFile(config.SettingsFile, data, 0644) } args := []string{} @@ -241,7 +242,7 @@ func (server *Server) Run() error { args = append(args, "--bind", server.BindIP, "--port", strconv.Itoa(server.Port), - "--server-settings", filepath.Join(config.FactorioConfigDir, config.SettingsFile), + "--server-settings", config.SettingsFile, "--rcon-port", strconv.Itoa(config.FactorioRconPort), "--rcon-password", config.FactorioRconPass) diff --git a/src/main.go b/src/main.go index 79224a3..e70e23c 100644 --- a/src/main.go +++ b/src/main.go @@ -1,12 +1,13 @@ package main import ( - "github.com/mroote/factorio-server-manager/api" - "github.com/mroote/factorio-server-manager/bootstrap" - "github.com/mroote/factorio-server-manager/factorio" "log" "net/http" "os" + + "github.com/mroote/factorio-server-manager/api" + "github.com/mroote/factorio-server-manager/bootstrap" + "github.com/mroote/factorio-server-manager/factorio" ) func main() { @@ -19,7 +20,7 @@ func main() { // Initialize Factorio Server struct err := factorio.NewFactorioServer() if err != nil { - log.Printf("Error occurred during Server initializaion: %v\n", err) + log.Printf("Error occurred during Server initialization: %v\n", err) return }