using System.Text;
namespace PropertyWireTests;
///
/// Builds messages byte for byte the way ACE writes them, from the part the client's
/// UIQueueManager::ProcessNetBlobData hands on: a game event from its event type (the 0xF7B0
/// header - guid, sequence - is already stripped), a top-level message from its opcode.
/// Each writer below mirrors the ACE.Server code named in its comment.
///
internal sealed class AceMessage
{
private readonly MemoryStream _ms = new();
public readonly BinaryWriter W;
private AceMessage(uint first)
{
W = new BinaryWriter(_ms);
W.Write(first);
}
/// GameEventMessage: [F7B0][guid][seq][event type] - the client sees the type on.
public static AceMessage GameEvent(uint eventType) => new(eventType);
/// GameMessage: the opcode, then the body.
public static AceMessage Message(uint opcode) => new(opcode);
public byte[] ToArray() => _ms.ToArray();
private static uint PadMultiple(uint length, uint multiple) => multiple * ((length + multiple - 1u) / multiple) - length;
/// ACE Extensions.WriteString16L: u16 length, cp1252 bytes, pad (2 + length) to 4.
public void String16L(string data)
{
W.Write((ushort)data.Length);
W.Write(Encoding.Latin1.GetBytes(data));
W.Write(new byte[PadMultiple(2u + (uint)data.Length, 4u)]);
}
/// ACE Extensions.Align: pad the stream to a multiple of 4.
public void Align() => W.Write(new byte[PadMultiple((uint)_ms.Length, 4u)]);
/// ACE PackableHashTable.WriteHeader: u16 count, u16 bucket count.
public void HashHeader(int count, int buckets)
{
W.Write((ushort)count);
W.Write((ushort)buckets);
}
public void IntTable(Dictionary t) { HashHeader(t.Count, 16); foreach (var kv in t) { W.Write(kv.Key); W.Write(kv.Value); } }
public void Int64Table(Dictionary t) { HashHeader(t.Count, 8); foreach (var kv in t) { W.Write(kv.Key); W.Write(kv.Value); } }
public void BoolTable(Dictionary t) { HashHeader(t.Count, 8); foreach (var kv in t) { W.Write(kv.Key); W.Write(Convert.ToUInt32(kv.Value)); } }
public void FloatTable(Dictionary t) { HashHeader(t.Count, 8); foreach (var kv in t) { W.Write(kv.Key); W.Write(kv.Value); } }
public void StringTable(Dictionary t) { HashHeader(t.Count, 8); foreach (var kv in t) { W.Write(kv.Key); String16L(kv.Value); } }
public void UIntTable(Dictionary t) { HashHeader(t.Count, 8); foreach (var kv in t) { W.Write(kv.Key); W.Write(kv.Value); } }
}
/// ACE ArmorProfile: slash, pierce, bludgeon, cold, fire, acid, nether, lightning.
internal sealed record ArmorProfile(float Slash, float Pierce, float Bludgeon, float Cold, float Fire, float Acid, float Nether, float Lightning);
/// ACE CreatureProfile (Flags: HasBuffsDebuffs 0x1, ShowAttributes 0x8).
internal sealed record CreatureProfile(uint Flags, uint Health, uint HealthMax, uint[]? Attributes = null, ushort Highlights = 0, ushort Colors = 0);
/// ACE WeaponProfile.
internal sealed record WeaponProfile(uint DamageType, uint WeaponTime, uint WeaponSkill, uint Damage,
double DamageVariance, double DamageMod, double WeaponLength, double MaxVelocity, double WeaponOffense, uint MaxVelocityEstimated = 0);
/// ACE HookProfile (Flags: Inscribable 1, IsHealer 2, IsFood 4, IsLockpick 8).
internal sealed record HookProfile(uint Flags, uint ValidLocations, uint AmmoType);
/// ACE AppraiseInfo: what BuildProfile leaves in it; Write() below mirrors AppraiseInfoExtensions.Write.
internal sealed class AppraiseInfo
{
public bool Success = true;
public Dictionary Ints = new();
public Dictionary Int64s = new();
public Dictionary Bools = new();
public Dictionary Floats = new();
public Dictionary Strings = new();
public Dictionary Dids = new();
public List SpellBook = new();
public ArmorProfile? Armor;
public CreatureProfile? Creature;
public WeaponProfile? Weapon;
public HookProfile? Hook;
public ushort ArmorHighlight, ArmorColor, WeaponHighlight, WeaponColor, ResistHighlight, ResistColor;
public uint[]? ArmorLevels; // 9 values
// ACE IdentifyResponseFlags
private const uint IntStatsTable = 0x0001, BoolStatsTable = 0x0002, FloatStatsTable = 0x0004, StringStatsTable = 0x0008,
SpellBookF = 0x0010, WeaponProfileF = 0x0020, HookProfileF = 0x0040, ArmorProfileF = 0x0080, CreatureProfileF = 0x0100,
ArmorEnchantmentBitfield = 0x0200, ResistEnchantmentBitfield = 0x0400, WeaponEnchantmentBitfield = 0x0800,
DidStatsTable = 0x1000, Int64StatsTable = 0x2000, ArmorLevelsF = 0x4000;
/// ACE AppraiseInfo.BuildFlags.
public uint Flags
{
get
{
uint f = 0;
if (Ints.Count > 0) f |= IntStatsTable;
if (Int64s.Count > 0) f |= Int64StatsTable;
if (Bools.Count > 0) f |= BoolStatsTable;
if (Floats.Count > 0) f |= FloatStatsTable;
if (Strings.Count > 0) f |= StringStatsTable;
if (Dids.Count > 0) f |= DidStatsTable;
if (SpellBook.Count > 0) f |= SpellBookF;
if (ResistHighlight != 0) f |= ResistEnchantmentBitfield;
if (Armor != null) f |= ArmorProfileF;
if (Creature != null) f |= CreatureProfileF;
if (Weapon != null) f |= WeaponProfileF;
if (Hook != null) f |= HookProfileF;
if (ArmorHighlight != 0) f |= ArmorEnchantmentBitfield;
if (WeaponHighlight != 0) f |= WeaponEnchantmentBitfield;
if (ArmorLevels != null) f |= ArmorLevelsF;
return f;
}
}
/// GameEventIdentifyObjectResponse: the object guid, then AppraiseInfoExtensions.Write.
public byte[] ToIdentifyMessage(uint guid)
{
var m = AceMessage.GameEvent(0x00C9);
var w = m.W;
w.Write(guid);
uint flags = Flags;
w.Write(flags);
w.Write(Convert.ToUInt32(Success));
if ((flags & IntStatsTable) != 0) m.IntTable(Ints);
if ((flags & Int64StatsTable) != 0) m.Int64Table(Int64s);
if ((flags & BoolStatsTable) != 0) m.BoolTable(Bools);
if ((flags & FloatStatsTable) != 0) m.FloatTable(Floats);
if ((flags & StringStatsTable) != 0) m.StringTable(Strings);
if ((flags & DidStatsTable) != 0) m.UIntTable(Dids);
if ((flags & SpellBookF) != 0) { w.Write(SpellBook.Count); foreach (uint s in SpellBook) w.Write(s); } // PackableList
if (Armor is { } a)
{
w.Write(a.Slash); w.Write(a.Pierce); w.Write(a.Bludgeon); w.Write(a.Cold);
w.Write(a.Fire); w.Write(a.Acid); w.Write(a.Nether); w.Write(a.Lightning);
}
if (Creature is { } c)
{
w.Write(c.Flags); w.Write(c.Health); w.Write(c.HealthMax);
if ((c.Flags & 0x8) != 0)
foreach (uint v in c.Attributes ?? new uint[10]) w.Write(v);
if ((c.Flags & 0x1) != 0) { w.Write(c.Highlights); w.Write(c.Colors); }
}
if (Weapon is { } p)
{
w.Write(p.DamageType); w.Write(p.WeaponTime); w.Write(p.WeaponSkill); w.Write(p.Damage);
w.Write(p.DamageVariance); w.Write(p.DamageMod); w.Write(p.WeaponLength); w.Write(p.MaxVelocity);
w.Write(p.WeaponOffense); w.Write(p.MaxVelocityEstimated);
}
if (Hook is { } h) { w.Write(h.Flags); w.Write(h.ValidLocations); w.Write(h.AmmoType); }
if ((flags & ArmorEnchantmentBitfield) != 0) { w.Write(ArmorHighlight); w.Write(ArmorColor); }
if ((flags & WeaponEnchantmentBitfield) != 0) { w.Write(WeaponHighlight); w.Write(WeaponColor); }
if ((flags & ResistEnchantmentBitfield) != 0) { w.Write(ResistHighlight); w.Write(ResistColor); }
if (ArmorLevels != null) foreach (uint v in ArmorLevels) w.Write(v);
return m.ToArray();
}
}
/// The property part of ACE GameEventPlayerDescription.WriteEventBody (and a tail after it).
internal sealed class PlayerDescription
{
public Dictionary Ints = new();
public Dictionary Int64s = new();
public Dictionary Bools = new();
public Dictionary Floats = new();
public Dictionary Strings = new();
public Dictionary Dids = new();
public Dictionary Iids = new();
public byte[] ToMessage()
{
var m = AceMessage.GameEvent(0x0013);
var w = m.W;
uint f = 0;
if (Ints.Count > 0) f |= 0x0001;
if (Int64s.Count > 0) f |= 0x0080;
if (Bools.Count > 0) f |= 0x0002;
if (Floats.Count > 0) f |= 0x0004;
if (Strings.Count > 0) f |= 0x0010;
if (Dids.Count > 0) f |= 0x0008;
if (Iids.Count > 0) f |= 0x0040;
f |= 0x0020; // Position (LastOutsideDeath): follows the tables, not read
w.Write(f);
w.Write(1u); // WeenieType.Creature... (Player = 43, the value is skipped)
if (Ints.Count > 0) m.IntTable(Ints);
if (Int64s.Count > 0) m.Int64Table(Int64s);
if (Bools.Count > 0) m.BoolTable(Bools);
if (Floats.Count > 0) m.FloatTable(Floats);
if (Strings.Count > 0) m.StringTable(Strings);
if (Dids.Count > 0) m.UIntTable(Dids);
if (Iids.Count > 0) m.UIntTable(Iids);
// Position table (1 entry, 16 buckets) + the vectors, options and inventory that follow.
m.HashHeader(1, 16);
w.Write(14u); // PositionType.LastOutsideDeath
w.Write(0x7D640013u); w.Write(10f); w.Write(20f); w.Write(30f); w.Write(1f); w.Write(0f); w.Write(0f); w.Write(0f);
w.Write(0x00000303u); // vector flags ... (garbage to the parser)
return m.ToArray();
}
}
/// ACE GameMessage{Private,Public}UpdateProperty* / UpdateDataID / UpdateInstanceID.
internal static class Updates
{
private const byte Seq = 0x5A;
public static byte[] PrivateInt(uint key, int v) { var m = AceMessage.Message(0x02CD); m.W.Write(Seq); m.W.Write(key); m.W.Write(v); return m.ToArray(); }
public static byte[] PublicInt(uint guid, uint key, int v) { var m = AceMessage.Message(0x02CE); m.W.Write(Seq); m.W.Write(guid); m.W.Write(key); m.W.Write(v); return m.ToArray(); }
public static byte[] PrivateInt64(uint key, long v) { var m = AceMessage.Message(0x02CF); m.W.Write(Seq); m.W.Write(key); m.W.Write(v); return m.ToArray(); }
public static byte[] PublicInt64(uint guid, uint key, long v) { var m = AceMessage.Message(0x02D0); m.W.Write(Seq); m.W.Write(guid); m.W.Write(key); m.W.Write(v); return m.ToArray(); }
public static byte[] PrivateBool(uint key, bool v) { var m = AceMessage.Message(0x02D1); m.W.Write(Seq); m.W.Write(key); m.W.Write(Convert.ToUInt32(v)); return m.ToArray(); }
public static byte[] PublicBool(uint guid, uint key, bool v) { var m = AceMessage.Message(0x02D2); m.W.Write(Seq); m.W.Write(guid); m.W.Write(key); m.W.Write(Convert.ToUInt32(v)); return m.ToArray(); }
public static byte[] PrivateFloat(uint key, double v) { var m = AceMessage.Message(0x02D3); m.W.Write(Seq); m.W.Write(key); m.W.Write(v); return m.ToArray(); }
public static byte[] PublicFloat(uint guid, uint key, double v) { var m = AceMessage.Message(0x02D4); m.W.Write(Seq); m.W.Write(guid); m.W.Write(key); m.W.Write(v); return m.ToArray(); }
/// GameMessagePrivateUpdatePropertyString: seq, key, Align(), WriteString16L.
public static byte[] PrivateString(uint key, string v) { var m = AceMessage.Message(0x02D5); m.W.Write(Seq); m.W.Write(key); m.Align(); m.String16L(v); return m.ToArray(); }
/// GameMessagePublicUpdatePropertyString: seq, key, THEN guid, Align(), WriteString16L.
public static byte[] PublicString(uint guid, uint key, string v) { var m = AceMessage.Message(0x02D6); m.W.Write(Seq); m.W.Write(key); m.W.Write(guid); m.Align(); m.String16L(v); return m.ToArray(); }
public static byte[] PrivateDataId(uint key, uint v) { var m = AceMessage.Message(0x02D7); m.W.Write(Seq); m.W.Write(key); m.W.Write(v); return m.ToArray(); }
public static byte[] PublicDataId(uint guid, uint key, uint v) { var m = AceMessage.Message(0x02D8); m.W.Write(Seq); m.W.Write(guid); m.W.Write(key); m.W.Write(v); return m.ToArray(); }
public static byte[] PrivateInstanceId(uint key, uint v) { var m = AceMessage.Message(0x02D9); m.W.Write(Seq); m.W.Write(key); m.W.Write(v); return m.ToArray(); }
public static byte[] PublicInstanceId(uint guid, uint key, uint v) { var m = AceMessage.Message(0x02DA); m.W.Write(Seq); m.W.Write(guid); m.W.Write(key); m.W.Write(v); return m.ToArray(); }
}