Skip to content

Commit

Permalink
fix: Fixes critical serial dupe bug and deserialization/serialization…
Browse files Browse the repository at this point in the history
… issues. (#1245)
  • Loading branch information
kamronbatman authored Nov 13, 2022
1 parent e66efef commit 6d00b2c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 16 deletions.
10 changes: 5 additions & 5 deletions Projects/Server/Items/Containers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,6 @@ public override void Deserialize(IGenericReader reader)
Owner = reader.ReadEntity<Mobile>();
Opened = reader.ReadBool();

if (Owner == null)
{
Delete();
}

break;
}
}
Expand All @@ -81,6 +76,11 @@ public override void Deserialize(IGenericReader reader)
{
ItemID = 0xE7C;
}

if (Owner == null)
{
Timer.DelayCall(Delete);
}
}

public void Close()
Expand Down
6 changes: 3 additions & 3 deletions Projects/Server/Items/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2680,7 +2680,7 @@ public virtual void Deserialize(IGenericReader reader)

if (m_Parent == null && (parent.IsMobile || parent.IsItem))
{
Delete();
Timer.DelayCall(Delete);
}
}

Expand Down Expand Up @@ -2841,7 +2841,7 @@ public virtual void Deserialize(IGenericReader reader)

if (m_Parent == null && (parent.IsMobile || parent.IsItem))
{
Delete();
Timer.DelayCall(Delete);
}
}

Expand Down Expand Up @@ -2959,7 +2959,7 @@ public virtual void Deserialize(IGenericReader reader)

if (m_Parent == null && (parent.IsMobile || parent.IsItem))
{
Delete();
Timer.DelayCall(Delete);
}

var count = reader.ReadInt();
Expand Down
51 changes: 43 additions & 8 deletions Projects/Server/World/World.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ public static Serial NewMobile
{
get
{
#if THREADGUARD
if (Thread.CurrentThread != Core.Thread)
{
logger.Error(
"Attempted to get a new mobile serial from the wrong thread!\n{StackTrace}",
new StackTrace()
);
}
#endif
var last = _lastMobile;
var maxMobile = (Serial)MaxMobileSerial;

Expand All @@ -75,7 +84,7 @@ public static Serial NewMobile
last = (Serial)1;
}

if (FindMobile(last) == null)
if (FindMobile(last, true) == null)
{
return _lastMobile = last;
}
Expand All @@ -90,6 +99,15 @@ public static Serial NewItem
{
get
{
#if THREADGUARD
if (Thread.CurrentThread != Core.Thread)
{
logger.Error(
"Attempted to get a new item serial from the wrong thread!\n{StackTrace}",
new StackTrace()
);
}
#endif
var last = _lastItem;

for (int i = 0; i < _maxItems; i++)
Expand All @@ -101,7 +119,7 @@ public static Serial NewItem
last = (Serial)ItemOffset;
}

if (FindItem(last) == null)
if (FindItem(last, true) == null)
{
return _lastItem = last;
}
Expand Down Expand Up @@ -317,15 +335,19 @@ private static void ProcessSafetyQueues()
AddEntity(entity);
}

_pendingAdd.Clear();

foreach (var entity in _pendingDelete.Values)
{
if (_pendingAdd.ContainsKey(entity.Serial))
{
logger.Warning("Entity {Entity} was both pending both deletion and addition after save", entity);
logger.Warning("Entity {Entity} was both pending deletion and addition after save", entity);
}

RemoveEntity(entity);
}

_pendingDelete.Clear();
}

private static void AppendSafetyLog(string action, ISerializable entity)
Expand Down Expand Up @@ -597,17 +619,30 @@ public static T FindEntity<T>(Serial serial, bool returnDeleted = false) where T
case WorldState.Saving:
case WorldState.WritingSave:
{
if (_pendingDelete.TryGetValue(serial, out var entity))
if (returnDeleted && _pendingDelete.TryGetValue(serial, out var entity))
{
return !returnDeleted ? null : entity as T;
return entity as T;
}

if (_pendingAdd.TryGetValue(serial, out entity))
if (!_pendingAdd.TryGetValue(serial, out entity))
{
return entity as T;
if (serial.IsItem)
{
if (Items.TryGetValue(serial, out var item))
{
entity = item;
}
}
else // if (serial.IsMobile)
{
if (Mobiles.TryGetValue(serial, out var mob))
{
entity = mob;
}
}
}

goto case WorldState.Running;
return entity?.Deleted == false || returnDeleted ? entity as T : null;
}
case WorldState.Running:
{
Expand Down

0 comments on commit 6d00b2c

Please sign in to comment.