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

@ -67,7 +67,7 @@ namespace Server.Factions
set
{
CurrentState = value;
LastStateTime = DateTime.UtcNow;
LastStateTime = Core.Now;
}
}
@ -89,12 +89,7 @@ namespace Server.Factions
_ => PendingPeriod
};
var until = LastStateTime + period - DateTime.UtcNow;
if (until < TimeSpan.Zero)
{
until = TimeSpan.Zero;
}
var until = Utility.Max(LastStateTime + period - Core.Now, TimeSpan.Zero);
return until;
}
@ -108,7 +103,7 @@ namespace Server.Factions
_ => PendingPeriod
};
LastStateTime = DateTime.UtcNow - period + value;
LastStateTime = Core.Now - period + value;
}
}
@ -290,7 +285,7 @@ namespace Server.Factions
{
case ElectionState.Pending:
{
if (LastStateTime + PendingPeriod > DateTime.UtcNow)
if (LastStateTime + PendingPeriod > Core.Now)
{
break;
}
@ -304,7 +299,7 @@ namespace Server.Factions
}
case ElectionState.Campaign:
{
if (LastStateTime + CampaignPeriod > DateTime.UtcNow)
if (LastStateTime + CampaignPeriod > Core.Now)
{
break;
}
@ -346,7 +341,7 @@ namespace Server.Factions
}
case ElectionState.Election:
{
if (LastStateTime + VotingPeriod > DateTime.UtcNow)
if (LastStateTime + VotingPeriod > Core.Now)
{
break;
}
@ -413,7 +408,7 @@ namespace Server.Factions
Address = IPAddress.None;
}
Time = DateTime.UtcNow;
Time = Core.Now;
}
public Voter(IGenericReader reader, Mobile candidate)

View file

@ -235,7 +235,7 @@ namespace Server.Factions
}
else
{
recvState.LastHonorTime = DateTime.UtcNow;
recvState.LastHonorTime = Core.Now;
giveState.KillPoints -= 5;
recvState.KillPoints += 4;
@ -604,7 +604,7 @@ namespace Server.Factions
return false;
}
if (pl.Leaving + LeavePeriod >= DateTime.UtcNow)
if (pl.Leaving + LeavePeriod >= Core.Now)
{
return false;
}
@ -911,13 +911,13 @@ namespace Server.Factions
var sigil = sigils[i];
if (!sigil.IsBeingCorrupted && sigil.GraceStart != DateTime.MinValue &&
sigil.GraceStart + Sigil.CorruptionGrace < DateTime.UtcNow)
sigil.GraceStart + Sigil.CorruptionGrace < Core.Now)
{
if (sigil.LastMonolith is StrongholdMonolith &&
(sigil.Corrupted == null || sigil.LastMonolith.Faction != sigil.Corrupted))
{
sigil.Corrupting = sigil.LastMonolith.Faction;
sigil.CorruptionStart = DateTime.UtcNow;
sigil.CorruptionStart = Core.Now;
}
else
{
@ -930,21 +930,21 @@ namespace Server.Factions
if (sigil.LastMonolith?.Sigil == null)
{
if (sigil.LastStolen + Sigil.ReturnPeriod < DateTime.UtcNow)
if (sigil.LastStolen + Sigil.ReturnPeriod < Core.Now)
{
sigil.ReturnHome();
}
}
else
{
if (sigil.IsBeingCorrupted && sigil.CorruptionStart + Sigil.CorruptionPeriod < DateTime.UtcNow)
if (sigil.IsBeingCorrupted && sigil.CorruptionStart + Sigil.CorruptionPeriod < Core.Now)
{
sigil.Corrupted = sigil.Corrupting;
sigil.Corrupting = null;
sigil.CorruptionStart = DateTime.MinValue;
sigil.GraceStart = DateTime.MinValue;
}
else if (sigil.IsPurifying && sigil.PurificationStart + Sigil.PurificationPeriod < DateTime.UtcNow)
else if (sigil.IsPurifying && sigil.PurificationStart + Sigil.PurificationPeriod < Core.Now)
{
sigil.PurificationStart = DateTime.MinValue;
sigil.Corrupted = null;

View file

@ -49,13 +49,13 @@ namespace Server.Factions
return true;
}
return Expiration != DateTime.MinValue && DateTime.UtcNow >= Expiration;
return Expiration != DateTime.MinValue && Core.Now >= Expiration;
}
}
public void StartExpiration()
{
Expiration = DateTime.UtcNow + ExpirationPeriod;
Expiration = Core.Now + ExpirationPeriod;
}
public void CheckAttach()

View file

@ -65,7 +65,7 @@ namespace Server.Factions
if (version < 5)
{
LastAtrophy = DateTime.UtcNow;
LastAtrophy = Core.Now;
}
if (version < 4)
@ -161,7 +161,7 @@ namespace Server.Factions
{
for (var i = 0; i < m_LastBroadcasts.Length; ++i)
{
if (DateTime.UtcNow >= m_LastBroadcasts[i] + BroadcastPeriod)
if (Core.Now >= m_LastBroadcasts[i] + BroadcastPeriod)
{
return true;
}
@ -171,7 +171,7 @@ namespace Server.Factions
}
}
public bool IsAtrophyReady => DateTime.UtcNow >= LastAtrophy + TimeSpan.FromHours(47.0);
public bool IsAtrophyReady => Core.Now >= LastAtrophy + TimeSpan.FromHours(47.0);
public List<FactionItem> FactionItems { get; set; }
@ -217,13 +217,13 @@ namespace Server.Factions
public int CheckAtrophy()
{
if (DateTime.UtcNow < LastAtrophy + TimeSpan.FromHours(47.0))
if (Core.Now < LastAtrophy + TimeSpan.FromHours(47.0))
{
return 0;
}
var distrib = 0;
LastAtrophy = DateTime.UtcNow;
LastAtrophy = Core.Now;
var members = new List<PlayerState>(Members);
@ -252,9 +252,9 @@ namespace Server.Factions
{
for (var i = 0; i < m_LastBroadcasts.Length; ++i)
{
if (DateTime.UtcNow >= m_LastBroadcasts[i] + BroadcastPeriod)
if (Core.Now >= m_LastBroadcasts[i] + BroadcastPeriod)
{
m_LastBroadcasts[i] = DateTime.UtcNow;
m_LastBroadcasts[i] = Core.Now;
break;
}
}

View file

@ -121,7 +121,7 @@ namespace Server.Factions
break;
}
var remaining = pl.Leaving + Faction.LeavePeriod - DateTime.UtcNow;
var remaining = pl.Leaving + Faction.LeavePeriod - Core.Now;
if (remaining.TotalDays >= 1)
{

View file

@ -9,13 +9,13 @@ namespace Server.Factions
public SilverGivenEntry(Mobile givenTo)
{
GivenTo = givenTo;
TimeOfGift = DateTime.UtcNow;
TimeOfGift = Core.Now;
}
public Mobile GivenTo { get; }
public DateTime TimeOfGift { get; }
public bool IsExpired => TimeOfGift + ExpirePeriod < DateTime.UtcNow;
public bool IsExpired => TimeOfGift + ExpirePeriod < Core.Now;
}
}

View file

@ -71,7 +71,7 @@ namespace Server.Factions
set => m_State.LastTaxChange = value;
}
public bool TaxChangeReady => m_State.LastTaxChange + TaxChangePeriod < DateTime.UtcNow;
public bool TaxChangeReady => m_State.LastTaxChange + TaxChangePeriod < Core.Now;
public int FinanceUpkeep
{
@ -236,7 +236,7 @@ namespace Server.Factions
public void CheckIncome()
{
if (LastIncome + IncomePeriod > DateTime.UtcNow || Owner == null)
if (LastIncome + IncomePeriod > Core.Now || Owner == null)
{
return;
}
@ -246,7 +246,7 @@ namespace Server.Factions
public void ProcessIncome()
{
LastIncome = DateTime.UtcNow;
LastIncome = Core.Now;
var flow = NetCashFlow;
@ -454,7 +454,7 @@ namespace Server.Factions
if (m_State.Owner == null) // going from unowned to owned
{
LastIncome = DateTime.UtcNow;
LastIncome = Core.Now;
f.Silver += SilverCaptureBonus;
}
else if (f == null) // going from owned to unowned

View file

@ -28,7 +28,7 @@ namespace Server.Factions
{
case ElectionState.Pending:
{
var toGo = election.LastStateTime + Election.PendingPeriod - DateTime.UtcNow;
var toGo = election.LastStateTime + Election.PendingPeriod - Core.Now;
var days = (int)(toGo.TotalDays + 0.5);
AddHtmlLocalized(20, 40, 380, 20, 1038034); // A new election campaign is pending
@ -47,7 +47,7 @@ namespace Server.Factions
}
case ElectionState.Campaign:
{
var toGo = election.LastStateTime + Election.CampaignPeriod - DateTime.UtcNow;
var toGo = election.LastStateTime + Election.CampaignPeriod - Core.Now;
var days = (int)(toGo.TotalDays + 0.5);
AddHtmlLocalized(20, 40, 380, 20, 1018058); // There is an election campaign in progress.
@ -81,7 +81,7 @@ namespace Server.Factions
}
case ElectionState.Election:
{
var toGo = election.LastStateTime + Election.VotingPeriod - DateTime.UtcNow;
var toGo = election.LastStateTime + Election.VotingPeriod - Core.Now;
var days = (int)Math.Ceiling(toGo.TotalDays);
AddHtmlLocalized(20, 40, 380, 20, 1018060); // There is an election vote in progress.

View file

@ -209,7 +209,7 @@ namespace Server.Factions
if (m_From.AccessLevel == AccessLevel.Player && !m_Town.TaxChangeReady)
{
var remaining = DateTime.UtcNow - (m_Town.LastTaxChange + Town.TaxChangePeriod);
var remaining = Core.Now - (m_Town.LastTaxChange + Town.TaxChangePeriod);
if (remaining.TotalMinutes < 4)
{
@ -243,7 +243,7 @@ namespace Server.Factions
if (m_From.AccessLevel == AccessLevel.Player)
{
m_Town.LastTaxChange = DateTime.UtcNow;
m_Town.LastTaxChange = Core.Now;
}
}

View file

@ -56,7 +56,7 @@ namespace Server.Factions
if (pl != null)
{
pl.Leaving = DateTime.UtcNow;
pl.Leaving = Core.Now;
if (TimeSpan.FromDays(3.0) == Faction.LeavePeriod)
{
@ -87,7 +87,7 @@ namespace Server.Factions
if (pl != null)
{
pl.Leaving = DateTime.UtcNow;
pl.Leaving = Core.Now;
if (TimeSpan.FromDays(3.0) == Faction.LeavePeriod)
{

View file

@ -72,7 +72,7 @@ namespace Server
try
{
using var op = new StreamWriter("faction-power-items.log", true);
op.WriteLine("{0}\t{1}\t{2}\t{3}", DateTime.UtcNow, killer, victim, obj);
op.WriteLine("{0}\t{1}\t{2}\t{3}", Core.Now, killer, victim, obj);
}
catch
{

View file

@ -101,25 +101,10 @@ namespace Server.Factions
public bool IsCorrupting => m_Corrupting != null && m_Corrupting != m_Corrupted;
[CommandProperty(AccessLevel.GameMaster)]
public TimeSpan TimeUntilCorruption
{
get
{
if (!IsBeingCorrupted)
{
return TimeSpan.Zero;
}
var ts = CorruptionStart + CorruptionPeriod - DateTime.UtcNow;
if (ts < TimeSpan.Zero)
{
ts = TimeSpan.Zero;
}
return ts;
}
}
public TimeSpan TimeUntilCorruption =>
!IsBeingCorrupted ?
TimeSpan.Zero :
Utility.Max(CorruptionStart + CorruptionPeriod - Core.Now, TimeSpan.Zero);
public static List<Sigil> Sigils { get; } = new();
@ -263,7 +248,7 @@ namespace Server.Factions
private void BeginCorrupting(Faction faction)
{
m_Corrupting = faction;
CorruptionStart = DateTime.UtcNow;
CorruptionStart = Core.Now;
}
private void ClearCorrupting()
@ -340,7 +325,7 @@ namespace Server.Factions
BeginCorrupting(newController);
}
}
else if (GraceStart > DateTime.MinValue && GraceStart + CorruptionGrace < DateTime.UtcNow)
else if (GraceStart > DateTime.MinValue && GraceStart + CorruptionGrace < Core.Now)
{
if (m_Corrupted != newController)
{
@ -359,7 +344,7 @@ namespace Server.Factions
}
else if (GraceStart == DateTime.MinValue)
{
GraceStart = DateTime.UtcNow;
GraceStart = Core.Now;
}
PurificationStart = DateTime.MinValue;
@ -381,7 +366,7 @@ namespace Server.Factions
tm.Sigil = this;
m_Corrupting = null;
PurificationStart = DateTime.UtcNow;
PurificationStart = Core.Now;
CorruptionStart = DateTime.MinValue;
m_Town.Capture(m_Corrupted);

View file

@ -22,7 +22,7 @@ namespace Server.Factions
Visible = false;
Faction = f;
TimeOfPlacement = DateTime.UtcNow;
TimeOfPlacement = Core.Now;
Placer = m;
}
@ -204,7 +204,7 @@ namespace Server.Factions
return false;
}
if (TimeOfPlacement + decayPeriod < DateTime.UtcNow)
if (TimeOfPlacement + decayPeriod < Core.Now)
{
Timer.DelayCall(Delete);
return true;

View file

@ -225,17 +225,17 @@ namespace Server.Factions
else if (Town.FromRegion(Region) == m_Town)
{
Say(1042180); // Your orders, sire?
m_OrdersEnd = DateTime.UtcNow + TimeSpan.FromSeconds(10.0);
m_OrdersEnd = Core.Now + TimeSpan.FromSeconds(10.0);
}
}
else if (DateTime.UtcNow < m_OrdersEnd)
else if (Core.Now < m_OrdersEnd)
{
if (m_Town?.IsSheriff(from) != true || Town.FromRegion(Region) != m_Town)
{
return;
}
m_OrdersEnd = DateTime.UtcNow + TimeSpan.FromSeconds(10.0);
m_OrdersEnd = Core.Now + TimeSpan.FromSeconds(10.0);
var understood = true;
ReactionType newType = 0;

View file

@ -90,7 +90,7 @@ namespace Server.Factions
if (entry.Chance > Utility.Random(100))
{
releaseTime = DateTime.UtcNow + entry.Hold;
releaseTime = Core.Now + entry.Hold;
return entry.Spell.CreateInstance<Spell>(mob, null);
}
}
@ -133,7 +133,7 @@ namespace Server.Factions
return TimeSpan.MaxValue;
}
var ts = m_BandageStart + m_Bandage.Timer.Delay - DateTime.UtcNow;
var ts = m_BandageStart + m_Bandage.Timer.Delay - Core.Now;
if (ts < TimeSpan.FromSeconds(-1.0))
{
@ -141,12 +141,7 @@ namespace Server.Factions
return TimeSpan.MaxValue;
}
if (ts < TimeSpan.Zero)
{
ts = TimeSpan.Zero;
}
return ts;
return Utility.Max(ts, TimeSpan.Zero);
}
}
@ -187,7 +182,7 @@ namespace Server.Factions
}
m_Bandage = BandageContext.BeginHeal(m_Guard, m_Guard);
m_BandageStart = DateTime.UtcNow;
m_BandageStart = Core.Now;
return m_Bandage != null;
}
@ -487,10 +482,10 @@ namespace Server.Factions
if (m_Guard.Target != null && m_ReleaseTarget == DateTime.MinValue)
{
m_ReleaseTarget = DateTime.UtcNow + TimeSpan.FromSeconds(10.0);
m_ReleaseTarget = Core.Now + TimeSpan.FromSeconds(10.0);
}
if (m_Guard.Target != null && DateTime.UtcNow > m_ReleaseTarget)
if (m_Guard.Target != null && Core.Now > m_ReleaseTarget)
{
var targ = m_Guard.Target;