Decompiled source of AutoSaveInterval v1.0.1

plugins/AutoSaveInterval.dll

Decompiled 3 days ago
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.CompilerServices;
using BepInEx;
using BepInEx.Configuration;
using UnityEngine;

[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]
[assembly: AssemblyVersion("0.0.0.0")]
[BepInPlugin("fix.autosave.interval", "AutoSaveInterval", "1.0.1")]
public class AutoSaveInterval : BaseUnityPlugin
{
	private static ConfigEntry<int> saveIntervalSeconds;

	private static ConfigEntry<int> preRestartMinutes;

	private static ConfigEntry<string> restartTimesUTC;

	private bool running = false;

	private HashSet<string> preRestartSavesDone = new HashSet<string>();

	private void Awake()
	{
		saveIntervalSeconds = ((BaseUnityPlugin)this).Config.Bind<int>("General", "SaveIntervalSeconds", 600, "How often to auto-save the world, in seconds. Default 600 (10 minutes).");
		preRestartMinutes = ((BaseUnityPlugin)this).Config.Bind<int>("ServerRestart", "PreRestartSaveMinutes", 2, "Minutes before a scheduled restart to trigger a save. Default 2.");
		restartTimesUTC = ((BaseUnityPlugin)this).Config.Bind<string>("ServerRestart", "RestartTimesUTC", "17:00,05:00", "Comma-separated UTC times matching your ServerRestart schedule. Default: 17:00,05:00 (0200,1400 JST).");
		((BaseUnityPlugin)this).Logger.LogInfo((object)("AutoSaveInterval loaded — interval " + saveIntervalSeconds.Value + "s, pre-restart save " + preRestartMinutes.Value + "m before " + restartTimesUTC.Value));
	}

	private void Update()
	{
		if (!running && !((Object)(object)ZNet.instance == (Object)null) && ZNet.instance.IsServer())
		{
			running = true;
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Server detected, starting auto-save loop");
			((MonoBehaviour)this).StartCoroutine(SaveLoop());
			((MonoBehaviour)this).StartCoroutine(PreRestartSaveLoop());
		}
	}

	private IEnumerator SaveLoop()
	{
		while (true)
		{
			yield return (object)new WaitForSeconds((float)saveIntervalSeconds.Value);
			if ((Object)(object)ZNet.instance == (Object)null || !ZNet.instance.IsServer())
			{
				break;
			}
			((BaseUnityPlugin)this).Logger.LogInfo((object)"Auto-save triggered (interval)");
			ZNet.instance.Save(false, false, false);
		}
		((BaseUnityPlugin)this).Logger.LogWarning((object)"Lost ZNet server instance, stopping save loop");
		running = false;
	}

	private IEnumerator PreRestartSaveLoop()
	{
		while (true)
		{
			yield return (object)new WaitForSeconds(15f);
			if ((Object)(object)ZNet.instance == (Object)null || !ZNet.instance.IsServer())
			{
				break;
			}
			DateTime utcNow = DateTime.UtcNow;
			string today = utcNow.ToString("yyyy-MM-dd");
			int minutesBefore = preRestartMinutes.Value;
			string[] times = restartTimesUTC.Value.Split(new char[1] { ',' });
			string[] array = times;
			foreach (string text in array)
			{
				string text2 = text.Trim();
				if (string.IsNullOrEmpty(text2))
				{
					continue;
				}
				string[] array2 = text2.Split(new char[1] { ':' });
				if (array2.Length >= 2 && int.TryParse(array2[0], out var result) && int.TryParse(array2[1], out var result2))
				{
					DateTime dateTime = new DateTime(utcNow.Year, utcNow.Month, utcNow.Day, result, result2, 0, DateTimeKind.Utc).AddMinutes(-minutesBefore);
					double totalSeconds = (utcNow - dateTime).TotalSeconds;
					string item = today + "_" + text2;
					if (totalSeconds >= 0.0 && totalSeconds < 30.0 && !preRestartSavesDone.Contains(item))
					{
						preRestartSavesDone.Add(item);
						((BaseUnityPlugin)this).Logger.LogInfo((object)("Pre-restart save triggered (" + minutesBefore + "m before " + text2 + " UTC restart)"));
						ZNet.instance.Save(false, false, false);
					}
				}
			}
			if (preRestartSavesDone.Count > 20)
			{
				preRestartSavesDone.Clear();
			}
		}
	}
}