273 lines
12 KiB
Plaintext
273 lines
12 KiB
Plaintext
[ENABLE]
|
|
{$lua}
|
|
if syntaxcheck then return end
|
|
if rawget(_G,'PBZ_DustEditor') then error('Disable the existing Remnant Dust editor first.') end
|
|
local state={pid=getOpenedProcessID(),nameCache={},classCache={}}
|
|
local signatures={world="48 8B 05 ?? ?? ?? ?? 48 85 C0 75 ?? 80 3D ?? ?? ?? ?? 02 72 ?? 48 8D 15 ?? ?? ?? ?? 48 8D 0D ?? ?? ?? ?? E8 ?? ?? ?? ?? 33 C0 48 83 C4 20 5B C3",names="89 03 48 8B C3 89 7B 04 48 8B 5C 24 48 48 83 C4 30 5F C3 80 3D ?? ?? ?? ?? 00 74 09 48 8D 05 ?? ?? ?? ?? EB 13 48 8D 0D ?? ?? ?? ?? E8 ?? ?? ?? ?? C6 05 ?? ?? ?? ?? 01 0F 28 44 24 20"}
|
|
local function ptr(p) return type(p)=='number' and p>0x10000 and p<0x7FFFFFFFFFFF end
|
|
local function q(p) return ptr(p) and readQword(p) or nil end
|
|
local function finite(x) return type(x)=='number' and x==x and math.abs(x)<1e12 end
|
|
local function vector(p)
|
|
if not ptr(p) then return end
|
|
local x,y,z=readDouble(p),readDouble(p+8),readDouble(p+16)
|
|
if finite(x) and finite(y) and finite(z) then return {x,y,z} end
|
|
end
|
|
local function array(p,limit)
|
|
local data,num,cap=q(p),readInteger(p+8),readInteger(p+12)
|
|
if not num or not cap or num>limit or cap<num or cap>1000000 then return end
|
|
if num>0 and not ptr(data) then return end
|
|
return data,num
|
|
end
|
|
local function exactString(p,n,wide)
|
|
local bytes=readBytes(p,n*(wide and 2 or 1),true)
|
|
if not bytes or #bytes~=n*(wide and 2 or 1) then return '' end
|
|
if not wide then return string.char(table.unpack(bytes)) end
|
|
local chars={};local i=1
|
|
while i<=#bytes do
|
|
local unit=bytes[i]+bytes[i+1]*256;i=i+2
|
|
if unit>=0xD800 and unit<=0xDBFF and i<#bytes then
|
|
local low=bytes[i]+bytes[i+1]*256
|
|
if low>=0xDC00 and low<=0xDFFF then unit=0x10000+(unit-0xD800)*1024+low-0xDC00;i=i+2
|
|
else unit=0xFFFD end
|
|
elseif unit>=0xD800 and unit<=0xDFFF then unit=0xFFFD end
|
|
if unit==0 then break end
|
|
chars[#chars+1]=utf8.char(unit)
|
|
end
|
|
return table.concat(chars)
|
|
end
|
|
local function fname(p)
|
|
local id=readInteger(p)
|
|
if not id then return '' end
|
|
local text=state.nameCache[id]
|
|
if not text then
|
|
local block=id>>16
|
|
if block>8191 then return '' end
|
|
local b=q(state.namePool+0x10+block*8)
|
|
if not ptr(b) then return '' end
|
|
local entry=b+(id&0xFFFF)*2
|
|
local h=readSmallInteger(entry)
|
|
if not h then return '' end
|
|
local n=h>>6
|
|
if n<1 or n>1023 then return '' end
|
|
text=exactString(entry+2,n,(h&1)~=0)
|
|
if not text then return '' end
|
|
state.nameCache[id]=text
|
|
end
|
|
local number=readInteger(p+4)
|
|
if number and number>0 then text=text..'_'..tostring(number-1) end
|
|
return text
|
|
end
|
|
local function fstring(p)
|
|
local data,n=array(p,1024)
|
|
if not data or n<2 then return '' end
|
|
return exactString(data,n-1,true)
|
|
end
|
|
local function meta(cl)
|
|
if not ptr(cl) then return end
|
|
local nameId=q(cl+0x18)
|
|
local super=q(cl+0x40)
|
|
if not nameId or super==nil then return end
|
|
local old=state.classCache[cl]
|
|
if old and old.nameId==nameId and old.super==super then return old end
|
|
local m={nameId=nameId,super=super,name=fname(cl+0x18)}
|
|
state.classCache[cl]=m
|
|
return m
|
|
end
|
|
local function properties(cl)
|
|
local m=meta(cl);if not m then return {} end
|
|
if m.fields then return m.fields end
|
|
local fields={};local f=q(cl+0x50);local seen={}
|
|
for i=1,512 do
|
|
if not ptr(f) or seen[f] then break end
|
|
seen[f]=true
|
|
local off,size=readInteger(f+0x44),readInteger(f+0x34)
|
|
if off and off<0x100000 and size and size<0x100000 then
|
|
fields[fname(f+0x20)]={offset=off,size=size,type=q(f+0x70),
|
|
byte=readBytes(f+0x71,1),mask=readBytes(f+0x72,1)}
|
|
end
|
|
f=q(f+0x10)
|
|
end
|
|
m.fields=fields;return fields
|
|
end
|
|
local function field(cl,name,required)
|
|
local seen={}
|
|
for i=1,32 do
|
|
if not ptr(cl) or seen[cl] then break end
|
|
seen[cl]=true
|
|
local p=properties(cl)[name];if p then return p end
|
|
local m=meta(cl);cl=m and m.super
|
|
end
|
|
if required then error('Missing reflected field: '..name) end
|
|
end
|
|
local function offset(cl,name) return field(cl,name,true).offset end
|
|
local function classOf(a) return q(a+0x10) end
|
|
local function uniqueScan(pattern)
|
|
local scan=createMemScan();local found=createFoundList(scan)
|
|
local ok,result=pcall(function()
|
|
local base=getAddress('PBZ.exe')
|
|
scan.firstScan(soExactValue,vtByteArray,rtRounded,pattern,'',base,base+getModuleSize('PBZ.exe')-1,
|
|
'+X',fsmNotAligned,'1',true,false,false,false)
|
|
scan.waitTillDone();found.initialize()
|
|
if found.Count~=1 then error('ESP signature expected 1 match; found '..found.Count) end
|
|
return getAddress(found.Address[0])
|
|
end)
|
|
found.destroy();scan.destroy()
|
|
if not ok then error(result) end
|
|
return result
|
|
end
|
|
local MARKER='PBZ_CONSCIOUSNESS_DUST_EDITOR_V1'
|
|
local function resolveBalance()
|
|
if getOpenedProcessID()~=state.pid then return nil,'Process changed. Re-enable after attaching.' end
|
|
local world=q(state.worldSlot)
|
|
if not ptr(world) then return nil,'Load into the game first.' end
|
|
if state.world~=world then state.classCache={};state.world=world end
|
|
local gi=q(world+offset(classOf(world),'OwningGameInstance'))
|
|
if not ptr(gi) then return nil,'Waiting for the game instance.' end
|
|
local data,num=array(gi+offset(classOf(gi),'LocalPlayers'),8)
|
|
if not data or num<1 then return nil,'Waiting for the local player.' end
|
|
local lp=q(data);if not ptr(lp) then return nil,'Waiting for the local player.' end
|
|
local pc=q(lp+offset(classOf(lp),'PlayerController'))
|
|
if not ptr(pc) then return nil,'Waiting for the controller.' end
|
|
local pawnOffset=offset(classOf(pc),'Pawn')
|
|
local pawn=q(pc+pawnOffset)
|
|
if not ptr(pawn) then return nil,'Waiting for the local pawn.' end
|
|
local cl=classOf(pawn)
|
|
local dust=field(cl,'ConsciousnessDust')
|
|
if not dust or dust.size~=4 then return nil,'This pawn has no supported Remnant Dust field.' end
|
|
local address=pawn+dust.offset;local value=readInteger(address,true)
|
|
if not value or value<0 then return nil,'The current balance is unavailable.' end
|
|
return {world=world,controller=pc,pawnOffset=pawnOffset,pawn=pawn,class=cl,
|
|
nameId=q(pawn+0x18),index=readInteger(pawn+0xC),address=address,value=value}
|
|
end
|
|
local function safeResolve()
|
|
local ok,result,message=pcall(resolveBalance)
|
|
if not ok then state.lastError=tostring(result);return nil,'Currency unavailable. Re-enable after loading.' end
|
|
return result,message
|
|
end
|
|
local function cleanup()
|
|
if state.cleaned then return end
|
|
state.cleaned=true
|
|
for _,key in ipairs({'timer','closeTimer'}) do
|
|
local timer=state[key];if timer then timer.Enabled=false;timer.destroy();state[key]=nil end
|
|
end
|
|
if state.form then state.form.OnClose=nil;state.form.destroy();state.form=nil end
|
|
if rawget(_G,'PBZ_DustEditor')==state then _G.PBZ_DustEditor=nil end
|
|
end
|
|
state.cleanup=cleanup
|
|
local function requestClose()
|
|
if state.disabling then return end
|
|
state.disabling=true
|
|
local timer=createTimer(nil,false);state.closeTimer=timer;timer.Interval=1
|
|
timer.OnTimer=function()
|
|
timer.Enabled=false
|
|
local ok,err=pcall(function()
|
|
local record;local list=getAddressList()
|
|
for i=0,list.Count-1 do
|
|
local candidate=list.getMemoryRecord(i)
|
|
if candidate.Active and (candidate.Script or ''):find(MARKER,1,true) then
|
|
if record then error('Multiple Dust editors active. Uncheck the intended entry in CE.') end
|
|
record=candidate
|
|
end
|
|
end
|
|
if not record then error('Cannot find the active entry. Uncheck it in CE.') end
|
|
record.Active=false
|
|
if record.Active then error('Could not disable the currency editor.') end
|
|
end)
|
|
if not ok and not state.cleaned then state.disabling=false;state.status.Caption=tostring(err) end
|
|
if not state.cleaned and state.closeTimer then state.closeTimer.destroy();state.closeTimer=nil end
|
|
end
|
|
timer.Enabled=true
|
|
end
|
|
local function rounded(control,radius)
|
|
local region=executeCodeLocalEx('gdi32.CreateRoundRectRgn',0,0,control.Width+1,control.Height+1,radius,radius)
|
|
if not region or region==0 then error('Could not create a rounded window region') end
|
|
if executeCodeLocalEx('user32.SetWindowRgn',control.Handle,region,1)==0 then
|
|
executeCodeLocalEx('gdi32.DeleteObject',region);error('Could not apply a rounded window region')
|
|
end
|
|
end
|
|
local function applyAmount()
|
|
if state.cleaned or state.disabling then return end
|
|
local text=state.edit.Text:match('^%s*(.-)%s*$')
|
|
if not text:match('^%d+$') or #text>10 then state.status.Caption='Enter a whole number from 0 to 2147483647.';return end
|
|
local amount=tonumber(text,10)
|
|
if not amount or amount>2147483647 then state.status.Caption='Enter a whole number from 0 to 2147483647.';return end
|
|
local balance,message=safeResolve()
|
|
if not balance then state.status.Caption=message;return end
|
|
if getOpenedProcessID()~=state.pid or q(state.worldSlot)~=balance.world
|
|
or q(balance.controller+balance.pawnOffset)~=balance.pawn or classOf(balance.pawn)~=balance.class
|
|
or q(balance.pawn+0x18)~=balance.nameId or readInteger(balance.pawn+0xC)~=balance.index then
|
|
state.status.Caption='The local pawn changed. Try Apply again after loading.';return
|
|
end
|
|
local ok=writeInteger(balance.address,amount)
|
|
local actual=readInteger(balance.address,true)
|
|
if not ok or actual~=amount then
|
|
state.lastError='Currency write/readback failed';state.status.Caption='Could not apply the amount. Try again in gameplay.';return
|
|
end
|
|
state.lastApplied=amount;state.current.Caption='Current amount: '..tostring(actual)
|
|
state.status.Caption='Applied '..tostring(actual)..' Remnant Dust.'
|
|
state.edit.Text=tostring(actual);state.userEdited=false
|
|
end
|
|
local function buildForm()
|
|
local f=createForm(false);state.form=f
|
|
f.Caption='Phantom Blade Zero - Remnant Dust';f.Name='PBZDustEditor';f.DoNotSaveInTable=true
|
|
f.setBorderStyle('bsNone');f.ClientWidth=474;f.ClientHeight=282;f.Color=0x211A16
|
|
f.Font.Name='Segoe UI';f.Font.Size=10;f.Font.Color=0xEFE8E0;f.centerScreen()
|
|
local function label(text,x,y,w,h,size)
|
|
local l=createLabel(f);l.Parent=f;l.Caption=text;l.AutoSize=false
|
|
l.Left,l.Top,l.Width,l.Height=x,y,w,h;if size then l.Font.Size=size end;return l
|
|
end
|
|
local title=label('REMNANT DUST',23,18,428,30,15);title.Font.Style='[fsBold]'
|
|
title.OnMouseDown=function() f.dragNow() end
|
|
state.current=label('Current amount: waiting for the local pawn...',24,62,426,26,10)
|
|
label('New amount',24,103,426,23,10)
|
|
local edit=createEdit(f);state.edit=edit;edit.Parent=f
|
|
edit.Left,edit.Top,edit.Width,edit.Height=24,131,426,34
|
|
edit.Font.Size=13;edit.Color=0x2D251F;edit.Font.Color=0xEFE8E0;edit.Text=''
|
|
edit.OnChange=function() state.userEdited=true end
|
|
rounded(edit,12)
|
|
state.status=label('Enter an amount, then select Apply.',24,179,426,26,9)
|
|
local function button(text,x,click)
|
|
local b=createButton(f);b.Parent=f;b.Caption=text;b.Left,b.Top,b.Width,b.Height=x,228,204,33
|
|
b.OnClick=click;rounded(b,18);return b
|
|
end
|
|
state.applyButton=button('Apply',24,applyAmount)
|
|
button('Close',246,requestClose)
|
|
f.OnClose=function() requestClose();return caNone end
|
|
rounded(f,26);f.show()
|
|
end
|
|
local function refresh()
|
|
if state.cleaned then return end
|
|
local balance,message=safeResolve()
|
|
state.applyButton.Enabled=balance~=nil
|
|
if balance then
|
|
state.current.Caption='Current amount: '..tostring(balance.value)
|
|
if state.unavailable then state.status.Caption='Enter an amount, then select Apply.';state.unavailable=false end
|
|
if not state.initialized or (state.lastPawn~=balance.pawn and not state.userEdited) then
|
|
state.edit.Text=tostring(balance.value);state.userEdited=false;state.initialized=true
|
|
end
|
|
state.lastPawn=balance.pawn
|
|
else state.unavailable=true;state.current.Caption='Current amount: unavailable';state.status.Caption=message end
|
|
end
|
|
local ok,err=pcall(function()
|
|
local worldRef=uniqueScan(signatures.world)
|
|
state.worldSlot=worldRef+7+readInteger(worldRef+3,true)
|
|
local poolRef=uniqueScan(signatures.names)+0x1C
|
|
state.namePool=poolRef+7+readInteger(poolRef+3,true)
|
|
_G.PBZ_DustEditor=state
|
|
buildForm();refresh()
|
|
state.timer=createTimer(nil,false);state.timer.Interval=250
|
|
state.timer.OnTimer=function()
|
|
local good,message=pcall(refresh)
|
|
if not good then state.lastError=tostring(message);state.applyButton.Enabled=false;state.status.Caption='Currency unavailable. Re-enable after loading.' end
|
|
end
|
|
state.timer.Enabled=true
|
|
end)
|
|
if not ok then cleanup();error('Remnant Dust editor: '..tostring(err)) end
|
|
{$asm}
|
|
[DISABLE]
|
|
{$lua}
|
|
if syntaxcheck then return end
|
|
local state=rawget(_G,'PBZ_DustEditor')
|
|
if state and state.cleanup then state.cleanup() end
|
|
{$asm}
|