using System.Text;

namespace QCRPG.Framework;

public class PacketMatchPattern
{
	public int startIndex;

	public int[] bytePattern;

	public PacketMatchPattern(int startIndex, int[] bytePattern)
	{
		this.startIndex = startIndex;
		this.bytePattern = bytePattern;
	}

	public bool CheckForMatch(PacketBase packet)
	{
		byte[] array = packet.ToByteArray();
		if (startIndex + bytePattern.Length > array.Length)
		{
			return false;
		}
		for (int i = 0; i < bytePattern.Length; i++)
		{
			if (bytePattern[i] >= 0 && bytePattern[i] <= 255 && (byte)bytePattern[i] != array[startIndex + i])
			{
				return false;
			}
		}
		return true;
	}

	public override string ToString()
	{
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.Append("Start Index: " + startIndex);
		stringBuilder.Append(", ");
		stringBuilder.Append("Byte Pattern: ");
		int[] array = bytePattern;
		for (int i = 0; i < array.Length; i++)
		{
			int num = array[i];
			if (num < 0 || num > 255)
			{
				stringBuilder.Append("XX ");
			}
			else
			{
				stringBuilder.Append(num.ToString("X2 "));
			}
		}
		return stringBuilder.ToString().Trim();
	}
}
