Imports System.Net.Sockets
Namespace Dofus
Public Class DofusReader
Inherits IO.BinaryReader
Sub New(ByVal input As IO.Stream)
MyBase.New(input)
End Sub
Sub New(ByVal input As IO.Stream, ByVal encoding As System.Text.Encoding)
MyBase.New(input, encoding)
End Sub
Public Function readUnsignedByte()
Return ReadByte()
End Function
Public Overrides Function ReadByte() As Byte
Return MyBase.ReadByte()
End Function
Public Function ReadUnSignedshort()
Return ReadUInt16()
End Function
Public Overrides Function ReadUInt16() As UShort
Return (CUShort(ReadByte()) << 8) + ReadByte()
End Function
Public Overrides Function ReadBoolean() As Boolean
Return ReadByte() = 1
End Function
Public Function ReadShort()
Return ReadInt16()
End Function
Public Overrides Function ReadInt16() As Short
Dim Value As UShort = ReadUInt16()
If Value > Short.MaxValue Then
Dim Value2 As Short = -(UShort.MaxValue - Value) - 1
Return Value2
End If
Return Value
End Function
Public Function ReadInt()
Return ReadInt32()
End Function
Public Overrides Function ReadInt32() As Integer
Dim Value As UInteger = ReadUInt32()
If Value > Integer.MaxValue Then
Dim Value2 As Integer = -(UInteger.MaxValue - Value) - 1
Return Value2
End If
Return Value
End Function
Public Overrides Function ReadUInt32() As UInteger
Return (CUInt(ReadByte()) << 24) + (CUInt(ReadByte()) << 16) + (CUInt(ReadByte()) << 8) + ReadByte()
End Function
Public Overrides Function ReadDouble() As Double
Dim Bytes() As Byte = ReadBytes(8)
Array.Reverse(Bytes)
Return BitConverter.ToDouble(Bytes, 0)
End Function
Public Function ReadUTF()
Return ReadString()
End Function
Public Overrides Function ReadString() As String
Dim ByteArray() As Byte = MyBase.ReadBytes(ReadUInt16())
Return System.Text.Encoding.UTF8.GetString(ByteArray)
End Function
End Class
Public Class DofusWriter
Shared m_Bytes As New List(Of Byte)
Private Shared Function ComputeStaticHeader(ByVal PacketId As Integer, ByVal MessageLenghtType As Integer) As Short
Return (PacketId << 2) Or MessageLenghtType
End Function
Private Shared Function ComputeTypeLen(ByVal MessageLenght As Integer) As Short
Select Case MessageLenght
Case Is > UShort.MaxValue
Return 3
Case Is > Byte.MaxValue
Return 2
Case Is > 0
Return 1
Case Else
Return 0
End Select
End Function
Friend Shared Sub Send(ByVal PacketID As Integer, ByVal sock As NetworkStream)
Dim MessageLenghtType As Integer = ComputeTypeLen(m_Bytes.Count)
Dim Header As Short = ComputeStaticHeader(PacketID, MessageLenghtType)
Dim BytesToWrite(2 + MessageLenghtType + m_Bytes.Count - 1) As Byte
BytesToWrite(0) = Header >> 8
BytesToWrite(1) = Header - 256 * BytesToWrite(0)
Dim index As Integer
Select Case MessageLenghtType
Case 1
BytesToWrite(2) = m_Bytes.Count
index = 3
Case 2
BytesToWrite(2) = m_Bytes.Count >> 8
BytesToWrite(3) = m_Bytes.Count - 256 * BytesToWrite(2)
index = 4
Case 3
BytesToWrite(2) = m_Bytes.Count >> 16
BytesToWrite(3) = m_Bytes.Count >> 8
BytesToWrite(4) = m_Bytes.Count - 256 * BytesToWrite(3) - 256 * 256 * BytesToWrite(2)
index = 5
End Select
Dim identificator As New PacketIdentificator
identificator.GetClasseName(PacketID)
Console.WriteLine("envoie : id = " & PacketID & ", Class : " & identificator.ClasseName & ", taille = " & m_Bytes.Count) ' on ecrit l'ID et la taille
Console.Write("0x")
For i As Integer = index To m_Bytes.Count + index - 1
BytesToWrite(i) = m_Bytes(i - index)
Console.Write(BytesToWrite(i).ToString("X2"))
Next
Console.WriteLine()
sock.send(BytesToWrite)
m_Bytes.Clear()
End Sub
Public Sub WriteByte(ByVal Value As Byte)
m_Bytes.Add(Value)
End Sub
Public Sub WriteBoolean(ByVal Value As Boolean)
If Value Then
WriteByte(1)
Else
WriteByte(0) ' ton cryptpasse et pas bon c'est pas le bon projet que t'a lancé
End If
End Sub
Public Sub WriteShort(ByVal Value As Short)
WriteByte(CByte(Value >> 8))
WriteByte(CByte(Value))
End Sub
Public Sub WriteUShort(ByVal Value As UShort)
WriteByte(CByte(Value >> 8))
WriteByte(CByte(Value And 255))
End Sub
Public Sub WriteUInt32(ByVal Value As UInteger)
WriteByte(CByte(Value >> 24))
Value -= (Value >> 24) << 24
WriteByte(CByte(Value >> 16))
Value -= (Value >> 16) << 16
WriteByte(CByte(Value >> 8))
Value -= (Value >> 8) << 8
WriteByte(CByte(Value))
End Sub
Public Sub WriteDouble(ByVal Value As UInteger)
WriteByte(CByte(Value >> 56))
Value -= (Value >> 56) << 56
WriteByte(CByte(Value >> 48))
Value -= (Value >> 48) << 48
WriteByte(CByte(Value >> 40))
Value -= (Value >> 40) << 40
WriteByte(CByte(Value >> 32))
Value -= (Value >> 32) << 32
WriteByte(CByte(Value >> 24))
Value -= (Value >> 24) << 24
WriteByte(CByte(Value >> 16))
Value -= (Value >> 16) << 16
WriteByte(CByte(Value >> 8))
Value -= (Value >> 8) << 8
WriteByte(CByte(Value))
End Sub
Public Sub WriteString(ByVal Value As String)
WriteUTF(Value)
End Sub
Public Sub WriteUTF(ByVal Value As String)
Dim BytesToWrite() As Byte = System.Text.Encoding.UTF8.GetBytes(Value)
WriteUShort(BytesToWrite.Length)
For Each Bit As Byte In BytesToWrite
WriteByte(Bit)
Next
End Sub
Sub WriteInt32(ByVal Target As Integer)
Dim arr As Byte()
arr = BitConverter.GetBytes(Target)
WriteByte(arr(3))
WriteByte(arr(2))
WriteByte(arr(1))
WriteByte(arr(0))
End Sub
Sub WriteInt16(ByVal Target As Integer)
Dim arr As Byte()
arr = BitConverter.GetBytes(Target)
WriteByte(arr(1))
WriteByte(arr(0))
End Sub
Sub WriteSingle(ByVal Target As Double)
Dim arr As Byte()
arr = BitConverter.GetBytes(Target)
For Each bytte As Byte In arr
WriteByte(bytte)
Next
End Sub
End Class
End Namespace