Advertisement
Guest User

Containers

a guest
Jul 29th, 2013
413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.77 KB | None | 0 0
  1. // ContainersStructure.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <Windows.h>
  6. #include <iostream>
  7. #include <TlHelp32.h>
  8. #include <list>
  9.  
  10. #pragma region Structures
  11. struct ContainersEntry
  12. {
  13.     int unk[2]; // offset 0, 0x4
  14.     int address; // offset 0x8
  15.     int count; // offset 0xC
  16.     // size = 0x10
  17. };
  18.  
  19. struct ContainerNode
  20. {
  21.     int left; // offset 0
  22.     int parent; // offset 0x4
  23.     int right; // offset 0x8
  24.     int containerIndex; // offset 0xC
  25.     int containerAddress; // offset 0x10
  26.     // size = 0x14
  27. };
  28.  
  29. struct TibiaColor
  30. {
  31.     int r; // offset 0
  32.     int g; // offset 0x4
  33.     int b; // offset 0x8
  34.     // size = 0xC
  35. };
  36.  
  37. struct Item
  38. {
  39.     int unk; // offset 0
  40.     int count; // offset 0x4
  41.     int id; // offset 0x8
  42.     TibiaColor color; // offset 0xC
  43.     int isVisible; // offset 0x18
  44.     // size = 0x1C
  45. };
  46.  
  47. struct StdString
  48. {
  49.     union
  50.     {
  51.         char buffer[0x10];
  52.         int pointer;
  53.     } field; // offset 0x0, length = 0x10
  54.  
  55.     int length; // offset 0x10
  56.     int maxLength; // offset 0x14
  57.     int unk; // offset 0x18
  58.     // size = 0x1C
  59.  
  60. };
  61.  
  62. struct Container
  63. {
  64.     int index; // offset 0;
  65.     Item asItem; // offset 0x4
  66.     StdString name; // offset 0x20
  67.     int unk; // offset 0x3C
  68.     int slotsCount; // offset 0x40
  69.     int itemsCount; // offset 0x44
  70.     int firstObjectIndex; // offset 0x48
  71.     int itemsAddress; // offset 0x4C
  72.     // size = 0x50
  73. };
  74. #pragma endregion
  75.  
  76. #pragma region Variables
  77. #define CONTAINERS_BASE_POINTER 0x5E72C0 // Tibia 10.10
  78. HANDLE process;
  79. int baseAddress;
  80. #pragma endregion
  81.  
  82. #pragma region Reading / Converting Memory
  83. int ReadInt(int address)
  84. {
  85.     int value;
  86.     ReadProcessMemory(process, (LPCVOID)address, &value, sizeof(int), NULL);
  87.     return value;
  88. }
  89. void ReadString(int address, char* buffer, int length)
  90. {
  91.     ReadProcessMemory(process, (LPCVOID)address, buffer, length, NULL);
  92. }
  93. void ConvertToString(StdString stdString, char* buffer, int* length)
  94. {
  95.     *length = stdString.length;
  96.     if (stdString.maxLength <= 0xF)
  97.     {
  98.         strcpy(buffer, stdString.field.buffer);
  99.     }
  100.     else
  101.     {
  102.         ReadString(stdString.field.pointer, buffer, stdString.length + 1);
  103.     }
  104. }
  105. int ReadContainersPointer()
  106. {
  107.     return ReadInt(CONTAINERS_BASE_POINTER + baseAddress);
  108. }
  109. ContainersEntry ReadContainersEntry(int address)
  110. {
  111.     ContainersEntry value;
  112.     ReadProcessMemory(process, (LPCVOID)(address), &value, sizeof(ContainersEntry), NULL);
  113.     return value;
  114. }
  115. ContainerNode ReadContainerNode(int address)
  116. {
  117.     ContainerNode value;
  118.     ReadProcessMemory(process, (LPCVOID)address, &value, sizeof(ContainerNode), NULL);
  119.     return value;
  120. }
  121. Container ReadContainer(int address)
  122. {
  123.     Container value;
  124.     ReadProcessMemory(process, (LPCVOID)address, &value, sizeof(Container), NULL);
  125.     return value;
  126. }
  127. void GetItems(int address, Item* items, int itemsCount)
  128. {
  129.     ReadProcessMemory(process, (LPCVOID)address, items, itemsCount * sizeof(Item), NULL);
  130. }
  131. #pragma endregion
  132.  
  133. #pragma region Window / Process Utils
  134. MODULEENTRY32 GetMainModule(DWORD processId)
  135. {
  136.     MODULEENTRY32 moduleEntry = {0};
  137.     HANDLE snapShot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, processId);
  138.  
  139.     if(!snapShot)
  140.         return moduleEntry;
  141.  
  142.     moduleEntry.dwSize = sizeof(moduleEntry);
  143.     Module32First(snapShot, &moduleEntry);
  144.  
  145.     CloseHandle(snapShot);
  146.     return moduleEntry;
  147. }
  148. BYTE* GetTibiaBaseAddress(DWORD processId)
  149. {
  150.     return GetMainModule(processId).modBaseAddr;
  151. }
  152. HWND FindTibiaWindow()
  153. {
  154.     return FindWindow(L"TibiaClient", NULL);
  155. }
  156. void OpenTibiaProcess(HWND windowHandle)
  157. {
  158.     DWORD processId;
  159.     GetWindowThreadProcessId(windowHandle, &processId);
  160.     baseAddress = (int)GetTibiaBaseAddress(processId);
  161.  
  162.     process = OpenProcess(PROCESS_ALL_ACCESS, false, processId);
  163. }
  164. #pragma endregion
  165.  
  166. #pragma region Container Utils
  167. void RecursiveMethod(ContainerNode *node, int entryAddress, std::list<Container> *containers)
  168. {
  169.     if (node->left != entryAddress)
  170.     {
  171.         ContainerNode leftNode = ReadContainerNode(node->left);
  172.         RecursiveMethod(&leftNode, entryAddress, containers);
  173.     }
  174.  
  175.     containers->push_back(ReadContainer(node->containerAddress));
  176.  
  177.     if (node->right != entryAddress)
  178.     {
  179.         ContainerNode rightNode = ReadContainerNode(node->right);
  180.         RecursiveMethod(&rightNode, entryAddress, containers);
  181.     }
  182. }
  183. std::list<Container> GetContainers()
  184. {
  185.     std::list<Container> containers;
  186.  
  187.     ContainersEntry containersInfo = ReadContainersEntry(ReadContainersPointer());
  188.  
  189.     if (containersInfo.count > 0)
  190.     {
  191.         ContainerNode entryNode = ReadContainerNode(containersInfo.address);
  192.         entryNode = ReadContainerNode(entryNode.parent);
  193.  
  194.         RecursiveMethod(&entryNode, containersInfo.address, &containers);
  195.     }
  196.  
  197.     return containers;
  198. }
  199. #pragma endregion
  200.  
  201. #pragma region Application Entrypoint
  202. int _tmain(int argc, _TCHAR* argv[])
  203. {
  204.     HWND tibiaWindow = FindTibiaWindow();
  205.     if (tibiaWindow)
  206.     {
  207.         OpenTibiaProcess(tibiaWindow);
  208.  
  209.         char input;
  210.         do
  211.         {
  212.             std::list<Container> containers = GetContainers();
  213.             std::cout << "Containers Count: " << containers.size() << std::endl;
  214.        
  215.             for (std::list<Container>::const_iterator container = containers.begin(); container != containers.end(); container++)
  216.             {
  217.                 Item* items = new Item[container->itemsCount];
  218.                 GetItems(container->itemsAddress, items, container->itemsCount);
  219.            
  220.                 char name[32];
  221.                 int length;
  222.                 ConvertToString(container->name, name, &length);
  223.  
  224.                 printf("\r\nContainer [%d] %s\r\n\r\n", container->asItem.id, name);
  225.                 for (int i = 0; i < container->itemsCount; i++)
  226.                     printf("    Count: %3d, Id: %5d\r\n", items[i].count, items[i].id);
  227.        
  228.                 delete [] items;
  229.             }
  230.  
  231.             std::cout << "\r\nPress Y to continue..." << std::endl;
  232.             std::cin >> input;
  233.             std::cout << "\r\n";
  234.         }
  235.         while (input == 'Y' || input == 'y');
  236.  
  237.         CloseHandle(process);
  238.     }
  239.     else
  240.     {
  241.         std::cout << "Tibia not found.\r\n\r\n";
  242.     }
  243.     system("pause");
  244.  
  245.     return 0;
  246. }
  247. #pragma endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement