Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed .Count and used methods inside DynamicUpdateField to loop over #591

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 69 additions & 13 deletions WowPacketParser/Misc/DynamicUpdateField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using WowPacketParser.Store.Objects.UpdateFields.LegacyImplementation;

namespace WowPacketParser.Misc
{
Expand All @@ -10,25 +11,72 @@ public class DynamicUpdateField<T>
private readonly List<T> _values = new List<T>();
public BitArray UpdateMask { get; private set; } = new BitArray(0);

public int Count => _values.Count;
public delegate void ActionUpdater(int idx, ref T value);

public T this[int index]
public void ReadAll(Action<T> action)
{
get { return _values[index]; }
set { _values[index] = value; }
lock (_values)
{
for (int i = 0; i < _values.Count; ++i)
{
action(_values[i]);
}
}
}

public void UpdateAllByUpdateMask(Func<int, T, T> updater)
{
lock (_values)
{
for (int i = 0; i < _values.Count; ++i)
{
if (UpdateMask[i])
{
_values[i] = updater(i, _values[i]);
}
}
}
}

public void FillAllByUpdateMask(Func<int, T> updater)
{
lock (_values)
{
for (int i = 0; i < _values.Count; ++i)
{
if (UpdateMask[i])
{
_values[i] = updater(i);
}
}
}
}

public void FillAll(Func<int, T> reader)
{
lock (_values)
{
for (int i = 0; i < _values.Count; ++i)
{
_values[i] = reader(i);
}
}
}

public void Resize(uint newSize)
{
int newCount = (int)newSize; // this is stupid but packets have unsigned values in them and this method is intended to be used in packet readers
int current = _values.Count;
if (newCount < current)
_values.RemoveRange(newCount, current - newCount);
else if (newCount > current)
lock (_values)
{
if (newCount > _values.Capacity)
_values.Capacity = newCount;
_values.AddRange(Enumerable.Repeat(default(T), newCount - current));
int newCount = (int)newSize; // this is stupid but packets have unsigned values in them and this method is intended to be used in packet readers
int current = _values.Count;
if (newCount < current)
_values.RemoveRange(newCount, current - newCount);
else if (newCount > current)
{
if (newCount > _values.Capacity)
_values.Capacity = newCount;
_values.AddRange(Enumerable.Repeat(default(T), newCount - current));
}
}
}

Expand All @@ -51,7 +99,15 @@ public void ReadUpdateMask(Packet packet, int bitSizeCount = 32)
if ((newSize % 32) != 0)
rawMask[newSize / 32] = (int)packet.ReadBits((int)newSize % 32);

UpdateMask = new BitArray(rawMask);
lock (_values)
{
UpdateMask = new BitArray(rawMask);
}
}

public void SetUnsafe(int idx, T value)
{
_values[idx] = value;
}
}
}
14 changes: 7 additions & 7 deletions WowPacketParser/Store/Objects/ConversationTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,26 @@ public override void LoadValuesFromUpdateFields()

var actorTemplates = new List<ConversationActorTemplate>();
var actors = ConversationData.Actors;
for (var i = 0; i < actors.Count; ++i)
actors.ReadAll((item) =>
{
var actor = new ConversationActorTemplate
{
Type = actors[i].Type
Type = item.Type
};

if (actor.Type == (uint)ActorType.WorldObjectActor)
actor.Guid = actors[i].ActorGUID;
actor.Guid = item.ActorGUID;
else if (actor.Type == (uint)ActorType.CreatureActor)
{
actor.Id = actors[i].Id;
actor.CreatureId = actors[i].CreatureID;
actor.CreatureModelId = actors[i].CreatureDisplayInfoID;
actor.Id = item.Id;
actor.CreatureId = item.CreatureID;
actor.CreatureModelId = item.CreatureDisplayInfoID;

Storage.ConversationActorTemplates.Add(actor);
}

actorTemplates.Add(actor);
}
});

var lines = ConversationData.Lines;
for (var i = 0; i < lines.Length; ++i)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public DynamicUpdateField<IConversationActor> Actors
}
}

field[i / ActorSize] = actor;
field.SetUnsafe(i / ActorSize, actor);
}

return field;
Expand Down
Loading