Advertisement
Guest User

Containers

a guest
Jul 31st, 2013
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 16.01 KB | None | 0 0
  1. Imports System.Linq
  2. Imports System.Diagnostics
  3. Imports System.Runtime.InteropServices
  4. Imports System.Collections.Generic
  5.  
  6. Public Class Form1
  7.  
  8. #Region "P/Invoke"
  9.     <DllImport("kernel32.dll")> _
  10.     Private Shared Function ReadProcessMemory(processHandle As IntPtr, address As Integer, buffer As IntPtr, size As Integer, ByRef numberOfBytesRead As Integer) As Integer
  11.     End Function
  12. #End Region
  13. #Region "Structures"
  14.     Private Structure STD_STRING
  15.         <MarshalAs(UnmanagedType.ByValArray, SizeConst:=&H10)> _
  16.         Private buffer As Byte()
  17.         ' offset 0x0, length = 0x10
  18.         Private length As Integer
  19.         ' offset 0x10
  20.         Private maxLength As Integer
  21.         ' offset 0x14
  22.         Private unk As Integer
  23.         ' offset 0x18
  24.         Public Function Text() As String
  25.             If Me.maxLength > &HF Then
  26.                 Return ReadString(BitConverter.ToInt32(buffer, 0), Me.length, False)
  27.             End If
  28.  
  29.             Dim value As Char() = New Char(Me.length - 1) {}
  30.             For i As Integer = 0 To Me.length - 1
  31.                 If Me.buffer(i) = 0 Then
  32.                     Return New String(value, 0, i)
  33.                 Else
  34.                     value(i) = ChrW(buffer(i))
  35.                 End If
  36.             Next
  37.  
  38.             Return New String(value)
  39.         End Function
  40.     End Structure
  41.  
  42.     Private Structure CONTAINER_NODE
  43.         Private m_left As Integer
  44.         ' offset 0x0
  45.         Private m_center As Integer
  46.         ' offset 0x4
  47.         Private m_right As Integer
  48.         ' offset 0x8
  49.         Private m_containerIndex As Integer
  50.         ' offset 0xC
  51.         Private m_containerAddress As Integer
  52.         ' offset 0x10
  53.         Public ReadOnly Property Left() As Integer
  54.             Get
  55.                 Return Me.m_left
  56.             End Get
  57.         End Property
  58.         Public ReadOnly Property Center() As Integer
  59.             Get
  60.                 Return Me.m_center
  61.             End Get
  62.         End Property
  63.         Public ReadOnly Property Right() As Integer
  64.             Get
  65.                 Return Me.m_right
  66.             End Get
  67.         End Property
  68.         Public ReadOnly Property ContainerIndex() As Integer
  69.             Get
  70.                 Return Me.m_containerIndex
  71.             End Get
  72.         End Property
  73.         Public ReadOnly Property ContainerAddress() As Integer
  74.             Get
  75.                 Return Me.m_containerAddress
  76.             End Get
  77.         End Property
  78.  
  79.         Public ReadOnly Property HasLeft() As Boolean
  80.             Get
  81.                 Return Me.m_left <> 0
  82.             End Get
  83.         End Property
  84.         Public ReadOnly Property HasCenter() As Boolean
  85.             Get
  86.                 Return Me.m_center <> 0
  87.             End Get
  88.         End Property
  89.         Public ReadOnly Property HasRight() As Boolean
  90.             Get
  91.                 Return Me.m_right <> 0
  92.             End Get
  93.         End Property
  94.         Public ReadOnly Property HasContainer() As Boolean
  95.             Get
  96.                 Return Me.m_containerAddress <> 0
  97.             End Get
  98.         End Property
  99.  
  100.         Public Function Container() As Container
  101.             Return ReadContainer(Me.m_containerAddress)
  102.         End Function
  103.         Public Function CompareTo(other As CONTAINER_NODE) As Integer
  104.             Return Me.m_containerIndex.CompareTo(other.ContainerIndex)
  105.         End Function
  106.         Public Function Compare(x As CONTAINER_NODE, y As CONTAINER_NODE) As Integer
  107.             Return x.ContainerIndex.CompareTo(y.ContainerIndex)
  108.         End Function
  109.     End Structure
  110.  
  111.     Private Structure CONTAINERS_ENTRY
  112.         Private unk0 As Integer
  113.         ' offset 0x0
  114.         Private unk1 As Integer
  115.         ' offset 0x4
  116.         Private m_containersAddress As Integer
  117.         ' offset 0x8
  118.         Private m_containersCount As Integer
  119.         ' offset 0xC
  120.         Public ReadOnly Property ContainersAddress() As Integer
  121.             Get
  122.                 Return Me.m_containersAddress
  123.             End Get
  124.         End Property
  125.         Public ReadOnly Property ContainersCount() As Integer
  126.             Get
  127.                 Return Me.m_containersCount
  128.             End Get
  129.         End Property
  130.     End Structure
  131.  
  132.     Private Structure Item
  133.         Private unk As Integer
  134.         ' offset 0x0
  135.         Private m_count As Integer
  136.         ' offset 0x4
  137.         Private m_id As Integer
  138.         ' offset 0x8
  139.         Private m_r As Integer
  140.         ' offset 0xC
  141.         Private m_g As Integer
  142.         ' offset 0x10
  143.         Private m_b As Integer
  144.         ' offset 0x14
  145.         Private m_isVisible As Integer
  146.         ' offset 0x18
  147.         Public ReadOnly Property Count() As Integer
  148.             Get
  149.                 Return Me.m_count
  150.             End Get
  151.         End Property
  152.         Public ReadOnly Property Id() As Integer
  153.             Get
  154.                 Return Me.m_id
  155.             End Get
  156.         End Property
  157.         Public ReadOnly Property R() As Integer
  158.             Get
  159.                 Return Me.m_r
  160.             End Get
  161.         End Property
  162.         Public ReadOnly Property G() As Integer
  163.             Get
  164.                 Return Me.m_g
  165.             End Get
  166.         End Property
  167.         Public ReadOnly Property B() As Integer
  168.             Get
  169.                 Return Me.m_b
  170.             End Get
  171.         End Property
  172.         Public ReadOnly Property IsVisible() As Boolean
  173.             Get
  174.                 Return Me.m_isVisible <> 0
  175.             End Get
  176.         End Property
  177.     End Structure
  178.  
  179.     Private Structure Container
  180.         Private m_index As Integer
  181.         ' offset 0
  182.         Private m_asItem As Item
  183.         ' offset 0x4
  184.         Private name As STD_STRING
  185.         ' offset 0x20
  186.         Private unk0 As Integer
  187.         ' offset 0x3C
  188.         Private m_slotsCount As Integer
  189.         ' offset 0x40
  190.         Private m_itemsCount As Integer
  191.         ' offset 0x44
  192.         Private unk1 As Integer
  193.         ' offset 0x48
  194.         Private itemsAddress As Integer
  195.         ' offset 0x4C
  196.         Public ReadOnly Property Index() As Integer
  197.             Get
  198.                 Return Me.m_index
  199.             End Get
  200.         End Property
  201.         Public ReadOnly Property AsItem() As Item
  202.             Get
  203.                 Return Me.m_asItem
  204.             End Get
  205.         End Property
  206.         Public ReadOnly Property SlotsCount() As Integer
  207.             Get
  208.                 Return Me.m_slotsCount
  209.             End Get
  210.         End Property
  211.         Public ReadOnly Property ItemsCount() As Integer
  212.             Get
  213.                 Return Me.m_itemsCount
  214.             End Get
  215.         End Property
  216.  
  217.         Public Function Namee() As String
  218.             Return Me.name.Text()
  219.         End Function
  220.         Public Function Items() As Item()
  221.             Return ReadItems(Me.itemsAddress, Me.m_itemsCount)
  222.         End Function
  223.     End Structure
  224. #End Region
  225. #Region "Reading Memory"
  226.     Private Shared Function ReadInt32(address As Integer) As Integer
  227.         Dim numberOfBytesRead As Integer
  228.         Dim size As Integer = 4
  229.  
  230.         Dim memoryBlock As IntPtr = Marshal.AllocHGlobal(size)
  231.         ReadProcessMemory(processHandle, address, memoryBlock, size, numberOfBytesRead)
  232.  
  233.         Dim value As Integer = CInt(Marshal.PtrToStructure(memoryBlock, GetType(Integer)))
  234.         Marshal.FreeHGlobal(memoryBlock)
  235.  
  236.         Return value
  237.     End Function
  238.     Private Shared Function ReadString(address As Integer, length As Integer, Optional checkNullChar As Boolean = True) As String
  239.         Dim numberOfBytesRead As Integer
  240.         Dim size As Integer = length
  241.  
  242.         Dim memoryBlock As IntPtr = Marshal.AllocHGlobal(size)
  243.         ReadProcessMemory(processHandle, address, memoryBlock, size, numberOfBytesRead)
  244.  
  245.         Dim value As String = Marshal.PtrToStringAnsi(memoryBlock, length)
  246.         Marshal.FreeHGlobal(memoryBlock)
  247.  
  248.         If checkNullChar Then
  249.             Dim index As Integer = value.IndexOf(ControlChars.NullChar)
  250.             Return If(index = -1, value, value.Substring(0, index))
  251.         End If
  252.         Return value
  253.     End Function
  254.     Private Shared Function ReadStdString(address As Integer) As String
  255.         Dim numberOfBytesRead As Integer
  256.         Dim type As Type = GetType(STD_STRING)
  257.         Dim size As Integer = Marshal.SizeOf(type)
  258.  
  259.         Dim memoryBlock As IntPtr = Marshal.AllocHGlobal(size)
  260.         ReadProcessMemory(processHandle, address, memoryBlock, size, numberOfBytesRead)
  261.  
  262.         Dim value As STD_STRING = CType(Marshal.PtrToStructure(memoryBlock, type), STD_STRING)
  263.         Marshal.FreeHGlobal(memoryBlock)
  264.  
  265.         Return value.Text()
  266.     End Function
  267.     Private Shared Function ReadContainersPointer() As Integer
  268.         Return ReadInt32(containersPointer + baseAddress)
  269.     End Function
  270.     Private Shared Function ReadContainersEntry(address As Integer) As CONTAINERS_ENTRY
  271.         Dim numberOfBytesRead As Integer
  272.         Dim type As Type = GetType(CONTAINERS_ENTRY)
  273.         Dim size As Integer = Marshal.SizeOf(type)
  274.  
  275.         Dim memoryBlock As IntPtr = Marshal.AllocHGlobal(size)
  276.         ReadProcessMemory(processHandle, address, memoryBlock, size, numberOfBytesRead)
  277.  
  278.         Dim value As CONTAINERS_ENTRY = CType(Marshal.PtrToStructure(memoryBlock, type), CONTAINERS_ENTRY)
  279.         Marshal.FreeHGlobal(memoryBlock)
  280.  
  281.         Return value
  282.     End Function
  283.     Private Shared Function ReadContainerNode(address As Integer) As CONTAINER_NODE
  284.         Dim numberOfBytesRead As Integer
  285.         Dim type As Type = GetType(CONTAINER_NODE)
  286.         Dim size As Integer = Marshal.SizeOf(type)
  287.  
  288.         Dim memoryBlock As IntPtr = Marshal.AllocHGlobal(size)
  289.         ReadProcessMemory(processHandle, address, memoryBlock, size, numberOfBytesRead)
  290.  
  291.         Dim value As CONTAINER_NODE = CType(Marshal.PtrToStructure(memoryBlock, type), CONTAINER_NODE)
  292.         Marshal.FreeHGlobal(memoryBlock)
  293.  
  294.         Return value
  295.     End Function
  296.     Private Shared Function ReadItems(address As Integer, itemsCount As Integer) As Item()
  297.         Dim numberOfBytesRead As Integer
  298.         Dim itemType As Type = GetType(Item)
  299.         Dim itemSize As Integer = Marshal.SizeOf(itemType)
  300.         Dim size As Integer = itemsCount * itemSize
  301.  
  302.         Dim memoryBlock As IntPtr = Marshal.AllocHGlobal(size)
  303.         ReadProcessMemory(processHandle, address, memoryBlock, size, numberOfBytesRead)
  304.  
  305.         Dim value As Item() = New Item(itemsCount - 1) {}
  306.         For i As Integer = 0 To itemsCount - 1
  307.             value(i) = CType(Marshal.PtrToStructure(IntPtr.Add(memoryBlock, i * itemSize), itemType), Item)
  308.         Next
  309.  
  310.         Marshal.FreeHGlobal(memoryBlock)
  311.  
  312.         Return value
  313.     End Function
  314.     Private Shared Function ReadContainer(address As Integer) As Container
  315.         Dim numberOfBytesRead As Integer
  316.         Dim type As Type = GetType(Container)
  317.         Dim size As Integer = Marshal.SizeOf(type)
  318.  
  319.         Dim memoryBlock As IntPtr = Marshal.AllocHGlobal(size)
  320.         ReadProcessMemory(processHandle, address, memoryBlock, size, numberOfBytesRead)
  321.  
  322.         Dim value As Container = CType(Marshal.PtrToStructure(memoryBlock, type), Container)
  323.         Marshal.FreeHGlobal(memoryBlock)
  324.  
  325.         Return value
  326.     End Function
  327. #End Region
  328. #Region "Variables"
  329.     Shared processHandle As IntPtr
  330.     Shared baseAddress As Integer
  331.     Const containersPointer As Integer = &H5E72C0
  332. #End Region
  333. #Region "Containers Handler"
  334.     Private Class ContainersHandler
  335. #Region "Variables"
  336.         Private nodes As List(Of CONTAINER_NODE)
  337.         Private addresses As HashSet(Of Integer)
  338.         Private entry As CONTAINERS_ENTRY
  339. #End Region
  340.  
  341. #Region "Properties"
  342.         Public ReadOnly Property Count() As Integer
  343.             Get
  344.                 Return Me.entry.ContainersCount
  345.             End Get
  346.         End Property
  347. #End Region
  348.  
  349. #Region "Methods"
  350.         Public Sub Reset()
  351.             Me.nodes.Clear()
  352.             Me.addresses.Clear()
  353.             Me.entry = ReadContainersEntry(ReadContainersPointer())
  354.         End Sub
  355.         Public Function Containers() As IEnumerable(Of Container)
  356.             If Me.Count = 0 Then
  357.                 Return New List(Of Container)()
  358.             End If
  359.  
  360.             Dim firstNodeAddress As Integer = ReadInt32(Me.entry.ContainersAddress)
  361.             Dim firstNode As CONTAINER_NODE = ReadContainerNode(firstNodeAddress)
  362.             RecursiveMethod(firstNode, firstNodeAddress)
  363.  
  364.             Return From node In Me.nodes Select node.Container()
  365.  
  366.         End Function
  367. #End Region
  368.  
  369. #Region "Private Methods"
  370.         Private Sub RecursiveMethod(node As CONTAINER_NODE, address As Integer)
  371.             Me.addresses.Add(address)
  372.  
  373.             If Me.nodes.Contains(node) Then
  374.                 Return
  375.             End If
  376.  
  377.             If node.ContainerAddress <> 0 Then
  378.                 Me.nodes.Add(node)
  379.             End If
  380.  
  381.             If CanSearchOnCenter(node) Then
  382.                 RecursiveMethod(ReadContainerNode(node.Center), node.Center)
  383.             End If
  384.  
  385.             If CanSearchOnLeft(node) Then
  386.                 RecursiveMethod(ReadContainerNode(node.Left), node.Left)
  387.             End If
  388.  
  389.             If CanSearchOnRight(node) Then
  390.                 RecursiveMethod(ReadContainerNode(node.Right), node.Right)
  391.             End If
  392.         End Sub
  393.         Private Function CanSearchOnCenter(node As CONTAINER_NODE) As Boolean
  394.             Return node.HasCenter AndAlso Me.nodes.Count < Me.Count AndAlso Not Me.addresses.Contains(node.Center)
  395.         End Function
  396.         Private Function CanSearchOnLeft(node As CONTAINER_NODE) As Boolean
  397.             Return node.HasLeft AndAlso Me.nodes.Count < Me.Count AndAlso Not Me.addresses.Contains(node.Left)
  398.         End Function
  399.         Private Function CanSearchOnRight(node As CONTAINER_NODE) As Boolean
  400.             Return node.HasRight AndAlso Me.nodes.Count < Me.Count AndAlso Not Me.addresses.Contains(node.Right)
  401.         End Function
  402. #End Region
  403.  
  404. #Region "Constructors"
  405.         Public Sub New()
  406.             Me.addresses = New HashSet(Of Integer)()
  407.             Me.nodes = New List(Of CONTAINER_NODE)()
  408.             Me.entry = ReadContainersEntry(ReadContainersPointer())
  409.         End Sub
  410. #End Region
  411.     End Class
  412. #End Region
  413.  
  414.  
  415.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  416.         Dim tibias As Process() = Process.GetProcessesByName("Tibia")
  417.         If tibias.Length = 0 Then
  418.             MessageBox.Show("Tibia not found")
  419.         Else
  420.             Dim process__1 As Process = tibias(tibias.Length - 1)
  421.             processHandle = process__1.Handle
  422.             baseAddress = process__1.MainModule.BaseAddress.ToInt32()
  423.  
  424.             Dim counter As Integer = 0
  425.  
  426.             Dim containersHandler As New ContainersHandler()
  427.             If True Then
  428.                 ListBox1.Items.Add("Displayed {0} times" & vbCr & vbLf & System.Threading.Interlocked.Increment(counter))
  429.                 ListBox1.Items.Add("Containers Count: " & containersHandler.Count)
  430.                 ListBox1.Items.Add("")
  431.  
  432.                 For Each container As Container In containersHandler.Containers()
  433.                     Dim name As String = container.Namee()
  434.                     ListBox1.Items.Add("Container Index: " & container.Index)
  435.                     ListBox1.Items.Add("Container ID: " & container.AsItem.Id)
  436.                     ListBox1.Items.Add("Container Name: " & name)
  437.                     ListBox1.Items.Add("Slots Count: " & container.SlotsCount)
  438.                     ListBox1.Items.Add("Items Count: " & container.ItemsCount)
  439.                     ListBox1.Items.Add("Items:")
  440.  
  441.                     Dim items As Item() = container.Items()
  442.                     For i As Integer = 0 To items.Length - 1
  443.                         ListBox1.Items.Add((("Item" & "ID: ") & items(i).Id & " " & "Count: ") & items(i).Count)
  444.                     Next
  445.  
  446.                     ListBox1.Items.Add("")
  447.                 Next
  448.  
  449.                 containersHandler.Reset()
  450.  
  451.             End If
  452.         End If
  453.     End Sub
  454.  
  455.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  456.         ListBox1.Items.Clear()
  457.     End Sub
  458. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement