Default config: server: serverFolder: "factorio" worldFile: "factorio/saves/newworld.zip" serverSettings: "factorio/data/server-settings.json" serverExec: "factorio/bin/x64/factorio" port: 34197 factoryman: screen: True screenName: "Factorio" backupDir: "factorio/backups" username: "" apitoken: ""
79 lines
2.1 KiB
Go
79 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
func DefCfg() *DefConfig {
|
|
c := &DefConfig{}
|
|
// server settings
|
|
c.Server.ServDir = "factorio"
|
|
c.Server.ServPort = 34197
|
|
c.Server.ServCfg = "factorio/data/server-settings.json"
|
|
c.Server.ServExec = "factorio/bin/x64/factorio"
|
|
c.Server.WorldFile = "factorio/saves/newworld.zip"
|
|
c.Factoryman.UseScreen = true
|
|
c.Factoryman.ScreenName = "factorio"
|
|
c.Factoryman.BackupDir = "factorio/backups"
|
|
c.Factoryman.UserName = ""
|
|
c.Factoryman.ApiToken = ""
|
|
return c
|
|
}
|
|
func readCfg(factCfg string) UsrConfig {
|
|
//read config file (YAML)
|
|
fileBytes, err := os.ReadFile(factCfg)
|
|
if err != nil {
|
|
fmt.Printf("Error reading config.yml file, using defaults\n")
|
|
return UsrConfig(*DefCfg())
|
|
}
|
|
// return Struct
|
|
var config UsrConfig
|
|
err = yaml.Unmarshal(fileBytes, &config)
|
|
if err != nil {
|
|
log.Fatalf("Error unmarshalling YAML file: %v", err)
|
|
}
|
|
return config
|
|
}
|
|
func isItReal(path string) bool { //check if path exists
|
|
if _, err := os.Stat(path); err == nil {
|
|
return true
|
|
} else if errors.Is(err, os.ErrNotExist) {
|
|
return false
|
|
} else {
|
|
return false
|
|
}
|
|
}
|
|
func verifyConfig(config UsrConfig) bool { // check each file/dir to see if it exists
|
|
if !isItReal(config.Server.ServDir) { //check for Server directory
|
|
fmt.Printf("PATH NOT FOUND: %s", config.Server.ServDir)
|
|
return false
|
|
}
|
|
if !isItReal(config.Server.WorldFile) { // check for world file
|
|
fmt.Printf("PATH NOT FOUND: %s", config.Server.WorldFile)
|
|
return false
|
|
}
|
|
if !isItReal(config.Server.ServCfg) { // check for server config
|
|
fmt.Printf("PATH NOT FOUND: %s", config.Server.ServCfg)
|
|
return false
|
|
}
|
|
if !isItReal(config.Server.ServExec) { // check for server exec file to lanch server
|
|
fmt.Printf("PATH NOT FOUND: %s", config.Server.ServExec)
|
|
return false
|
|
}
|
|
if !isItReal(config.Factoryman.BackupDir) { // check for backup directory
|
|
fmt.Printf("PATH NOT FOUND: %s", config.Factoryman.BackupDir)
|
|
return false
|
|
}
|
|
|
|
if config.Factoryman.ApiToken == "" || config.Factoryman.UserName == "" {
|
|
fmt.Printf("API Token/Username not set (to download mods add api token and username to config)")
|
|
}
|
|
return true
|
|
|
|
}
|