Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.Linq
- Imports System.Diagnostics
- Imports System.Runtime.InteropServices
- Imports System.Collections.Generic
- Public Class Form1
- #Region "P/Invoke"
- <DllImport("kernel32.dll")> _
- Private Shared Function ReadProcessMemory(processHandle As IntPtr, address As Integer, buffer As IntPtr, size As Integer, ByRef numberOfBytesRead As Integer) As Integer
- End Function
- #End Region
- #Region "Structures"
- Private Structure STD_STRING
- <MarshalAs(UnmanagedType.ByValArray, SizeConst:=&H10)> _
- Private buffer As Byte()
- ' offset 0x0, length = 0x10
- Private length As Integer
- ' offset 0x10
- Private maxLength As Integer
- ' offset 0x14
- Private unk As Integer
- ' offset 0x18
- Public Function Text() As String
- If Me.maxLength > &HF Then
- Return ReadString(BitConverter.ToInt32(buffer, 0), Me.length, False)
- End If
- Dim value As Char() = New Char(Me.length - 1) {}
- For i As Integer = 0 To Me.length - 1
- If Me.buffer(i) = 0 Then
- Return New String(value, 0, i)
- Else
- value(i) = ChrW(buffer(i))
- End If
- Next
- Return New String(value)
- End Function
- End Structure
- Private Structure CONTAINER_NODE
- Private m_left As Integer
- ' offset 0x0
- Private m_center As Integer
- ' offset 0x4
- Private m_right As Integer
- ' offset 0x8
- Private m_containerIndex As Integer
- ' offset 0xC
- Private m_containerAddress As Integer
- ' offset 0x10
- Public ReadOnly Property Left() As Integer
- Get
- Return Me.m_left
- End Get
- End Property
- Public ReadOnly Property Center() As Integer
- Get
- Return Me.m_center
- End Get
- End Property
- Public ReadOnly Property Right() As Integer
- Get
- Return Me.m_right
- End Get
- End Property
- Public ReadOnly Property ContainerIndex() As Integer
- Get
- Return Me.m_containerIndex
- End Get
- End Property
- Public ReadOnly Property ContainerAddress() As Integer
- Get
- Return Me.m_containerAddress
- End Get
- End Property
- Public ReadOnly Property HasLeft() As Boolean
- Get
- Return Me.m_left <> 0
- End Get
- End Property
- Public ReadOnly Property HasCenter() As Boolean
- Get
- Return Me.m_center <> 0
- End Get
- End Property
- Public ReadOnly Property HasRight() As Boolean
- Get
- Return Me.m_right <> 0
- End Get
- End Property
- Public ReadOnly Property HasContainer() As Boolean
- Get
- Return Me.m_containerAddress <> 0
- End Get
- End Property
- Public Function Container() As Container
- Return ReadContainer(Me.m_containerAddress)
- End Function
- Public Function CompareTo(other As CONTAINER_NODE) As Integer
- Return Me.m_containerIndex.CompareTo(other.ContainerIndex)
- End Function
- Public Function Compare(x As CONTAINER_NODE, y As CONTAINER_NODE) As Integer
- Return x.ContainerIndex.CompareTo(y.ContainerIndex)
- End Function
- End Structure
- Private Structure CONTAINERS_ENTRY
- Private unk0 As Integer
- ' offset 0x0
- Private unk1 As Integer
- ' offset 0x4
- Private m_containersAddress As Integer
- ' offset 0x8
- Private m_containersCount As Integer
- ' offset 0xC
- Public ReadOnly Property ContainersAddress() As Integer
- Get
- Return Me.m_containersAddress
- End Get
- End Property
- Public ReadOnly Property ContainersCount() As Integer
- Get
- Return Me.m_containersCount
- End Get
- End Property
- End Structure
- Private Structure Item
- Private unk As Integer
- ' offset 0x0
- Private m_count As Integer
- ' offset 0x4
- Private m_id As Integer
- ' offset 0x8
- Private m_r As Integer
- ' offset 0xC
- Private m_g As Integer
- ' offset 0x10
- Private m_b As Integer
- ' offset 0x14
- Private m_isVisible As Integer
- ' offset 0x18
- Public ReadOnly Property Count() As Integer
- Get
- Return Me.m_count
- End Get
- End Property
- Public ReadOnly Property Id() As Integer
- Get
- Return Me.m_id
- End Get
- End Property
- Public ReadOnly Property R() As Integer
- Get
- Return Me.m_r
- End Get
- End Property
- Public ReadOnly Property G() As Integer
- Get
- Return Me.m_g
- End Get
- End Property
- Public ReadOnly Property B() As Integer
- Get
- Return Me.m_b
- End Get
- End Property
- Public ReadOnly Property IsVisible() As Boolean
- Get
- Return Me.m_isVisible <> 0
- End Get
- End Property
- End Structure
- Private Structure Container
- Private m_index As Integer
- ' offset 0
- Private m_asItem As Item
- ' offset 0x4
- Private name As STD_STRING
- ' offset 0x20
- Private unk0 As Integer
- ' offset 0x3C
- Private m_slotsCount As Integer
- ' offset 0x40
- Private m_itemsCount As Integer
- ' offset 0x44
- Private unk1 As Integer
- ' offset 0x48
- Private itemsAddress As Integer
- ' offset 0x4C
- Public ReadOnly Property Index() As Integer
- Get
- Return Me.m_index
- End Get
- End Property
- Public ReadOnly Property AsItem() As Item
- Get
- Return Me.m_asItem
- End Get
- End Property
- Public ReadOnly Property SlotsCount() As Integer
- Get
- Return Me.m_slotsCount
- End Get
- End Property
- Public ReadOnly Property ItemsCount() As Integer
- Get
- Return Me.m_itemsCount
- End Get
- End Property
- Public Function Namee() As String
- Return Me.name.Text()
- End Function
- Public Function Items() As Item()
- Return ReadItems(Me.itemsAddress, Me.m_itemsCount)
- End Function
- End Structure
- #End Region
- #Region "Reading Memory"
- Private Shared Function ReadInt32(address As Integer) As Integer
- Dim numberOfBytesRead As Integer
- Dim size As Integer = 4
- Dim memoryBlock As IntPtr = Marshal.AllocHGlobal(size)
- ReadProcessMemory(processHandle, address, memoryBlock, size, numberOfBytesRead)
- Dim value As Integer = CInt(Marshal.PtrToStructure(memoryBlock, GetType(Integer)))
- Marshal.FreeHGlobal(memoryBlock)
- Return value
- End Function
- Private Shared Function ReadString(address As Integer, length As Integer, Optional checkNullChar As Boolean = True) As String
- Dim numberOfBytesRead As Integer
- Dim size As Integer = length
- Dim memoryBlock As IntPtr = Marshal.AllocHGlobal(size)
- ReadProcessMemory(processHandle, address, memoryBlock, size, numberOfBytesRead)
- Dim value As String = Marshal.PtrToStringAnsi(memoryBlock, length)
- Marshal.FreeHGlobal(memoryBlock)
- If checkNullChar Then
- Dim index As Integer = value.IndexOf(ControlChars.NullChar)
- Return If(index = -1, value, value.Substring(0, index))
- End If
- Return value
- End Function
- Private Shared Function ReadStdString(address As Integer) As String
- Dim numberOfBytesRead As Integer
- Dim type As Type = GetType(STD_STRING)
- Dim size As Integer = Marshal.SizeOf(type)
- Dim memoryBlock As IntPtr = Marshal.AllocHGlobal(size)
- ReadProcessMemory(processHandle, address, memoryBlock, size, numberOfBytesRead)
- Dim value As STD_STRING = CType(Marshal.PtrToStructure(memoryBlock, type), STD_STRING)
- Marshal.FreeHGlobal(memoryBlock)
- Return value.Text()
- End Function
- Private Shared Function ReadContainersPointer() As Integer
- Return ReadInt32(containersPointer + baseAddress)
- End Function
- Private Shared Function ReadContainersEntry(address As Integer) As CONTAINERS_ENTRY
- Dim numberOfBytesRead As Integer
- Dim type As Type = GetType(CONTAINERS_ENTRY)
- Dim size As Integer = Marshal.SizeOf(type)
- Dim memoryBlock As IntPtr = Marshal.AllocHGlobal(size)
- ReadProcessMemory(processHandle, address, memoryBlock, size, numberOfBytesRead)
- Dim value As CONTAINERS_ENTRY = CType(Marshal.PtrToStructure(memoryBlock, type), CONTAINERS_ENTRY)
- Marshal.FreeHGlobal(memoryBlock)
- Return value
- End Function
- Private Shared Function ReadContainerNode(address As Integer) As CONTAINER_NODE
- Dim numberOfBytesRead As Integer
- Dim type As Type = GetType(CONTAINER_NODE)
- Dim size As Integer = Marshal.SizeOf(type)
- Dim memoryBlock As IntPtr = Marshal.AllocHGlobal(size)
- ReadProcessMemory(processHandle, address, memoryBlock, size, numberOfBytesRead)
- Dim value As CONTAINER_NODE = CType(Marshal.PtrToStructure(memoryBlock, type), CONTAINER_NODE)
- Marshal.FreeHGlobal(memoryBlock)
- Return value
- End Function
- Private Shared Function ReadItems(address As Integer, itemsCount As Integer) As Item()
- Dim numberOfBytesRead As Integer
- Dim itemType As Type = GetType(Item)
- Dim itemSize As Integer = Marshal.SizeOf(itemType)
- Dim size As Integer = itemsCount * itemSize
- Dim memoryBlock As IntPtr = Marshal.AllocHGlobal(size)
- ReadProcessMemory(processHandle, address, memoryBlock, size, numberOfBytesRead)
- Dim value As Item() = New Item(itemsCount - 1) {}
- For i As Integer = 0 To itemsCount - 1
- value(i) = CType(Marshal.PtrToStructure(IntPtr.Add(memoryBlock, i * itemSize), itemType), Item)
- Next
- Marshal.FreeHGlobal(memoryBlock)
- Return value
- End Function
- Private Shared Function ReadContainer(address As Integer) As Container
- Dim numberOfBytesRead As Integer
- Dim type As Type = GetType(Container)
- Dim size As Integer = Marshal.SizeOf(type)
- Dim memoryBlock As IntPtr = Marshal.AllocHGlobal(size)
- ReadProcessMemory(processHandle, address, memoryBlock, size, numberOfBytesRead)
- Dim value As Container = CType(Marshal.PtrToStructure(memoryBlock, type), Container)
- Marshal.FreeHGlobal(memoryBlock)
- Return value
- End Function
- #End Region
- #Region "Variables"
- Shared processHandle As IntPtr
- Shared baseAddress As Integer
- Const containersPointer As Integer = &H5E72C0
- #End Region
- #Region "Containers Handler"
- Private Class ContainersHandler
- #Region "Variables"
- Private nodes As List(Of CONTAINER_NODE)
- Private addresses As HashSet(Of Integer)
- Private entry As CONTAINERS_ENTRY
- #End Region
- #Region "Properties"
- Public ReadOnly Property Count() As Integer
- Get
- Return Me.entry.ContainersCount
- End Get
- End Property
- #End Region
- #Region "Methods"
- Public Sub Reset()
- Me.nodes.Clear()
- Me.addresses.Clear()
- Me.entry = ReadContainersEntry(ReadContainersPointer())
- End Sub
- Public Function Containers() As IEnumerable(Of Container)
- If Me.Count = 0 Then
- Return New List(Of Container)()
- End If
- Dim firstNodeAddress As Integer = ReadInt32(Me.entry.ContainersAddress)
- Dim firstNode As CONTAINER_NODE = ReadContainerNode(firstNodeAddress)
- RecursiveMethod(firstNode, firstNodeAddress)
- Return From node In Me.nodes Select node.Container()
- End Function
- #End Region
- #Region "Private Methods"
- Private Sub RecursiveMethod(node As CONTAINER_NODE, address As Integer)
- Me.addresses.Add(address)
- If Me.nodes.Contains(node) Then
- Return
- End If
- If node.ContainerAddress <> 0 Then
- Me.nodes.Add(node)
- End If
- If CanSearchOnCenter(node) Then
- RecursiveMethod(ReadContainerNode(node.Center), node.Center)
- End If
- If CanSearchOnLeft(node) Then
- RecursiveMethod(ReadContainerNode(node.Left), node.Left)
- End If
- If CanSearchOnRight(node) Then
- RecursiveMethod(ReadContainerNode(node.Right), node.Right)
- End If
- End Sub
- Private Function CanSearchOnCenter(node As CONTAINER_NODE) As Boolean
- Return node.HasCenter AndAlso Me.nodes.Count < Me.Count AndAlso Not Me.addresses.Contains(node.Center)
- End Function
- Private Function CanSearchOnLeft(node As CONTAINER_NODE) As Boolean
- Return node.HasLeft AndAlso Me.nodes.Count < Me.Count AndAlso Not Me.addresses.Contains(node.Left)
- End Function
- Private Function CanSearchOnRight(node As CONTAINER_NODE) As Boolean
- Return node.HasRight AndAlso Me.nodes.Count < Me.Count AndAlso Not Me.addresses.Contains(node.Right)
- End Function
- #End Region
- #Region "Constructors"
- Public Sub New()
- Me.addresses = New HashSet(Of Integer)()
- Me.nodes = New List(Of CONTAINER_NODE)()
- Me.entry = ReadContainersEntry(ReadContainersPointer())
- End Sub
- #End Region
- End Class
- #End Region
- Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
- Dim tibias As Process() = Process.GetProcessesByName("Tibia")
- If tibias.Length = 0 Then
- MessageBox.Show("Tibia not found")
- Else
- Dim process__1 As Process = tibias(tibias.Length - 1)
- processHandle = process__1.Handle
- baseAddress = process__1.MainModule.BaseAddress.ToInt32()
- Dim counter As Integer = 0
- Dim containersHandler As New ContainersHandler()
- If True Then
- ListBox1.Items.Add("Displayed {0} times" & vbCr & vbLf & System.Threading.Interlocked.Increment(counter))
- ListBox1.Items.Add("Containers Count: " & containersHandler.Count)
- ListBox1.Items.Add("")
- For Each container As Container In containersHandler.Containers()
- Dim name As String = container.Namee()
- ListBox1.Items.Add("Container Index: " & container.Index)
- ListBox1.Items.Add("Container ID: " & container.AsItem.Id)
- ListBox1.Items.Add("Container Name: " & name)
- ListBox1.Items.Add("Slots Count: " & container.SlotsCount)
- ListBox1.Items.Add("Items Count: " & container.ItemsCount)
- ListBox1.Items.Add("Items:")
- Dim items As Item() = container.Items()
- For i As Integer = 0 To items.Length - 1
- ListBox1.Items.Add((("Item" & "ID: ") & items(i).Id & " " & "Count: ") & items(i).Count)
- Next
- ListBox1.Items.Add("")
- Next
- containersHandler.Reset()
- End If
- End If
- End Sub
- Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
- ListBox1.Items.Clear()
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement