- [X] Fixes several bugs - [X] Updates more ordinal issues - [X] Cleans up the code a bit - [X] Turns classes static that should have been - [X] Changes TcpServer.Instances to a HashSet Bumps release version
207 lines
5.7 KiB
C#
207 lines
5.7 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
namespace Server.Misc
|
|
{
|
|
public class AutoSave : Timer
|
|
{
|
|
private static readonly TimeSpan m_Delay = TimeSpan.FromMinutes(5.0);
|
|
private static readonly TimeSpan m_Warning = TimeSpan.Zero;
|
|
|
|
private static readonly string[] m_Backups =
|
|
{
|
|
"Third Backup",
|
|
"Second Backup",
|
|
"Most Recent"
|
|
};
|
|
|
|
public AutoSave() : base(m_Delay - m_Warning, m_Delay) => Priority = TimerPriority.OneMinute;
|
|
|
|
public static bool SavesEnabled { get; set; } = true;
|
|
// private static TimeSpan m_Warning = TimeSpan.FromSeconds( 15.0 );
|
|
|
|
public static void Initialize()
|
|
{
|
|
new AutoSave().Start();
|
|
CommandSystem.Register("SetSaves", AccessLevel.Administrator, SetSaves_OnCommand);
|
|
}
|
|
|
|
[Usage("SetSaves <true | false>"), Description("Enables or disables automatic shard saving.")]
|
|
public static void SetSaves_OnCommand(CommandEventArgs e)
|
|
{
|
|
if (e.Length == 1)
|
|
{
|
|
SavesEnabled = e.GetBoolean(0);
|
|
e.Mobile.SendMessage("Saves have been {0}.", SavesEnabled ? "enabled" : "disabled");
|
|
}
|
|
else
|
|
{
|
|
e.Mobile.SendMessage("Format: SetSaves <true | false>");
|
|
}
|
|
}
|
|
|
|
protected override void OnTick()
|
|
{
|
|
if (!SavesEnabled || AutoRestart.Restarting)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (m_Warning == TimeSpan.Zero)
|
|
{
|
|
Save();
|
|
}
|
|
else
|
|
{
|
|
var s = (int)m_Warning.TotalSeconds;
|
|
var m = s / 60;
|
|
s %= 60;
|
|
|
|
if (m > 0 && s > 0)
|
|
{
|
|
World.Broadcast(
|
|
0x35,
|
|
true,
|
|
"The world will save in {0} minute{1} and {2} second{3}.",
|
|
m,
|
|
m != 1 ? "s" : "",
|
|
s,
|
|
s != 1 ? "s" : ""
|
|
);
|
|
}
|
|
else if (m > 0)
|
|
{
|
|
World.Broadcast(0x35, true, "The world will save in {0} minute{1}.", m, m != 1 ? "s" : "");
|
|
}
|
|
else
|
|
{
|
|
World.Broadcast(0x35, true, "The world will save in {0} second{1}.", s, s != 1 ? "s" : "");
|
|
}
|
|
|
|
DelayCall(m_Warning, Save);
|
|
}
|
|
}
|
|
|
|
public static void Save()
|
|
{
|
|
if (AutoRestart.Restarting || !World.Running)
|
|
{
|
|
return;
|
|
}
|
|
|
|
World.WaitForWriteCompletion();
|
|
|
|
try
|
|
{
|
|
Backup();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine("WARNING: Automatic backup FAILED: {0}", e);
|
|
}
|
|
|
|
World.Save();
|
|
}
|
|
|
|
private static void Backup()
|
|
{
|
|
if (m_Backups.Length == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var root = Path.Combine(Core.BaseDirectory, "Backups/Automatic");
|
|
|
|
AssemblyHandler.EnsureDirectory(root);
|
|
|
|
var existing = Directory.GetDirectories(root);
|
|
|
|
for (var i = 0; i < m_Backups.Length; ++i)
|
|
{
|
|
var dir = Match(existing, m_Backups[i]);
|
|
|
|
if (dir == null)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
if (i > 0)
|
|
{
|
|
var timeStamp = FindTimeStamp(dir.Name);
|
|
|
|
if (timeStamp != null)
|
|
{
|
|
try
|
|
{
|
|
dir.MoveTo(FormatDirectory(root, m_Backups[i - 1], timeStamp));
|
|
}
|
|
catch
|
|
{
|
|
// ignored
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
dir.Delete(true);
|
|
}
|
|
catch
|
|
{
|
|
// ignored
|
|
}
|
|
}
|
|
}
|
|
|
|
var saves = Path.Combine(Core.BaseDirectory, "Saves");
|
|
|
|
if (Directory.Exists(saves))
|
|
{
|
|
Directory.Move(saves, FormatDirectory(root, m_Backups[^1], GetTimeStamp()));
|
|
}
|
|
}
|
|
|
|
private static DirectoryInfo Match(string[] paths, string match)
|
|
{
|
|
for (var i = 0; i < paths.Length; ++i)
|
|
{
|
|
var info = new DirectoryInfo(paths[i]);
|
|
|
|
if (info.Name.StartsWith(match, StringComparison.Ordinal))
|
|
{
|
|
return info;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private static string FormatDirectory(string root, string name, string timeStamp) =>
|
|
Path.Combine(root, $"{name} ({timeStamp})");
|
|
|
|
private static string FindTimeStamp(string input)
|
|
{
|
|
var start = input.IndexOf('(', StringComparison.Ordinal);
|
|
|
|
if (start >= 0)
|
|
{
|
|
var end = input.IndexOf(')', ++start);
|
|
|
|
if (end >= start)
|
|
{
|
|
return input.Substring(start, end - start);
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private static string GetTimeStamp()
|
|
{
|
|
var now = DateTime.UtcNow;
|
|
|
|
return $"{now.Day}-{now.Month}-{now.Year} {now.Hour}-{now.Minute:D2}-{now.Second:D2}";
|
|
}
|
|
}
|
|
}
|