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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
script_name 'mimgui sandbox'
|
2 |
+
script_author 'FYP'
|
3 |
+
|
4 |
+
local imgui, ffi = require 'mimgui', require 'ffi'
|
5 |
+
local new, str = imgui.new, ffi.string
|
6 |
+
|
7 |
+
local imguiStack = {}
|
8 |
+
local function wrapPushFunction(fn, popFn)
|
9 |
+
return function(...)
|
10 |
+
table.insert(imguiStack, popFn)
|
11 |
+
return fn(...)
|
12 |
+
end
|
13 |
+
end
|
14 |
+
|
15 |
+
local function wrapPopFunction(fn)
|
16 |
+
return function(...)
|
17 |
+
if #imguiStack > 0 then
|
18 |
+
table.remove(imguiStack, #imguiStack)
|
19 |
+
end
|
20 |
+
return fn(...)
|
21 |
+
end
|
22 |
+
end
|
23 |
+
|
24 |
+
local function useStrict(tbl)
|
25 |
+
local getinfo, error, rawset, rawget = debug.getinfo, error, rawset, rawget
|
26 |
+
local mt = getmetatable(tbl)
|
27 |
+
if mt == nil then
|
28 |
+
mt = {}
|
29 |
+
setmetatable(tbl, mt)
|
30 |
+
end
|
31 |
+
mt.__declared = {}
|
32 |
+
mt.__newindex = function (t, n, v)
|
33 |
+
if not mt.__declared[n] then
|
34 |
+
local info = getinfo(2, "S")
|
35 |
+
if info and info.linedefined > 0 then
|
36 |
+
error("assign to undeclared variable '"..n.."'", 2)
|
37 |
+
end
|
38 |
+
mt.__declared[n] = true
|
39 |
+
end
|
40 |
+
rawset(t, n, v)
|
41 |
+
end
|
42 |
+
mt.__index = function (t, n)
|
43 |
+
if not mt.__declared[n] then
|
44 |
+
local info = getinfo(2, "S")
|
45 |
+
if info and info.what ~= 'C' then
|
46 |
+
error("variable '"..n.."' is not declared", 2)
|
47 |
+
end
|
48 |
+
end
|
49 |
+
return rawget(t, n)
|
50 |
+
end
|
51 |
+
end
|
52 |
+
|
53 |
+
local function createSandboxEnv()
|
54 |
+
local env = {}
|
55 |
+
for k, v in pairs(_G) do
|
56 |
+
env[k] = v
|
57 |
+
end
|
58 |
+
env.imgui = {}
|
59 |
+
for k, v in pairs(imgui) do
|
60 |
+
env.imgui[k] = v
|
61 |
+
end
|
62 |
+
setmetatable(env.imgui, {__index = getmetatable(imgui).__index})
|
63 |
+
env.main = nil
|
64 |
+
env.ffi = ffi
|
65 |
+
env._G = env
|
66 |
+
env.imgui.Begin = wrapPushFunction(imgui.Begin, imgui.End)
|
67 |
+
env.imgui.End = wrapPopFunction(imgui.End)
|
68 |
+
useStrict(env)
|
69 |
+
return env
|
70 |
+
end
|
71 |
+
|
72 |
+
local sbEnv = createSandboxEnv()
|
73 |
+
local window = new.bool(false)
|
74 |
+
local imguiDemo = new.bool(false)
|
75 |
+
local initBox = new.char[0x1000]([[I, new, str = imgui, imgui.new, ffi.string
|
76 |
+
]])
|
77 |
+
local codeBox = new.char[0x2000]([[
|
78 |
+
I.Begin('Sandbox')
|
79 |
+
I.Text('Hello, World!')
|
80 |
+
I.End()
|
81 |
+
]])
|
82 |
+
local sbDraw, sbInitError, sbDrawError
|
83 |
+
local sbInitAutoupdate, sbDrawAutoupdate = new.bool(false), new.bool(false)
|
84 |
+
local sbInitUpdate, sbDrawUpdate = 0, 0
|
85 |
+
local monospaceFont = nil
|
86 |
+
local createView, globalView, imguiView, mimguiView = nil, nil, nil, nil
|
87 |
+
|
88 |
+
local function drawSandboxEditor()
|
89 |
+
local center = imgui.ImVec2(imgui.GetIO().DisplaySize.x / 2, imgui.GetIO().DisplaySize.y / 2)
|
90 |
+
imgui.SetNextWindowPos(center, imgui.Cond.FirstUseEver, imgui.ImVec2(0.5, 0.5))
|
91 |
+
imgui.SetNextWindowSize(imgui.ImVec2(550, 550), imgui.Cond.FirstUseEver)
|
92 |
+
imgui.Begin('ImGui Sandbox', window, imgui.WindowFlags.MenuBar)
|
93 |
+
imgui.BeginMenuBar()
|
94 |
+
if imgui.BeginMenu('Vars') then
|
95 |
+
if imgui.MenuItemBool('ImGui') then
|
96 |
+
if not imguiView then
|
97 |
+
imguiView = createView('imgui', getmetatable(sbEnv.imgui).__index)
|
98 |
+
end
|
99 |
+
imguiView.show[0] = true
|
100 |
+
end
|
101 |
+
if imgui.MenuItemBool('mimgui') then
|
102 |
+
if not mimguiView then
|
103 |
+
mimguiView = createView('mimgui', sbEnv.imgui)
|
104 |
+
end
|
105 |
+
mimguiView.show[0] = true
|
106 |
+
end
|
107 |
+
if imgui.MenuItemBool('Global') then
|
108 |
+
if not globalView then
|
109 |
+
globalView = createView('_G', sbEnv)
|
110 |
+
end
|
111 |
+
globalView.show[0] = true
|
112 |
+
end
|
113 |
+
imgui.EndMenu()
|
114 |
+
end
|
115 |
+
if imgui.MenuItemBool('ImGui Demo') then
|
116 |
+
imguiDemo[0] = true
|
117 |
+
end
|
118 |
+
if imgui.MenuItemBool('Restart') then
|
119 |
+
script.this:reload()
|
120 |
+
end
|
121 |
+
imgui.EndMenuBar()
|
122 |
+
imgui.Text('Initialization code:')
|
123 |
+
imgui.SameLine()
|
124 |
+
imgui.PushStyleVarVec2(imgui.StyleVar.FramePadding, imgui.ImVec2(0, 0))
|
125 |
+
if imgui.Checkbox('Auto update##1', sbInitAutoupdate) then
|
126 |
+
if sbInitAutoupdate[0] then sbInitUpdate = localClock() end
|
127 |
+
end
|
128 |
+
imgui.PopStyleVar()
|
129 |
+
if not sbInitAutoupdate[0] then
|
130 |
+
imgui.SameLine()
|
131 |
+
if imgui.SmallButton('Update##1') then
|
132 |
+
sbInitUpdate = localClock()
|
133 |
+
end
|
134 |
+
end
|
135 |
+
imgui.PushFont(monospaceFont)
|
136 |
+
if imgui.InputTextMultiline('##Init', initBox, ffi.sizeof(initBox) - 1, imgui.ImVec2(-1, 150), imgui.InputTextFlags.AllowTabInput) then
|
137 |
+
if sbInitAutoupdate[0] then
|
138 |
+
sbInitUpdate = localClock() + 0.5
|
139 |
+
end
|
140 |
+
end
|
141 |
+
imgui.PopFont()
|
142 |
+
if sbInitError then
|
143 |
+
imgui.TextColored(imgui.ImVec4(1, 0, 0, 1), "%s", sbInitError)
|
144 |
+
end
|
145 |
+
imgui.Separator()
|
146 |
+
imgui.Text('Drawing code:')
|
147 |
+
imgui.SameLine()
|
148 |
+
imgui.PushStyleVarVec2(imgui.StyleVar.FramePadding, imgui.ImVec2(0, 0))
|
149 |
+
if imgui.Checkbox('Auto update##2', sbDrawAutoupdate) then
|
150 |
+
if sbDrawAutoupdate[0] then sbDrawUpdate = localClock() end
|
151 |
+
end
|
152 |
+
imgui.PopStyleVar()
|
153 |
+
if not sbDrawAutoupdate[0] then
|
154 |
+
imgui.SameLine()
|
155 |
+
if imgui.SmallButton('Update##2') then
|
156 |
+
sbDrawUpdate = localClock()
|
157 |
+
end
|
158 |
+
end
|
159 |
+
imgui.PushFont(monospaceFont)
|
160 |
+
if imgui.InputTextMultiline('##Code', codeBox, ffi.sizeof(codeBox) - 1, imgui.ImVec2(-1, 250), imgui.InputTextFlags.AllowTabInput) then
|
161 |
+
if sbDrawAutoupdate[0] then
|
162 |
+
sbDrawUpdate = localClock() + 0.5
|
163 |
+
end
|
164 |
+
end
|
165 |
+
imgui.PopFont()
|
166 |
+
if sbDrawError then
|
167 |
+
imgui.TextColored(imgui.ImVec4(1, 0, 0, 1), "%s", sbDrawError)
|
168 |
+
end
|
169 |
+
imgui.End()
|
170 |
+
end
|
171 |
+
|
172 |
+
local function updateSandbox()
|
173 |
+
if sbInitUpdate ~= 0 and localClock() >= sbInitUpdate then
|
174 |
+
sbInitUpdate = 0
|
175 |
+
local f, err = load(str(initBox), nil, 't', sbEnv)
|
176 |
+
sbInitError = err
|
177 |
+
if f then
|
178 |
+
local res, err = pcall(f)
|
179 |
+
if not res then
|
180 |
+
sbInitError = err
|
181 |
+
end
|
182 |
+
end
|
183 |
+
end
|
184 |
+
|
185 |
+
if sbDrawUpdate ~= 0 and localClock() >= sbDrawUpdate then
|
186 |
+
sbDrawUpdate = 0
|
187 |
+
local f, err = load(str(codeBox), nil, 't', sbEnv)
|
188 |
+
sbDrawError = err
|
189 |
+
if f then
|
190 |
+
sbDraw = f
|
191 |
+
end
|
192 |
+
end
|
193 |
+
end
|
194 |
+
|
195 |
+
local function drawSandbox()
|
196 |
+
if sbDraw and not sbDrawError then
|
197 |
+
local res, err = pcall(sbDraw)
|
198 |
+
if not res then
|
199 |
+
sbDrawError = err
|
200 |
+
for i = #imguiStack, 1, -1 do
|
201 |
+
imguiStack[i]()
|
202 |
+
imguiStack[i] = nil
|
203 |
+
end
|
204 |
+
end
|
205 |
+
end
|
206 |
+
end
|
207 |
+
|
208 |
+
local function drawLuaViews()
|
209 |
+
if imguiView then
|
210 |
+
imguiView:draw()
|
211 |
+
end
|
212 |
+
if globalView then
|
213 |
+
globalView:draw()
|
214 |
+
end
|
215 |
+
if mimguiView then
|
216 |
+
mimguiView:draw()
|
217 |
+
end
|
218 |
+
end
|
219 |
+
|
220 |
+
local function drawImguiDemo()
|
221 |
+
if imguiDemo[0] then
|
222 |
+
imgui.ShowDemoWindow(imguiDemo)
|
223 |
+
end
|
224 |
+
end
|
225 |
+
|
226 |
+
imgui.OnInitialize(function()
|
227 |
+
local defaultFontCfg = imgui.GetIO().Fonts.ConfigData.Data[0]
|
228 |
+
local fontPath = getFolderPath(0x14) .. '\\consola.ttf'
|
229 |
+
monospaceFont = imgui.GetIO().Fonts:AddFontFromFileTTF(fontPath, 15.0, nil, defaultFontCfg.GlyphRanges)
|
230 |
+
end)
|
231 |
+
|
232 |
+
imgui.OnFrame(function() return window[0] end,
|
233 |
+
function() -- before frame
|
234 |
+
updateSandbox()
|
235 |
+
end,
|
236 |
+
function()
|
237 |
+
drawSandboxEditor()
|
238 |
+
drawSandbox()
|
239 |
+
drawLuaViews()
|
240 |
+
drawImguiDemo()
|
241 |
+
end)
|
242 |
+
|
243 |
+
function imgui.PushTextColor(color)
|
244 |
+
return imgui.PushStyleColorU32(imgui.Col.Text, color)
|
245 |
+
end
|
246 |
+
|
247 |
+
local valueColors = {
|
248 |
+
['table'] = 0xFFDD78C6,
|
249 |
+
['function'] = 0xFFEFAF61,
|
250 |
+
['string'] = 0xFF79C398,
|
251 |
+
['number'] = 0xFF669AD1,
|
252 |
+
['userdata'] = 0xFF756CE0,
|
253 |
+
}
|
254 |
+
|
255 |
+
createView = function(name, tbl)
|
256 |
+
assert(type(tbl) == 'table')
|
257 |
+
local view = {
|
258 |
+
name = name,
|
259 |
+
table = tbl,
|
260 |
+
show = new.bool(true),
|
261 |
+
filter = imgui.ImGuiTextFilter(),
|
262 |
+
sort = new.bool(false)
|
263 |
+
}
|
264 |
+
|
265 |
+
function view:drawItem(parent, key, value, path)
|
266 |
+
local vstr = tostring(value)
|
267 |
+
local str = key .. ' = ' .. vstr
|
268 |
+
local vtype = type(value)
|
269 |
+
local color = valueColors[vtype] or 0xFFFFFFFF
|
270 |
+
if vtype == 'table' then
|
271 |
+
imgui.PushTextColor(color)
|
272 |
+
local tree = imgui.TreeNodeStr(str)
|
273 |
+
imgui.PopStyleColor()
|
274 |
+
if tree then
|
275 |
+
for key, val in pairs(value) do
|
276 |
+
self:drawItem(value, key, val, path .. '.' .. key)
|
277 |
+
end
|
278 |
+
local mt = getmetatable(value)
|
279 |
+
if mt then
|
280 |
+
self:drawItem(value, '<metatable>', mt, path .. '.<metatable>')
|
281 |
+
end
|
282 |
+
imgui.TreePop()
|
283 |
+
end
|
284 |
+
else
|
285 |
+
imgui.PushTextColor(color)
|
286 |
+
imgui.Text(str)
|
287 |
+
imgui.PopStyleColor()
|
288 |
+
end
|
289 |
+
if imgui.IsItemHovered() then
|
290 |
+
if imgui.IsMouseClicked(1) then
|
291 |
+
setClipboardText(path)
|
292 |
+
end
|
293 |
+
imgui.BeginTooltip()
|
294 |
+
imgui.PushTextWrapPos(450.0)
|
295 |
+
imgui.Text('Click right mouse button to copy name')
|
296 |
+
imgui.Separator()
|
297 |
+
imgui.TextUnformatted(vtype .. '\n' .. vstr)
|
298 |
+
imgui.PopTextWrapPos()
|
299 |
+
imgui.EndTooltip()
|
300 |
+
end
|
301 |
+
end
|
302 |
+
|
303 |
+
function view:draw()
|
304 |
+
if self.show[0] then
|
305 |
+
local window_name = ('%s (%s)'):format(self.name, tostring(self.table))
|
306 |
+
imgui.SetNextWindowSize(imgui.ImVec2(400, 500), imgui.Cond.FirstUseEver)
|
307 |
+
imgui.Begin('Lua View: ' .. window_name, self.show)
|
308 |
+
self.filter:Draw('Filter', -100)
|
309 |
+
imgui.SameLine()
|
310 |
+
imgui.Checkbox('Sort', self.sort)
|
311 |
+
imgui.BeginChild(window_name)
|
312 |
+
|
313 |
+
local processItem = function(key, val)
|
314 |
+
if val ~= self.table then
|
315 |
+
local text = key .. ' = ' .. tostring(val)
|
316 |
+
if self.filter:PassFilter(text) then
|
317 |
+
self:drawItem(self.table, key, val, name .. '.' .. key)
|
318 |
+
end
|
319 |
+
end
|
320 |
+
end
|
321 |
+
if self.sort[0] then
|
322 |
+
local sorted = {} -- cache?
|
323 |
+
for key, val in pairs(self.table) do
|
324 |
+
table.insert(sorted, {key, val})
|
325 |
+
end
|
326 |
+
table.sort(sorted, function(v1, v2) return v1[1] < v2[1] end)
|
327 |
+
for i, v in ipairs(sorted) do
|
328 |
+
processItem(v[1], v[2])
|
329 |
+
end
|
330 |
+
else
|
331 |
+
for key, val in pairs(self.table) do
|
332 |
+
processItem(key, val)
|
333 |
+
end
|
334 |
+
end
|
335 |
+
|
336 |
+
imgui.EndChild()
|
337 |
+
imgui.End()
|
338 |
+
end
|
339 |
+
return self.show[0]
|
340 |
+
end
|
341 |
+
|
342 |
+
return view
|
343 |
+
end
|
344 |
+
|
345 |
+
function main()
|
346 |
+
sbInitUpdate = localClock()
|
347 |
+
sbDrawUpdate = localClock()
|
348 |
+
while true do
|
349 |
+
wait(0)
|
350 |
+
if testCheat('MSB') then
|
351 |
+
window[0] = not window[0]
|
352 |
+
end
|
353 |
+
end
|
354 |
+
end
|