Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
! Rich Presence crash fix
Browse files Browse the repository at this point in the history
The game will no longer crash if it fails to load the Rich Presence dll file.

This fixes the engine on non-Windows devices!

Additionally, the crash screen should be more prone to weird character
sequences.
  • Loading branch information
jakubg1 committed Jan 1, 2023
1 parent 85e21c9 commit 22bdb61
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
43 changes: 34 additions & 9 deletions src/DiscordRichPresence.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ local class = require "com/class"
---@overload fun():DiscordRichPresence
local DiscordRichPresence = class:derive("DiscordRichPresence")

local discordRPCMain = require("com/discordRPC")



---Constructs this class.
function DiscordRichPresence:new()
self.rpcMain = nil

self.enabled = false
self.connected = false
self.username = nil
Expand All @@ -26,19 +26,32 @@ function DiscordRichPresence:new()



function discordRPCMain.ready(userId, username, discriminator, avatar)
local success, err = pcall(function() self:init() end)
if not success then
self.rpcMain = nil
_Log:printt("DiscordRichPresence", string.format("Failed to load the Discord Rich Presence module!\nMore info:\n%s\nDiscord Rich Presence will be inactive in this session.", err))
end
end



---Includes and initializes the actual Rich Presence code.
function DiscordRichPresence:init()
self.rpcMain = require("com/discordRPC")

function self.rpcMain.ready(userId, username, discriminator, avatar)
self.connected = true
self.username = string.format("%s#%s", username, discriminator)
_Debug.console:print({{0, 1, 1}, "[DiscordRPC] ", {0, 1, 0}, string.format("Connected! (username: %s)", self.username)})
end

function discordRPCMain.disconnected(errorCode, message)
function self.rpcMain.disconnected(errorCode, message)
self.connected = false
self.username = nil
_Log:printt("DiscordRPC", string.format("Disconnected (%d: %s)", errorCode, message))
end

function discordRPCMain.errored(errorCode, message)
function self.rpcMain.errored(errorCode, message)
_Log:printt("DiscordRPC", string.format("Error (%d: %s)", errorCode, message))
end
end
Expand Down Expand Up @@ -75,31 +88,43 @@ end
---Internal function; use `:update()` instead.
---@param dt number Delta time in seconds.
function DiscordRichPresence:updateRun(dt)
if not self.rpcMain then
return
end

self.updateTime = self.updateTime + dt
if self.updateTime >= self.UPDATE_INTERVAL then
self.updateTime = 0
discordRPCMain.updatePresence(self.status)
self.rpcMain.updatePresence(self.status)
end
discordRPCMain.runCallbacks()
self.rpcMain.runCallbacks()
end



---Connects Discord Rich Presence.
function DiscordRichPresence:connect()
if not self.rpcMain then
return
end

if self.enabled then return end
_Log:printt("DiscordRPC", "Connecting...")
discordRPCMain.initialize(_DISCORD_APPLICATION_ID, true)
self.rpcMain.initialize(_DISCORD_APPLICATION_ID, true)
self.enabled = true
end



---Disconnects Discord Rich Presence.
function DiscordRichPresence:disconnect()
if not self.rpcMain then
return
end

if not self.enabled then return end
_Log:printt("DiscordRPC", "Disconnecting...")
discordRPCMain.shutdown()
self.rpcMain.shutdown()
self.enabled = false
self.connected = false
self.username = nil
Expand Down
5 changes: 4 additions & 1 deletion src/Kernel/CrashScreen.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ function CrashScreen:draw()

-- Error text
love.graphics.setFont(self.font)
love.graphics.print(self.err, 30, 180)
local result = pcall(function() love.graphics.print(self.err, 30, 180) end)
if not result then
love.graphics.print("Unable to print the crash message. Look at the console for more information!", 30, 180)
end

-- Button hovering
love.graphics.setFont(self.fontBig)
Expand Down

0 comments on commit 22bdb61

Please sign in to comment.