Page 1 of 1

remaking genFromStruct

Posted: Wed Feb 02, 2022 8:16 am
by Frouk

Code: Select all

local useGroupMenu = true
local extGroupMenuCaption = 'Extra Extensions'
local extItemCaption = 'GenFromStructName'
local mf = getMainForm()
local mm = mf.Menu
local extMenu = nil

if useGroupMenu then
  for i=0,mm.Items.Count-1 do
      if mm.Items.Item[i].Caption == extGroupMenuCaption then
          extMenu = mm.Items.Item[i]
          break
      end
  end
  if not extMenu then
      extMenu = createMenuItem(mm)
      extMenu.Caption = extGroupMenuCaption
      mm.Items.add(extMenu)
  end
else
  extMenu = mm.Items
end

local extMenuItem = createMenuItem(extMenu)
extMenuItem.Caption = extItemCaption
extMenu.add(extMenuItem)

extMenuItem.OnClick = function(sender)
genStructFromName()
end

function genStructFromName()
local memrecSelected = getAddressList().getSelectedRecord()
local structName = inputQuery("Input structure name:","",nil)
if memrecSelected.IsGroupHeader and memrecSelected.Address == "" then printf("ERROR:\nSelected memrec should be header or group with address") return end
if not memrecSelected.IsGroupHeader and memrecSelected.Address == "" then printf("ERROR:\nSelected memrec should be header or group with address") return end
if memrecSelected == nil or structName == nil then return end
if memrecSelected.IsGroupHeader and memrecSelected.Address == "" then return end
while memrecSelected.Count>0 do
  memrecSelected[0].destroy()
end

local foundStruct

for i=0,getStructureCount()-1 do
  if getStructure(i).Name==structName then foundStruct=getStructure(i) break end
end

if not foundStruct then error('structure "'..structName..'" not found') end

for i=0,foundStruct.Count-1 do
  local elemStruct = foundStruct.Element[i]
  local elemChild = foundStruct.Element[i].ChildStruct
  local elementMemRec = AddressList.createMemoryRecord()
  elementMemRec.Address = '+'..string.format("%X",elemStruct.Offset)
  elementMemRec.Description = elemStruct.Name
  elementMemRec.Type = elemStruct.Vartype

  if elemStruct.CustomType then elementMemRec.CustomTypeName = elemStruct.CustomType.name end

  if elemStruct.Vartype==vtString then
    elementMemRec.String.Size = elemStruct.Bytesize
  end
  if elemStruct.Vartype==vtUnicodeString then
    elementMemRec.Type = vtString
    elementMemRec.String.Size = elemStruct.Bytesize
    elementMemRec.String.Unicode = true
  end
  if elemStruct.Vartype==vtByteArray then
    elementMemRec.Aob.Size = elemStruct.Bytesize
  end
  if elemStruct.Vartype==vtPointer then
    if targetIs64Bit() then
        elementMemRec.Type = vtQword
    else
        elementMemRec.Type = vtDword
    end
  end
  if elemChild then
    for k = 0,elemChild.Count-1 do
      local childMemrec = AddressList.createMemoryRecord()
      local childOffset = elemChild.Element[k].Offset
      local childType = elemChild.Element[k].Vartype
      local childName = elemChild.Element[k].Name
      childMemrec.Type = childType
      childMemrec.Address = '+'..childOffset
      if childType == vtPointer then
        if targetIs64Bit() then
          childMemrec.Type = vtQword
        else
          childMemrec.Type = vtDword
        end
        childMemrec.OffsetCount = 1
        childMemrec.Offset[0] = "+"..childOffset
      end
      childMemrec.Description = childName
      childMemrec.appendToEntry(elementMemRec)
      elementMemRec.Options = "moHideChildren"
      elementMemRec.Type = vtCustom
    end
  end

  elementMemRec.appendToEntry(memrecSelected)
  elementMemRec.DontSave = true
  memrecSelected.Options = "moHideChildren"
end
end
this is just remake of genFromStruct
i can't solve one problem: child offsets are adding themselves with their owner

Re: remaking genFromStruct

Posted: Wed Feb 02, 2022 9:15 am
by Frouk
nvm fixed

Re: remaking genFromStruct

Posted: Wed Feb 02, 2022 9:36 pm
by LeFiXER
I would recommend adding some validation checks:

Code: Select all

if structName == "" then 
   ShowMessage("Enter a name!")
end
Also, it's good practice to ensure logic that fails returns nil, and to initialise variables with a value.