summaryrefslogtreecommitdiffstatshomepage
path: root/plugins/discord/init.lua
diff options
context:
space:
mode:
author cracyc <cracyc@users.noreply.github.com>2018-06-08 18:25:39 -0500
committer R. Belmont <rb6502@users.noreply.github.com>2018-06-08 19:25:39 -0400
commita99407afb50a9c08f2c6ea7857452da8ad0cb484 (patch)
treecfe085e2e2f08001656a141d564c0bbb67023e7b /plugins/discord/init.lua
parenteee8d0d2d999d564162e803a57536855120ec51d (diff)
Discord plugin try 2 (#3640)
* plugins/discord: discord presence plugin [Carl] * plugins/discord: use domain sockets and pipes [Carl] * winptty: fix connecting to existing socket (nw) plugins/discord: show pause state (nw) * plugins/discord: fix pause behavior (nw)
Diffstat (limited to 'plugins/discord/init.lua')
-rwxr-xr-xplugins/discord/init.lua106
1 files changed, 106 insertions, 0 deletions
diff --git a/plugins/discord/init.lua b/plugins/discord/init.lua
new file mode 100755
index 00000000000..2b534b98377
--- /dev/null
+++ b/plugins/discord/init.lua
@@ -0,0 +1,106 @@
+-- license:BSD-3-Clause
+-- copyright-holders:Carl
+local exports = {}
+exports.name = "discord"
+exports.version = "0.0.1"
+exports.description = "Discord presence"
+exports.license = "The BSD 3-Clause License"
+exports.author = { name = "Carl" }
+
+local discord = exports
+
+function discord.startplugin()
+ local pipe = emu.file("rw")
+ local json = require("json")
+ local nonce = 1
+ local starttime = 0
+
+ local function init()
+ local path
+ if package.config:sub(1,1) == '\\' then
+ path = "\\\\.\\pipe\\discord-ipc-0"
+ else
+ path = os.getenv("XDG_RUNTIME_DIR") or os.getenv("TMPDIR") or os.getenv("TMP") or os.getenv("TEMP") or '/tmp'
+ path = "domain." .. path .. "/discord-ipc-0"
+ end
+ local err = pipe:open(path)
+ if err then
+ error("discord: unable to connect, " .. err .. "\n")
+ end
+ local output = json.stringify({v = 1, client_id = "453309506152169472"})
+ --print(output)
+ pipe:write(string.pack("<I4I4", 0, #output) .. output)
+ local time = os.time()
+ local data = ""
+ repeat
+ local res = pipe:read(100)
+ data = data .. res
+ until #res == 0 and #data > 0 or time + 1 < os.time()
+ --print(data)
+ if data:find("code", 1, true) then
+ error("discord: bad RPC reply, " .. data:sub(8) .. "\n")
+ end
+ end
+
+ local function update(status)
+ local running = emu.romname() ~= "___empty"
+ local state = not running and "In menu" or status
+ local details = running and manager:machine():system().description or nil
+ if emu.softname() ~= "" then
+ for name, dev in pairs(manager:machine().images) do
+ if dev:longname() then
+ details = details .. " (" .. dev:longname() .. ")"
+ break
+ end
+ end
+ end
+ local status = {
+ cmd = "SET_ACTIVITY",
+ args = {
+ pid = emu.pid(),
+ activity = {
+ state = state,
+ details = details,
+ timestamps = {
+ start = starttime
+ }
+ }
+ },
+ nonce = nonce
+ }
+ nonce = nonce + 1
+ local output = json.stringify(status)
+ --print(output)
+ pipe:write(string.pack("<I4I4", 1, #output) .. output)
+ local time = os.time()
+ local data = ""
+ repeat
+ local res = pipe:read(100)
+ data = data .. res
+ until #res == 0 and #data > 0 or time + 1 < os.time()
+ --print(data)
+ end
+
+ do
+ local stat, err = pcall(init)
+ if not stat then
+ emu.print_error(err)
+ return
+ end
+ end
+
+ emu.register_start(function()
+ starttime = os.time()
+ update("Playing")
+ end)
+
+ emu.register_pause(function()
+ update("Paused")
+ end)
+
+ emu.register_resume(function()
+ update("Playing")
+ end)
+end
+
+return exports