File size: 12,042 Bytes
7e9dc27 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
-- This file is part of mimgui project
-- Licensed under the MIT License
-- Copyright (c) 2018, FYP <https://github.com/THE-FYP>
assert(getMoonloaderVersion() >= 025)
local imgui = require 'mimgui.imgui'
local DX9 = require 'mimgui.dx9'
local ffi = require 'ffi'
local winmsg = require 'windows.message'
local memory = require 'memory'
local mimgui = {}
local renderer = nil
local subscriptionsInitialize = {}
local subscriptionsNewFrame = {}
local eventsRegistered = false
local active = false
local cursorActive = false
local playerLocked = false
local iniFilePath = nil
local defaultGlyphRanges = nil
setmetatable(mimgui, {__index = imgui, __newindex = function(t, k, v)
if imgui[k] then
print('[mimgui] Warning! Overwriting existing key "'..k..'"!')
end
rawset(t, k, v)
end})
-- background "Shift" triggering fix
memory.fill(0x00531155, 0x90, 5, true)
local function ShowCursor(show)
if show then
showCursor(true)
elseif cursorActive then
showCursor(false)
end
cursorActive = show
end
local function LockPlayer(lock)
if lock then
lockPlayerControl(true)
elseif playerLocked then
lockPlayerControl(false)
end
playerLocked = lock
end
-- MoonLoader v.027
if not isCursorActive then
isCursorActive = function() return cursorActive end
end
local function InitializeRenderer()
-- init renderer
local hwnd = ffi.cast('HWND', readMemory(0x00C8CF88, 4, false))
local d3ddevice = ffi.cast('LPDIRECT3DDEVICE9', getD3DDevicePtr())
renderer = assert(DX9.new(d3ddevice, hwnd))
renderer:SwitchContext()
-- configure imgui
imgui.GetIO().ImeWindowHandle = nil -- default causes crash. TODO: why?
imgui.GetIO().LogFilename = nil
local confdir = getWorkingDirectory() .. [[\config\mimgui\]]
if not doesDirectoryExist(confdir) then
createDirectory(confdir)
end
iniFilePath = ffi.new('char[260]', confdir .. script.this.filename .. '.ini')
imgui.GetIO().IniFilename = iniFilePath
-- change font
local fontFile = getFolderPath(0x14) .. '\\trebucbd.ttf'
assert(doesFileExist(fontFile), '[mimgui] Font "' .. fontFile .. '" doesn\'t exist!')
local builder = imgui.ImFontGlyphRangesBuilder()
builder:AddRanges(imgui.GetIO().Fonts:GetGlyphRangesCyrillic())
builder:AddText([[‚„…†‡€‰‹‘’“”•–—™›№]])
defaultGlyphRanges = imgui.ImVector_ImWchar()
builder:BuildRanges(defaultGlyphRanges)
imgui.GetIO().Fonts:AddFontFromFileTTF(fontFile, 14, nil, defaultGlyphRanges[0].Data)
-- invoke initializers
for _, cb in ipairs(subscriptionsInitialize) do
cb()
end
end
local function RegisterEvents()
addEventHandler('onD3DPresent', function()
if active then
if not renderer then
InitializeRenderer()
end
if renderer and not renderer.lost then
renderer:SwitchContext()
for _, sub in ipairs(subscriptionsNewFrame) do
if sub._render and sub._before then
sub:_before()
end
end
renderer:NewFrame()
local hideCursor = true
for _, sub in ipairs(subscriptionsNewFrame) do
if sub._render then
sub:_draw()
hideCursor = hideCursor and sub.HideCursor
end
end
if hideCursor and not isCursorActive() then
imgui.SetMouseCursor(imgui.lib.ImGuiMouseCursor_None)
end
renderer:EndFrame()
end
end
end)
local keyState = {}
local WM_MOUSEHWHEEL = 0x020E
local mouseMsgs = {
[WM_MOUSEHWHEEL]=true,
[winmsg.WM_LBUTTONDOWN]=true,
[winmsg.WM_LBUTTONDBLCLK]=true,
[winmsg.WM_RBUTTONDOWN]=true,
[winmsg.WM_RBUTTONDBLCLK]=true,
[winmsg.WM_MBUTTONDOWN]=true,
[winmsg.WM_MBUTTONDBLCLK]=true,
[winmsg.WM_LBUTTONUP]=true,
[winmsg.WM_RBUTTONUP]=true,
[winmsg.WM_MBUTTONUP]=true,
[winmsg.WM_MOUSEWHEEL]=true,
[winmsg.WM_SETCURSOR]=true
}
local keyboardMsgs = {
[winmsg.WM_KEYDOWN]=true,
[winmsg.WM_SYSKEYDOWN]=true,
[winmsg.WM_KEYUP]=true,
[winmsg.WM_SYSKEYUP]=true,
[winmsg.WM_CHAR]=true
}
addEventHandler('onWindowMessage', function(msg, wparam, lparam)
if not renderer then
return
end
if not mimgui.DisableInput then
local keyboard = keyboardMsgs[msg]
local mouse = mouseMsgs[msg]
if active and (keyboard or mouse) then
renderer:SwitchContext()
local io = imgui.GetIO()
renderer:WindowMessage(msg, wparam, lparam)
if (keyboard and io.WantCaptureKeyboard) or (mouse and io.WantCaptureMouse) then
if msg == winmsg.WM_KEYDOWN or msg == winmsg.WM_SYSKEYDOWN then
keyState[wparam] = false
consumeWindowMessage(true, true, true)
elseif msg == winmsg.WM_KEYUP or msg == winmsg.WM_SYSKEYUP then
if not keyState[wparam] then
consumeWindowMessage(true, true, true)
end
else
consumeWindowMessage(true, true, true)
end
end
end
end
-- save key states to prevent key sticking
if msg == winmsg.WM_KILLFOCUS then
keyState = {}
elseif wparam < 256 then
if msg == winmsg.WM_KEYDOWN or msg == winmsg.WM_SYSKEYDOWN then
keyState[wparam] = true
elseif msg == winmsg.WM_KEYUP or msg == winmsg.WM_SYSKEYUP then
keyState[wparam] = false
end
end
end)
addEventHandler('onD3DDeviceLost', function()
if renderer and not renderer.lost then
renderer:InvalidateDeviceObjects()
renderer.lost = true
end
end)
addEventHandler('onD3DDeviceReset', function()
if renderer then
renderer.lost = false
end
end)
addEventHandler('onScriptTerminate', function(scr)
if scr == script.this then
ShowCursor(false)
LockPlayer(false)
end
end)
local updaterThread = lua_thread.create(function()
while true do
wait(0)
local activate, hideCursor, lockPlayer = false, true, false
if #subscriptionsNewFrame > 0 then
for i, sub in ipairs(subscriptionsNewFrame) do
if type(sub.Condition) == 'function' then
sub._render = sub.Condition()
else
sub._render = sub.Condition and true
end
if sub._render then
hideCursor = hideCursor and sub.HideCursor
lockPlayer = lockPlayer or sub.LockPlayer
end
activate = activate or sub._render
end
end
active = activate
ShowCursor(active and not hideCursor)
LockPlayer(active and lockPlayer)
end
end)
updaterThread.work_in_pause = true
end
local function Unsubscribe(t, sub)
for i, v in ipairs(t) do
if v == sub then
table.remove(t, i)
return
end
end
end
local function ImGuiEnum(name)
return setmetatable({__name = name}, {__index = function(t, k)
return imgui.lib[t.__name .. k]
end})
end
--- API ---
mimgui._VERSION = '1.7.0'
mimgui.DisableInput = false
mimgui.ComboFlags = ImGuiEnum('ImGuiComboFlags_')
mimgui.Dir = ImGuiEnum('ImGuiDir_')
mimgui.ColorEditFlags = ImGuiEnum('ImGuiColorEditFlags_')
mimgui.Col = ImGuiEnum('ImGuiCol_')
mimgui.WindowFlags = ImGuiEnum('ImGuiWindowFlags_')
mimgui.NavInput = ImGuiEnum('ImGuiNavInput_')
mimgui.FocusedFlags = ImGuiEnum('ImGuiFocusedFlags_')
mimgui.Cond = ImGuiEnum('ImGuiCond_')
mimgui.BackendFlags = ImGuiEnum('ImGuiBackendFlags_')
mimgui.TreeNodeFlags = ImGuiEnum('ImGuiTreeNodeFlags_')
mimgui.StyleVar = ImGuiEnum('ImGuiStyleVar_')
mimgui.DrawCornerFlags = ImGuiEnum('ImDrawCornerFlags_')
mimgui.DragDropFlags = ImGuiEnum('ImGuiDragDropFlags_')
mimgui.SelectableFlags = ImGuiEnum('ImGuiSelectableFlags_')
mimgui.InputTextFlags = ImGuiEnum('ImGuiInputTextFlags_')
mimgui.MouseCursor = ImGuiEnum('ImGuiMouseCursor_')
mimgui.FontAtlasFlags = ImGuiEnum('ImFontAtlasFlags_')
mimgui.HoveredFlags = ImGuiEnum('ImGuiHoveredFlags_')
mimgui.ConfigFlags = ImGuiEnum('ImGuiConfigFlags_')
mimgui.DrawListFlags = ImGuiEnum('ImDrawListFlags_')
mimgui.DataType = ImGuiEnum('ImGuiDataType_')
mimgui.Key = ImGuiEnum('ImGuiKey_')
function mimgui.OnInitialize(cb)
assert(type(cb) == 'function')
table.insert(subscriptionsInitialize, cb)
return {Unsubscribe = function() Unsubscribe(subscriptionsInitialize, cb) end}
end
function mimgui.OnFrame(cond, cbBeforeFrame, cbDraw)
assert(type(cond) == 'function')
assert(type(cbBeforeFrame) == 'function')
if cbDraw then assert(type(cbDraw) == 'function') end
if not eventsRegistered then
RegisterEvents()
eventsRegistered = true
end
local sub = {
Condition = cond,
LockPlayer = false,
HideCursor = false,
_before = cbDraw and cbBeforeFrame or nil,
_draw = cbDraw or cbBeforeFrame,
_render = false,
}
function sub:Unsubscribe()
Unsubscribe(subscriptionsNewFrame, self)
end
function sub:IsActive()
return self._render
end
table.insert(subscriptionsNewFrame, sub)
return sub
end
function mimgui.SwitchContext()
return renderer:SwitchContext()
end
function mimgui.CreateTextureFromFile(path)
return renderer:CreateTextureFromFile(path)
end
function mimgui.CreateTextureFromFileInMemory(src, size)
return renderer:CreateTextureFromFileInMemory(src, size)
end
function mimgui.ReleaseTexture(tex)
return renderer:ReleaseTexture(tex)
end
function mimgui.CreateFontsTexture()
return renderer:CreateFontsTexture()
end
function mimgui.InvalidateFontsTexture()
return renderer:InvalidateFontsTexture()
end
function mimgui.GetRenderer()
return renderer
end
function mimgui.IsInitialized()
return renderer ~= nil
end
function mimgui.StrCopy(dst, src, len)
if len or tostring(ffi.typeof(dst)):find('*', 1, true) then
ffi.copy(dst, src, len)
else
len = math.min(ffi.sizeof(dst) - 1, #src)
ffi.copy(dst, src, len)
dst[len] = 0
end
end
local new = {}
setmetatable(new, {
__index = function(self, key)
local basetype = ffi.typeof(key)
local mt = {
__index = function(self, sz)
return setmetatable({type = ffi.typeof('$[$]', self.type, sz)}, getmetatable(self))
end,
__call = function(self, ...)
return self.type(...)
end
}
return setmetatable({type = ffi.typeof('$[1]', basetype), basetype = basetype}, {
__index = function(self, sz)
return setmetatable({type = ffi.typeof('$[$]', self.basetype, sz)}, mt)
end,
__call = mt.__call
})
end,
__call = function(self, t, ...)
return ffi.new(t, ...)
end
})
mimgui.new = new
return mimgui
|