ModernUO/Projects/UOContent/Engines/Ethics/Core/Player.cs
Kamron Batman 4ddb3de026
fix(core): Fixes several serialization issues (#355)
- [X] Fixes an issue where a buffer smaller than 8 bytes would not double with enough space in some cases.
- [X] Fixes an issue with dupe copying the savebuffer reference (ugh).
- [X] Streamlines the IGenericWriter API to use better generics.
- [X] Streamlines the IGenericReader API to use better generics.
- [X] Forces `tidying` of a List/HashSet to be done externally since Writers/Readers should not have side effects.
- [X] Fixes an issue where Tidying a list didn't TrimExcess, causing memory leaks.
- [X] Reverted the meaning of `World.Running` to specifically refer to any world state post world loading.
  - NOTE: Do not use this if you want to block on world saves. Instead use checks against `WorldState.Saving` states.
- [X] Fixes an issue with serializing negative DateTime deltas.
- [X] Fixes a potential issue with serializing non-UTC DateTime.

Bumps release version
2020-12-23 07:11:41 -08:00

175 lines
4.1 KiB
C#

using System;
using System.Collections.ObjectModel;
using Server.Mobiles;
namespace Server.Ethics
{
public class PlayerCollection : Collection<Player>
{
}
[PropertyObject]
public class Player
{
private DateTime m_Shield;
public Player(Ethic ethic, Mobile mobile)
{
Ethic = ethic;
Mobile = mobile;
Power = 5;
History = 5;
}
public Player(Ethic ethic, IGenericReader reader)
{
Ethic = ethic;
var version = reader.ReadEncodedInt();
switch (version)
{
case 0:
{
Mobile = reader.ReadEntity<Mobile>();
Power = reader.ReadEncodedInt();
History = reader.ReadEncodedInt();
Steed = reader.ReadEntity<Mobile>();
Familiar = reader.ReadEntity<Mobile>();
m_Shield = reader.ReadDeltaTime();
break;
}
}
}
public Ethic Ethic { get; }
public Mobile Mobile { get; }
[CommandProperty(AccessLevel.GameMaster, AccessLevel.Administrator)]
public int Power { get; set; }
[CommandProperty(AccessLevel.GameMaster, AccessLevel.Administrator)]
public int History { get; set; }
[CommandProperty(AccessLevel.GameMaster, AccessLevel.Administrator)]
public Mobile Steed { get; set; }
[CommandProperty(AccessLevel.GameMaster, AccessLevel.Administrator)]
public Mobile Familiar { get; set; }
[CommandProperty(AccessLevel.GameMaster)]
public bool IsShielded
{
get
{
if (m_Shield == DateTime.MinValue)
{
return false;
}
if (DateTime.UtcNow < m_Shield + TimeSpan.FromHours(1.0))
{
return true;
}
FinishShield();
return false;
}
}
public static Player Find(Mobile mob) => Find(mob, false);
public static Player Find(Mobile mob, bool inherit)
{
var pm = mob as PlayerMobile;
if (pm == null)
{
if (inherit && mob is BaseCreature bc)
{
if (bc.Controlled)
{
pm = bc.ControlMaster as PlayerMobile;
}
else if (bc.Summoned)
{
pm = bc.SummonMaster as PlayerMobile;
}
}
if (pm == null)
{
return null;
}
}
var pl = pm.EthicPlayer;
if (pl?.Ethic.IsEligible(pl.Mobile) == false)
{
pm.EthicPlayer = pl = null;
}
return pl;
}
public void BeginShield()
{
m_Shield = DateTime.UtcNow;
}
public void FinishShield()
{
m_Shield = DateTime.MinValue;
}
public void CheckAttach()
{
if (Ethic.IsEligible(Mobile))
{
Attach();
}
}
public void Attach()
{
if (Mobile is PlayerMobile mobile)
{
mobile.EthicPlayer = this;
}
Ethic.Players.Add(this);
}
public void Detach()
{
if (Mobile is PlayerMobile mobile)
{
mobile.EthicPlayer = null;
}
Ethic.Players.Remove(this);
}
public void Serialize(IGenericWriter writer)
{
writer.WriteEncodedInt(0); // version
writer.Write(Mobile);
writer.WriteEncodedInt(Power);
writer.WriteEncodedInt(History);
writer.Write(Steed);
writer.Write(Familiar);
writer.WriteDeltaTime(m_Shield);
}
}
}