-
Notifications
You must be signed in to change notification settings - Fork 16
/
CharacterDisplayPacketTranslator.cs
73 lines (57 loc) · 2.54 KB
/
CharacterDisplayPacketTranslator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System.Collections.Generic;
using System.IO;
using EOLib.Domain.Character;
using EOLib.IO.Extensions;
using EOLib.IO.Repositories;
namespace EOLib.Net.Translators
{
public abstract class CharacterDisplayPacketTranslator<T> : IPacketTranslator<T>
where T : ITranslatedData
{
private readonly IEIFFileProvider _eifFileProvider;
protected CharacterDisplayPacketTranslator(IEIFFileProvider eifFileProvider)
{
_eifFileProvider = eifFileProvider;
}
public abstract T TranslatePacket(IPacket packet);
protected IEnumerable<ICharacter> GetCharacters(IPacket packet)
{
var characters = new List<ICharacter>();
var numberOfCharacters = (int)packet.ReadChar();
// Optional AddByte call. EOSERV sends either 1 or 2, but GameServer appears
// to not send it on character delete
packet.ReadBreakString();
for (int i = 0; i < numberOfCharacters; ++i)
{
characters.Add(GetNextCharacter(packet));
if (packet.ReadByte() != 255)
throw new MalformedPacketException($"{packet.Family}_{packet.Action} packet missing character separator byte", packet);
}
return characters;
}
private ICharacter GetNextCharacter(IPacket packet)
{
ICharacter character = new Character()
.WithName(packet.ReadBreakString())
.WithID(packet.ReadInt());
var stats = new CharacterStats()
.WithNewStat(CharacterStat.Level, packet.ReadChar());
var renderProperties = new CharacterRenderProperties()
.WithGender(packet.ReadChar())
.WithHairStyle(packet.ReadChar())
.WithHairColor(packet.ReadChar())
.WithRace(packet.ReadChar());
character = character.WithAdminLevel((AdminLevel)packet.ReadChar());
renderProperties = renderProperties
.WithBootsGraphic(packet.ReadShort())
.WithArmorGraphic(packet.ReadShort())
.WithHatGraphic(packet.ReadShort())
.WithShieldGraphic(packet.ReadShort());
var weaponGraphic = packet.ReadShort();
renderProperties = renderProperties.WithWeaponGraphic(weaponGraphic, _eifFileProvider.EIFFile.IsRangedWeapon(weaponGraphic));
return character
.WithRenderProperties(renderProperties)
.WithStats(stats);
}
}
}