Advertisement
Guest User

Untitled

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