/// <summary>
        /// Starts a new engine to repair all loaded power blocks once based on the settings in PersistentData
        /// and saves results back into PersistentData. To be called in background thread.
        /// </summary>
        private static void RepairPowerBlocks()
        {
            try
            {
                Log.Out("Detected NRE TileEntityPoweredTrigger.write. Starting integrity scan in background ...");
                var repairEngine = new RepairEngine("p", PersistentData.Instance.RepairSimulate);
                repairEngine.Start();

                if (repairEngine._problemsFound >= 1)
                {
                    PersistentData.Instance.RepairCounter += repairEngine._problemsFound;
                    PersistentData.Instance.SaveLater();
                }
            }
            catch (Exception ex)
            {
                Log.Error("Error while running power block repair in background: " + ex);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Called when the game started and all objects are ready (e.g. GameManager.Instance.World)
        /// </summary>
        public override void GameStartDone()
        {
            try
            {
                Log.Debug("Api.GameStartDone called.");
                Log.Out("Initializing phase 3/3 ...");
                EacTools.Init();
                CommandTools.InitEvents();
                RepairEngine.InitAuto();
                TelemetryTools.Init();
                Log.Out($"Done initializing {Constants.ModName}.");

                CommandTools.InvokeScriptEvents(ScriptEvent.gameStartDone, () => new ScriptEventArgs());
            }
            catch (Exception ex)
            {
                CommandTools.HandleEventException(ex);
            }
        }
        /// <summary>
        /// Activates automatic background repairs based on the settings in PersistentData.Instance.Repair*.
        /// Will NOT reset the counters so that it can be also used to continue background checks after restart.
        /// </summary>
        public static void AutoOn()
        {
            string tasks         = PersistentData.Instance.RepairTasks;
            bool   simulate      = PersistentData.Instance.RepairSimulate;
            int    timerInterval = PersistentData.Instance.RepairInterval; // seconds

            if (tasks.Contains("p"))
            {
                Application.logMessageReceived += LogMessageReceived;
            }

            _backgroundTimer = new System.Threading.Timer(delegate
            {
                Log.Debug("Automatic background repair timer started.");
                try
                {
                    var repairEngine = new RepairEngine(PersistentData.Instance.RepairTasks, PersistentData.Instance.RepairSimulate);
                    repairEngine.Start();

                    if (repairEngine._problemsFound >= 1)
                    {
                        PersistentData.Instance.RepairCounter += repairEngine._problemsFound;
                        PersistentData.Instance.SaveLater();
                    }
                    Log.Debug("Automatic background repair timer ended.");
                }
                catch (Exception ex)
                {
                    Log.Error("Error while running repair in background: " + ex);
                    throw;
                }
            }, null, TimerDelay, timerInterval * 1000);

            SdtdConsole.Instance.LogAndOutput($"Automatic background {(simulate ? "scan (without repair)" : "repair")} for server problem{(tasks.Length == 1 ? "" : "s")} '{tasks}' every {timerInterval} seconds turned ON.");
            SdtdConsole.Instance.Output("To turn off, enter \"dj-repair /auto\" again.");
        }