import ctypes
import ctypes.wintypes as w
import struct
import time
import subprocess
import win32gui
import win32con

k32 = ctypes.WinDLL("kernel32", use_last_error=True)
u32 = ctypes.WinDLL("user32", use_last_error=True)

k32.OpenProcess.restype = w.HANDLE
k32.ReadProcessMemory.argtypes = [
    w.HANDLE, ctypes.c_void_p, ctypes.c_void_p, ctypes.c_size_t, ctypes.POINTER(ctypes.c_size_t)
]
k32.ReadProcessMemory.restype = w.BOOL

def rpm(h, addr, size):
    buf = (ctypes.c_ubyte * size)()
    got = ctypes.c_size_t(0)
    if not k32.ReadProcessMemory(h, ctypes.c_void_p(addr), buf, size, ctypes.byref(got)):
        return None
    return bytes(buf[: got.value])

out = subprocess.check_output(['powershell', '-Command', '(Get-Process -Name *PathOfExile*).Id'])
pid = int(out.decode().split()[0])
h = k32.OpenProcess(0x1F0FFF, False, pid)

hwnd = win32gui.FindWindow("POEWindowClass", "Path of Exile 2")
if hwnd:
    win32gui.SetForegroundWindow(hwnd)
    time.sleep(0.2)

igs = 0x48514e50f10
cam = struct.unpack("<Q", rpm(h, igs + 0x368, 8))[0]
print(f"Monitoring Camera @ {cam:#x} (size 0x600)...")

b_prev = rpm(h, cam, 0x600)

print("Scrolling mouse wheel UP (zoom in)...")
for _ in range(5):
    u32.mouse_event(0x0800, 0, 0, 120, 0) # WHEEL UP
    time.sleep(0.05)

time.sleep(0.2)
b_curr = rpm(h, cam, 0x600)
diffs = []
for off in range(0, 0x600, 4):
    v1 = struct.unpack_from("<f", b_prev, off)[0]
    v2 = struct.unpack_from("<f", b_curr, off)[0]
    if abs(v1 - v2) > 0.001 and (abs(v1) < 1e5 and abs(v2) < 1e5):
        diffs.append((off, v1, v2))

print(f"Diffs after Wheel UP:")
for off, v1, v2 in diffs:
    print(f"  +{off:#04x}: {v1:.4f} -> {v2:.4f}")

b_prev = b_curr
print("\nScrolling mouse wheel DOWN (zoom out)...")
for _ in range(5):
    u32.mouse_event(0x0800, 0, 0, -120, 0) # WHEEL DOWN
    time.sleep(0.05)

time.sleep(0.2)
b_curr = rpm(h, cam, 0x600)
diffs2 = []
for off in range(0, 0x600, 4):
    v1 = struct.unpack_from("<f", b_prev, off)[0]
    v2 = struct.unpack_from("<f", b_curr, off)[0]
    if abs(v1 - v2) > 0.001 and (abs(v1) < 1e5 and abs(v2) < 1e5):
        diffs2.append((off, v1, v2))

print(f"Diffs after Wheel DOWN:")
for off, v1, v2 in diffs2:
    print(f"  +{off:#04x}: {v1:.4f} -> {v2:.4f}")
