-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathc_jl_debug.lua
261 lines (229 loc) · 8.62 KB
/
c_jl_debug.lua
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
sx, sy = guiGetScreenSize()
local _dxText = dxDrawText
local dxDrawText = function(text, x, y, w, h, ...) _dxText(text, x, y, x+(w or 0), y+(h or 0), ...) end
local dxDrawShadowedText = function(text, x, y, w, h, color, ...)
dxDrawText(text, x+1.75, y+1.75, w, h, tocolor(0, 0, 0, 255), ...)
dxDrawText(text, x, y, w, h, color, ...)
end
local iColorWhite = tocolor(255, 255, 255)
local fDebug = {}
local sSettings = {
unknownFile = "Unknown",
unknownLine = "Unknown",
msgPattern = "dup [lvl][side] file:line -> msg",
noDup = "",
dup = "dupCountx",
lvl = {
[0] = "Custom",
[1] = "Error",
[2] = "Warning",
[3] = "Info"
},
side = {
[1] = "Server",
[2] = "Client"
},
fontPath = "files/fonts/roboto.ttf"
}
local bTextShadow = true
--> this is set when the resource starts.
local uFont
iSettings = {
w = 500,
h = 200,
x = 50,
y = sy-210,
}
local iDefaultiSettings = table.copy(iSettings)
local sMsgDup = {}
local sDbgMsgs = {}
local iRowHeight = 0--This is also set when the res strts
local iMaxRows = 5
local iCurrentRow = 1
local textRenderTarget
bEditDebug = false
bDebugEnabled = false
local sPath = "settings.json"
local uHandle = fileExists(sPath) and fileOpen(sPath)
--> aDbgMsgData -> {msg = msg, lvl = lvl, file = file, line = line, color={r, g, b}}
--> side -> client or server
debug.addMessage = function (aDbgMsgData, iSide)
aDbgMsgData.file = aDbgMsgData.file or sSettings.unknownFile
aDbgMsgData.line = aDbgMsgData.line or sSettings.unknownLine
local k = aDbgMsgData.file..aDbgMsgData.line..aDbgMsgData.msg --> the key for the MsgDups table
createIndexIfNil(sMsgDup, k, {
count = 0,
line = #sDbgMsgs+1,
})
sMsgDup[k].count = sMsgDup[k].count+1
sDbgMsgs[sMsgDup[k].line] = debug.sParseMessage(aDbgMsgData, iSide, sMsgDup[k].count)
debug.reloadRTarget()
end
debug.sParseMessage = function (aDbgMsgData, iSide, iDupCount)
aDbgMsgData.side = sSettings.side[iSide]
aDbgMsgData.dup = ((iDupCount>1 and sSettings.dup) or (sSettings.noDup)):gsub("dupCount", iDupCount)
aDbgMsgData.lvl = sSettings.lvl[aDbgMsgData.lvl]
local str = sSettings.msgPattern
for k, v in pairs(aDbgMsgData) do
str = str:gsub(k, v)
end
return str
end
debug.render = function()
if (bEditDebug) then
drawResizeAndMove()
else
dxDrawImage(iSettings.x, iSettings.y, iSettings.w, iSettings.h, textRenderTarget)
end
dxDrawRectangle(iSettings.x, iSettings.y, iSettings.w, iSettings.h, tocolor(0, 0, 0, 80))
end
do
local sText = 0
local addY = 0
debug.reloadRTarget = function ()
dxSetRenderTarget(textRenderTarget, true)
dxDrawRectangle(iSettings.x, iSettings.y, iSettings.w, iSettings.h, tocolor(0, 0, 0, 0))
sText = sDbgMsgs[iCurrentRow]
addY = 2
for i = iCurrentRow, iCurrentRow+iMaxRows do
if not (sText) then --[[print("breaked at", i, "Dbgmsgs size", #sDbgMsgs)]] break end
if (bTextShadow) then dxDrawText(sText, 2+1.75, addY+1.75, 0, 0, tocolor(0, 0, 0, 255), 1, uFont, "left", "top", false, false, false, true) end
dxDrawText(sText, 2, addY, 0, 0, iColorWhite, 1, uFont, "left", "top", false, false, false, true)
addY = addY+iRowHeight
for _ in string.gmatch(sText, "\n") do addY = addY+iRowHeight end
_, sText = next(sDbgMsgs, i)
end
dxSetRenderTarget()
return true
end
end
debug.tog = function(sDataName)
bDebugEnabled = getElementData(localPlayer, "debug:state")
local func = ((bDebugEnabled and addEventHandler) or (removeEventHandler)) --> MTA is retarded, so i need to use this form..
func("onClientRender", root, debug.render)
func("onClientClick", root, clickHandler)
loadSelf()
end
function calculateMaxRows()
iMaxRows = math.floor(iSettings.h/iRowHeight)-1
end
function recreateRTarget(bSkipReload)
if (isElement(textRenderTarget)) then destroyElement(textRenderTarget) end--> we destroy the old rtaget, so we free up vram
textRenderTarget = dxCreateRenderTarget(iSettings.w, iSettings.h, true)
if not (bSkipReload) then debug.reloadRTarget() end
end
--> event handlers
addEventHandler("onClientElementDataChange", localPlayer,
function(sDataName)
if (sDataName=="debug:state") then
debug.tog()
end
end)
addEventHandler("onClientResourceStart", resourceRoot,
function ()
if (uHandle) then
local tbl = fromJSON(fileRead(uHandle, fileGetSize(uHandle)))
--> Because there are 2 tables in the loaded file we need to go thru them one by one.
if (tbl) then
for sSettingsTblName, sSettingsTbl in pairs({sSettings = sSettings, iSettings = iSettings}) do
if (tbl[sSettingsTblName]) then
for k, v in pairs(sSettingsTbl) do
if (type(tbl[sSettingsTblName][k])==type(v)) then
sSettingsTbl[k] = tbl[sSettingsTblName][k]
else
outputDebugString(string.format("Failed to set value %s[Types: %s, %s]"), k, type(tbl[sSettingsTblName][k]), type(v))
end
end
else
outputDebugString("Failed to load "..sSettingsTblName.." tbl.", 1)
end
end
else
outputDebugString("Failed to load settings file.")
end
--> we set the variables outside the loop, since we cant(actually, its possible with loadstring, but lets do it the harder way.) set it with loop.
tbl = tbl.vars
bTextShadow = tbl.bTextShadow
end
--> if the font specified doesnt exists we load the roboto font, since it exists always, because its included in the meta.
if not (fileExists(sSettings.fontPath)) then outputDebugString("Changed font from the set to 'files/fonts/roboto.ttf', because the set font is probably deleted.", 1) sSettings.fontPath = "files/fonts/roboto.ttf" end
uFont = dxCreateFont(sSettings.fontPath , iSettings.fontSize or 10, false) or "default"
iRowHeight = dxGetFontHeight(1, uFont)
debug.tog()
calculateMaxRows()
recreateRTarget(true)
end
)
addEventHandler("onClientResourceStop", resourceRoot,
function()
--> Save the settings
uHandle = uHandle or fileCreate(sPath)
fileSetPos(uHandle, 0)
fileWrite(uHandle, toJSON({sSettings = sSettings, iSettings = iSettings, vars = {bTextShadow = bTextShadow}}))
fileFlush(uHandle)
fileClose(uHandle)
end
)
addEventHandler("onClientDebugMessage", root,
function(msg, lvl, file, line, r, g, b)
debug.addMessage({
msg = msg,
lvl = lvl,
file = file,
line = line,
color = {r, g, b},
}, 2)
end
)
addEvent("jlDebug:serverDebugMsg", true)
addEventHandler("jlDebug:serverDebugMsg", localPlayer,
function (aDebugMessages)
for _,v in pairs(aDebugMessages) do
debug.addMessage(v, 1)
end
end
)
--> scrolling
bindKey("mouse_wheel_down", "down",
function()
if (bDebugEnabled) then
if (iCurrentRow<#sDbgMsgs-iMaxRows) then
iCurrentRow = iCurrentRow+1
end
end
end
)
bindKey("mouse_wheel_up", "down",
function()
if (bDebugEnabled) then
if (iCurrentRow>1) then
iCurrentRow = iCurrentRow-1
end
end
end
)
debug.togEditMode = function()
if (bDebugEnabled) then if (isCursorShowing()) then bEditDebug = not bEditDebug end end
end
bindKey("lctrl", "both", debug.togEditMode)
bindKey("rctrl", "both", debug.togEditMode)
--> command handlers
addCommandHandler("cleard",
function()
sDbgMsgs = {}
sMsgDup = {}
--> just to clear the rTarget, and because thers no text at all, we dont need to refresh it.
dxSetRenderTarget(textRenderTarget, true);dxSetRenderTarget()
iCurrentRow = 1
outputChatBox("#073b84[Info]:#FFFFFF Successfully cleared the debug.", 255, 255, 255, true)
end)
--> TODO REMOVE
addCommandHandler("printtexttodebug",
function(_, ...)
outputDebugString(table.concat({...}, " "))
end, false)
addCommandHandler("resetdebug",
function()
iSettings = iDefaultiSettings
outputChatBox("#073b84[Info]:#FFFFFF Successfully resetted the debug.", 255, 255, 255, true)
end)