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

@ -51,7 +51,7 @@ namespace Server.Misc
{
var info = list[i];
if (info.Attacker == m2 && DateTime.UtcNow < info.LastCombatTime + Delay)
if (info.Attacker == m2 && Core.Now < info.LastCombatTime + Delay)
{
return true;
}
@ -63,7 +63,7 @@ namespace Server.Misc
{
var info = list[i];
if (info.Attacker == m1 && DateTime.UtcNow < info.LastCombatTime + Delay)
if (info.Attacker == m1 && Core.Now < info.LastCombatTime + Delay)
{
return true;
}

View file

@ -29,7 +29,7 @@ namespace Server
: this(iconID, titleCliloc, secondaryCliloc)
{
TimeLength = length;
TimeStart = DateTime.UtcNow;
TimeStart = Core.TickCount;
Timer = Timer.DelayCall(length, RemoveBuff, m, this);
}
@ -101,7 +101,7 @@ namespace Server
public TimeSpan TimeLength { get; }
public DateTime TimeStart { get; }
public long TimeStart { get; }
public Timer Timer { get; }
@ -152,12 +152,12 @@ namespace Server
TitleCliloc,
SecondaryCliloc,
Args,
TimeStart != DateTime.MinValue ? TimeStart + TimeLength - DateTime.UtcNow : TimeSpan.Zero
TimeStart == 0 ? 0 : Math.Max(TimeStart + (long)TimeLength.TotalMilliseconds - Core.TickCount, 0)
);
public static void SendAddBuffPacket(
NetState ns, Serial mob, BuffIcon iconID, int titleCliloc, int secondaryCliloc, TextDefinition args,
TimeSpan ts
long ticks
)
{
if (ns == null)
@ -178,7 +178,7 @@ namespace Server
writer.Write((short)iconID);
writer.Write((short)0x1); // command (0 = remove, 1 = add, 2 = data)
writer.Write(0);
writer.Write((short)(ts <= TimeSpan.Zero ? 0 : ts.TotalSeconds));
writer.Write((short)(ticks / 1000));
writer.Clear(3);
writer.Write(titleCliloc);
writer.Write(secondaryCliloc);

View file

@ -86,7 +86,7 @@ namespace Server.Misc
if (Required != null && version < Required && (m_OldClientResponse == OldClientResponse.Kick ||
m_OldClientResponse == OldClientResponse.LenientKick &&
DateTime.UtcNow - state.Mobile.CreationTime > m_AgeLeniency &&
Core.Now - state.Mobile.CreationTime > m_AgeLeniency &&
state.Mobile is PlayerMobile mobile &&
mobile.GameTime > m_GameTimeLeniency))
{

View file

@ -24,7 +24,7 @@ namespace Server.Misc
}
var sender = entry.Sender;
var time = DateTime.UtcNow;
var time = Core.Now;
var message = new MimeMessage();
message.From.Add(EmailConfiguration.FromAddress);
@ -107,7 +107,7 @@ namespace Server.Misc
return;
}
var now = DateTime.UtcNow;
var now = Core.Now;
var messageID = $"<{now:yyyyMMdd}.{now:HHmmssff}@{EmailConfiguration.EmailServer}>";
message.Headers.Add("Message-ID", messageID);
message.From.Add(EmailConfiguration.FromAddress);

View file

@ -500,7 +500,7 @@ namespace Server.Guilds
return WarStatus.Win;
}
if (WarLength != TimeSpan.Zero && WarBeginning + WarLength < DateTime.UtcNow)
if (WarLength != TimeSpan.Zero && WarBeginning + WarLength < Core.Now)
{
if (Kills > w.Kills)
{
@ -598,7 +598,7 @@ namespace Server.Guilds
Candidates = new List<Mobile>();
Accepted = new List<Mobile>();
LastFealty = DateTime.UtcNow;
LastFealty = Core.Now;
m_Name = name;
m_Abbreviation = abbreviation;
@ -766,7 +766,7 @@ namespace Server.Guilds
if (m_Type != value)
{
m_Type = value;
TypeLastChange = DateTime.UtcNow;
TypeLastChange = Core.Now;
InvalidateMemberProperties();
}
@ -1157,7 +1157,7 @@ namespace Server.Guilds
public override void Serialize(IGenericWriter writer)
{
if (LastFealty + TimeSpan.FromDays(1.0) < DateTime.UtcNow)
if (LastFealty + TimeSpan.FromDays(1.0) < Core.Now)
{
CalculateGuildmaster();
}
@ -1547,7 +1547,7 @@ namespace Server.Guilds
m?.Deleted == false && m.Guild == this;
public bool CanBeVotedFor(Mobile m) =>
(!NewGuildSystem || m is PlayerMobile pm && pm.LastOnline + InactiveTime >= DateTime.UtcNow) &&
(!NewGuildSystem || m is PlayerMobile pm && pm.LastOnline + InactiveTime >= Core.Now) &&
m?.Deleted == false && m.Guild == this;
public void CalculateGuildmaster()
@ -1615,7 +1615,7 @@ namespace Server.Guilds
}
Leader = winner;
LastFealty = DateTime.UtcNow;
LastFealty = Core.Now;
}
private class GuildPropsTarget : Target

View file

@ -172,7 +172,7 @@ namespace Server
PartialInstalled = reader.ReadByte(),
Language = reader.ReadLittleUniSafe(4),
Unknown = reader.ReadAsciiSafe(64),
TimeReceived = DateTime.UtcNow
TimeReceived = Core.Now
};
if (state.Account is Account acct)

View file

@ -342,7 +342,7 @@ namespace Server.Misc
actual = Notoriety.Murderer;
}
if (DateTime.UtcNow >= target.TimeOfDeath + Corpse.MonsterLootRightSacrifice)
if (Core.Now >= target.TimeOfDeath + Corpse.MonsterLootRightSacrifice)
{
return actual;
}

View file

@ -70,7 +70,7 @@ namespace Server.Misc
return "";
}
var ts = DateTime.UtcNow - a.Created;
var ts = Core.Now - a.Created;
if (Format(ts.TotalDays, "This account is {0} day{1} old.", out var v))
{

View file

@ -47,32 +47,10 @@ namespace Server.Misc
public DateTime StartTime { get; set; }
[CommandProperty(AccessLevel.GameMaster, AccessLevel.Administrator)]
public TimeSpan TimeRemaining
{
get
{
if (StartTime == DateTime.MinValue || !m_Active)
{
return TimeSpan.Zero;
}
try
{
var ts = StartTime + Duration - DateTime.UtcNow;
if (ts < TimeSpan.Zero)
{
return TimeSpan.Zero;
}
return ts;
}
catch
{
return TimeSpan.Zero;
}
}
}
public TimeSpan TimeRemaining =>
StartTime == DateTime.MinValue || !m_Active
? TimeSpan.Zero
: Utility.Max(StartTime + Duration - Core.Now, TimeSpan.Zero);
[CommandProperty(AccessLevel.GameMaster, AccessLevel.Administrator)]
public bool Active
@ -89,7 +67,7 @@ namespace Server.Misc
if (m_Active)
{
StartTime = DateTime.UtcNow;
StartTime = Core.Now;
m_ActivePollers.Add(this);
}
else

View file

@ -293,7 +293,7 @@ namespace Server.Misc
}
if (from is PlayerMobile pm && skill.SkillName == pm.AcceleratedSkill &&
pm.AcceleratedStart > DateTime.UtcNow)
pm.AcceleratedStart > Core.Now)
{
toGain *= Utility.RandomMinMax(2, 5);
}
@ -433,51 +433,51 @@ namespace Server.Misc
{
if (from is BaseCreature creature && creature.Controlled)
{
if (creature.LastStrGain + m_PetStatGainDelay >= DateTime.UtcNow)
if (creature.LastStrGain + m_PetStatGainDelay >= Core.Now)
{
return;
}
}
else if (from.LastStrGain + m_StatGainDelay >= DateTime.UtcNow)
else if (from.LastStrGain + m_StatGainDelay >= Core.Now)
{
return;
}
from.LastStrGain = DateTime.UtcNow;
from.LastStrGain = Core.Now;
break;
}
case Stat.Dex:
{
if (from is BaseCreature creature && creature.Controlled)
{
if (creature.LastDexGain + m_PetStatGainDelay >= DateTime.UtcNow)
if (creature.LastDexGain + m_PetStatGainDelay >= Core.Now)
{
return;
}
}
else if (from.LastDexGain + m_StatGainDelay >= DateTime.UtcNow)
else if (from.LastDexGain + m_StatGainDelay >= Core.Now)
{
return;
}
from.LastDexGain = DateTime.UtcNow;
from.LastDexGain = Core.Now;
break;
}
case Stat.Int:
{
if (from is BaseCreature creature && creature.Controlled)
{
if (creature.LastIntGain + m_PetStatGainDelay >= DateTime.UtcNow)
if (creature.LastIntGain + m_PetStatGainDelay >= Core.Now)
{
return;
}
}
else if (from.LastIntGain + m_StatGainDelay >= DateTime.UtcNow)
else if (from.LastIntGain + m_StatGainDelay >= Core.Now)
{
return;
}
from.LastIntGain = DateTime.UtcNow;
from.LastIntGain = Core.Now;
break;
}
}