Useless files removed, small bugfix

develop
Warmist 2012-09-15 15:44:15 +03:00
parent 85fc3384dd
commit 4f9732bfda
9 changed files with 3 additions and 1560 deletions

@ -18,7 +18,7 @@ function adv_tools.reincarnate(swap_soul) --only for adventurer i guess
for i=#events-1,0,-1 do -- reverse search because almost always it will be last entry
if df.history_event_hist_figure_diedst:is_instance(events[i]) then
--print("is instance:"..i)
if events[i].hfid==hist_fig.id then
if events[i].victim==hist_fig.id then
--print("Is same id:"..i)
trg_hist_fig=events[i].slayer
if trg_hist_fig then
@ -29,12 +29,12 @@ function adv_tools.reincarnate(swap_soul) --only for adventurer i guess
end
end
if trg_hist_fig ==nil then
error("Slayer not found")
qerror("Slayer not found")
end
local trg_unit=trg_hist_fig.unit_id
if trg_unit==nil then
error("Unit id not found!")
qerror("Unit id not found!")
end
local trg_unit_final=df.unit.find(trg_unit)

@ -1,24 +0,0 @@
ptr_building={}
ptr_building.RTI={off=0,rtype=DWORD}
ptr_building.xs={off=4,rtype=DWORD}
ptr_building.ys={off=6,rtype=DWORD}
ptr_building.zs={off=8,rtype=DWORD}
ptr_building.xe={off=12,rtype=DWORD}
ptr_building.ye={off=16,rtype=DWORD}
ptr_building.ze={off=20,rtype=DWORD}
ptr_building.flags={off=24,rtype=ptt_dfflag.new(4)}
ptr_building.materials={off=28,rtype=DWORD}
ptr_building.builditems={off=228,rtype=ptr_vector}
function ptr_building.getname(self,RTI)
if RTI == nil then
return string.sub(RTTI_GetName(self.RTI),5,-3)
else
return string.sub(RTTI_GetName(RTI),5,-3)
end
end
ptr_subbuilding={}
ptr_subbuilding["building_trapst"]={}
ptr_subbuilding["building_trapst"].state={off=250,rtype=DWORD} -- atleast lever has this
ptr_subbuilding["building_doorst"]={}
ptr_subbuilding["building_doorst"].flg={off=248,rtype=WORD} --maybe flags?
ptr_subbuilding["building_doorst"].state={off=250,rtype=DWORD}

@ -1,150 +0,0 @@
function getTypename(object)
local tbl
local ret={}
if getmetatable(object)~=nil then
local tbl=getmetatable(object)
for k,v in pairs(xtypes) do
if v==tbl then
return k
end
end
for k,v in pairs(xtypes.containers) do
if v==tbl then
return k
end
end
end
if object.name~= nil then
return object.name
end
return "?"
end
function getFields(object)
local tbl
local ret={}
if getmetatable(object)==xtypes["struct-type"].wrap then
tbl=rawget(object,"mtype")
elseif getmetatable(object)==xtypes["struct-type"] then
tbl=object
else
error("Not an class_type or a class_object")
end
for k,v in pairs(tbl.types) do
table.insert(ret,{k,v[2],getTypename(v[1])})
--ret[v[2]]=k
--print(string.format("%s %x",k,v[2]))
end
table.sort(ret,function (a,b) return a[2]>b[2] end)
return ret
end
function editField(tbl,field,typename)
if EditType[typename] ~= nil then
EditType[typename](tbl[field])
else
print("Cur value:"..tostring(tbl[field]))
val=getline("Enter newvalue:")
tbl[field]=val
end
end
EditType={}
EditType["df-flagarray"]=function(trg)
local fields=rawget(trg,"mtype").index.names
print("Flag count:"..trg.size)
print("Name count:"..#fields)
for i=0,#fields do
print(string.format("%3d %20s %s",i,fields[i],tostring(trg[i-1])))
end
number=getline("enter flag id to flip:")
number=tonumber(number)
if number then
trg[fields[number]]= not trg[fields[number]]
end
end
EditType["enum-type"]=function(trg)
local fields=rawget(trg,"mtype").names
local typename=getTypename(rawget(trg,"mtype").etype)
for k,v in pairs(fields) do
print(string.format("%3d %s",k,v))
end
local cval=trg:get()
if fields[cval]~= nil then
print(string.format("Current value:%d (%s)",cval,fields[cval]))
else
print(string.format("Current value:%d",cval))
end
number=getline("enter new value:")
number=tonumber(number)
if number then
trg:set(number)
end
end
EditType["static-array"]=function(trg)
local item_type=rawget(trg,"mtype").item_type
local typename=getTypename(item_type)
number=getline(string.format("Select item (max %d, item-type '%s'):",trg.size,typename))
number=tonumber(number)
if number then
EditType[typename](trg[number])
end
end
EditType["stl-vector"]=EditType["static-array"]
EditType["df-array"]=EditType["static-array"]
EditType["struct-type"]=function(trg)
local mtype=rawget(trg,"mtype")
local fields=getFields(trg)
for k,v in pairs(fields) do
print(string.format("%4d %25s %s",k,v[1],v[3]))
end
number=getline("Choose field to edit:")
number=tonumber(number)
if number then
local v=fields[number]
editField(trg,v[1],v[3])
end
end
EditType["pointer"]=function(trg)
local mtype=rawget(trg,"mtype").ptype
local typename=getTypename(mtype)
if(trg:tonumber()==0) then
print("pointer points to nowhere!")
return
end
print("Auto dereferencing pointer! type:"..typename)
if EditType[typename] ~= nil then
EditType[typename](trg:deref())
else
print("Cur value:"..tostring(trg:deref()))
val=getline("Enter newvalue:")
trg:setref(val)
end
end
function EditDF()
local i=1
local tbl={}
for k,v in pairs(rawget(df,"types")) do
print(string.format("%4d %25s %s",i,k,getTypename(v)))
tbl[i]={k,getTypename(v)}
i=i+1
end
number=dfhack.lineedit("select item to edit (q to quit):")
if number and tonumber(number) then
local entry=tbl[tonumber(number)]
if entry==nil then
return
end
editField(df,entry[1],entry[2])
--[=[
if EditType[entry[2]] ~= nil then
EditType[entry[2]](df[entry[1]])
else
print("Cur value:"..tostring(df[entry[1]]))
val=getline("Enter newvalue:")
df[entry[1]]=val
end
--]=]
end
end
function EditObject(obj)
EditType[getTypename(obj)](obj)
end

@ -1,62 +0,0 @@
--dofile("patterns2.lua") moved to common.lua
ptr_item={}
ptr_item.RTI={off=0,rtype=DWORD}
ptr_item.x={off=4,rtype=WORD}
ptr_item.y={off=6,rtype=WORD}
ptr_item.z={off=8,rtype=WORD}
ptr_item.ref={off=0x28,rtype=ptr_vector}
ptr_item.mat={off=0x78,rtype=WORD}
ptr_item.submat={off=0x7A,rtype=WORD}
ptr_item.submat2={off=0x7C,rtype=DWORD}
ptr_item.legendid={off=0x80,rtype=DWORD} -- i don't remember writing this...
ptr_item.decorations={off=0x90,rtype=ptr_vector}
ptr_item.flags={off=0xC,rtype=ptt_dfflag.new(8)}
ptr_item.ptr_covering={off=0x64,rtype=DWORD}
ptr_item.stack={off=0x58,rtype=WORD}
function ptr_item.getname(self,RTI)
if RTI == nil then
return string.sub(RTTI_GetName(self.RTI),5,-3)
else
return string.sub(RTTI_GetName(RTI),5,-3)
end
end
ptr_subitems={}
ptr_subitems["item_slabst"]={}
ptr_subitems["item_slabst"].msgptr={off=0xA0,rtype=ptt_dfstring}
ptr_subitems["item_slabst"].signtype={off=0xC0,rtype=DWORD}
ptr_subitems["item_fisthst"]={}
ptr_subitems["item_fisthst"].fisthtype={off=0x78,rtype=WORD}
ptr_subitems["item_eggst"]={}
ptr_subitems["item_eggst"].race={off=0x78,rtype=DWORD}
ptr_subitems["item_eggst"].isfertile={off=0xa0,rtype=DWORD} --0 or 1
ptr_subitems["item_eggst"].hatchtime={off=0xa4,rtype=DWORD}
ptr_decoration_gen={}
ptr_decoration_gen.RTI={off=0,rtype=DWORD}
ptr_decoration_gen.material={off=0x04,rtype=WORD} -- same for all?
ptr_decoration_gen.submat={off=0x08,rtype=DWORD}
function ptr_decoration_gen.getname(self,RTI)
if RTI == nil then
return string.sub(RTTI_GetName(self.RTI),21,-5)
else
return string.sub(RTTI_GetName(RTI),21,-5)
end
end
ptr_decoration={}
ptr_decoration["covered"]={}
ptr_decoration["covered"].material={off=0x04,rtype=WORD}
ptr_decoration["covered"].submat={off=0x08,rtype=DWORD}
ptr_decoration["art_image"]={}
ptr_decoration["art_image"].material={off=0x04,rtype=WORD}
ptr_decoration["art_image"].submat={off=0x08,rtype=DWORD}
ptr_decoration["art_image"].image={off=0x24,rtype=DWORD}
ptr_decoration["bands"]={}
ptr_decoration["bands"].material={off=0x04,rtype=WORD}
ptr_decoration["bands"].submat={off=0x08,rtype=DWORD}
ptr_cover={} --covering of various types (blood, water, etc)
ptr_cover.mat={off=0,rtype=WORD}
ptr_cover.submat={off=4,rtype=DWORD}
ptr_cover.state={off=8,rtype=WORD}

@ -1,248 +0,0 @@
ptt_dfstring={}
if WINDOWS then
ptt_dfstring.ptr={off=0,rtype=DWORD}
ptt_dfstring.size={off=16,rtype=DWORD}
ptt_dfstring.alloc={off=20,rtype=DWORD}
function ptt_dfstring:getval()
--print(string.format("GETTING FROM:%x",self.__offset))
if self.size<16 then
--print(string.format("GETTING FROM:%x",self.__offset))
return string.sub(engine.peekstr(self.__offset),1,self.size)
else
--print(string.format("GETTING FROM:%x",self.ptr))
return string.sub(engine.peekstr(self.ptr),1,self.size)
end
end
function ptt_dfstring:setval(newstring)
local offset=self.__offset
local strl=string.len(newstring)
if strl<16 then
--print(string.format("GETTING FROM:%x",self.__offset))
engine.poked(offset+ptt_dfstring.size.off,strl)
engine.poked(offset+ptt_dfstring.alloc.off,15)
engine.pokestr(offset,newstring)
else
local loc
if engine.peekd(offset+ptt_dfstring.alloc.off) > strl then
loc=engine.peekd(offset)
print("Will fit:"..loc.." len:"..strl)
else
loc=Allocate(strl+1)
engine.poked(offset+ptt_dfstring.alloc.off,strl)
print("Will not fit:"..loc.." len:"..strl)
end
--print(string.format("GETTING FROM:%x",self.ptr))
engine.poked(self.__offset+ptt_dfstring.size.off,strl)
engine.pokestr(loc,newstring)
engine.poked(self.__offset,loc)
end
end
else
--ptt_dfstring.ptr={off=0,rtype=DWORD}
function ptt_dfstring:getval()
return engine.peekstr_stl(self.__offset)
end
end
--if(COMPATMODE) then
--ptr_vector={}
--ptr_vector.st={off=4,rtype=DWORD}
--ptr_vector.en={off=8,rtype=DWORD}
--else
ptr_vector={}
ptr_vector.st={off=0,rtype=DWORD}
ptr_vector.en={off=4,rtype=DWORD}
ptr_vector.alloc={off=8,rtype=DWORD}
--end
function ptr_vector:clone(settype)
local ret={}
for k,v in pairs(self) do
ret[k]=v
end
ret.type=settype
return ret
end
function ptr_vector:size()
return (self.en-self.st)/engine.sizeof(self.type)
end
ptr_vector.type=DWORD
function ptr_vector:getval(num)
if self.st==0 then return error("Vector empty.") end
--print("Wants:"..num.." size:"..self:size())
if num>=self:size() then error("Index out of bounds in vector.") end
return engine.peek(self.st+engine.sizeof(self.type)*num,self.type)
end
function ptr_vector:setval(num,val)
return engine.poke(self.st+engine.sizeof(self.type)*num,self.type,val)
end
function ptr_vector:append(val)
if self.alloc - self.en > 0 then
local num=self:size()
self.en=self.en+engine.sizeof(self.type)
self:setval(val,num)
else
error("larger than allocated arrays not implemented yet")
local num=self:size()
local ptr=Allocate(num*2)
end
end
ptt_dfflag={}
function ptt_dfflag.flip(self,num) --flip one bit in flags
local of=math.floor (num/8);
self[of]=bit.bxor(self[of],bit.lshift(1,num%8))
end
function ptt_dfflag.get(self,num) -- get one bit in flags
local of=math.floor (num/8);
if bit.band(self[of],bit.lshift(1,num%8))~=0 then
return true
else
return false
end
end
function ptt_dfflag.set(self,num,val) --set to on or off one bit in flags
if (self:get(num)~=val) then
self:flip(num)
end
end
function ptt_dfflag.new(size) -- create new flag pattern of size(in bytes)
local ret={}
for i=0,size-1 do
ret[i]={off=i,rtype=BYTE};
end
ret.flip=ptt_dfflag.flip --change to metatable stuff...
ret.get=ptt_dfflag.get
ret.set=ptt_dfflag.set
return ret;
end
--[[
Creature:
0 name (df_string) 28
28 nick (df_string) 56
56 surname- namearray(7*dword(4)) 84 ...
140 race (dword) 144
224 flags
264 civ (dword)
252 ID
592 following ID
904 bleed vector? hurt vector or sth...
0x790 legends id?
2128 known names? or knowledge?
flags:
0 Can the dwarf move or are they waiting for their movement timer
1 Dead (might also be set for incoming/leaving critters that are alive)
2 Currently in mood
3 Had a mood
4 "marauder" -- wide class of invader/inside creature attackers
5 Drowning
6 Active merchant
7 "forest" (used for units no longer linked to merchant/diplomacy, they just try to leave mostly)
8 Left (left the map)
9 Rider
10 Incoming
11 Diplomat
12 Zombie
13 Skeleton
14 Can swap tiles during movement (prevents multiple swaps)
15 On the ground (can be conscious)
16 Projectile
17 Active invader (for organized ones)
18 Hidden in ambush
19 Invader origin (could be inactive and fleeing)
20 Will flee if invasion turns around
21 Active marauder/invader moving inward
22 Marauder resident/invader moving in all the way
23 Check against flows next time you get a chance
24 Ridden
25 Caged
26 Tame
27 Chained
28 Royal guard
29 Fortress guard
30 Suppress wield for beatings/etc
31 Is an important historical figure
32 swiming
]]--
ptr_Creature={}
local posoff=0 --VersionInfo.getGroup("Creatures"):getGroup("creature"):getOffset("position")
ptr_Creature.x={off=posoff,rtype=WORD} --ok
ptr_Creature.y={off=posoff+2,rtype=WORD} --ok
ptr_Creature.z={off=posoff+4,rtype=WORD} --ok
ptr_Creature.flags={off=0,rtype=ptt_dfflag.new(10)}
ptr_Creature.name={off=0,rtype=ptt_dfstring}
ptr_Creature.ID={off=252,rtype=DWORD} --ok i guess
ptr_Creature.followID={off=592,rtype=DWORD} --ok
ptr_Creature.race={off=140,rtype=DWORD} --ok
ptr_Creature.civ={off=264,rtype=DWORD}
ptr_Creature.legends={off=344,rtype=ptr_vector} --ok
ptr_Creature.hurt1={off=0x308,rtype=ptr_vector:clone(BYTE)} --byte vector...
ptr_Creature.hurt2={off=0x338,rtype=ptr_vector}
ptr_Creature.wounds={off=0x388,rtype=ptr_vector}
ptr_Creature.itemlist1={off=0x1D0,rtype=ptr_vector}
ptr_Creature.itemlist2={off=0x288,rtype=ptr_vector}
ptr_Creature.bloodlvl={off=0x490,rtype=DWORD}
ptr_Creature.bleedlvl={off=0x494,rtype=DWORD}
ptr_CrGloss={}
ptr_CrGloss.token={off=0,rtype=ptt_dfstring}
ptr_CrGloss.castes={off=296,rtype=ptr_vector}
ptr_CrCaste={}
ptr_CrCaste.name={off=0,rtype=ptt_dfstring}
ptr_CrCaste.flags_ptr={off=0x5A0,rtype=DWORD} --size 17?
--[=[
Flags:
57 - is sentient (allows setting labours)
--]=]
ptr_LEntry={} -- all size 256
ptr_LEntry.name={off=36,rtype=ptt_dfstring}
ptr_LEntry.id1={off=160,rtype=DWORD}
ptr_LEntry.id2={off=164,rtype=DWORD}
ptr_LEntry.somlist={off=220,rtype=DWORD}
ptr_dfname={}
for i=0,6 do
ptr_dfname[i]={off=i*4,rtype=DWORD}
end
--[[
Site docs:
0x38 name struct todo...
0x78 type:
0 - mountain halls (yours)
1 - dark fort
2 - cave
3 - mountain hall (other)
4 - forest
5 - hamlet
6 - imp location
7 - lair
8 - fort
9 - camp
0x7a some sort of id?
0x84 some vec (ids)
0x94 some other vec (ids)
0xa4 some vec (prts)
0x118 ptr to sth
0x14c ptr to mapdata
]]--
ptr_site={}
ptr_site.type={off=0x78,rtype=WORD}
ptr_site.id={off=0x7a,rtype=DWORD}
ptr_site.name={off=0x38,rtype=ptr_dfname}
ptr_site.flagptr={off=0x118,rtype=DWORD}
ptr_legends2={}
ptr_legends2.id={off=0,rtype=DWORD}
ptr_legends2.follow={off=0x18,rtype=DWORD}
ptr_material={}
ptr_material.token={off=0,rtype=ptt_dfstring}

@ -1,29 +0,0 @@
ptr_COL={} -- complete object locator...
ptr_COL.sig={off=0,rtype=DWORD}
ptr_COL.offset={off=4,rtype=DWORD} --offset of this vtable in the complete class
ptr_COL.cdoffset={off=8,rtype=DWORD} -- constructor displacement
ptr_COL.typePointer={off=12,rtype=DWORD}
ptr_COL.hierarchyPointer={off=16,rtype=DWORD}
ptr_RTTI_Type={}
ptr_RTTI_Type.vftPointer={off=0,rtype=DWORD}
ptr_RTTI_Type.name={off=8,rtype=STD_STRING}
function RTTI_GetName(vtable)
local COLoff=engine.peek(vtable-4,DWORD)
--print(string.format("Look:%x vtable:%x",vtable,engine.peek(vtable-4,DWORD)))
COL=engine.peek(COLoff,ptr_COL)
--print(string.format("COL:%x Typeptr:%x Type:%s",COLoff,COL.typePointer,engine.peek(COL.typePointer,ptr_RTTI_Type.name)))
return engine.peek(COL.typePointer,ptr_RTTI_Type.name)
end
ptr_RTTI_Hierarchy={}
ptr_RTTI_Hierarchy.sig={off=0,rtype=DWORD}
ptr_RTTI_Hierarchy.attributes={off=4,rtype=DWORD}
ptr_RTTI_Hierarchy.numBaseClasses={off=8,rtype=DWORD}
ptr_RTTI_Hierarchy.ptrBases={off=12,rtype=DWORD}
ptr_RTTI_BaseClass={}
ptr_RTTI_BaseClass.typePointer={off=0,rtype=DWORD}
ptr_RTTI_BaseClass.numContained={off=4,rtype=DWORD}
--todo PMD
--todo flags

@ -1,151 +0,0 @@
if types ~= nil then
return
end
dofile("dfusion/xml_types.lua")
function parseTree(t)
for k,v in ipairs(t) do
if v.xarg~=nil and v.xarg["type-name"]~=nil and v.label=="ld:global-type" then
local name=v.xarg["type-name"];
if(types[name]==nil) then
--for kk,vv in pairs(v.xarg) do
-- print("\t"..kk.." "..tostring(vv))
--end
types[name]=makeType(v)
--print("found "..name.." or type:"..v.xarg.meta or v.xarg.base)
else
types[name]=makeType(v,types[name])
end
end
end
end
function parseTreeGlobals(t)
local glob={}
--print("Parsing global-objects")
for k,v in ipairs(t) do
if v.xarg~=nil and v.label=="ld:global-object" then
local name=v.xarg["name"];
--print("Parsing:"..name)
local subitem=v[1]
if subitem==nil then
error("Global-object subitem is nil:"..name)
end
local ret=makeType(subitem)
if ret==nil then
error("Make global returned nil!")
end
glob[name]=ret
end
end
--print("Printing globals:")
--for k,v in pairs(glob) do
-- print(k)
--end
return glob
end
function findAndParse(tname)
--if types[tname]==nil then types[tname]={} end
-- [=[
for k,v in ipairs(main_tree) do
local name=v.xarg["type-name"];
if v.xarg~=nil and v.xarg["type-name"]~=nil and v.label=="ld:global-type" then
if(name ==tname) then
--print("Parsing:"..name)
--for kk,vv in pairs(v.xarg) do
-- print("\t"..kk.." "..tostring(vv))
--end
types[name]=makeType(v,types[name])
end
--print("found "..name.." or type:"..v.xarg.meta or v.xarg.base)
end
end
--]=]
end
df={}
df.types=rawget(df,"types") or {} --temporary measure for debug
local df_meta={}
function df_meta:__index(key)
local addr=VersionInfo.getAddress(key)
local vartype=rawget(df,"types")[key];
if addr==0 then
error("No such global address exist")
elseif vartype==nil then
error("No such global type exist")
else
return type_read(vartype,addr)
end
end
function df_meta:__newindex(key,val)
local addr=VersionInfo.getAddress(key)
local vartype=rawget(df,"types")[key];
if addr==0 then
error("No such global address exist")
elseif vartype==nil then
error("No such global type exist")
else
return type_write(vartype,addr,val)
end
end
setmetatable(df,df_meta)
--------------------------------
types=types or {}
dofile("dfusion/patterns/xml_angavrilov.lua")
-- [=[
main_tree=parseXmlFile("dfusion/patterns/supplementary.xml")[1]
parseTree(main_tree)
main_tree=parseXmlFile("dfusion/patterns/codegen.out.xml")[1]
parseTree(main_tree)
rawset(df,"types",parseTreeGlobals(main_tree))
--]=]
--[=[labels={}
for k,v in ipairs(t) do
labels[v.label]=labels[v.label] or {meta={}}
if v.label=="ld:global-type" and v.xarg~=nil and v.xarg.meta ~=nil then
labels[v.label].meta[v.xarg.meta]=1
end
end
for k,v in pairs(labels) do
print(k)
if v.meta~=nil then
for kk,vv in pairs(v.meta) do
print("=="..kk)
end
end
end--]=]
function addressOf(var,key)
if key== nil then
local addr=rawget(var,"ptr")
return addr
else
local meta=getmetatable(var)
if meta== nil then
error("Failed to get address, no metatable")
end
if meta.__address == nil then
error("Failed to get address, no __address function")
end
return meta.__address(var,key)
end
end
function printGlobals()
print("Globals:")
for k,v in pairs(rawget(df,"types")) do
print(k)
end
end
function printFields(object)
local tbl
if getmetatable(object)==xtypes["struct-type"].wrap then
tbl=rawget(object,"mtype")
elseif getmetatable(object)==xtypes["struct-type"] then
tbl=object
else
error("Not an class_type or a class_object")
end
for k,v in pairs(tbl.types) do
print(string.format("%s %x",k,v[2]))
end
end

@ -1,734 +0,0 @@
--<angavrilov> otherwise you just maintain alignment granularity in addition to size for all fields,
-- round up current offset to field alignment,
-- assign structs the max alignment of any field, and round up struct size to its alignment
function type_read(valtype,address)
if valtype.issimple then
--print("Simple read:"..tostring(valtype.ctype))
return engine.peek(address,valtype.ctype)
else
return valtype:makewrap(address)
end
end
function type_write(valtype,address,val)
if valtype.issimple then
engine.poke(address,valtype.ctype,val)
else
engine.poke(address,DWORD,rawget(val,"ptr"))
end
end
function first_of_type(node,labelname)
for k,v in ipairs(node) do
if type(v)=="table" and v.label==labelname then
return v
end
end
end
xtypes={} -- list of all types prototypes (e.g. enum-type -> announcement_type)
-- type must have size, new and makewrap (that makes a wrapper around ptr)
if WINDOWS then
dofile("dfusion/xml_types_windows.lua")
elseif LINUX then
dofile("dfusion/xml_types_linux.lua")
end
function padAddress(curoff,typetoadd) --return new offset to place things... Maybe linux is different?
--windows -> sizeof(x)==alignof(x)
--[=[
if sizetoadd>8 then sizetoadd=8 end
if(math.mod(curoff,sizetoadd)==0) then
return curoff
else
return curoff+(sizetoadd-math.mod(curoff,sizetoadd))
end
--]=]
if typetoadd==nil or typetoadd.__align==nil then
return curoff
else
if(math.mod(curoff,typetoadd.__align)==0) then
return curoff
else
if PRINT_PADS then
print("padding off:"..curoff.." with align:"..typetoadd.__align.." pad="..(typetoadd.__align-math.mod(curoff,typetoadd.__align)))
end
return curoff+(typetoadd.__align-math.mod(curoff,typetoadd.__align))
end
end
end
local sarr={}
sarr.__index=sarr
function sarr.new(node,obj)
local o=obj or {}
setmetatable(o,sarr)
--print("Making array.")
o.count=tonumber(node.xarg.count)
--print("got count:"..o.count)
o.item_type=makeType(first_of_type(node,"ld:item"))
o.__align=o.item_type.__align or 4
o.size=o.count*o.item_type.size
--print("got subtypesize:"..o.item_type.size)
return o
end
function sarr:makewrap(address)
local o={}
o.mtype=self
o.ptr=address
setmetatable(o,self.wrap)
return o
end
sarr.wrap={}
function sarr.wrap:__index(key)
if key=="size" then
return rawget(self,"mtype").count
end
local num=tonumber(key)
local mtype=rawget(self,"mtype")
if num~=nil and num<mtype.count then
return type_read(mtype.item_type,num*mtype.item_type.size+rawget(self,"ptr"))
else
error("invalid key to static-array")
end
end
function sarr.wrap:__newindex(key,val)
local num=tonumber(key)
if num~=nil and num<rawget(self,"mtype").count then
return type_write(mtype.item_type,num*mtype.item_type.size+rawget(self,"ptr"),val)
else
error("invalid key to static-array")
end
end
--]=]
xtypes["static-array"]=sarr
simpletypes={}
simpletypes["s-float"]={FLOAT,4,4}
simpletypes.int64_t={QWORD,8,8}
simpletypes.uint32_t={DWORD,4,4}
simpletypes.uint16_t={WORD,2,2}
simpletypes.uint8_t={BYTE,1,1}
simpletypes.int32_t={DWORD,4,4}
simpletypes.int16_t={WORD,2,2}
simpletypes.int8_t={BYTE,1,1}
simpletypes.bool={BYTE,1,1}
simpletypes["stl-string"]={STD_STRING,28,4}
function getSimpleType(typename,obj)
if simpletypes[typename] == nil then return end
local o=obj or {}
o.name=typename
o.ctype=simpletypes[typename][1]
o.size=simpletypes[typename][2]
o.__align=simpletypes[typename][3]
o.issimple=true
return o
end
local type_enum={}
type_enum.__index=type_enum
function type_enum.new(node,obj)
local o=obj or {}
setmetatable(o,type_enum)
o.names={}
for k,v in pairs(node) do
if v.label=="enum-item" then
if v.xarg~=nil and v.xarg.name then
--print("\t"..k.." "..v.xarg.name)
o.names[k-1]=v.xarg.name
else
o.names[k-1]=string.format("unk_%d",k-1)
end
end
end
local btype=node.xarg["base-type"] or "uint32_t"
--print(btype.." t="..convertType(btype))
o:setbtype(btype)
return o
end
function type_enum:setbtype(btype)
self.etype=getSimpleType(btype) -- should be simple type
self.size=self.etype.size
self.__align=self.etype.__align
end
function type_enum:makewrap(address)
local o={}
o.mtype=self
o.ptr=address
setmetatable(o,self.wrap)
return o
end
type_enum.wrap={}
type_enum.wrap.__index=type_enum.wrap
type_enum.wrap.set=function (tbl,val)
local mtype=rawget(tbl,"mtype")
local ptr=rawget(tbl,"ptr")
type_write(mtype.etype,ptr,val)
end
type_enum.wrap.get=function (tbl)
local mtype=rawget(tbl,"mtype")
local ptr=rawget(tbl,"ptr")
return type_read(mtype.etype,ptr)
end
xtypes["enum-type"]=type_enum
local type_bitfield={} --bitfield can be accessed by number (bf[1]=true) or by name bf.DO_MEGA=true
type_bitfield.__index=type_bitfield
function type_bitfield.new(node,obj)
local o=obj or {}
setmetatable(o,type_bitfield)
o.size=0
o.fields_named={}
o.fields_numed={}
o.__align=1
for k,v in pairs(node) do
if type(v)=="table" and v.xarg~=nil then
local name=v.xarg.name
if name==nil then
name="anon_"..tostring(k)
end
--print("\t"..k.." "..name)
o.fields_numed[k]=name
o.fields_named[name]=k
o.size=o.size+1
end
end
--o.size=o.size/8 -- size in bytes, not bits.
o.size=4
o.size=math.ceil(o.size)
--[=[if math.mod(o.size,o.__align) ~= 0 then
o.size=o.size+ (o.__align-math.mod(o.size,o.__align))
end]=]
return o
end
function type_bitfield:bitread(addr,nbit)
local byte=engine.peekb(addr+nbit/8)
if bit.band(byte,bit.lshift(1,nbit%8))~=0 then
return true
else
return false
end
end
function type_bitfield:bitwrite(addr,nbit,value)
local byte=engine.peekb(addr+nbit/8)
if self:bitread(addr,nbit)~= value then
local byte=bit.bxor(byte,bit.lshift(1,nbit%8))
engine.pokeb(addr+nbit/8,byte)
end
end
type_bitfield.wrap={}
function type_bitfield:makewrap(address)
local o={}
o.mtype=self
o.ptr=address
setmetatable(o,self.wrap)
return o
end
function type_bitfield.wrap.__next(tbl,key)
local kk,vv=next(rawget(tbl,"mtype").fields_named,key)
if kk ~= nil then
return kk,tbl[kk]
else
return nil
end
end
function type_bitfield.wrap:__index(key)
local myptr=rawget(self,"ptr")
local mytype=rawget(self,"mtype")
local num=tonumber(key)
if num~=nil then
if mytype.fields_numed[num]~=nil then
return mytype:bitread(myptr,num-1)
else
error("No bit with index:"..tostring(num))
end
elseif mytype.fields_named[key]~= nil then
return mytype:bitread(myptr,mytype.fields_named[key]-1)
else
error("No such field exists")
end
end
function type_bitfield.wrap:__newindex(key,value)
local myptr=rawget(self,"ptr")
local mytype=rawget(self,"mtype")
local num=tonumber(key)
if num~=nil then
if mytype.fields_numed[num]~=nil then
return mytype:bitwrite(myptr,num-1,value)
else
error("No bit with index:"..tostring(num))
end
elseif mytype.fields_named[key]~= nil then
return mytype:bitwrite(myptr,mytype.fields_named[key]-1,value)
else
error("No such field exists")
end
end
xtypes["bitfield-type"]=type_bitfield
local type_class={}
type_class.__index=type_class
function type_class.new(node,obj)
local o=obj or {}
setmetatable(o,type_class)
o.types={}
o.base={}
o.size=0
local isunion=false
if node.xarg["is-union"]=="true" then
--error("unions not supported!")
isunion=true
end
local firsttype;
--o.baseoffset=0
if node.xarg["inherits-from"]~=nil then
table.insert(o.base,getGlobal(node.xarg["inherits-from"]))
--error("Base class:"..node.xarg["inherits-from"])
end
for k,v in ipairs(o.base) do
for kk,vv in pairs(v.types) do
o.types[kk]={vv[1],vv[2]+o.size}
end
o.size=o.size+v.size
end
--o.baseoffset=o.size;
--o.size=0
for k,v in pairs(node) do
if type(v)=="table" and v.label=="ld:field" and v.xarg~=nil then
local t_name=""
local name=v.xarg.name or v.xarg["anon-name"] or ("unk_"..k)
--print("\t"..k.." "..name.."->"..v.xarg.meta.." ttype:"..v.label)
local ttype=makeType(v)
if ttype.size==0 then error("Field with 0 size! type="..node.xarg["type-name"].."."..name) end
if ttype.size-math.ceil(ttype.size) ~= 0 then error("Field with real offset! type="..node.xarg["type-name"].."."..name) end
--for k,v in pairs(ttype) do
-- print(k..tostring(v))
--end
local off=padAddress(o.size,ttype)
--[=[if PRINT_PADS then
if ttype.__align then
print(name.." "..ttype.__align .. " off:"..off.." "..math.mod(off,ttype.__align))
end
if off~=o.size then
print("var:"..name.." size:"..ttype.size)
end
end]=]
--print("var:"..name.." ->"..tostring(off).. " :"..ttype.size)
if isunion then
if ttype.size > o.size then
o.size=ttype.size
end
o.types[name]={ttype,0}
else
o.size=off
o.types[name]={ttype,o.size}--+o.baseoffset
o.size=o.size+ttype.size
end
if firsttype== nil then
firsttype=o.types[name][1]
end
end
end
if isunion then
o.__align=0
for k,v in pairs(o.types) do
if v[1].__align~= nil and v[1].__align>o.__align then
o.__align=v[1].__align
end
end
else
if o.base[1]~= nil then
o.__align=o.base[1].__align
elseif firsttype~= nil then
o.__align=firsttype.__align
--if o.__align~=nil then
--print("\t\t setting align to:"..(o.__align or ""))
--else
--o.__align=4
--print("\t\t NIL ALIGNMENT!")
--end
end
end
return o
end
type_class.wrap={}
function type_class.wrap:__address(key)
local myptr=rawget(self,"ptr")
local mytype=rawget(self,"mtype")
if mytype.types[key] ~= nil then
return myptr+mytype.types[key][2]
else
error("No such field exists")
end
end
function type_class.wrap:__index(key)
local myptr=rawget(self,"ptr")
local mytype=rawget(self,"mtype")
if mytype.types[key] ~= nil then
return type_read(mytype.types[key][1],myptr+mytype.types[key][2])
else
error("No such field exists")
end
end
function type_class.wrap:__newindex(key,value)
local myptr=rawget(self,"ptr")
local mytype=rawget(self,"mtype")
if mytype.types[key] ~= nil then
return type_write(mytype.types[key][1],myptr+mytype.types[key][2],value)
else
error("No such field exists")
end
end
function type_class:makewrap(ptr)
local o={}
o.ptr=ptr
o.mtype=self
setmetatable(o,self.wrap)
return o
end
xtypes["struct-type"]=type_class
xtypes["class-type"]=type_class
local type_pointer={}
type_pointer.__index=type_pointer
function type_pointer.new(node,obj)
local o=obj or {}
setmetatable(o,type_pointer)
local subnode=first_of_type(node,"ld:item")
if subnode~=nil then
o.ptype=makeType(subnode,nil,true)
end
o.size=4
o.__align=4
return o
end
type_pointer.wrap={}
type_pointer.wrap.__index=type_pointer.wrap
function type_pointer.wrap:tonumber()
local myptr=rawget(self,"ptr")
return engine.peekd(myptr)--type_read(DWORD,myptr)
end
function type_pointer.wrap:__setup(trg)
if trg~= nil then
self:fromnumber(trg)
else
self:fromnumber(0)
end
end
function type_pointer.wrap:fromnumber(num)
local myptr=rawget(self,"ptr")
return engine.poked(myptr,num)--type_write(DWORD,myptr,num)
end
function type_pointer.wrap:deref()
local myptr=rawget(self,"ptr")
local mytype=rawget(self,"mtype")
return type_read(mytype.ptype,engine.peekd(myptr))
end
function type_pointer.wrap:setref(val)
local myptr=rawget(self,"ptr")
local mytype=rawget(self,"mtype")
return type_write(mytype.ptype,engine.peekd(myptr),val)
end
function type_pointer.wrap:newref(val)
local myptr=rawget(self,"ptr")
local mytype=rawget(self,"mtype")
local ptr=engine.alloc(mytype.ptype.size)
self:fromnumber(ptr)
return ptr
end
function type_pointer:makewrap(ptr)
local o={}
o.ptr=ptr
o.mtype=self
setmetatable(o,self.wrap)
return o
end
xtypes["pointer"]=type_pointer
--------------------------------------------
--stl-vector (beginptr,endptr,allocptr)
--df-flagarray (ptr,size)
xtypes.containers=xtypes.containers or {}
local dfarr={}
dfarr.__index=dfarr
function dfarr.new(node,obj)
local o=obj or {}
setmetatable(o,dfarr)
o.size=8
o.__align=4
o.item_type=makeType(first_of_type(node,"ld:item"))
return o
end
function dfarr:makewrap(address)
local o={}
o.mtype=self
o.ptr=address
setmetatable(o,self.wrap)
return o
end
dfarr.wrap={}
function dfarr.wrap:__setup(size)
local mtype=rawget(self,"mtype")
engine.pokew(rawget(self,"ptr")+4,size)
local newptr=engine.alloc(size*mtype.item_type.size)
engine.poked(rawget(self,"ptr"),newptr)
end
function dfarr.wrap:__index(key)
local num=tonumber(key)
local mtype=rawget(self,"mtype")
local size=engine.peekw(rawget(self,"ptr")+4)
if key=="size" then
return size
end
local item_start=engine.peekd(rawget(self,"ptr"))
if num~=nil and num<sizethen then
return type_read(mtype.item_type,num*mtype.item_type.size+item_start)
else
error("invalid key to df-array")
end
end
function dfarr.wrap:__newindex(key,val)
local num=tonumber(key)
local size=engine.peekw(rawget(self,"ptr")+4)
local item_start=engine.peekd(rawget(self,"ptr"))
if num~=nil and num<size then
return type_write(mtype.item_type,num*mtype.item_type.size+item_start,val)
else
error("invalid key to df-array")
end
end
xtypes.containers["df-array"]=dfarr
local farr={}
farr.__index=farr
function farr.new(node,obj)
local o=obj or {}
setmetatable(o,farr)
o.size=8
o.__align=4
if node.xarg["index-enum"]~= nil then
o.index=getGlobal(node.xarg["index-enum"],true)
end
return o
end
function farr:makewrap(address)
local o={}
o.mtype=self
o.ptr=address
setmetatable(o,self.wrap)
return o
end
function farr:bitread(addr,nbit)
local byte=engine.peekb(addr+nbit/8)
if bit.band(byte,bit.lshift(1,nbit%8))~=0 then
return true
else
return false
end
end
function farr:bitwrite(addr,nbit,value)
local byte=engine.peekb(addr+nbit/8)
if self:bitread(addr,nbit)~= value then
local byte=bit.bxor(byte,bit.lshift(1,nbit%8))
engine.pokeb(addr+nbit/8,byte)
end
end
type_bitfield.wrap={}
function type_bitfield:makewrap(address)
local o={}
o.mtype=self
o.ptr=address
setmetatable(o,self.wrap)
return o
end
farr.wrap={}
function farr.wrap.__next(tbl,key)
error("TODO")
end
function farr.wrap:__index(key)
local num=tonumber(key)
local mtype=rawget(self,"mtype")
local size=engine.peekd(rawget(self,"ptr")+4)*8
if key=="size" then
return size;
end
if mtype.index~=nil and num==nil then
--print("Requested:"..key)
for k,v in pairs(mtype.index.names) do
if v==key then
num=k
break
end
end
--print("is key:"..num)
end
if num~=nil and num<size then
return mtype:bitread(rawget(self,"ptr"),num)
else
error("invalid key to df-flagarray")
end
end
function farr.wrap:__newindex(key,val)
local num=tonumber(key)
local size=engine.peekd(rawget(self,"ptr")+4)*8
local mtype=rawget(self,"mtype")
if mtype.index~=nil and num==nil then
--print("Requested:"..key)
for k,v in pairs(mtype.index.names) do
if v==key then
num=k
break
end
end
--print("is key:"..num)
end
if num~=nil and num<size then
return mtype:bitwrite(rawget(self,"ptr"),num,val)
else
error("invalid key to df-flagarray")
end
end
xtypes.containers["df-flagarray"]=farr
--------------------------------------------
local bytes_pad={}
bytes_pad.__index=bytes_pad
function bytes_pad.new(node,obj)
local o=obj or {}
setmetatable(o,bytes_pad)
o.size=tonumber(node.xarg.size)
if node.xarg.alignment~=nil then
--print("Aligned bytes!")
o.__align=tonumber(node.xarg.alignment)
end
return o
end
xtypes["bytes"]=bytes_pad
--------------------------------------------
function getGlobal(name,canDelay)
if types[name]== nil then
if canDelay then
types[name]={}
return types[name]
end
findAndParse(name)
if types[name]== nil then
error("type:"..name.." failed find-and-parse")
end
--error("type:"..node.xarg["type-name"].." should already be ready")
end
if types[name].size==nil and canDelay==nil then --was delayed, now need real type
findAndParse(name)
if types[name]== nil then
error("type:"..name.." failed find-and-parse")
end
end
return types[name]
end
parser={}
parser["ld:global-type"]=function (node,obj)
return xtypes[node.xarg.meta].new(node,obj)
end
parser["ld:global-object"]=function (node)
end
parser["ld:field"]=function (node,obj,canDelay)
local meta=node.xarg.meta
if meta=="number" or (meta=="primitive" and node.xarg.subtype=="stl-string") then
return getSimpleType(node.xarg.subtype,obj)
elseif meta=="static-array" then
return xtypes["static-array"].new(node,obj)
elseif meta=="global" then
local ltype=getGlobal(node.xarg["type-name"],canDelay)
if node.xarg["base-type"]~=nil then
ltype:setbtype(node.xarg["base-type"])
end
return ltype
elseif meta=="compound" then
if node.xarg.subtype==nil then
return xtypes["struct-type"].new(node,obj)
else
return xtypes[node.xarg.subtype.."-type"].new(node,obj)
end
elseif meta=="container" then
local subtype=node.xarg.subtype
if xtypes.containers[subtype]==nil then
error(subtype.." not implemented... (container)")
else
return xtypes.containers[subtype].new(node,obj)
end
elseif meta=="pointer" then
return xtypes["pointer"].new(node,obj)
elseif meta=="bytes" then
return xtypes["bytes"].new(node,obj)
else
error("Unknown meta:"..meta)
end
end
parser["ld:item"]=parser["ld:field"]
function makeType(node,obj,canDelay)
local label=node.label
if parser[label] ~=nil then
--print("Make Type with:"..label)
local ret=parser[label](node,obj,canDelay)
if ret==nil then
error("Error parsing:"..label.." nil returned!")
else
return ret
end
else
for k,v in pairs(node) do
print(k.."->"..tostring(v))
end
error("Node parser not found: "..label)
end
--[=[
if getSimpleType(node)~=nil then
return getSimpleType(node)
end
print("Trying to make:"..node.xarg.meta)
if xtypes[node.xarg.meta]~=nil then
return xtypes[node.xarg.meta].new(node,obj)
end
if node.xarg.meta=="global" then
--print(node.xarg["type-name"])
if types[node.xarg["type-name"]]== nil then
error("type:"..node.xarg["type-name"].." should already be ready")
end
return types[node.xarg["type-name"]]
end
]=]
--[=[for k,v in pairs(node) do
print(k.."=>"..tostring(v))
if type(v)=="table" then
for kk,vv in pairs(v) do
print("\t"..kk.."=>"..tostring(vv))
end
end
end]=]
end

@ -1,159 +0,0 @@
xtypes.containers=xtypes.containers or {}
local stl_vec={}
--[=[
(make-instance 'pointer :name $start)
(make-instance 'pointer :name $end)
(make-instance 'pointer :name $block-end)
(make-instance 'padding :name $pad :size 4 :alignment 4)
--]=]
stl_vec.__index=stl_vec
function stl_vec.new(node,obj)
local o=obj or {}
o.size=16
o.__align=4
local titem=first_of_type(node,"ld:item")
if titem~=nil then
o.item_type=makeType(titem)
else
o.item_type=getSimpleType("uint32_t")
end
setmetatable(o,stl_vec)
return o
end
function stl_vec:makewrap(address)
local o=obj or {}
o.mtype=self
o.ptr=address
setmetatable(o,self.wrap)
return o
end
stl_vec.wrap={}
function stl_vec.wrap:__index(key)
local num=tonumber(key)
local mtype=rawget(self,"mtype")
local ptr=rawget(self,"ptr")
local p_begin=engine.peek(ptr,DWORD)
local p_end=engine.peek(ptr+4,DWORD)
local size=(p_end-p_begin)/mtype.item_type.size
if key=="size" then
return size
end
--allocend=type_read(ptr+8,DWORD)
if num~=nil and num<size then
return type_read(mtype.item_type,num*mtype.item_type.size+p_begin)
else
error(string.format("Invalid key: %s max: %d",key,size))
end
end
function stl_vec.wrap:__newindex(key,val)
local num=tonumber(key)
error("TODO make __newindex for stl_vec")
if num~=nil and num<rawget(self,"mtype").count then
return type_write(mtype.item_type,num*mtype.item_type.size+rawget(self,"ptr"),val)
else
error("invalid key to stl vector")
end
end
function stl_vec.wrap.__next(tbl,key)
--print("next with:"..tostring(key))
if key==nil then
return 0,tbl[0]
else
if key<tbl.size-1 then
return key+1,tbl[key+1]
else
return nil
end
end
end
xtypes.containers["stl-vector"]=stl_vec
local stl_vec_bit={}
--[=[
(make-instance 'pointer :name $start)
(make-instance 'pointer :name $end)
(make-instance 'pointer :name $block-end)
(make-instance 'padding :name $pad :size 4)
(make-instance 'int32_t :name $size)
--]=]
stl_vec_bit.__index=stl_vec_bit
function stl_vec_bit.new(node,obj)
local o=obj or {}
setmetatable(o,stl_vec_bit)
o.size=20
o.align=4
return o
end
function stl_vec_bit:makewrap(address)
local o=obj or {}
o.mtype=self
o.ptr=address
setmetatable(o,self.wrap)
return o
end
stl_vec_bit.wrap={}
function stl_vec_bit.wrap:__index(key)
local num=tonumber(key)
local mtype=rawget(self,"item_type")
local ptr=rawget(self,"ptr")
local p_begin=type_read(ptr,DWORD)
local p_end=type_read(ptr+4,DWORD)
--allocend=type_read(ptr+8,DWORD)
error("TODO make __index for stl_vec_bit")
if num~=nil and num<sizethen then
return type_read(mtype.ctype,num*mtype.ctype.size+rawget(self,"ptr"))
else
error("invalid key to df-flagarray")
end
end
function stl_vec_bit.wrap:__newindex(key,val)
local num=tonumber(key)
error("TODO make __index for stl_vec_bit")
if num~=nil and num<rawget(self,"mtype").count then
return type_write(mtype.ctype,num*mtype.ctype.size+rawget(self,"ptr"),val)
else
error("invalid key to static-array")
end
end
function stl_vec_bit.wrap:__next(tbl,key)
if key==nil then
return 0,self[0]
else
if key+1<self.size then
return key+1,self[key+1]
else
return nil
end
end
end
xtypes.containers["stl-bit-vector"]=stl_vec_bit
local stl_deque={}
--[=[
(make-instance 'pointer :name $proxy)
(make-instance 'pointer :name $map)
(make-instance 'int32_t :name $map-size)
(make-instance 'int32_t :name $off)
(make-instance 'int32_t :name $size)
(make-instance 'padding :size 4 :alignment 4)
--]=]
stl_deque.__index=stl_deque
function stl_deque.new(node,obj)
local o=obj or {}
setmetatable(o,stl_deque)
o.size=24
o.__align=4
return o
end
xtypes.containers["stl-deque"]=stl_deque
local dfllist={}
function dfllist.new(node,obj)
local realtype=makeType(first_of_type(node,"ld:item"))
return realtype --.new(node,obj)
end
xtypes.containers["df-linked-list"]=dfllist