fix(core): Caches DateTime.NowUtc (#548)

- [X] Caches DateTime.NowUtc on the game loop (not other threads)
- [X] Replaces all locations where it makes sense
- [X] Adds Min/Max for `IComparable` (TimeSpan, DateTimes, etc)

Closes #261
This commit is contained in:
Kamron Batman 2021-03-13 01:32:04 -08:00 • committed by GitHub
parent 5de6edbeb8
commit 6c4308bce6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
208 changed files with 660 additions and 793 deletions

View file

@ -289,39 +289,19 @@ namespace Server.Mobiles
[CommandProperty(AccessLevel.GameMaster)]
public TimeSpan SavagePaintExpiration
{
get
{
var ts = m_SavagePaintExpiration - DateTime.UtcNow;
if (ts < TimeSpan.Zero)
{
ts = TimeSpan.Zero;
}
return ts;
}
set => m_SavagePaintExpiration = DateTime.UtcNow + value;
get => Utility.Max(m_SavagePaintExpiration - Core.Now, TimeSpan.Zero);
set => m_SavagePaintExpiration = Core.Now + value;
}
[CommandProperty(AccessLevel.GameMaster)]
public TimeSpan NextSmithBulkOrder
{
get
{
var ts = m_NextSmithBulkOrder - DateTime.UtcNow;
if (ts < TimeSpan.Zero)
{
ts = TimeSpan.Zero;
}
return ts;
}
get => Utility.Max(m_NextSmithBulkOrder - Core.Now, TimeSpan.Zero);
set
{
try
{
m_NextSmithBulkOrder = DateTime.UtcNow + value;
m_NextSmithBulkOrder = Core.Now + value;
}
catch
{
@ -333,22 +313,12 @@ namespace Server.Mobiles
[CommandProperty(AccessLevel.GameMaster)]
public TimeSpan NextTailorBulkOrder
{
get
{
var ts = m_NextTailorBulkOrder - DateTime.UtcNow;
if (ts < TimeSpan.Zero)
{
ts = TimeSpan.Zero;
}
return ts;
}
get => Utility.Max(m_NextTailorBulkOrder - Core.Now, TimeSpan.Zero);
set
{
try
{
m_NextTailorBulkOrder = DateTime.UtcNow + value;
m_NextTailorBulkOrder = Core.Now + value;
}
catch
{
@ -381,7 +351,7 @@ namespace Server.Mobiles
{
if (NetState != null)
{
return m_GameTime + (DateTime.UtcNow - SessionStart);
return m_GameTime + (Core.Now - SessionStart);
}
return m_GameTime;
@ -1140,7 +1110,7 @@ namespace Server.Mobiles
}
}
if (m_MountBlock?.m_Timer.Running != true || m_MountBlock.m_Timer.Next < DateTime.UtcNow + duration)
if (m_MountBlock?.m_Timer.Running != true || m_MountBlock.m_Timer.Next < Core.Now + duration)
{
m_MountBlock = new MountBlock(duration, type, this);
}
@ -1568,12 +1538,12 @@ namespace Server.Mobiles
{
if (m is PlayerMobile pm)
{
pm.SessionStart = DateTime.UtcNow;
pm.SessionStart = Core.Now;
pm.Quest?.StartTimer();
pm.BedrollLogout = false;
pm.LastOnline = DateTime.UtcNow;
pm.LastOnline = Core.Now;
}
DisguiseTimers.StartTimer(m);
@ -1615,13 +1585,13 @@ namespace Server.Mobiles
if (from is PlayerMobile pm)
{
pm.m_GameTime += DateTime.UtcNow - pm.SessionStart;
pm.m_GameTime += Core.Now - pm.SessionStart;
pm.Quest?.StopTimer();
pm.SpeechLog = null;
pm.ClearQuestArrow();
pm.LastOnline = DateTime.UtcNow;
pm.LastOnline = Core.Now;
}
DisguiseTimers.StopTimer(from);
@ -2661,7 +2631,7 @@ namespace Server.Mobiles
}
}
if (Kills >= 5 && DateTime.UtcNow >= m_NextJustAward)
if (Kills >= 5 && Core.Now >= m_NextJustAward)
{
var m = FindMostRecentDamager(false);
@ -2694,7 +2664,7 @@ namespace Server.Mobiles
m.FixedParticles(0x375A, 9, 20, 5027, EffectLayer.Waist);
m.PlaySound(0x1F7);
m_NextJustAward = DateTime.UtcNow + TimeSpan.FromMinutes(pointsToGain / 3.0);
m_NextJustAward = Core.Now + TimeSpan.FromMinutes(pointsToGain / 3.0);
}
}
}
@ -2949,7 +2919,7 @@ namespace Server.Mobiles
if (tbl.TryGetValue(obj, out var count))
{
if (count.TimeStamp + SkillCheck.AntiMacroExpire <= DateTime.UtcNow)
if (count.TimeStamp + SkillCheck.AntiMacroExpire <= Core.Now)
{
count.Count = 1;
return true;
@ -3292,7 +3262,7 @@ namespace Server.Mobiles
foreach (var (k, v) in t)
{
if (v.TimeStamp + SkillCheck.AntiMacroExpire <= DateTime.UtcNow)
if (v.TimeStamp + SkillCheck.AntiMacroExpire <= Core.Now)
{
toRemove.Add(k);
}
@ -4132,7 +4102,7 @@ namespace Server.Mobiles
for (var i = 0; i < m_StuckMenuUses.Length; ++i)
{
if (DateTime.UtcNow - m_StuckMenuUses[i] > TimeSpan.FromDays(1.0))
if (Core.Now - m_StuckMenuUses[i] > TimeSpan.FromDays(1.0))
{
return true;
}
@ -4150,9 +4120,9 @@ namespace Server.Mobiles
for (var i = 0; i < m_StuckMenuUses.Length; ++i)
{
if (DateTime.UtcNow - m_StuckMenuUses[i] > TimeSpan.FromDays(1.0))
if (Core.Now - m_StuckMenuUses[i] > TimeSpan.FromDays(1.0))
{
m_StuckMenuUses[i] = DateTime.UtcNow;
m_StuckMenuUses[i] = Core.Now;
return;
}
}
@ -4428,9 +4398,9 @@ namespace Server.Mobiles
return false;
}
if (DateTime.UtcNow - m_LastYoungMessage > TimeSpan.FromMinutes(1.0))
if (Core.Now - m_LastYoungMessage > TimeSpan.FromMinutes(1.0))
{
m_LastYoungMessage = DateTime.UtcNow;
m_LastYoungMessage = Core.Now;
// A monster looks at you menacingly but does not attack.
// You would be under attack now if not for your status as a new citizen of Britannia.
SendLocalizedMessage(1019067);
@ -4441,9 +4411,9 @@ namespace Server.Mobiles
public bool CheckYoungHealTime()
{
if (DateTime.UtcNow - m_LastYoungHeal > TimeSpan.FromMinutes(5.0))
if (Core.Now - m_LastYoungHeal > TimeSpan.FromMinutes(5.0))
{
m_LastYoungHeal = DateTime.UtcNow;
m_LastYoungHeal = Core.Now;
return true;
}
@ -4658,7 +4628,7 @@ namespace Server.Mobiles
set
{
m_Count = value;
TimeStamp = DateTime.UtcNow;
TimeStamp = Core.Now;
}
}
}