Skip to content

Commit

Permalink
cleanup of factorio running/booted status
Browse files Browse the repository at this point in the history
  • Loading branch information
Distortions81 committed Jan 20, 2025
1 parent 4457224 commit ed1ed3d
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 45 deletions.
4 changes: 2 additions & 2 deletions commands/moderator/newFactorio.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ func StartFact(cmd *glob.CommandData, i *discordgo.InteractionCreate) {
disc.InteractionEphemeralResponse(i, "Status:", buf)
}

fact.FactAutoStart = true
fact.SetAutolaunch(true, false)
glob.RelaunchThrottle = 0
}

/* StopServer saves the map and closes Factorio. */
func StopFact(cmd *glob.CommandData, i *discordgo.InteractionCreate) {
glob.RelaunchThrottle = 0
fact.FactAutoStart = false
fact.SetAutolaunch(false, false)

if fact.FactorioBooted || fact.FactIsRunning {

Expand Down
35 changes: 32 additions & 3 deletions fact/factUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,34 @@ func CheckSave(path, name string, showError bool) (good bool, folder string) {
return false, ""
}

var AutoLaunchLock sync.Mutex

func SetAutolaunch(autolaunch, report bool) {
AutoLaunchLock.Lock()
defer AutoLaunchLock.Unlock()

if !autolaunch && FactAutoStart {
FactAutoStart = false
if report {
glob.BootMessage = disc.SmartEditDiscordEmbed(cfg.Local.Channel.ChatChannel, glob.BootMessage, "Warning", "Auto-reboot has been turned OFF.", glob.COLOR_RED)
}
cwlog.DoLogCW("Autolaunch disabled.")
} else if autolaunch && !FactAutoStart {
FactAutoStart = true
cwlog.DoLogCW("Autolaunch enabled.")
if report {
glob.BootMessage = disc.SmartEditDiscordEmbed(cfg.Local.Channel.ChatChannel, glob.BootMessage, "Warning", "Auto-reboot has been ENABLED.", glob.COLOR_RED)
}
}

}

var FactRunningLock sync.Mutex

func SetFactRunning(run, report bool) {
FactRunningLock.Lock()
defer FactRunningLock.Unlock()

wasrun := FactIsRunning
FactIsRunning = run

Expand All @@ -131,6 +158,9 @@ func SetFactRunning(run, report bool) {
glob.NoResponseCount = 0

if wasrun != run {
if !run {
FactorioBooted = false
}
if report {
if run {
cwlog.DoLogGame("Factorio " + FactorioVersion + " is now online.")
Expand Down Expand Up @@ -395,7 +425,6 @@ func WriteFact(input string) {
} else {
//cwlog.DoLogCW("An error occurred when attempting to write to Factorio (nil pipe)")
SetFactRunning(false, true)
FactorioBooted = false
return
}
}
Expand Down Expand Up @@ -915,7 +944,7 @@ func DoChangeMap(arg string) {
return
}

FactAutoStart = false
SetAutolaunch(false, false)
QuitFactorio("Server rebooting for map vote!")
WaitFactQuit(false)
selSaveName := path + "/" + saveStr
Expand Down Expand Up @@ -959,6 +988,6 @@ func DoChangeMap(arg string) {
msg := fmt.Sprintf("Loading save: %v", arg)
LogGameCMS(false, cfg.Local.Channel.ChatChannel, msg)
glob.RelaunchThrottle = 0
FactAutoStart = true
SetAutolaunch(true, false)

}
4 changes: 2 additions & 2 deletions fact/mapReset.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func Map_reset(doReport bool) {
DoUpdateFactorio = false //Skip queued updates
cfg.WriteLCfg()

FactAutoStart = false
SetAutolaunch(false, false)
QuitFactorio("Server rebooting for map reset!!!")
} else {
return
Expand Down Expand Up @@ -180,7 +180,7 @@ func Map_reset(doReport bool) {
WriteVotes()

LogGameCMS(false, cfg.Local.Channel.ChatChannel, "Map reset complete, booting.")
FactAutoStart = true
SetAutolaunch(true, false)
}

func GenNewMap() string {
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func main() {
go support.HandleChat()

if cfg.Local.Options.AutoStart {
fact.FactAutoStart = true
fact.SetAutolaunch(true, false)
}

if *htmlTest {
Expand All @@ -86,7 +86,7 @@ func main() {
<-sc

_ = os.Remove("cw.lock")
fact.FactAutoStart = false
fact.SetAutolaunch(false, false)
glob.DoRebootCW = false
fact.QueueReboot = false
fact.QueueFactReboot = false
Expand Down
6 changes: 3 additions & 3 deletions support/factLauncher.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,15 +391,15 @@ func launchFactorio() {
glob.BootMessage = disc.SmartEditDiscordEmbed(cfg.Local.Channel.ChatChannel, glob.BootMessage, "ERROR", "Factorio is not installed. Use `/factorio install-factorio` to install it.", glob.COLOR_RED)

cwlog.DoLogCW("Factorio does not appear to be installed at the configured path: " + checkFactPath)
fact.FactAutoStart = false
fact.SetAutolaunch(false, true)
return
}

/* Find, test and load newest save game available */
found, fileName, folderName := GetSaveGame(true)
if !found {
glob.BootMessage = disc.SmartEditDiscordEmbed(cfg.Local.Channel.ChatChannel, glob.BootMessage, "ERROR", "Unable to access save-games.", glob.COLOR_RED)
fact.FactAutoStart = false
fact.SetAutolaunch(false, true)
return
}

Expand Down Expand Up @@ -502,7 +502,7 @@ func launchFactorio() {

/* Generate config file for Factorio server, if it fails stop everything.*/
if !fact.GenerateFactorioConfig() {
fact.FactAutoStart = false
fact.SetAutolaunch(false, true)

glob.BootMessage = disc.SmartEditDiscordEmbed(cfg.Local.Channel.ChatChannel, glob.BootMessage, "ERROR", "Unable to write a config file for Fatorio.", glob.COLOR_RED)

Expand Down
6 changes: 3 additions & 3 deletions support/mainLoops.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func MainLoops() {

if fact.QueueFactReboot {
if cfg.Local.Options.AutoStart {
fact.FactAutoStart = true
fact.SetAutolaunch(true, false)
}
fact.QueueFactReboot = false

Expand Down Expand Up @@ -524,7 +524,7 @@ func MainLoops() {
if _, err = os.Stat(".stop"); err == nil {
if errb = os.Remove(".stop"); errb == nil {
fact.LogGameCMS(false, cfg.Local.Channel.ChatChannel, "Factorio stopping!")
fact.FactAutoStart = false
fact.SetAutolaunch(false, false)
fact.QuitFactorio("Server stopping for maintenance.")
} else if !failureReported {
failureReported = true
Expand All @@ -545,7 +545,7 @@ func MainLoops() {
/* Start game */
if _, err = os.Stat(".start"); err == nil {
if errb = os.Remove(".start"); errb == nil {
fact.FactAutoStart = true
fact.SetAutolaunch(true, false)
fact.LogGameCMS(false, cfg.Local.Channel.ChatChannel, "Factorio starting!")
} else if !failureReported {
failureReported = true
Expand Down
39 changes: 11 additions & 28 deletions support/pipeHandle.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,6 @@ func handleFactGoodbye(input *handleData) bool {
if strings.HasPrefix(input.noTimecode, "Goodbye") {
fact.SetLastBan("")

fact.FactorioBooted = false
fact.FactorioBootedAt = time.Time{}
fact.SetFactRunning(false, true)
return true
Expand Down Expand Up @@ -862,23 +861,20 @@ func handleCrashes(input *handleData) bool {
} else {
fact.LogCMS(cfg.Local.Channel.ChatChannel, "Factorio failed loading game mods.")
}
fact.FactAutoStart = false
fact.FactorioBooted = false
fact.SetAutolaunch(false, true)
fact.SetFactRunning(false, true)
return true
}
/* Lock error */
if strings.Contains(input.noTimecode, "Couldn't acquire exclusive lock") {
fact.LogCMS(cfg.Local.Channel.ChatChannel, "Factorio is already running.")
fact.FactAutoStart = false
fact.FactorioBooted = false
fact.SetAutolaunch(false, true)
fact.SetFactRunning(false, true)
return true
}
/* Mod Errors */
if strings.Contains(input.noTimecode, "caused a non-recoverable error.") {
fact.LogCMS(cfg.Local.Channel.ChatChannel, "**Factorio encountered a lua error and will reboot.**")
fact.FactorioBooted = false
fact.SetFactRunning(false, true)
return true
}
Expand All @@ -889,40 +885,35 @@ func handleCrashes(input *handleData) bool {
} else {
fact.LogCMS(cfg.Local.Channel.ChatChannel, "Factorio was unable to load a multiplayer game.")
}
fact.FactAutoStart = false
fact.FactorioBooted = false
fact.SetAutolaunch(false, true)
fact.SetFactRunning(false, true)
return true
}
/* level.dat */
if strings.Contains(input.noTimecode, "level.dat not found.") {
fact.LogCMS(cfg.Local.Channel.ChatChannel, "Unable to load save-game.")
fact.FactAutoStart = false
fact.FactorioBooted = false
fact.SetAutolaunch(false, true)
fact.SetFactRunning(false, true)
return true
}
/* Stack traces */
if strings.Contains(input.noTimecode, "Unexpected error occurred.") {
fact.LogCMS(cfg.Local.Channel.ChatChannel, "**Factorio crashed.**")
fact.FactorioBooted = false
fact.SetFactRunning(false, true)
return true
}
if strings.Contains(input.noTimecode, "CommandLineMultiplayer") {
if strings.Contains(input.noTimecode, "No latest save file found in") {
fact.CMS(cfg.Local.Channel.ChatChannel, "No save-game found.")
fact.FactAutoStart = false
fact.FactorioBooted = false
fact.SetAutolaunch(false, true)
fact.SetFactRunning(false, true)
return true
}
}
if strings.Contains(input.noTimecode, "Scenario") && strings.HasSuffix(input.noTimecode, "not found") {
fact.CMS(cfg.Local.Channel.ChatChannel, "Invalid scenario specified, clearing scenario setting.")
cfg.Local.Settings.Scenario = ""
fact.FactAutoStart = false
fact.FactorioBooted = false
fact.SetAutolaunch(false, true)
fact.SetFactRunning(false, true)
return true
}
Expand All @@ -942,29 +933,25 @@ func handleCrashes(input *handleData) bool {

if strings.Contains(input.noTimecode, "cannot be loaded because it is higher than the game version") {
fact.CMS(cfg.Local.Channel.ChatChannel, "**Factorio version is too old for the save game.**")
fact.FactAutoStart = false
fact.FactorioBooted = false
fact.SetAutolaunch(false, true)
fact.SetFactRunning(false, true)
return true
}
if strings.Contains(input.noTimecode, "syntax error") || strings.Contains(input.noTimecode, "unexpected symbol") ||
strings.Contains(input.noTimecode, "expected") {
fact.CMS(cfg.Local.Channel.ChatChannel, "**Factorio encountered a lua syntax error and will stop.**")
fact.FactAutoStart = false
fact.FactorioBooted = false
fact.SetAutolaunch(false, true)
fact.SetFactRunning(false, true)
return true
}
if strings.Contains(input.noTimecode, "Error while running command") {
fact.CMS(cfg.Local.Channel.ChatChannel, "**Factorio encountered a lua command error.**")
fact.FactorioBooted = false
fact.SetFactRunning(false, true)
return true
}
if strings.Contains(input.noTimecode, "info.json not found") {
fact.CMS(cfg.Local.Channel.ChatChannel, "Unable to load save-game.")
fact.FactAutoStart = false
fact.FactorioBooted = false
fact.SetAutolaunch(false, true)
fact.SetFactRunning(false, true)
return true
}
Expand All @@ -975,8 +962,7 @@ func handleCrashes(input *handleData) bool {
err := os.Remove(input.trimmedWords[7])
if err != nil {
cwlog.DoLogCW("Unable to remove bad zip file: " + input.trimmedWords[7])
fact.FactAutoStart = false
fact.FactorioBooted = false
fact.SetAutolaunch(false, true)
fact.SetFactRunning(false, true)
return true
} else {
Expand All @@ -993,23 +979,20 @@ func handleCrashes(input *handleData) bool {
if errs != nil {
cwlog.DoLogCW("Unable to delete corrupt savegame. Details:\nfile: %v\nerr: %v", fact.GameMapPath, errs)
fact.CMS(cfg.Local.Channel.ChatChannel, "Unable to remove corrupted save-game.")
fact.FactAutoStart = false
fact.FactorioBooted = false
fact.SetAutolaunch(false, true)
fact.SetFactRunning(false, true)
return true
} else {
cwlog.DoLogCW("Deleted corrupted savegame.")
fact.CMS(cfg.Local.Channel.ChatChannel, "Save-game corrupted, performing automatic roll-back.")
}

fact.FactorioBooted = false
fact.SetFactRunning(false, true)
return true
}

if strings.Contains(input.noTimecode, "Exception at tick") {
fact.CMS(cfg.Local.Channel.ChatChannel, "**Factorio crashed.**")
fact.FactorioBooted = false
fact.SetFactRunning(false, true)
}
}
Expand Down
4 changes: 2 additions & 2 deletions support/supportUtil.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func checkHours() {
}

if !WithinHours() {
fact.FactAutoStart = false
fact.SetAutolaunch(false, false)
fact.QuitFactorio("Time is up...")
}

Expand All @@ -63,7 +63,7 @@ func checkHours() {
cfg.Local.Options.PlayEndHour)

fact.LogGameCMS(false, cfg.Local.Channel.ChatChannel, buf)
fact.FactAutoStart = true
fact.SetAutolaunch(true, false)
}
}

Expand Down

0 comments on commit ed1ed3d

Please sign in to comment.