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

@ -29,7 +29,7 @@ namespace Server.Items
m_Entries = new List<CampfireEntry>();
Created = DateTime.UtcNow;
Created = Core.Now;
m_Timer = Timer.DelayCall(TimeSpan.FromSeconds(1.0), TimeSpan.FromSeconds(1.0), OnTick);
}
@ -94,7 +94,7 @@ namespace Server.Items
private void OnTick()
{
var now = DateTime.UtcNow;
var now = Core.Now;
var age = now - Created;
if (age >= TimeSpan.FromSeconds(100.0))
@ -191,7 +191,7 @@ namespace Server.Items
{
Player = player;
Fire = fire;
Start = DateTime.UtcNow;
Start = Core.Now;
m_Safe = false;
}

View file

@ -113,9 +113,9 @@ namespace Server.Items
public static int GetDelay(Mobile m)
{
if (m_Delay.TryGetValue(m, out var timer) && timer.Next > DateTime.UtcNow)
if (m_Delay.TryGetValue(m, out var timer) && timer.Next > Core.Now)
{
return (int)(timer.Next - DateTime.UtcNow).TotalSeconds;
return (int)(timer.Next - Core.Now).TotalSeconds;
}
return 0;
@ -185,7 +185,7 @@ namespace Server.Items
MoveToWorld(loc, map);
From = from;
m_End = DateTime.UtcNow + TimeSpan.FromSeconds(10);
m_End = Core.Now + TimeSpan.FromSeconds(10);
SetDamage(min, max);
@ -292,7 +292,7 @@ namespace Server.Items
return;
}
if (DateTime.UtcNow > m_End)
if (Core.Now > m_End)
{
m_Item.Delete();
Stop();

View file

@ -104,7 +104,7 @@ namespace Server.Items
continue;
}
mon.Pacify(from, DateTime.UtcNow + TimeSpan.FromSeconds(5.0)); // TODO check
mon.Pacify(from, Core.Now + TimeSpan.FromSeconds(5.0)); // TODO check
}
}
}
@ -131,9 +131,9 @@ namespace Server.Items
public static int GetDelay(Mobile m)
{
if (m_Delay.TryGetValue(m, out var timer) && timer.Next > DateTime.UtcNow)
if (m_Delay.TryGetValue(m, out var timer) && timer.Next > Core.Now)
{
return (int)(timer.Next - DateTime.UtcNow).TotalSeconds;
return (int)(timer.Next - Core.Now).TotalSeconds;
}
return 0;

View file

@ -342,7 +342,7 @@ namespace Server.Items
return;
}
if (DateTime.UtcNow < NextUse)
if (Core.Now < NextUse)
{
from.SendLocalizedMessage(502406); // This book needs time to recharge.
return;
@ -359,7 +359,7 @@ namespace Server.Items
{
if (!Core.SA)
{
NextUse = DateTime.UtcNow + UseDelay;
NextUse = Core.Now + UseDelay;
}
}

View file

@ -108,7 +108,7 @@ namespace Server.Items
{
if (value != m_ReplenishesCharges && value)
{
m_LastReplenished = DateTime.UtcNow;
m_LastReplenished = Core.Now;
}
m_ReplenishesCharges = value;
@ -159,15 +159,15 @@ namespace Server.Items
return;
}
if (m_LastReplenished + ChargeReplenishRate < DateTime.UtcNow)
if (m_LastReplenished + ChargeReplenishRate < Core.Now)
{
var timeDifference = DateTime.UtcNow - m_LastReplenished;
var timeDifference = Core.Now - m_LastReplenished;
m_UsesRemaining = Math.Min(
m_UsesRemaining + (int)(timeDifference.Ticks / ChargeReplenishRate.Ticks),
InitMaxUses
); // How rude of TimeSpan to not allow timespan division.
m_LastReplenished = DateTime.UtcNow;
m_LastReplenished = Core.Now;
if (invalidate)
{

View file

@ -315,13 +315,7 @@ namespace Server.Items
return;
}
var ts = t.Next - DateTime.UtcNow;
if (ts < TimeSpan.Zero)
{
ts = TimeSpan.Zero;
}
t.Delay = ts;
t.Delay = Utility.Max(t.Next - Core.Now, TimeSpan.Zero);
t.Stop();
}
@ -334,7 +328,7 @@ namespace Server.Items
}
public static TimeSpan TimeRemaining(Mobile m) =>
Timers.TryGetValue(m, out var t) ? t.Next - DateTime.UtcNow : TimeSpan.Zero;
Timers.TryGetValue(m, out var t) ? t.Next - Core.Now : TimeSpan.Zero;
private class InternalTimer : Timer
{

View file

@ -39,7 +39,7 @@ namespace Server.Items
var m = entry.Key;
writer.Write(m);
writer.Write(entry.Value.Next - DateTime.UtcNow);
writer.Write(entry.Value.Next - Core.Now);
writer.Write(m.NameMod);
}
}

View file

@ -33,7 +33,7 @@ namespace Server.Items
public static void Initialize()
{
ServerStart = DateTime.UtcNow;
ServerStart = Core.Now;
}
public static MoonPhase GetMoonPhase(Map map, int x, int y)
@ -55,7 +55,7 @@ namespace Server.Items
public static void GetTime(Map map, int x, int y, out int hours, out int minutes, out int totalMinutes)
{
var timeSpan = DateTime.UtcNow - WorldStart;
var timeSpan = Core.Now - WorldStart;
totalMinutes = (int)(timeSpan.TotalSeconds / SecondsPerUOMinute);