Fixes code according to modern code style. Fixes a few expression bugs.

This commit is contained in:
Kamron Batman 2018-09-15 09:58:51 -07:00
parent 89eea25e5f
commit 970fd563b2
3324 changed files with 441118 additions and 433755 deletions

View file

@ -4,170 +4,186 @@ using Server.Commands;
namespace Server.Misc
{
public class AutoSave : Timer
{
private static TimeSpan m_Delay = TimeSpan.FromMinutes( 5.0 );
private static TimeSpan m_Warning = TimeSpan.Zero;
//private static TimeSpan m_Warning = TimeSpan.FromSeconds( 15.0 );
public class AutoSave : Timer
{
private static TimeSpan m_Delay = TimeSpan.FromMinutes(5.0);
private static TimeSpan m_Warning = TimeSpan.Zero;
public static void Initialize()
{
new AutoSave().Start();
CommandSystem.Register( "SetSaves", AccessLevel.Administrator, SetSaves_OnCommand );
}
private static string[] m_Backups =
{
"Third Backup",
"Second Backup",
"Most Recent"
};
public static bool SavesEnabled { get; set; } = true;
public AutoSave() : base(m_Delay - m_Warning, m_Delay)
{
Priority = TimerPriority.OneMinute;
}
[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>" );
}
}
public static bool SavesEnabled{ get; set; } = true;
//private static TimeSpan m_Warning = TimeSpan.FromSeconds( 15.0 );
public AutoSave() : base( m_Delay - m_Warning, m_Delay )
{
Priority = TimerPriority.OneMinute;
}
public static void Initialize()
{
new AutoSave().Start();
CommandSystem.Register("SetSaves", AccessLevel.Administrator, SetSaves_OnCommand);
}
protected override void OnTick()
{
if ( !SavesEnabled || AutoRestart.Restarting )
return;
[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>");
}
}
if ( m_Warning == TimeSpan.Zero )
{
Save( true );
}
else
{
int s = (int)m_Warning.TotalSeconds;
int m = s / 60;
s %= 60;
protected override void OnTick()
{
if (!SavesEnabled || AutoRestart.Restarting)
return;
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" : "" );
if (m_Warning == TimeSpan.Zero)
{
Save(true);
}
else
{
int s = (int)m_Warning.TotalSeconds;
int m = s / 60;
s %= 60;
DelayCall( m_Warning, Save );
}
}
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" : "");
public static void Save()
{
Save( false );
}
DelayCall(m_Warning, Save);
}
}
public static void Save( bool permitBackgroundWrite )
{
if ( AutoRestart.Restarting )
return;
public static void Save()
{
Save(false);
}
World.WaitForWriteCompletion();
public static void Save(bool permitBackgroundWrite)
{
if (AutoRestart.Restarting)
return;
try{ Backup(); }
catch ( Exception e ) { Console.WriteLine("WARNING: Automatic backup FAILED: {0}", e); }
World.WaitForWriteCompletion();
World.Save( true, permitBackgroundWrite );
}
try
{
Backup();
}
catch (Exception e)
{
Console.WriteLine("WARNING: Automatic backup FAILED: {0}", e);
}
private static string[] m_Backups = {
"Third Backup",
"Second Backup",
"Most Recent"
};
World.Save(true, permitBackgroundWrite);
}
private static void Backup()
{
if ( m_Backups.Length == 0 )
return;
private static void Backup()
{
if (m_Backups.Length == 0)
return;
string root = Path.Combine( Core.BaseDirectory, "Backups/Automatic" );
string root = Path.Combine(Core.BaseDirectory, "Backups/Automatic");
if ( !Directory.Exists( root ) )
Directory.CreateDirectory( root );
if (!Directory.Exists(root))
Directory.CreateDirectory(root);
string[] existing = Directory.GetDirectories( root );
string[] existing = Directory.GetDirectories(root);
for ( int i = 0; i < m_Backups.Length; ++i )
{
DirectoryInfo dir = Match( existing, m_Backups[i] );
for (int i = 0; i < m_Backups.Length; ++i)
{
DirectoryInfo dir = Match(existing, m_Backups[i]);
if ( dir == null )
continue;
if (dir == null)
continue;
if ( i > 0 )
{
string timeStamp = FindTimeStamp( dir.Name );
if (i > 0)
{
string timeStamp = FindTimeStamp(dir.Name);
if ( timeStamp != null )
{
try{ dir.MoveTo( FormatDirectory( root, m_Backups[i - 1], timeStamp ) ); }
catch{}
}
}
else
{
try{ dir.Delete( true ); }
catch{}
}
}
if (timeStamp != null)
try
{
dir.MoveTo(FormatDirectory(root, m_Backups[i - 1], timeStamp));
}
catch
{
}
}
else
{
try
{
dir.Delete(true);
}
catch
{
}
}
}
string saves = Path.Combine( Core.BaseDirectory, "Saves" );
string saves = Path.Combine(Core.BaseDirectory, "Saves");
if ( Directory.Exists( saves ) )
Directory.Move( saves, FormatDirectory( root, m_Backups[m_Backups.Length - 1], GetTimeStamp() ) );
}
if (Directory.Exists(saves))
Directory.Move(saves, FormatDirectory(root, m_Backups[m_Backups.Length - 1], GetTimeStamp()));
}
private static DirectoryInfo Match( string[] paths, string match )
{
for ( int i = 0; i < paths.Length; ++i )
{
DirectoryInfo info = new DirectoryInfo( paths[i] );
private static DirectoryInfo Match(string[] paths, string match)
{
for (int i = 0; i < paths.Length; ++i)
{
DirectoryInfo info = new DirectoryInfo(paths[i]);
if ( info.Name.StartsWith( match ) )
return info;
}
if (info.Name.StartsWith(match))
return info;
}
return null;
}
return null;
}
private static string FormatDirectory( string root, string name, string timeStamp )
{
return Path.Combine( root, $"{name} ({timeStamp})");
}
private static string FormatDirectory(string root, string name, string timeStamp)
{
return Path.Combine(root, $"{name} ({timeStamp})");
}
private static string FindTimeStamp( string input )
{
int start = input.IndexOf( '(' );
private static string FindTimeStamp(string input)
{
int start = input.IndexOf('(');
if ( start >= 0 )
{
int end = input.IndexOf( ')', ++start );
if (start >= 0)
{
int end = input.IndexOf(')', ++start);
if ( end >= start )
return input.Substring( start, end-start );
}
if (end >= start)
return input.Substring(start, end - start);
}
return null;
}
return null;
}
private static string GetTimeStamp()
{
DateTime now = DateTime.UtcNow;
private static string GetTimeStamp()
{
DateTime now = DateTime.UtcNow;
return $"{now.Day}-{now.Month}-{now.Year} {now.Hour}-{now.Minute:D2}-{now.Second:D2}";
}
}
return $"{now.Day}-{now.Month}-{now.Year} {now.Hour}-{now.Minute:D2}-{now.Second:D2}";
}
}
}