MemoryEditor: port of imgui_club imgui_memory_editor v0.59 (buffer / ByteData / byte tables), tests, Roblox + LÖVE examples

This commit is contained in:
4DBug committed 2026-10-17 08:15:51 -05:00
commit 10bd6c45d1
18 files changed
+1852

No files matched your search

+22
View File
@@ -0,0 +1,22 @@
-- LuaJIT: runs examples/love headless (core mock love) and types "AB" into byte 2 of the ByteData.
local ex = "examples/love"
dofile("tools/core/tools/test/love/mock_love.lua")
love.filesystem.getSource = function() return ex end
love.graphics.clear = function() end
dofile(ex .. "/main.lua")
love.load()
local function step() love.timer.step(1 / 60); love.update(1 / 60); love.draw() end
for i = 1, 5 do love.mousemoved(100 + i, 100); step() end
mem_edit:GotoAddrAndHighlight(2, 3)
for i = 1, 3 do step() end
love.textinput("A"); step(); love.textinput("B"); step(); step()
local ptr = require("ffi").cast("uint8_t*", data:getFFIPointer())
assert(ptr[2] == 0xAB, "byte 2 not written: " .. ptr[2])
assert(mem_edit.DataEditingAddr == 3, "editing did not advance")
-- preview decoding under LuaJIT (ImGuiDataType: U64 = 7, Float = 8; formats: Dec = 1, Hex = 2)
mem_edit.PreviewEndianness = 0
local ff = { 255, 255, 255, 255, 255, 255, 255, 255 }
assert(mem_edit:DrawPreviewData(0, ff, 8, 7, 1) == "18446744073709551615")
assert(mem_edit:DrawPreviewData(0, { 0, 0, 0xC0, 0x3F }, 4, 8, 2) == "0x1.8p+0")
assert(mem_edit:DrawPreviewData(0, { 0, 0, 0xC0, 0x3F }, 4, 8, 1) == "1.500000")
print("OK")
+61
View File
@@ -0,0 +1,61 @@
-- Hex editing in a Roblox buffer: GotoAddrAndHighlight focuses byte 2, typing "AB" writes 0xAB and moves to byte 3;
-- clicking a byte starts editing it; ReadOnly never writes; MouseHoveredAddr follows the mouse.
local RS = game:GetService("ReplicatedStorage")
local G = require(RS:WaitForChild("ImGui"))
local MemoryEditor = require(RS:WaitForChild("MemoryEditor"))(G)
local ImGui, ImVec2 = G.ImGui, G.ImVec2
ImGui.CreateContext()
G.ImGui_ImplRoblox.Init()
local io = ImGui.GetIO()
local mem = buffer.create(256)
for i = 0, 255 do buffer.writeu8(mem, i, i) end
local ed = MemoryEditor.new()
ed.OptShowDataPreview = true
local s = {}
local frame, done = 0, false
local cellPos -- screen pos of byte 0x15 (line 1, col 5), found from the child window
game:GetService("RunService").Stepped:Connect(function()
if done then return end
frame += 1
G.ImGui_ImplRoblox.NewFrame()
io.DeltaTime = 1 / 60
if frame == 3 then ed:GotoAddrAndHighlight(2, 3)
elseif frame == 6 then io:AddInputCharacter(string.byte("A"))
elseif frame == 7 then io:AddInputCharacter(string.byte("B"))
elseif frame == 12 then io:AddMousePosEvent(cellPos.x + 2, cellPos.y + 2)
elseif frame == 14 then io:AddMouseButtonEvent(0, true)
elseif frame == 15 then io:AddMouseButtonEvent(0, false)
elseif frame == 18 then ed.ReadOnly = true
elseif frame == 20 then io:AddInputCharacter(string.byte("F"))
elseif frame == 21 then io:AddInputCharacter(string.byte("F"))
end
ImGui.NewFrame()
ImGui.SetNextWindowPos(ImVec2(0, 0))
ed:DrawWindow("Memory", mem)
if frame == 11 then
local w = ImGui.FindWindowByName("Memory")
local child = w.DC.ChildWindows.Data[1]
ed:CalcSizes(s, 256, 0)
local x = child.Pos.x + s.OffsetHexMinX + s.HexCellWidth * 5
cellPos = ImVec2(x, child.Pos.y + child.WindowPadding.y + s.LineHeight * 1)
end
ImGui.Render()
G.ImGui_ImplRoblox.RenderDrawData(ImGui.GetDrawData())
if frame == 10 then
assert(buffer.readu8(mem, 2) == 0xAB, "byte 2 not written: " .. buffer.readu8(mem, 2))
assert(buffer.readu8(mem, 3) == 3, "byte 3 changed")
assert(ed.DataEditingAddr == 3, "editing did not advance: " .. ed.DataEditingAddr)
assert(ed.DataPreviewAddr == 3)
elseif frame == 13 then
assert(ed.MouseHovered and ed.MouseHoveredAddr == 0x15, "hovered " .. tostring(ed.MouseHoveredAddr))
elseif frame == 17 then
assert(ed.DataEditingAddr == 0x15, "click did not start editing: " .. ed.DataEditingAddr)
elseif frame == 26 then
done = true
assert(buffer.readu8(mem, 0x15) == 0x15, "ReadOnly wrote")
print("OK")
end
end)
+7
View File
@@ -0,0 +1,7 @@
local RS = game:GetService("ReplicatedStorage")
local G = require(RS:WaitForChild("ImGui"))
local MemoryEditor = require(RS:WaitForChild("MemoryEditor"))(G)
assert(type(MemoryEditor.new) == "function")
local ed = MemoryEditor.new()
assert(ed.Cols == 16 and ed.Open and not ed.ReadOnly and ed.OptShowAscii and ed.PreviewDataType == G.ImGuiDataType.S32)
print("OK")
+47
View File
@@ -0,0 +1,47 @@
-- DrawPreviewData: every data type x LE/BE x Dec/Hex/Bin, compared with C printf output.
local RS = game:GetService("ReplicatedStorage")
local G = require(RS:WaitForChild("ImGui"))
local MemoryEditor = require(RS:WaitForChild("MemoryEditor"))(G)
local DT = G.ImGuiDataType
local ed = MemoryEditor.new()
local BIN, DEC, HEX = MemoryEditor.DataFormat_Bin, MemoryEditor.DataFormat_Dec, MemoryEditor.DataFormat_Hex
local function check(bytes, dt, endian, fmt, want)
ed.PreviewEndianness = endian
local got = ed:DrawPreviewData(0, bytes, #bytes, dt, fmt)
assert(got == want, ("type %d endian %d fmt %d: got %q want %q"):format(dt, endian, fmt, got, want))
end
check({ 0xFF }, DT.S8, 0, DEC, "-1")
check({ 0xFF }, DT.U8, 0, DEC, "255")
check({ 0xFF }, DT.S8, 0, HEX, "0xff")
check({ 0x34, 0x12 }, DT.U16, 0, HEX, "0x1234")
check({ 0x34, 0x12 }, DT.U16, 1, HEX, "0x3412")
check({ 0x34, 0x12 }, DT.S16, 0, BIN, "00010010 00110100 ")
check({ 0xFE, 0xFF, 0xFF, 0xFF }, DT.S32, 0, DEC, "-2")
check({ 0xFE, 0xFF, 0xFF, 0xFF }, DT.U32, 0, DEC, "4294967294")
check({ 0xFE, 0xFF, 0xFF, 0xFF }, DT.S32, 0, HEX, "0xfffffffe")
check({ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, DT.U64, 0, DEC, "18446744073709551615")
check({ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }, DT.S64, 0, DEC, "-1")
check({ 0, 0, 0, 0, 0, 0, 0, 0x80 }, DT.S64, 0, DEC, "-9223372036854775808")
check({ 1, 2, 3, 4, 5, 6, 7, 8 }, DT.U64, 0, HEX, "0x0807060504030201")
check({ 1, 2, 3, 4, 5, 6, 7, 8 }, DT.U64, 1, HEX, "0x0102030405060708")
check({ 0, 0, 0xC0, 0x3F }, DT.Float, 0, DEC, "1.500000")
check({ 0x3F, 0xC0, 0, 0 }, DT.Float, 1, DEC, "1.500000")
check({ 0, 0, 0xC0, 0x3F }, DT.Float, 0, HEX, "0x1.8p+0")
check({ 0, 0, 0x80, 0xBF }, DT.Float, 0, HEX, "-0x1p+0")
check({ 0, 0, 0, 0 }, DT.Float, 0, HEX, "0x0p+0")
check({ 1, 0, 0, 0 }, DT.Float, 0, HEX, "0x1p-149") -- float subnormal is a normal double
check({ 0, 0, 0x80, 0x7F }, DT.Float, 0, DEC, "inf")
check({ 0x18, 0x2D, 0x44, 0x54, 0xFB, 0x21, 0x09, 0x40 }, DT.Double, 0, DEC, "3.141593")
check({ 0x18, 0x2D, 0x44, 0x54, 0xFB, 0x21, 0x09, 0x40 }, DT.Double, 0, HEX, "0x1.921fb54442d18p+1")
check({ 1, 0, 0, 0, 0, 0, 0, 0 }, DT.Double, 0, HEX, "0x0.0000000000001p-1022")
-- truncated at the end of memory: only the available bytes
check({ 0x34, 0x12 }, DT.U32, 0, DEC, "4660")
check({ 0x34, 0x12 }, DT.U32, 0, BIN, "00010010 00110100 ")
-- buffer, string and ReadFn sources
local b = buffer.create(4); buffer.writeu32(b, 0, 0xDEADBEEF)
assert(ed:DrawPreviewData(0, b, 4, DT.U32, HEX) == "0xdeadbeef")
assert(ed:DrawPreviewData(0, "\xEF\xBE\xAD\xDE", 4, DT.U32, HEX) == "0xdeadbeef")
ed.ReadFn = function(mem, off) return mem[off + 1] + 1 end
assert(ed:DrawPreviewData(0, { 0x10, 0x20 }, 2, DT.U16, HEX) == "0x2111")
print("OK")