Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Diagnostics;
- using System.Runtime.InteropServices;
- using System.Collections.Generic;
- namespace NewContainers
- {
- class Program
- {
- #region P/Invoke
- [DllImport("kernel32.dll")]
- static extern int ReadProcessMemory(
- IntPtr processHandle,
- int address,
- IntPtr buffer,
- int size,
- out int numberOfBytesRead
- );
- #endregion
- #region Structures
- struct STD_STRING
- {
- [MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)]
- byte[] buffer; // offset 0x0, length = 0x10
- int length; // offset 0x10
- int maxLength; // offset 0x14
- int unk; // offset 0x18
- public string Text()
- {
- if (this.maxLength > 0xF)
- return ReadString(BitConverter.ToInt32(buffer, 0), this.length, false);
- char[] value = new char[this.length];
- for (int i = 0; i < this.length; i++)
- if (this.buffer[i] == 0)
- return new string(value, 0, i);
- else
- value[i] = (char)buffer[i];
- return new string(value);
- }
- }
- struct CONTAINER_NODE : IComparable<CONTAINER_NODE>, IComparer<CONTAINER_NODE>
- {
- int left; // offset 0x0
- int center; // offset 0x4
- int right; // offset 0x8
- int containerIndex; // offset 0xC
- int containerAddress; // offset 0x10
- public int Left { get { return this.left; } }
- public int Center { get { return this.center; } }
- public int Right { get { return this.right; } }
- public int ContainerIndex { get { return this.containerIndex; } }
- public int ContainerAddress { get { return this.containerAddress; } }
- public bool HasLeft { get { return this.left != 0; } }
- public bool HasCenter { get { return this.center != 0; } }
- public bool HasRight { get { return this.right != 0; } }
- public bool HasContainer { get { return this.containerAddress != 0; } }
- public Container Container() { return ReadContainer(this.containerAddress); }
- public int CompareTo(CONTAINER_NODE other) { return this.containerIndex.CompareTo(other.containerIndex); }
- public int Compare(CONTAINER_NODE x, CONTAINER_NODE y) { return x.containerIndex.CompareTo(y.containerIndex); }
- }
- struct CONTAINERS_ENTRY
- {
- int unk0; // offset 0x0
- int unk1; // offset 0x4
- int containersAddress; // offset 0x8
- int containersCount; // offset 0xC
- public int ContainersAddress { get { return this.containersAddress; } }
- public int ContainersCount { get { return this.containersCount; } }
- }
- struct Item
- {
- int slot; // offset 0x0
- int count; // offset 0x4
- int id; // offset 0x8
- int r; // offset 0xC
- int g; // offset 0x10
- int b; // offset 0x14
- int isVisible; // offset 0x18
- int unk;//0xc
- int un0;//0x10
- public int Count { get { return this.count; } }
- public int Id { get { return this.id; } }
- public int R { get { return this.r; } }
- public int G { get { return this.g; } }
- public int B { get { return this.b; } }
- public bool IsVisible { get { return this.isVisible != 0; } }
- }
- struct Container
- {
- int index; // offset 0
- Item asItem; // offset 0x4
- STD_STRING name; // offset 0x20
- int unk0; // offset 0x3C
- int slotsCount; // offset 0x40
- int itemsCount; // offset 0x44
- int unk1; // offset 0x48
- int itemsAddress; // offset 0x4C
- public int Index { get { return this.index; } }
- public Item AsItem { get { return this.asItem; } }
- public int SlotsCount { get { return this.slotsCount; } }
- public int ItemsCount { get { return this.itemsCount; } }
- public string Name() { return this.name.Text(); }
- public Item[] Items() { return ReadItems(this.itemsAddress, this.itemsCount); }
- }
- #endregion
- #region Reading Memory
- static int ReadInt32(int address)
- {
- int numberOfBytesRead;
- int size = sizeof(int);
- IntPtr memoryBlock = Marshal.AllocHGlobal(size);
- ReadProcessMemory(processHandle, address, memoryBlock, size, out numberOfBytesRead);
- int value = (int)Marshal.PtrToStructure(memoryBlock, typeof(int));
- Marshal.FreeHGlobal(memoryBlock);
- return value;
- }
- static string ReadString(int address, int length, bool checkNullChar = true)
- {
- int numberOfBytesRead;
- int size = length;
- IntPtr memoryBlock = Marshal.AllocHGlobal(size);
- ReadProcessMemory(processHandle, address, memoryBlock, size, out numberOfBytesRead);
- string value = Marshal.PtrToStringAnsi(memoryBlock, length);
- Marshal.FreeHGlobal(memoryBlock);
- if (checkNullChar)
- {
- int index = value.IndexOf('\0');
- return index == -1 ? value : value.Substring(0, index);
- }
- return value;
- }
- static string ReadStdString(int address)
- {
- int numberOfBytesRead;
- Type type = typeof(STD_STRING);
- int size = Marshal.SizeOf(type);
- IntPtr memoryBlock = Marshal.AllocHGlobal(size);
- ReadProcessMemory(processHandle, address, memoryBlock, size, out numberOfBytesRead);
- STD_STRING value = (STD_STRING)Marshal.PtrToStructure(memoryBlock, type);
- Marshal.FreeHGlobal(memoryBlock);
- return value.Text();
- }
- static int ReadContainersPointer()
- {
- return ReadInt32(containersPointer + baseAddress);
- }
- static CONTAINERS_ENTRY ReadContainersEntry(int address)
- {
- int numberOfBytesRead;
- Type type = typeof(CONTAINERS_ENTRY);
- int size = Marshal.SizeOf(type);
- IntPtr memoryBlock = Marshal.AllocHGlobal(size);
- ReadProcessMemory(processHandle, address, memoryBlock, size, out numberOfBytesRead);
- CONTAINERS_ENTRY value = (CONTAINERS_ENTRY)Marshal.PtrToStructure(memoryBlock, type);
- Marshal.FreeHGlobal(memoryBlock);
- return value;
- }
- static CONTAINER_NODE ReadContainerNode(int address)
- {
- int numberOfBytesRead;
- Type type = typeof(CONTAINER_NODE);
- int size = Marshal.SizeOf(type);
- IntPtr memoryBlock = Marshal.AllocHGlobal(size);
- ReadProcessMemory(processHandle, address, memoryBlock, size, out numberOfBytesRead);
- CONTAINER_NODE value = (CONTAINER_NODE)Marshal.PtrToStructure(memoryBlock, type);
- Marshal.FreeHGlobal(memoryBlock);
- return value;
- }
- static Item[] ReadItems(int address, int itemsCount)
- {
- int numberOfBytesRead;
- Type itemType = typeof(Item);
- int itemSize = Marshal.SizeOf(itemType);
- int size = itemsCount * itemSize;
- IntPtr memoryBlock = Marshal.AllocHGlobal(size);
- ReadProcessMemory(processHandle, address, memoryBlock, size, out numberOfBytesRead);
- Item[] value = new Item[itemsCount];
- for (int i = 0; i < itemsCount; i++)
- {
- value[i] = (Item)Marshal.PtrToStructure(IntPtr.Add(memoryBlock, i * itemSize), itemType);
- }
- Marshal.FreeHGlobal(memoryBlock);
- return value;
- }
- static Container ReadContainer(int address)
- {
- int numberOfBytesRead;
- Type type = typeof(Container);
- int size = Marshal.SizeOf(type);
- IntPtr memoryBlock = Marshal.AllocHGlobal(size);
- ReadProcessMemory(processHandle, address, memoryBlock, size, out numberOfBytesRead);
- Container value = (Container)Marshal.PtrToStructure(memoryBlock, type);
- Marshal.FreeHGlobal(memoryBlock);
- return value;
- }
- #endregion
- #region Variables
- static IntPtr processHandle;
- static int baseAddress;
- const int containersPointer = 0x5EB3D4;
- #endregion
- #region Containers Handler
- class ContainersHandler
- {
- #region Variables
- SortedSet<CONTAINER_NODE> nodes;
- HashSet<int> addresses;
- CONTAINERS_ENTRY entry;
- #endregion
- #region Properties
- public int Count { get { return this.entry.ContainersCount; } }
- #endregion
- #region Methods
- public void Reset()
- {
- this.nodes.Clear();
- this.addresses.Clear();
- this.entry = ReadContainersEntry(ReadContainersPointer());
- }
- public IEnumerable<Container> Containers()
- {
- if (this.Count == 0)
- return new List<Container>();
- int firstNodeAddress = ReadInt32(this.entry.ContainersAddress);
- CONTAINER_NODE firstNode = ReadContainerNode(firstNodeAddress);
- RecursiveMethod(firstNode, firstNodeAddress);
- return from node in this.nodes select node.Container();
- }
- #endregion
- #region Private Methods
- void RecursiveMethod(CONTAINER_NODE node, int address)
- {
- this.addresses.Add(address);
- if (this.nodes.Contains(node))
- return;
- if (node.ContainerAddress != 0)
- this.nodes.Add(node);
- if (CanSearchOnCenter(node))
- RecursiveMethod(ReadContainerNode(node.Center), node.Center);
- if (CanSearchOnLeft(node))
- RecursiveMethod(ReadContainerNode(node.Left), node.Left);
- if (CanSearchOnRight(node))
- RecursiveMethod(ReadContainerNode(node.Right), node.Right);
- }
- bool CanSearchOnCenter(CONTAINER_NODE node)
- {
- return node.HasCenter && this.nodes.Count < this.Count && !this.addresses.Contains(node.Center);
- }
- bool CanSearchOnLeft(CONTAINER_NODE node)
- {
- return node.HasLeft && this.nodes.Count < this.Count && !this.addresses.Contains(node.Left);
- }
- bool CanSearchOnRight(CONTAINER_NODE node)
- {
- return node.HasRight && this.nodes.Count < this.Count && !this.addresses.Contains(node.Right);
- }
- #endregion
- #region Constructors
- public ContainersHandler()
- {
- this.addresses = new HashSet<int>();
- this.nodes = new SortedSet<CONTAINER_NODE>();
- this.entry = ReadContainersEntry(ReadContainersPointer());
- }
- #endregion
- }
- #endregion
- #region Application Entrypoint
- static void Main(string[] args)
- {
- Process[] tibias = Process.GetProcessesByName("Tibia");
- if (tibias.Length == 0)
- {
- Console.WriteLine("Tibia not found");
- }
- else
- {
- Process process = tibias[tibias.Length - 1];
- processHandle = process.Handle;
- baseAddress = process.MainModule.BaseAddress.ToInt32();
- int counter = 0;
- char input;
- ContainersHandler containersHandler = new ContainersHandler();
- do
- {
- Console.WriteLine("Displayed {0} times\r\n", ++counter);
- Console.WriteLine("Containers Count: " + containersHandler.Count);
- Console.WriteLine();
- foreach (Container container in containersHandler.Containers())
- {
- string name = container.Name();
- Console.WriteLine("Container [{0}]: {1}", container.AsItem.Id, name);
- Console.WriteLine("Slots Count: " + container.SlotsCount);
- Console.WriteLine("Items Count: " + container.ItemsCount);
- Console.WriteLine("Items:");
- //items.Length
- Item[] items = container.Items();
- for (int i = 0; i < items.Length; i++)
- Console.WriteLine("Item [Count: {0}, Id: {1}]", items[i].Count, items[i].Id);
- Console.WriteLine();
- }
- containersHandler.Reset();
- Console.WriteLine("Coninue? Press Y to confirm...");
- input = Console.ReadKey().KeyChar;
- Console.WriteLine("\r\n");
- }
- while (input == 'Y' || input == 'y');
- }
- Console.WriteLine("Press anything to exit...");
- Console.ReadKey();
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement