|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.IO; |
| 3 | +using System.Linq; |
| 4 | + |
| 5 | +namespace DemoInfo.DP.FastNetmessages |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// FastNetMessage adaptation of CCSUsrMsg_SayText2 protobuf message |
| 9 | + /// </summary> |
| 10 | + public struct SayText2 |
| 11 | + { |
| 12 | + public int EntIdx; |
| 13 | + private int _chat; |
| 14 | + public bool Chat => _chat != 0; |
| 15 | + private int _textAllChat; |
| 16 | + public bool TextAllChat => _textAllChat != 0; |
| 17 | + // Params is a 4 length array but only 2 are used [0] = sender nickname [1] = message text |
| 18 | + public IList<string> Params; |
| 19 | + public string MsgName; |
| 20 | + |
| 21 | + public void Parse(IBitStream bitstream, DemoParser parser) |
| 22 | + { |
| 23 | + Params = new List<string>(); |
| 24 | + while (!bitstream.ChunkFinished) |
| 25 | + { |
| 26 | + var desc = bitstream.ReadProtobufVarInt(); |
| 27 | + var wireType = desc & 7; |
| 28 | + var fieldnum = desc >> 3; |
| 29 | + |
| 30 | + if (wireType == 0 && fieldnum == 1) |
| 31 | + { |
| 32 | + EntIdx = bitstream.ReadProtobufVarInt(); |
| 33 | + } |
| 34 | + else if (wireType == 0 && fieldnum == 2) |
| 35 | + { |
| 36 | + _chat = bitstream.ReadProtobufVarInt(); |
| 37 | + } |
| 38 | + else if (wireType == 2 && fieldnum == 3) |
| 39 | + { |
| 40 | + MsgName = bitstream.ReadProtobufString(); |
| 41 | + } |
| 42 | + else if (wireType == 0 && fieldnum == 5) |
| 43 | + { |
| 44 | + _textAllChat = bitstream.ReadProtobufVarInt(); |
| 45 | + } |
| 46 | + else if (wireType == 2 && fieldnum == 4) |
| 47 | + { |
| 48 | + Params.Add(bitstream.ReadProtobufString()); |
| 49 | + } |
| 50 | + else |
| 51 | + throw new InvalidDataException(); |
| 52 | + } |
| 53 | + Raise(parser); |
| 54 | + } |
| 55 | + |
| 56 | + private void Raise(DemoParser parser) |
| 57 | + { |
| 58 | + // struct methods are called with a hidden ref to "this", |
| 59 | + // I have to make a local copy to be able to use it in the lambda expression |
| 60 | + IList<string> parameters = Params; |
| 61 | + SayText2EventArgs e = new SayText2EventArgs |
| 62 | + { |
| 63 | + Sender = parser.Players.Values.FirstOrDefault(x => x.Name == parameters[0]), |
| 64 | + Text = Params[1], |
| 65 | + IsChat = Chat, |
| 66 | + IsChatAll = TextAllChat |
| 67 | + }; |
| 68 | + |
| 69 | + parser.RaiseSayText2(e); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments