202 lines
11 KiB
Lua
202 lines
11 KiB
Lua
|
|
local owner='esp_projection'
|
|
local function geometryBytes(b)
|
|
if not b or #b<56 then return end
|
|
local g={string.unpack('<ffffffffffffff',b)}
|
|
for i=1,13 do if not EspSupport.finite(g[i])then return end end
|
|
if g[1]<=0 or g[2]<=0 then return end
|
|
return g
|
|
end
|
|
local function geometry(p)return geometryBytes(H('read',p,56))end
|
|
function StartEspProjection()
|
|
local old=PBZ_EspProjection;assert(not old,'请先关闭现有ESP')
|
|
local u=NativeDamageNumberDependencies();u.prepareObjects()
|
|
local c=assert(u.context(),'请先加载本地角色')
|
|
local classes=u.objectInventory({WB_MapNew_C='WidgetBlueprintGeneratedClass',MapSubsystem='Class',WidgetLayoutLibrary='Class'})
|
|
local function bind(cl,name,size,rva,fields)
|
|
local fn=assert(u.functions(cl,name),'缺少原生接口 '..name)
|
|
assert(readInteger(fn+0x100)&0x400~=0 and readSmallInteger(fn+0x106)==size and u.q(fn+0x128)==getAddress('PBZ.exe')+rva,'原生接口版本变化 '..name)
|
|
for name,spec in pairs(fields)do local f=u.field(fn,name,true);assert(f.offset==spec[1]and f.size==spec[2],'原生接口参数变化 '..name)end
|
|
return assert(u.identity(fn))
|
|
end
|
|
local screen=bind(u.classOf(c.pc),'ProjectWorldLocationToScreen',42,0x43B7510,{WorldLocation={0,24},ScreenLocation={24,16},bPlayerViewportRelative={40,1},ReturnValue={41,1}})
|
|
local project=bind(classes.MapSubsystem,'ProjectWorldLocationToMap',48,0x6547510,{Location={0,24},MapID={24,8},ReturnValue={32,16}})
|
|
local mapID=bind(classes.MapSubsystem,'GetMapIDByLocation',32,0x6546970,{Location={0,24},ReturnValue={24,8}})
|
|
local layer=bind(classes.MapSubsystem,'GetLayerByLocation',28,0x6546540,{Location={0,24},ReturnValue={24,4}})
|
|
local viewport=bind(classes.WidgetLayoutLibrary,'GetViewportWidgetGeometry',64,0x394CD40,{WorldContextObject={0,8},ReturnValue={8,56}})
|
|
local viewportCDO=u.staticTarget(classes.WidgetLayoutLibrary,'GetViewportWidgetGeometry',{WorldContextObject={0,8},ReturnValue={8,56}}).receiver
|
|
local s={u=u,classes=classes,context=c,results={},names={},roots={},lastSearch=-10000,generation=0,pid=getOpenedProcessID(),queries=0}
|
|
local chunks,count=u.objectSlots()
|
|
local function weak(a)local id=u.identity(a);return id and u.weakReference(id)end
|
|
s.weak=weak
|
|
local function visible(a)
|
|
local seen={}
|
|
for i=1,48 do
|
|
if not u.ptr(a)or seen[a]then return false end;seen[a]=true
|
|
local cl=u.classOf(a);local f=u.field(cl,'Visibility',true);local v=readBytes(a+f.offset,1)
|
|
if v~=0 and v~=3 and v~=4 then return false end
|
|
local opacity=u.field(cl,'RenderOpacity',true);local alpha=readFloat(a+opacity.offset)
|
|
if not alpha or alpha<=0.001 then return false end
|
|
local slot=u.q(a+0x30);if not u.ptr(slot)then return true end
|
|
a=u.q(slot+0x28);if not u.ptr(a)then return true end
|
|
end
|
|
return false
|
|
end
|
|
local function cached(a)
|
|
if not u.ptr(a)then return end
|
|
for _,offset in ipairs({0x118,0xF8})do
|
|
local sw,ctrl=u.q(a+offset),u.q(a+offset+8)
|
|
if u.ptr(sw)and u.ptr(ctrl)then
|
|
local refs=readInteger(ctrl+8)
|
|
if refs and refs>0 then
|
|
local g=geometry(sw+0xD0)
|
|
if g and u.q(a+offset)==sw and u.q(a+offset+8)==ctrl and readInteger(ctrl+8)>0 then return g end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
s.geometry=cached
|
|
local function stringAt(a,name)
|
|
local f=u.field(u.classOf(a),name,true);local data,n=u.array(a+f.offset,1024)
|
|
return data and n>1 and u.exactString(data,n-1,true)or ''
|
|
end
|
|
local function nameID(text)
|
|
local key=text:lower();if s.names[key]~=nil then return s.names[key]or nil end
|
|
local current=readInteger(u.namePool+8);local cursor=readInteger(u.namePool+12)
|
|
assert(current and current<8192 and cursor and cursor<=0x20000,'地图名称池不可读')
|
|
for block=0,current do
|
|
local address=u.q(u.namePool+0x10+block*8)
|
|
local data=address and H('read',address,block==current and cursor or 0x20000)
|
|
if data then local at=1
|
|
while at+1<=#data do
|
|
local header=string.unpack('<I2',data,at);local n=header>>6
|
|
if n==0 then break end
|
|
local bytes=n*((header&1)==0 and 1 or 2)
|
|
if n>1023 or at+1+bytes>#data then break end
|
|
if header&1==0 and n==#text and data:sub(at+2,at+1+bytes):lower()==key then
|
|
local id=(block<<16)|((at-1)//2);s.names[key]=id;return id
|
|
end
|
|
at=at+2+bytes;if at%2==0 then at=at+1 end
|
|
end
|
|
end
|
|
end
|
|
s.names[key]=false
|
|
end
|
|
local function refresh(c)
|
|
if s.searchWorld~=c.world then s.searchWorld=c.world;s.lastSearch=-10000;s.roots={};s.system=nil end
|
|
if getTickCount()-s.lastSearch<2000 then return end
|
|
local found=H('espObjects',chunks,count,classes.WB_MapNew_C,classes.MapSubsystem)
|
|
s.lastSearch=getTickCount();s.roots={};s.system=nil
|
|
for _,a in ipairs(found)do
|
|
local cl=u.classOf(a)
|
|
if cl==classes.MapSubsystem then
|
|
if u.q(a+0x20)==c.gi or u.q(a+0x20)==c.world then assert(not s.system,'地图子系统不唯一');s.system=u.identity(a)end
|
|
elseif u.field(cl,'Gadget')then s.roots[#s.roots+1]={address=a,weak=weak(a)}end
|
|
end
|
|
end
|
|
local function mapState(c)
|
|
refresh(c)
|
|
local selected
|
|
for _,root in ipairs(s.roots)do local a=root.address
|
|
if root.weak and weak(a)==root.weak and visible(a)then
|
|
local cl=u.classOf(a);local canvas=u.q(a+u.offset(cl,'Gadget'))
|
|
local cg=cached(canvas)
|
|
if cg then
|
|
assert(not selected,'多个地图同时可见,请关闭后重新打开地图')
|
|
local mode=readBytes(a+u.offset(cl,'BigMapMode'),1)
|
|
local name=stringAt(a,'CurrentDisplayMapID');local layerID=readInteger(a+u.offset(cl,'CurrentLayer'),true)
|
|
local clip=u.q(a+u.offset(cl,'MapClickSurface'))
|
|
selected={root=a,canvas=canvas,geometry=cg,clip=cached(clip),id=nameID(name),name=name,layer=layerID,overview=mode~=0}
|
|
end
|
|
end
|
|
end
|
|
return selected
|
|
end
|
|
local d,assembly=u.acquireDispatch(owner);assert(d.nativeEspProjectionVersion==1,'缺少ESP批量查询接口')
|
|
ProtectDamageNumberDispatcher(d,u.worldSlot);s.dispatcher=d
|
|
local function stamp(p)return ('%s:%s:%.6f:%.6f:%.6f'):format(p.address,p.weak,p.position[1],p.position[2],p.position[3])end
|
|
s.project=function(points,w,h)
|
|
if s.stopping then return {},'正在关闭ESP'end
|
|
local c=u.context();if not c then s.results={};return {},'等待本地角色'end
|
|
local map=mapState(c);s.map=map
|
|
local key=('%s:%s:%s:%s'):format(c.world,c.pc,map and map.root or 0,map and((map.id or 0)..':'..tostring(map.layer))or 'screen')
|
|
if key~=s.key then s.key=key;s.results={};s.screenQueries={};s.viewport=nil;s.generation=s.generation+1 end
|
|
if map and(map.overview or not map.id or not map.clip or not s.system or not u.valid(s.system))then return {},'请选择已加载区域的分区地图'end
|
|
for _,p in ipairs(points)do p.tag=stamp(p)end
|
|
if not s.pending and getAddressSafe('PBZ_ESP_QueryBuffer')then
|
|
local batch={}
|
|
for i,p in ipairs(points)do
|
|
if not map or not s.results[p.tag] then
|
|
batch[#batch+1]=p
|
|
if map and #batch==300 then break end
|
|
p.projectionOrder=s.screenQueries[p.weak] or 0
|
|
p.pointOrder=i
|
|
end
|
|
end
|
|
if #batch>300 then
|
|
-- Let unqueried and least recently queried points take the next screen batch.
|
|
table.sort(batch,function(a,b)
|
|
if a.projectionOrder==b.projectionOrder then return a.pointOrder<b.pointOrder end
|
|
return a.projectionOrder<b.projectionOrder
|
|
end)
|
|
for i=#batch,301,-1 do batch[i]=nil end
|
|
end
|
|
local buffer=getAddress('PBZ_ESP_QueryBuffer')
|
|
if #batch>0 then assert(H('write',buffer+64,EspSupport.packPoints(batch),false))end
|
|
local writes={{0,buffer},{8,#batch},{16,map and 1 or 2},{24,map and map.id or 0},{32,layer.address},{40,map and map.layer or 0},{48,viewportCDO.address},{56,viewport.address}}
|
|
local ids={u.identity(c.pc),screen,project,mapID,layer,viewport,viewportCDO,map and s.system or u.identity(c.pc)}
|
|
local weakIDs={};for i,id in ipairs(ids)do weakIDs[i]=u.weakReference(id)end
|
|
local generation=s.generation
|
|
local r={nativeEspProjection=true,owner=owner,world=c.world,pcID=ids[1],receiver=map and s.system or ids[1],fn=map and project or screen,extra=map and mapID or ids[1],resultKind='object',resultOffset=0,writes=writes}
|
|
r.validate=function()for i,id in ipairs(ids)do if not weakIDs[i]or u.weakReference(id)~=weakIDs[i]then return false end end;return true end
|
|
r.capture=function()
|
|
local raw=assert(H('read',buffer,64+#batch*48),'ESP查询结果不可读')
|
|
local result={viewport=map and geometryBytes(raw)or nil,points={}}
|
|
for i,p in ipairs(batch)do local at=65+(i-1)*48
|
|
local x,y,flag=string.unpack('<ddi8',raw,at+24)
|
|
local valid=flag==1 and EspSupport.validProjection(x,y,map~=nil)
|
|
result.points[#result.points+1]={tag=p.tag,address=p.address,weak=p.weak,x=x,y=y,valid=valid,time=getTickCount()}
|
|
end
|
|
return result
|
|
end
|
|
r.callback=function(result)
|
|
s.pending=false;if s.stopping or generation~=s.generation then return end
|
|
if not result.success then s.results={};s.error=result.error or '游戏拒绝ESP查询';return end
|
|
s.error=nil;s.viewport=result.payload.viewport;s.queries=s.queries+1
|
|
for _,v in ipairs(result.payload.points)do
|
|
s.results[v.tag]=v
|
|
if not map then s.screenQueries[v.weak]=s.queries end
|
|
end
|
|
end
|
|
s.pending=true;d.enqueue(r)
|
|
end
|
|
local keep,keepQueries={},{}
|
|
for _,p in ipairs(points)do keep[p.tag]=s.results[p.tag];keepQueries[p.weak]=s.screenQueries[p.weak]end
|
|
s.results,s.screenQueries=keep,keepQueries
|
|
local out={}
|
|
for _,p in ipairs(points)do local v=s.results[p.tag]
|
|
if v and v.valid and p.weak and weak(p.address)==p.weak and(map or EspSupport.screenFresh(getTickCount(),v.time))then
|
|
local x,y=v.x,v.y
|
|
if map then x,y=EspSupport.mapPixel(map.geometry,s.viewport,map.clip,x,y,w,h)end
|
|
if x and x>=0 and y>=0 and x<=w and y<=h then out[#out+1]={x=x,y=y,caption=p.caption,distance=p.distance,metres=p.metres,color=p.color}end
|
|
end
|
|
end
|
|
return out,s.error or(map and('大地图 · '..#out..' 个标记(当前已加载对象)')or('场景 · '..#out..' 个标记'))
|
|
end
|
|
s.prepareStop=function()
|
|
s.stopping=true
|
|
local ok,e=pcall(d.drain,owner)
|
|
if not ok then s.stopping=false;error(e,0) end
|
|
if s.pending then s.stopping=false;error('ESP原生查询仍在执行,请重试关闭',0) end
|
|
end
|
|
s.stop=function()assert(not s.pending,'请先完成ESP查询');local restore=d.release(owner);if PBZ_EspProjection==s then PBZ_EspProjection=nil end;return restore..'\nunregistersymbol(PBZ_ESP_QueryBuffer)\ndealloc(PBZ_ESP_QueryBuffer)'end
|
|
PBZ_EspProjection=s
|
|
return assembly..'\nalloc(PBZ_ESP_QueryBuffer,$4000)\nregistersymbol(PBZ_ESP_QueryBuffer)'
|
|
end
|
|
function PrepareEspProjectionDisable()
|
|
local s=PBZ_EspProjection;if s then s.prepareStop()end
|
|
end
|
|
function StopEspProjection()
|
|
local s=PBZ_EspProjection;return s and s.stop()or ''
|
|
end
|