Kod:
using System;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Collections.Generic;
namespace Astrabot.Objects
{
public class Cont
{
#region Help Data
/// <summary>
/// Index - Kolejność Backpacków
/// ItemsCount - Ilość itemów w backpacku
/// SlotsCount - Max ilość itemów w backpacku
/// .AsItem.Id - Id Otwartych backpacków
#endregion
#region Import
[DllImport("kernel32.dll")]
static extern int ReadProcessMemory(
IntPtr processHandle,
int address,
IntPtr buffer,
int size,
out int numberOfBytesRead
);
#endregion
#region Structures
public struct STD_STRING
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 0x10)]
public byte[] buffer; // offset 0x0, length = 0x10
public int length; // offset 0x10
public int maxLength; // offset 0x14
public 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);
}
}
public struct CONTAINER_NODE : IComparable<CONTAINER_NODE>, IComparer<CONTAINER_NODE>
{
public int left; // offset 0x0
public int center; // offset 0x4
public int right; // offset 0x8
public int containerIndex; // offset 0xC
public 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); }
}
public struct CONTAINERS_ENTRY
{
public int unk0; // offset 0x0
public int unk1; // offset 0x4
public int containersAddress; // offset 0x8
public int containersCount; // offset 0xC
public int ContainersAddress { get { return this.containersAddress; } }
public int ContainersCount { get { return this.containersCount; } }
}
public struct Item
{
public int slot; // offset 0x0
public int count; // offset 0x4
public int id; // offset 0x8
public int r; // offset 0xC
public int g; // offset 0x10
public int b; // offset 0x14
public int isVisible; // offset 0x18
public int unk;//0xc
public 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; } }
}
public struct Container
{
public int index; // offset 0
public Item asItem; // offset 0x4
public STD_STRING name; // offset 0x20
public int unk0; // offset 0x3C
public int slotsCount; // offset 0x40
public int itemsCount; // offset 0x44
public int unk1; // offset 0x48
public 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;
System.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((int)Addresses.containersPointer + baseAddress);
}
static CONTAINERS_ENTRY ReadContainersEntry(int address)
{
int numberOfBytesRead;
System.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;
System.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;
System.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;
System.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;
#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 Functions
public static int Get_Count()
{
int count = 0;
processHandle = Client.Tibia.Handle;
baseAddress = (int)Client.Tibia.MainModule.BaseAddress.ToInt32();
ContainersHandler containersHandler = new ContainersHandler();
count = containersHandler.Count;
return count;
}
public static IEnumerable<Container> GetContai()
{
processHandle = Client.Tibia.Handle;
baseAddress = (int)Client.Tibia.MainModule.BaseAddress.ToInt32();
List<Container> lista_bp = new List<Container>();
ContainersHandler containersHandler = new ContainersHandler();
foreach (Container container in containersHandler.Containers())
{
Item[] items = container.Items();
}
return containersHandler.Containers();
}
#endregion
}
}
Zakładki