Handle absolute paths in settings/binary, configurable BaseMod dir

This commit is contained in:
Forest Ames 2021-01-17 00:53:27 -07:00
parent 9ca71cb3bf
commit 0bad11aab8
No known key found for this signature in database
GPG Key ID: E3EA5FCF88D860DE
4 changed files with 34 additions and 15 deletions

View File

@ -4,8 +4,6 @@ import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"github.com/mroote/factorio-server-manager/bootstrap"
"github.com/mroote/factorio-server-manager/factorio"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
@ -16,6 +14,9 @@ import (
"sync" "sync"
"time" "time"
"github.com/mroote/factorio-server-manager/bootstrap"
"github.com/mroote/factorio-server-manager/factorio"
"github.com/gorilla/mux" "github.com/gorilla/mux"
) )
@ -628,7 +629,7 @@ func UpdateServerSettings(w http.ResponseWriter, r *http.Request) {
return return
} }
config := bootstrap.GetConfig() config := bootstrap.GetConfig()
err = ioutil.WriteFile(filepath.Join(config.FactorioConfigDir, config.SettingsFile), settings, 0644) err = ioutil.WriteFile(config.SettingsFile, settings, 0644)
if err != nil { if err != nil {
resp = fmt.Sprintf("Failed to save server settings: %v\n", err) resp = fmt.Sprintf("Failed to save server settings: %v\n", err)
log.Println(resp) log.Println(resp)

View File

@ -3,13 +3,15 @@ package bootstrap
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"github.com/jessevdk/go-flags"
"log" "log"
"math/rand" "math/rand"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"strings"
"time" "time"
"github.com/jessevdk/go-flags"
) )
type Flags struct { type Flags struct {
@ -31,6 +33,7 @@ type Flags struct {
type Config struct { type Config struct {
FactorioDir string `json:"factorio_dir"` FactorioDir string `json:"factorio_dir"`
FactorioSavesDir string `json:"saves_dir"` FactorioSavesDir string `json:"saves_dir"`
FactorioBaseModDir string `json:"basemod_dir"`
FactorioModsDir string `json:"mods_dir"` FactorioModsDir string `json:"mods_dir"`
FactorioModPackDir string `json:"mod_pack_dir"` FactorioModPackDir string `json:"mod_pack_dir"`
FactorioConfigFile string `json:"config_file"` FactorioConfigFile string `json:"config_file"`
@ -91,6 +94,14 @@ func (config *Config) loadServerConfig() {
err = decoder.Decode(&config) err = decoder.Decode(&config)
failOnError(err, "Error decoding JSON config file.") 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() config.FactorioRconPort = randomPort()
} }
@ -118,12 +129,17 @@ func mapFlags(flags Flags) Config {
FactorioModPackDir: flags.ModPackDir, FactorioModPackDir: flags.ModPackDir,
FactorioConfigDir: filepath.Join(flags.FactorioDir, "config"), FactorioConfigDir: filepath.Join(flags.FactorioDir, "config"),
FactorioConfigFile: filepath.Join(flags.FactorioDir, flags.FactorioConfigFile), FactorioConfigFile: filepath.Join(flags.FactorioDir, flags.FactorioConfigFile),
FactorioBinary: filepath.Join(flags.FactorioDir, flags.FactorioBinary),
FactorioCredentialsFile: "./factorio.auth", FactorioCredentialsFile: "./factorio.auth",
FactorioAdminFile: "server-adminlist.json", FactorioAdminFile: "server-adminlist.json",
MaxUploadSize: flags.FactorioMaxUpload, 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" { if runtime.GOOS == "windows" {
appdata := os.Getenv("APPDATA") appdata := os.Getenv("APPDATA")
config.FactorioLog = filepath.Join(appdata, "Factorio", "factorio-current.log") config.FactorioLog = filepath.Join(appdata, "Factorio", "factorio-current.log")

View File

@ -3,8 +3,6 @@ package factorio
import ( import (
"bufio" "bufio"
"encoding/json" "encoding/json"
"github.com/mroote/factorio-server-manager/api/websocket"
"github.com/mroote/factorio-server-manager/bootstrap"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
@ -16,6 +14,9 @@ import (
"strings" "strings"
"sync" "sync"
"github.com/mroote/factorio-server-manager/api/websocket"
"github.com/mroote/factorio-server-manager/bootstrap"
"github.com/majormjr/rcon" "github.com/majormjr/rcon"
) )
@ -85,7 +86,7 @@ func NewFactorioServer() (err error) {
return return
} }
settingsPath := filepath.Join(config.FactorioConfigDir, config.SettingsFile) settingsPath := config.SettingsFile
var settings *os.File var settings *os.File
if _, err = os.Stat(settingsPath); os.IsNotExist(err) { if _, err = os.Stat(settingsPath); os.IsNotExist(err) {
@ -165,7 +166,7 @@ func NewFactorioServer() (err error) {
} }
//Load baseMod version //Load baseMod version
baseModInfoFile := filepath.Join(config.FactorioDir, "data", "base", "info.json") baseModInfoFile := filepath.Join(config.FactorioBaseModDir, "info.json")
bmifBa, err := ioutil.ReadFile(baseModInfoFile) bmifBa, err := ioutil.ReadFile(baseModInfoFile)
if err != nil { if err != nil {
log.Printf("couldn't open baseMods info.json: %s", err) log.Printf("couldn't open baseMods info.json: %s", err)
@ -226,7 +227,7 @@ func (server *Server) Run() error {
if err != nil { if err != nil {
log.Println("Failed to marshal FactorioServerSettings: ", err) log.Println("Failed to marshal FactorioServerSettings: ", err)
} else { } else {
ioutil.WriteFile(filepath.Join(config.FactorioConfigDir, config.SettingsFile), data, 0644) ioutil.WriteFile(config.SettingsFile, data, 0644)
} }
args := []string{} args := []string{}
@ -241,7 +242,7 @@ func (server *Server) Run() error {
args = append(args, args = append(args,
"--bind", server.BindIP, "--bind", server.BindIP,
"--port", strconv.Itoa(server.Port), "--port", strconv.Itoa(server.Port),
"--server-settings", filepath.Join(config.FactorioConfigDir, config.SettingsFile), "--server-settings", config.SettingsFile,
"--rcon-port", strconv.Itoa(config.FactorioRconPort), "--rcon-port", strconv.Itoa(config.FactorioRconPort),
"--rcon-password", config.FactorioRconPass) "--rcon-password", config.FactorioRconPass)

View File

@ -1,12 +1,13 @@
package main package main
import ( import (
"github.com/mroote/factorio-server-manager/api"
"github.com/mroote/factorio-server-manager/bootstrap"
"github.com/mroote/factorio-server-manager/factorio"
"log" "log"
"net/http" "net/http"
"os" "os"
"github.com/mroote/factorio-server-manager/api"
"github.com/mroote/factorio-server-manager/bootstrap"
"github.com/mroote/factorio-server-manager/factorio"
) )
func main() { func main() {
@ -19,7 +20,7 @@ func main() {
// Initialize Factorio Server struct // Initialize Factorio Server struct
err := factorio.NewFactorioServer() err := factorio.NewFactorioServer()
if err != nil { if err != nil {
log.Printf("Error occurred during Server initializaion: %v\n", err) log.Printf("Error occurred during Server initialization: %v\n", err)
return return
} }