-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathmain.lua
More file actions
460 lines (404 loc) · 13.6 KB
/
main.lua
File metadata and controls
460 lines (404 loc) · 13.6 KB
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
local logger = require("common.lib.logger")
require("common.lib.mathExtensions")
local utf8 = require("common.lib.utf8Additions")
local DebugSettings = require("client.src.debug.DebugSettings")
local inputManager = require("client.src.inputManager")
require("client.src.globals")
local touchHandler = require("client.src.ui.touchHandler")
local inputFieldManager = require("client.src.ui.inputFieldManager")
local RunTimeGraph = require("client.src.RunTimeGraph")
local CustomRun = require("client.src.CustomRun")
local GraphicsUtil = require("client.src.graphics.graphics_util")
local prof = require("common.lib.zoneProfiler")
local ReplayV3 = require("common.data.ReplayV3")
require("common.lib.util")
local consts = require("common.engine.consts")
local system = require("client.src.system")
local Game = require("client.src.Game")
-- move to load once global dependencies have been resolved
GAME = Game()
-- We override love.run with a function that refers to `runInternal` for its gameloop function
-- so by overwriting that, the new runInternal will get used on the next iteration
love.runInternal = CustomRun.innerRun
-- Intentional override
---@diagnostic disable-next-line: duplicate-set-field
function love.run()
return CustomRun.run()
end
-- Called at the beginning to load the game
-- Either called directly or from auto_updater
-- Intentional override
---@diagnostic disable-next-line: duplicate-set-field
function love.load(args, rawArgs)
love.keyboard.setTextInput(false)
-- there is a bug on windows that causes the game to start with a size equal to the desktop causing the window handle to be offscreen
-- check for that and restore the window if that's the case:
local x, y, displayIndex = love.window.getPosition()
local desktopWidth, desktopHeight = love.window.getDesktopDimensions(displayIndex)
local w, windowHeight, flags = love.window.getMode()
if not flags.fullscreen and not flags.borderless and not system.isMobileOS() then
if y == 0 and windowHeight >= desktopHeight then
if love.window.isMaximized() then
love.window.restore()
end
local offset = math.ceil(desktopHeight / 32)
love.window.updateMode(desktopWidth, desktopHeight - offset, flags)
love.window.setPosition(x, offset, displayIndex)
end
if config.maximizeOnStartup and not love.window.isMaximized() then
love.window.maximize()
end
end
local newPixelWidth, newPixelHeight = love.graphics.getWidth(), love.graphics.getHeight()
logger.debug("Updating canvas scale from love.load")
GAME:updateCanvasPositionAndScale(newPixelWidth, newPixelHeight)
GAME:load()
if not PROFILE_MEMORY then
prof.enable(DebugSettings.getProfileFrameTimes())
prof.setDurationFilter(DebugSettings.getProfileThreshold() / 1000)
end
end
-- Intentional override
---@diagnostic disable-next-line: duplicate-set-field
function love.focus(f)
GAME.focused = f
end
-- Called every few fractions of a second to update the game
-- dt is the amount of time in seconds that has passed.
-- Intentional override
---@diagnostic disable-next-line: duplicate-set-field
function love.update(dt)
if DebugSettings.showRuntimeGraph() then
if CustomRun.runTimeGraph == nil then
CustomRun.runTimeGraph = RunTimeGraph()
end
else
CustomRun.runTimeGraph = nil
end
inputManager:update(dt)
inputFieldManager.update()
touchHandler:update(dt)
GAME:update(dt)
end
local statOrder -- in reverse of the desired display order
if system.meetsLoveVersionRequirement(12, 0) then
statOrder = {
"buffermemory",
"texturememory",
"buffers",
"textures",
"fonts",
"shaderswitches",
"canvasswitches",
"drawcallsbatched",
"drawcalls",
}
else
statOrder = {
"texturememory",
"canvases",
"images",
"fonts",
"shaderswitches",
"canvasswitches",
"drawcallsbatched",
"drawcalls",
}
end
-- Called whenever the game needs to draw.
-- Intentional override
---@diagnostic disable-next-line: duplicate-set-field
function love.draw()
GAME:draw()
if DebugSettings.drawGraphicsStats() then
local stats = love.graphics.getStats()
local width, height = love.graphics.getDimensions()
for i = #statOrder, 1, -1 do
local key = statOrder[i]
local value = stats[key]
if value then
if string.find(key, "memory") then
value = string.format("%.2f MB", value / 1024 / 1024)
end
love.graphics.printf(key .. ": " .. value, 0, height - i * 16, width, "right")
end
end
end
end
-- Handle a mouse or touch press
-- Intentional override
---@diagnostic disable-next-line: duplicate-set-field
function love.mousepressed(x, y, button)
x, y = GAME:transform_coordinates(x, y)
if button == 1 then
touchHandler:touch(x, y)
end
inputManager:mousePressed(x, y, button)
end
-- Intentional override
---@diagnostic disable-next-line: duplicate-set-field
function love.mousereleased(x, y, button)
x, y = GAME:transform_coordinates(x, y)
if button == 1 then
touchHandler:release(x, y)
end
inputManager:mouseReleased(x, y, button)
end
-- Intentional override
---@diagnostic disable-next-line: duplicate-set-field
function love.mousemoved( x, y, dx, dy, istouch )
x, y = GAME:transform_coordinates(x, y)
if love.mouse.isDown(1) then
touchHandler:drag(x, y)
end
inputManager:mouseMoved(x, y)
end
-- Intentional override
---@diagnostic disable-next-line: duplicate-set-field
function love.joystickpressed(joystick, button)
inputManager:joystickPressed(joystick, button)
end
-- Intentional override
---@diagnostic disable-next-line: duplicate-set-field
function love.joystickreleased(joystick, button)
inputManager:joystickReleased(joystick, button)
end
-- Intentional override
---@diagnostic disable-next-line: duplicate-set-field
function love.joystickadded(joystick)
GAME:onJoystickAdded(joystick)
end
-- Intentional override
---@diagnostic disable-next-line: duplicate-set-field
function love.joystickremoved(joystick)
GAME:onJoystickRemoved(joystick)
end
-- Handle a touch press
-- Note we are specifically not implementing this because mousepressed above handles mouse and touch
-- function love.touchpressed(id, x, y, dx, dy, pressure)
-- local _x, _y = GAME:transform_coordinates(x, y)
-- click_or_tap(_x, _y, {id = id, x = _x, y = _y, dx = dx, dy = dy, pressure = pressure})
-- end
-- quit handling
-- Intentional override
---@diagnostic disable-next-line: duplicate-set-field
function love.quit()
if prof.enabled then
prof.write()
end
if GAME.netClient and GAME.netClient:isConnected() then
GAME.netClient:logout()
end
love.audio.stop()
config.fullscreen = love.window.getFullscreen()
local x, y, displayIndex = love.window.getPosition()
config.display = displayIndex
if not config.fullscreen then
config.windowX = math.max(x, 0)
config.windowY = math.max(y, 0)
if config.windowY == 0 then
--don't let 'y' be zero, or the title bar will not be visible on next launch.
config.windowY = 30
end
config.windowWidth, config.windowHeight, _ = love.window.getMode()
config.maximizeOnStartup = love.window.isMaximized()
else
-- don't save the other values so the settings from previous windowed mode usage are preserved
end
write_conf_file()
pcall(love.filesystem.write, "debug.log", tostring(logger.messageBuffer))
if GAME.updater then
while GAME.updater.state ~= GAME_UPDATER_STATES.idle do
GAME.updater:update()
end
end
end
-- Intentional override
---@diagnostic disable-next-line: duplicate-set-field
function love.errorhandler(msg)
if lldebugger then
pcall(love.filesystem.write, "debug.log", tostring(logger.messageBuffer))
error(msg, 2)
end
if not love.window or not love.graphics or not love.event then
return
end
---@diagnostic disable-next-line: undefined-field
if not love.graphics.isCreated() or not love.window.isOpen() then
local success, status = pcall(love.window.setMode, 800, 600)
if not success or not status then
return
end
end
-- if we crashed during a match that is likely cause of the issue
-- we want it logged in a digestable form
if GAME.battleRoom and GAME.battleRoom.match then
pcall(function()
local match = GAME.battleRoom.match
match.engine.aborted = true
ReplayV3.finalizeReplay(match.engine, match.replay)
logger.info("Replay of match during crash:\n" .. json.encode(match.replay))
end)
end
msg = tostring(msg)
local sanitizedMessageLines = {}
for char in msg:gmatch(utf8.charpattern) do
table.insert(sanitizedMessageLines, char)
end
local sanitizedMessage = table.concat(sanitizedMessageLines)
local trace = GAME.crashTrace or debug.traceback("", 3)
local traceLines = {}
for l in trace:gmatch("(.-)\n") do
if not l:match("boot.lua") and not l:match("stack traceback:") then
table.insert(traceLines, l)
end
end
local sanitizedTrace = table.concat(traceLines, "\n")
local function getGameErrorData(sanitizedMessage, sanitizedTrace)
local errorData = Game.errorData(sanitizedMessage, sanitizedTrace)
local detailedErrorLogString = Game.detailedErrorLogString(errorData)
errorData.detailedErrorLogString = detailedErrorLogString
if GAME.updater and not DEBUG_ENABLED and not os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") then
GAME.netClient:sendErrorReport(errorData, consts.SERVER_LOCATION, 49569)
end
return detailedErrorLogString
end
local success, detailedErrorLogString = pcall(getGameErrorData, sanitizedMessage, sanitizedTrace)
local errorLines = {}
table.insert(errorLines, "Error: Please share your crash.log with the developers to get help with this!\n")
if success then
table.insert(errorLines, detailedErrorLogString)
logger.info(detailedErrorLogString)
else
table.insert(errorLines, sanitizedMessage)
logger.info(sanitizedMessage)
end
if logger.messageBuffer then
logger.info("config during crash: " .. table_to_string(config))
pcall(love.filesystem.write, "crash.log", tostring(logger.messageBuffer))
end
if #sanitizedMessage ~= #msg then
table.insert(errorLines, "Invalid UTF-8 string in error message.")
end
table.insert(errorLines, "\n")
local messageToDraw = table.concat(errorLines, "\n")
messageToDraw = messageToDraw:gsub("\t", " ")
messageToDraw = messageToDraw:gsub("%[string \"(.-)\"%]", "%1")
print(messageToDraw)
-- Reset state.
if love.mouse then
love.mouse.setVisible(true)
love.mouse.setGrabbed(false)
love.mouse.setRelativeMode(false)
if love.mouse.isCursorSupported() then
love.mouse.setCursor()
end
end
if love.joystick then
-- Stop all joystick vibrations.
for i, v in ipairs(love.joystick.getJoysticks()) do
v:setVibration()
end
end
if love.audio then
love.audio.stop()
end
love.graphics.reset()
local s, font = pcall(GraphicsUtil.getGlobalFontWithSize, GraphicsUtil.fontSize + 4)
if s then
love.graphics.setFont(font)
else
love.graphics.setNewFont(16)
end
love.graphics.setColor(1, 1, 1)
love.graphics.origin()
if GAME then
local success, canvasScale = pcall(GAME.newCanvasSnappedScale, GAME)
if success then
love.graphics.scale(canvasScale)
end
end
local function draw()
if not love.graphics.isActive() then
return
end
love.graphics.clear(love.graphics.getBackgroundColor())
local positionX = 40
local positionY = positionX
love.graphics.printf(messageToDraw, positionX, positionY, love.graphics.getWidth() - positionX)
love.graphics.present()
end
local fullErrorText = messageToDraw
local function copyToClipboard()
if not love.system then
return
end
love.system.setClipboardText(fullErrorText)
messageToDraw = messageToDraw .. "\nCopied to clipboard!"
end
if love.system then
messageToDraw = messageToDraw .. "\n\nPress Ctrl+C or tap to copy this error"
end
return function()
love.event.pump()
for e, a, b, c in love.event.poll() do
if e == "quit" then
return 1
elseif e == "keypressed" and a == "escape" then
return 1
elseif e == "keypressed" and a == "c" and love.keyboard.isDown("lctrl", "rctrl") then
copyToClipboard()
elseif e == "touchpressed" then
local name = love.window.getTitle()
if #name == 0 or name == "Untitled" then
name = "Game"
end
local buttons = {"OK", "Cancel"}
if love.system then
buttons[3] = "Copy to clipboard"
end
local pressed = love.window.showMessageBox("Quit " .. name .. "?", "", buttons)
if pressed == 1 then
return 1
elseif pressed == 3 then
copyToClipboard()
end
end
end
draw()
if love.timer then
love.timer.sleep(0.1)
end
end
end
-- Intentional override
---@diagnostic disable-next-line: duplicate-set-field
function love.resize(newWidth, newHeight)
if GAME then
logger.debug("Updating canvas scale from love.resize")
GAME:handleResize(newWidth, newHeight)
end
end
-- Intentional override
---@diagnostic disable-next-line: duplicate-set-field
function love.keypressed(key, scancode, rep)
logger.trace("key pressed: " .. key)
if scancode then
inputManager:keyPressed(key, scancode, rep)
end
end
-- Intentional override
---@diagnostic disable-next-line: duplicate-set-field
function love.textinput(text)
inputFieldManager.textInput(text)
end
-- Intentional override
---@diagnostic disable-next-line: duplicate-set-field
function love.keyreleased(key, unicode)
inputManager:keyReleased(key, unicode)
end
-- Intentional override
---@diagnostic disable-next-line: duplicate-set-field
function love.joystickaxis(joystick, axisIndex, value)
inputManager:joystickaxis(joystick, axisIndex, value)
end