diff --git a/commands/moderator/newFactorio.go b/commands/moderator/newFactorio.go index 856bbc9d..9d0108b0 100755 --- a/commands/moderator/newFactorio.go +++ b/commands/moderator/newFactorio.go @@ -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 { diff --git a/fact/factUtils.go b/fact/factUtils.go index 8109cceb..141ab281 100755 --- a/fact/factUtils.go +++ b/fact/factUtils.go @@ -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 @@ -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.") @@ -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 } } @@ -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 @@ -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) } diff --git a/fact/mapReset.go b/fact/mapReset.go index 2c3a1d8c..c59bbf6a 100755 --- a/fact/mapReset.go +++ b/fact/mapReset.go @@ -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 @@ -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 { diff --git a/main.go b/main.go index bec08300..572aa82f 100755 --- a/main.go +++ b/main.go @@ -73,7 +73,7 @@ func main() { go support.HandleChat() if cfg.Local.Options.AutoStart { - fact.FactAutoStart = true + fact.SetAutolaunch(true, false) } if *htmlTest { @@ -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 diff --git a/support/factLauncher.go b/support/factLauncher.go index aedea452..f76bb192 100755 --- a/support/factLauncher.go +++ b/support/factLauncher.go @@ -391,7 +391,7 @@ 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 } @@ -399,7 +399,7 @@ func launchFactorio() { 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 } @@ -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) diff --git a/support/mainLoops.go b/support/mainLoops.go index fa77e7e8..ce001cd5 100755 --- a/support/mainLoops.go +++ b/support/mainLoops.go @@ -46,7 +46,7 @@ func MainLoops() { if fact.QueueFactReboot { if cfg.Local.Options.AutoStart { - fact.FactAutoStart = true + fact.SetAutolaunch(true, false) } fact.QueueFactReboot = false @@ -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 @@ -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 diff --git a/support/pipeHandle.go b/support/pipeHandle.go index 9a81cfca..db536603 100755 --- a/support/pipeHandle.go +++ b/support/pipeHandle.go @@ -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 @@ -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 } @@ -889,31 +885,27 @@ 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 } @@ -921,8 +913,7 @@ func handleCrashes(input *handleData) bool { 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 } @@ -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 } @@ -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 { @@ -993,8 +979,7 @@ 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 { @@ -1002,14 +987,12 @@ func handleCrashes(input *handleData) bool { 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) } } diff --git a/support/supportUtil.go b/support/supportUtil.go index bdac3014..5f4ff404 100755 --- a/support/supportUtil.go +++ b/support/supportUtil.go @@ -53,7 +53,7 @@ func checkHours() { } if !WithinHours() { - fact.FactAutoStart = false + fact.SetAutolaunch(false, false) fact.QuitFactorio("Time is up...") } @@ -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) } }