コード例 #1
0
        public override string ToString()
        {
            StringBuilder Result = new StringBuilder();

            Result.Append(packageType.ToString());
            Result.Append("|");
            Result.Append(packageLenght.ToString());
            Result.Append("|");
            Result.Append(messageBody);

            return(Result.ToString());
        }
コード例 #2
0
        protected ByteBuffer BuildBuffer(T data)
        {
            var buffer = new ByteBuffer();

            buffer.WriteInteger((int)PackageType);
            buffer.WriteInteger(PackageId);
            buffer.WriteInteger(EntityId);
            DebugLog.LogPackets($"[NetworkMagic] Writing a packet of type {PackageType.ToString()}:");
            foreach (var field in typeof(T).GetFields())
            {
                if (field.Name == nameof(NetworkData.ConnId))
                {
                    continue;
                }

                var value = field.GetValue(data);
                switch (value)
                {
                case string s:
                    DebugLog.LogPackets($"writing a string for {field.Name}: {s.ToString()}");
                    buffer.WriteString(s);
                    break;

                case float f:
                    DebugLog.LogPackets($"writing a float for {field.Name}: {f.ToString()}");
                    buffer.WriteFloat(f);
                    break;

                case bool b:
                    DebugLog.LogPackets($"writing a bool for {field.Name}: {b.ToString()}");
                    buffer.WriteBool(b);
                    break;

                case int i:
                    DebugLog.LogPackets($"writing an int: {i.ToString()}");
                    buffer.WriteInteger(i);
                    break;

                case PointF p:
                    DebugLog.LogPackets($"writing a PointF: {p.ToString()}");
                    buffer.WriteFloat(p.X);
                    buffer.WriteFloat(p.Y);
                    break;

                case Vec2 p:
                    DebugLog.LogPackets($"writing a Vec2: X: {p.X}, Y: {p.Y}");
                    buffer.WriteFloat(p.X);
                    buffer.WriteFloat(p.Y);
                    break;

                case List <string> list:
                {
                    DebugLog.LogPackets($"writing a list of string: {string.Join(", ", list)}");
                    buffer.WriteInteger(list.Count);
                    foreach (var item in list)
                    {
                        buffer.WriteString(item);
                    }

                    break;
                }

                default:
                    DebugLog.Severe($"[NetworkMagic] You are trying to encrypt an unknown data type for {typeof(T).FullName}!");
                    break;
                }
            }
            DebugLog.LogPackets("Writing---");
            return(buffer);
        }