Advertisement
Guest User

this one by blequi

a guest
Nov 20th, 2013
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.72 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Diagnostics;
  4. using System.Runtime.InteropServices;
  5. using System.Collections.Generic;
  6.  
  7. namespace NewContainers
  8. {
  9.     class Program
  10.     {
  11.         #region P/Invoke
  12.         [DllImport("kernel32.dll")]
  13.         static extern int ReadProcessMemory(
  14.             IntPtr processHandle,
  15.             int address,
  16.             IntPtr buffer,
  17.             int size,
  18.             out int numberOfBytesRead
  19.         );
  20.         #endregion
  21.  
  22.         #region Structures
  23.         struct STD_STRING
  24.         {
  25.             [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)]
  26.             byte[] buffer; // offset 0x0, length = 0x10
  27.             int length; // offset 0x10
  28.             int maxLength; // offset 0x14
  29.             int unk; // offset 0x18
  30.             public string Text()
  31.             {
  32.                 if (this.maxLength > 0xF)
  33.                     return ReadString(BitConverter.ToInt32(buffer, 0), this.length, false);
  34.  
  35.                 char[] value = new char[this.length];
  36.                 for (int i = 0; i < this.length; i++)
  37.                     if (this.buffer[i] == 0)
  38.                         return new string(value, 0, i);
  39.                     else
  40.                         value[i] = (char)buffer[i];
  41.  
  42.                 return new string(value);
  43.             }
  44.         }
  45.  
  46.         struct CONTAINER_NODE : IComparable<CONTAINER_NODE>, IComparer<CONTAINER_NODE>
  47.         {
  48.             int left; // offset 0x0
  49.             int center; // offset 0x4
  50.             int right; // offset 0x8
  51.             int containerIndex; // offset 0xC
  52.             int containerAddress; // offset 0x10
  53.  
  54.             public int Left { get { return this.left; } }
  55.             public int Center { get { return this.center; } }
  56.             public int Right { get { return this.right; } }
  57.             public int ContainerIndex { get { return this.containerIndex; } }
  58.             public int ContainerAddress { get { return this.containerAddress; } }
  59.  
  60.             public bool HasLeft { get { return this.left != 0; } }
  61.             public bool HasCenter { get { return this.center != 0; } }
  62.             public bool HasRight { get { return this.right != 0; } }
  63.             public bool HasContainer { get { return this.containerAddress != 0; } }
  64.  
  65.             public Container Container() { return ReadContainer(this.containerAddress); }
  66.             public int CompareTo(CONTAINER_NODE other) { return this.containerIndex.CompareTo(other.containerIndex); }
  67.             public int Compare(CONTAINER_NODE x, CONTAINER_NODE y) { return x.containerIndex.CompareTo(y.containerIndex); }
  68.         }
  69.  
  70.         struct CONTAINERS_ENTRY
  71.         {
  72.             int unk0; // offset 0x0
  73.             int unk1; // offset 0x4
  74.             int containersAddress; // offset 0x8
  75.             int containersCount; // offset 0xC
  76.  
  77.             public int ContainersAddress { get { return this.containersAddress; } }
  78.             public int ContainersCount { get { return this.containersCount; } }
  79.         }
  80.  
  81.         struct Item
  82.         {
  83.             int slot; // offset 0x0
  84.             int count; // offset 0x4
  85.             int id; // offset 0x8
  86.             int r; // offset 0xC
  87.             int g; // offset 0x10          
  88.             int b; // offset 0x14              
  89.             int isVisible; // offset 0x18  
  90.             int unk;//0xc
  91.             int un0;//0x10
  92.             public int Count { get { return this.count; } }
  93.             public int Id { get { return this.id; } }
  94.             public int R { get { return this.r; } }
  95.             public int G { get { return this.g; } }
  96.             public int B { get { return this.b; } }
  97.             public bool IsVisible { get { return this.isVisible != 0; } }
  98.         }
  99.         struct Container
  100.         {
  101.             int index; // offset 0
  102.             Item asItem; // offset 0x4
  103.             STD_STRING name; // offset 0x20
  104.             int unk0; // offset 0x3C
  105.             int slotsCount; // offset 0x40
  106.             int itemsCount; // offset 0x44
  107.             int unk1; // offset 0x48
  108.             int itemsAddress; // offset 0x4C
  109.             public int Index { get { return this.index; } }
  110.             public Item AsItem { get { return this.asItem; } }
  111.             public int SlotsCount { get { return this.slotsCount; } }
  112.             public int ItemsCount { get { return this.itemsCount; } }
  113.             public string Name() { return this.name.Text(); }
  114.             public Item[] Items() { return ReadItems(this.itemsAddress, this.itemsCount); }
  115.         }
  116.         #endregion
  117.  
  118.         #region Reading Memory
  119.         static int ReadInt32(int address)
  120.         {
  121.             int numberOfBytesRead;
  122.             int size = sizeof(int);
  123.  
  124.             IntPtr memoryBlock = Marshal.AllocHGlobal(size);
  125.             ReadProcessMemory(processHandle, address, memoryBlock, size, out numberOfBytesRead);
  126.  
  127.             int value = (int)Marshal.PtrToStructure(memoryBlock, typeof(int));
  128.             Marshal.FreeHGlobal(memoryBlock);
  129.  
  130.             return value;
  131.         }
  132.         static string ReadString(int address, int length, bool checkNullChar = true)
  133.         {
  134.             int numberOfBytesRead;
  135.             int size = length;
  136.  
  137.             IntPtr memoryBlock = Marshal.AllocHGlobal(size);
  138.             ReadProcessMemory(processHandle, address, memoryBlock, size, out numberOfBytesRead);
  139.  
  140.             string value = Marshal.PtrToStringAnsi(memoryBlock, length);
  141.             Marshal.FreeHGlobal(memoryBlock);
  142.  
  143.             if (checkNullChar)
  144.             {
  145.                 int index = value.IndexOf('\0');
  146.                 return index == -1 ? value : value.Substring(0, index);
  147.             }
  148.             return value;
  149.         }
  150.         static string ReadStdString(int address)
  151.         {
  152.             int numberOfBytesRead;
  153.             Type type = typeof(STD_STRING);
  154.             int size = Marshal.SizeOf(type);
  155.  
  156.             IntPtr memoryBlock = Marshal.AllocHGlobal(size);
  157.             ReadProcessMemory(processHandle, address, memoryBlock, size, out numberOfBytesRead);
  158.  
  159.             STD_STRING value = (STD_STRING)Marshal.PtrToStructure(memoryBlock, type);
  160.             Marshal.FreeHGlobal(memoryBlock);
  161.  
  162.             return value.Text();
  163.         }
  164.         static int ReadContainersPointer()
  165.         {
  166.             return ReadInt32(containersPointer + baseAddress);
  167.         }
  168.         static CONTAINERS_ENTRY ReadContainersEntry(int address)
  169.         {
  170.             int numberOfBytesRead;
  171.             Type type = typeof(CONTAINERS_ENTRY);
  172.             int size = Marshal.SizeOf(type);
  173.  
  174.             IntPtr memoryBlock = Marshal.AllocHGlobal(size);
  175.             ReadProcessMemory(processHandle, address, memoryBlock, size, out numberOfBytesRead);
  176.  
  177.             CONTAINERS_ENTRY value = (CONTAINERS_ENTRY)Marshal.PtrToStructure(memoryBlock, type);
  178.             Marshal.FreeHGlobal(memoryBlock);
  179.  
  180.             return value;
  181.         }
  182.         static CONTAINER_NODE ReadContainerNode(int address)
  183.         {
  184.             int numberOfBytesRead;
  185.             Type type = typeof(CONTAINER_NODE);
  186.             int size = Marshal.SizeOf(type);
  187.  
  188.             IntPtr memoryBlock = Marshal.AllocHGlobal(size);
  189.             ReadProcessMemory(processHandle, address, memoryBlock, size, out numberOfBytesRead);
  190.  
  191.             CONTAINER_NODE value = (CONTAINER_NODE)Marshal.PtrToStructure(memoryBlock, type);
  192.             Marshal.FreeHGlobal(memoryBlock);
  193.  
  194.             return value;
  195.         }
  196.         static Item[] ReadItems(int address, int itemsCount)
  197.         {
  198.             int numberOfBytesRead;
  199.             Type itemType = typeof(Item);
  200.             int itemSize = Marshal.SizeOf(itemType);      
  201.             int size = itemsCount * itemSize;
  202.            
  203.  
  204.             IntPtr memoryBlock = Marshal.AllocHGlobal(size);
  205.             ReadProcessMemory(processHandle, address, memoryBlock, size, out numberOfBytesRead);
  206.  
  207.             Item[] value = new Item[itemsCount];
  208.             for (int i = 0; i < itemsCount; i++)
  209.             {
  210.                 value[i] = (Item)Marshal.PtrToStructure(IntPtr.Add(memoryBlock, i * itemSize), itemType);
  211.             }
  212.  
  213.             Marshal.FreeHGlobal(memoryBlock);
  214.  
  215.             return value;
  216.         }
  217.         static Container ReadContainer(int address)
  218.         {
  219.             int numberOfBytesRead;
  220.             Type type = typeof(Container);
  221.             int size = Marshal.SizeOf(type);
  222.  
  223.             IntPtr memoryBlock = Marshal.AllocHGlobal(size);
  224.             ReadProcessMemory(processHandle, address, memoryBlock, size, out numberOfBytesRead);
  225.  
  226.             Container value = (Container)Marshal.PtrToStructure(memoryBlock, type);
  227.             Marshal.FreeHGlobal(memoryBlock);
  228.  
  229.             return value;
  230.         }
  231.         #endregion
  232.  
  233.         #region Variables
  234.         static IntPtr processHandle;
  235.         static int baseAddress;
  236.         const int containersPointer = 0x5EB3D4;
  237.         #endregion
  238.  
  239.         #region Containers Handler
  240.         class ContainersHandler
  241.         {
  242.             #region Variables
  243.             SortedSet<CONTAINER_NODE> nodes;
  244.             HashSet<int> addresses;
  245.             CONTAINERS_ENTRY entry;
  246.             #endregion
  247.  
  248.             #region Properties
  249.             public int Count { get { return this.entry.ContainersCount; } }
  250.             #endregion
  251.  
  252.             #region Methods
  253.             public void Reset()
  254.             {
  255.                 this.nodes.Clear();
  256.                 this.addresses.Clear();
  257.                 this.entry = ReadContainersEntry(ReadContainersPointer());
  258.             }
  259.             public IEnumerable<Container> Containers()
  260.             {
  261.                 if (this.Count == 0)
  262.                     return new List<Container>();
  263.  
  264.                 int firstNodeAddress = ReadInt32(this.entry.ContainersAddress);
  265.                 CONTAINER_NODE firstNode = ReadContainerNode(firstNodeAddress);
  266.                 RecursiveMethod(firstNode, firstNodeAddress);
  267.  
  268.                 return from node in this.nodes select node.Container();
  269.             }
  270.             #endregion
  271.  
  272.             #region Private Methods
  273.             void RecursiveMethod(CONTAINER_NODE node, int address)
  274.             {
  275.                 this.addresses.Add(address);
  276.  
  277.                 if (this.nodes.Contains(node))
  278.                     return;
  279.  
  280.                 if (node.ContainerAddress != 0)
  281.                     this.nodes.Add(node);
  282.  
  283.                 if (CanSearchOnCenter(node))
  284.                     RecursiveMethod(ReadContainerNode(node.Center), node.Center);
  285.  
  286.                 if (CanSearchOnLeft(node))
  287.                     RecursiveMethod(ReadContainerNode(node.Left), node.Left);
  288.  
  289.                 if (CanSearchOnRight(node))
  290.                     RecursiveMethod(ReadContainerNode(node.Right), node.Right);
  291.             }
  292.             bool CanSearchOnCenter(CONTAINER_NODE node)
  293.             {
  294.                 return node.HasCenter && this.nodes.Count < this.Count && !this.addresses.Contains(node.Center);
  295.             }
  296.             bool CanSearchOnLeft(CONTAINER_NODE node)
  297.             {
  298.                 return node.HasLeft && this.nodes.Count < this.Count && !this.addresses.Contains(node.Left);
  299.             }
  300.             bool CanSearchOnRight(CONTAINER_NODE node)
  301.             {
  302.                 return node.HasRight && this.nodes.Count < this.Count && !this.addresses.Contains(node.Right);
  303.             }
  304.             #endregion
  305.  
  306.             #region Constructors
  307.             public ContainersHandler()
  308.             {
  309.                 this.addresses = new HashSet<int>();
  310.                 this.nodes = new SortedSet<CONTAINER_NODE>();
  311.                 this.entry = ReadContainersEntry(ReadContainersPointer());
  312.             }
  313.             #endregion
  314.         }
  315.         #endregion
  316.  
  317.         #region Application Entrypoint
  318.         static void Main(string[] args)
  319.         {
  320.             Process[] tibias = Process.GetProcessesByName("Tibia");
  321.             if (tibias.Length == 0)
  322.             {
  323.                 Console.WriteLine("Tibia not found");
  324.             }
  325.             else
  326.             {
  327.                 Process process = tibias[tibias.Length - 1];
  328.                 processHandle = process.Handle;
  329.                 baseAddress = process.MainModule.BaseAddress.ToInt32();
  330.  
  331.                 int counter = 0;
  332.                 char input;
  333.                 ContainersHandler containersHandler = new ContainersHandler();
  334.                 do
  335.                 {
  336.                     Console.WriteLine("Displayed {0} times\r\n", ++counter);
  337.                     Console.WriteLine("Containers Count: " + containersHandler.Count);
  338.                     Console.WriteLine();
  339.  
  340.                     foreach (Container container in containersHandler.Containers())
  341.                     {
  342.                         string name = container.Name();
  343.                         Console.WriteLine("Container [{0}]: {1}", container.AsItem.Id, name);
  344.                         Console.WriteLine("Slots Count: " + container.SlotsCount);
  345.                         Console.WriteLine("Items Count: " + container.ItemsCount);
  346.                         Console.WriteLine("Items:");
  347.                         //items.Length
  348.                         Item[] items = container.Items();
  349.                         for (int i = 0; i < items.Length; i++)
  350.                             Console.WriteLine("Item [Count: {0}, Id: {1}]", items[i].Count, items[i].Id);
  351.  
  352.                         Console.WriteLine();
  353.                     }
  354.  
  355.                     containersHandler.Reset();
  356.  
  357.                     Console.WriteLine("Coninue? Press Y to confirm...");
  358.  
  359.                     input = Console.ReadKey().KeyChar;
  360.                     Console.WriteLine("\r\n");
  361.                 }
  362.                 while (input == 'Y' || input == 'y');
  363.  
  364.             }
  365.             Console.WriteLine("Press anything to exit...");
  366.             Console.ReadKey();
  367.         }
  368.         #endregion
  369.     }
  370. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement