private void InitFromTemplate()
		{
			string[] stringChunks = SplitRegex.Split(template);
			chunks = new TemplateChunk[stringChunks.Length];

			for (int i = 0; i < stringChunks.Length; i++)
			{
				string chunk = stringChunks[i];
				if (i % 2 == 0)
				{
					// Text part.
					chunks[i] = new TemplateChunk(chunk, InvalidArgumentIndex);
				}
				else
				{
					// Separator, i.e. argument
					int argIndex = int.Parse(chunk.Substring(1), CultureInfo.InvariantCulture);
					chunks[i] = new TemplateChunk(stringChunks[i], argIndex);
				}
			}
		}
Exemplo n.º 2
0
        public DynamicSerializedPacketParser(byte [] Template)
        {
            int Position = 0;

            void CompileSubOperation(ref DynamicSerializedPacketCondition Parent)
            {
                Parent.SubOperation = new DynamicSerializedPacketCondition();
                Parent.SubOperation.MainOperation = (DynamicSerializedPacketConditionOperators)Template [Position];
                Position++;
                Parent.SubOperation.Right = new byte [BitConverter.ToInt32(Template, Position)];
                Position += 4;
                Parent.SubOperation.LeftADDR = BitConverter.ToInt32(Template, Position);
                Position += 4;
                Parent.SubOperation.LeftADDR_Rel = Template [Position] == 0 ? false : true;
                Position += 1;
                Array.Copy(Template, Position, Parent.SubOperation.Right, 0, Parent.SubOperation.Right.Length);
                Position += Parent.SubOperation.Right.Length;
                if (Parent.SubOperation.MainOperation == DynamicSerializedPacketConditionOperators.IS_AND ||
                    Parent.SubOperation.MainOperation == DynamicSerializedPacketConditionOperators.IS_NOT_AND)
                {
                    CompileSubOperation(ref Parent.SubOperation);
                }
            }

            TemplateChunk CompileChunk()
            {
                TemplateChunk Chunk = new TemplateChunk();

                if (Template [Position] == 255)  // PacketDataTypes.NONE == PatDataTypes.255_DefineCondition
                {
                    Position++;
                    Chunk.Condition = new DynamicSerializedPacketCondition();
                    Chunk.Condition.MainOperation = (DynamicSerializedPacketConditionOperators)Template [Position];
                    Position++;
                    Chunk.Condition.Right = new byte [BitConverter.ToInt32(Template, Position)];
                    Position += 4;
                    Chunk.Condition.LeftADDR = BitConverter.ToInt32(Template, Position);
                    Position += 4;
                    Chunk.Condition.LeftADDR_Rel = Template [Position] == 0 ? false : true;
                    Position += 1;
                    Array.Copy(Template, Position, Chunk.Condition.Right, 0, Chunk.Condition.Right.Length);
                    Position += Chunk.Condition.Right.Length;
                    if (Chunk.Condition.MainOperation == DynamicSerializedPacketConditionOperators.IS_AND ||
                        Chunk.Condition.MainOperation == DynamicSerializedPacketConditionOperators.IS_NOT_AND)
                    {
                        CompileSubOperation(ref Chunk.Condition.SubOperation);
                    }

                    Chunk.Condition.InsertTemplate = new TemplateChunk [BitConverter.ToInt32(Template, Position)];
                    Position += 4;
                    for (int x = 0; x < Chunk.Condition.InsertTemplate.Length; x++)
                    {
                        Chunk.Condition.InsertTemplate [x] = CompileChunk();
                    }

                    Chunk.Condition.ElseTemplate = new TemplateChunk [BitConverter.ToInt32(Template, Position)];
                    Position += 4;
                    for (int x = 0; x < Chunk.Condition.ElseTemplate.Length; x++)
                    {
                        Chunk.Condition.ElseTemplate [x] = CompileChunk();
                    }
                }
                else
                {
                    Chunk.ChunkType = (PacketDataTypes)Template [Position];
                }
                return(Chunk);
            }

            while (true)
            {
                Chunks.Add(CompileChunk());
                if (Position == Template.Length)
                {
                    break;
                }
            }
        }
Exemplo n.º 3
0
        public SerializedPacket Parse(byte [] Packet)
        {
            Stack <TemplateChunk> Trace = new Stack <TemplateChunk>();
            int Pos = 0;
            List <SerializedValue>            Data       = new List <SerializedValue>();
            List <byte []>                    DataBlocks = new List <byte []>();
            DynamicSerializedPacketDataParser Parser     = new DynamicSerializedPacketDataParser(Packet);

            TemplateChunk [] ResolveCondition(DynamicSerializedPacketCondition Condition)
            {
                byte [] Data1;
                if (Condition.LeftADDR_Rel)
                {
                    Data1 = DataBlocks [Data.Count - Condition.LeftADDR];
                }
                else
                {
                    Data1 = DataBlocks [Condition.LeftADDR];
                }

                switch (Condition.MainOperation)
                {
                case DynamicSerializedPacketConditionOperators.IS:
                {
                    if (AreSame(Data1, Condition.Right))
                    {
                        return(Condition.InsertTemplate);
                    }
                    else
                    {
                        return(Condition.ElseTemplate);
                    }
                }

                case DynamicSerializedPacketConditionOperators.IS_NOT:
                {
                    if (AreSame(Data1, Condition.Right))
                    {
                        return(Condition.ElseTemplate);
                    }
                    else
                    {
                        return(Condition.InsertTemplate);
                    }
                }

                case DynamicSerializedPacketConditionOperators.IS_AND:
                {
                    if (AreSame(Data1, Condition.Right))
                    {
                        return(ResolveCondition(Condition.SubOperation));
                    }
                    else
                    {
                        return(Condition.ElseTemplate);
                    }
                }

                case DynamicSerializedPacketConditionOperators.IS_NOT_AND:
                {
                    if (AreSame(Data1, Condition.Right))
                    {
                        return(Condition.ElseTemplate);
                    }
                    else
                    {
                        return(ResolveCondition(Condition.SubOperation));
                    }
                }

                default:
                    return(null);
                }
            }

            while (Packet.Length > Parser.Position && Pos < Chunks.Count)
            {
                if (Trace.Count == 0)
                {
                    Trace.Push(this.Chunks [Pos]);
                    Pos++;
                }
                TemplateChunk WorkingChunk = Trace.Pop();
                if (WorkingChunk.Condition != null)
                {
                    TemplateChunk [] Resolve = ResolveCondition(WorkingChunk.Condition);
                    if (Resolve != null)
                    {
                        for (int x = Resolve.Length - 1; x > -1; x--)
                        {
                            Trace.Push(Resolve [x]);
                        }
                    }
                }
                else
                {
                    switch ((byte)WorkingChunk.ChunkType)
                    {
                    case 0:
                        Data.Add(new SerializedValue(Parser.GetByte(), PacketDataTypes.Byte));
                        DataBlocks.Add(Parser.LastGetPacketData);
                        break;

                    case 1:
                        Data.Add(new SerializedValue(Parser.GetShort(), PacketDataTypes.Short));
                        DataBlocks.Add(Parser.LastGetPacketData);
                        break;

                    case 2:
                        Data.Add(new SerializedValue(Parser.GetInt(), PacketDataTypes.Int));
                        DataBlocks.Add(Parser.LastGetPacketData);
                        break;

                    case 3:
                        Data.Add(new SerializedValue(Parser.GetLong(), PacketDataTypes.Long));
                        DataBlocks.Add(Parser.LastGetPacketData);
                        break;

                    case 4:
                        Data.Add(new SerializedValue(Parser.GetUShort(), PacketDataTypes.UShort));
                        DataBlocks.Add(Parser.LastGetPacketData);
                        break;

                    case 5:
                        Data.Add(new SerializedValue(Parser.GetUInt(), PacketDataTypes.UInt));
                        DataBlocks.Add(Parser.LastGetPacketData);
                        break;

                    case 6:
                        Data.Add(new SerializedValue(Parser.GetULong(), PacketDataTypes.ULong));
                        DataBlocks.Add(Parser.LastGetPacketData);
                        break;

                    case 7:
                        Data.Add(new SerializedValue(Parser.GetULong(), PacketDataTypes.MessageType));
                        DataBlocks.Add(Parser.LastGetPacketData);
                        break;

                    case 8:
                        Data.Add(new SerializedValue(Parser.GetULong(), PacketDataTypes.Version));
                        DataBlocks.Add(Parser.LastGetPacketData);
                        break;

                    case 9:
                        Data.Add(new SerializedValue(Parser.GetULong(), PacketDataTypes.Chunk));
                        DataBlocks.Add(Parser.LastGetPacketData);
                        break;

                    case 10:
                        Data.Add(new SerializedValue(Parser.GetULong(), PacketDataTypes.String));
                        DataBlocks.Add(Parser.LastGetPacketData);
                        break;

                    case 254:
                        break;

                    case 255:
                        throw new Exception("Compiler Failed to Compile the template! 'this is done at this.ctor(...)'");

                    default:
                        throw new Exception("Unknown DataType, CANT PARSE!");
                    }
                }
            }
            return(new SerializedPacket(Data.ToArray()));
        }
Exemplo n.º 4
0
 public void InsertFullChunk(int pos, TemplateChunk Chunk) =>
 Chunks.Insert(pos, Chunk);
Exemplo n.º 5
0
 public void AddFullChunk(TemplateChunk Chunk) =>
 Chunks.Add(Chunk);
Exemplo n.º 6
0
 protected override void Visit(TemplateChunk chunk)
 {
 }
 protected abstract void Visit(TemplateChunk chunk);
Exemplo n.º 8
0
 protected virtual void Visit(TemplateChunk chunk)
 {
 }