|
| 1 | +package com.github.minecraft_ta.totalDebugCompanion.util; |
| 2 | + |
| 3 | + |
| 4 | +import de.jcm.discordgamesdk.Core; |
| 5 | +import de.jcm.discordgamesdk.CreateParams; |
| 6 | +import de.jcm.discordgamesdk.activity.Activity; |
| 7 | + |
| 8 | +import java.io.File; |
| 9 | +import java.io.IOException; |
| 10 | +import java.net.URL; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.time.Instant; |
| 13 | +import java.util.Locale; |
| 14 | +import java.util.zip.ZipEntry; |
| 15 | +import java.util.zip.ZipInputStream; |
| 16 | + |
| 17 | +public class DiscordRPCManager { |
| 18 | + private Core core; |
| 19 | + private Activity activity; |
| 20 | + private String lastActivity = ""; |
| 21 | + private boolean enabled = true; |
| 22 | + |
| 23 | + public DiscordRPCManager() { |
| 24 | + try { |
| 25 | + File discordLibrary = downloadDiscordLibrary(); |
| 26 | + Core.init(discordLibrary); |
| 27 | + } catch (IOException e) { |
| 28 | + this.enabled = false; |
| 29 | + System.err.println("Failed to download Discord Game SDK: "); |
| 30 | + e.printStackTrace(); |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + try (CreateParams params = new CreateParams()) { |
| 35 | + params.setClientID(1116710018369204286L); |
| 36 | + params.setFlags(CreateParams.getDefaultFlags()); |
| 37 | + this.core = new Core(params); |
| 38 | + } |
| 39 | + |
| 40 | + this.activity = new Activity(); |
| 41 | + this.activity.timestamps().setStart(Instant.now()); |
| 42 | + this.activity.assets().setLargeImage("default"); |
| 43 | + updateActivity(); |
| 44 | + |
| 45 | + Thread callbackThread = new Thread(() -> { |
| 46 | + while (true) { |
| 47 | + core.runCallbacks(); |
| 48 | + try { |
| 49 | + Thread.sleep(16); |
| 50 | + } catch (InterruptedException e) { |
| 51 | + e.printStackTrace(); |
| 52 | + } |
| 53 | + } |
| 54 | + }); |
| 55 | + callbackThread.setDaemon(true); |
| 56 | + callbackThread.start(); |
| 57 | + } |
| 58 | + |
| 59 | + private static File downloadDiscordLibrary() throws IOException { |
| 60 | + File tempDir = new File(System.getProperty("java.io.tmpdir"), "total_debug_discord_game_sdk"); |
| 61 | + |
| 62 | + // Find out which name Discord's library has (.dll for Windows, .so for Linux) |
| 63 | + String name = "discord_game_sdk"; |
| 64 | + String suffix; |
| 65 | + |
| 66 | + String osName = System.getProperty("os.name").toLowerCase(Locale.ROOT); |
| 67 | + String arch = System.getProperty("os.arch").toLowerCase(Locale.ROOT); |
| 68 | + |
| 69 | + if (osName.contains("windows")) { |
| 70 | + suffix = ".dll"; |
| 71 | + } else if (osName.contains("linux")) { |
| 72 | + suffix = ".so"; |
| 73 | + } else if (osName.contains("mac os")) { |
| 74 | + suffix = ".dylib"; |
| 75 | + } else { |
| 76 | + throw new RuntimeException("cannot determine OS type: " + osName); |
| 77 | + } |
| 78 | + |
| 79 | + /* |
| 80 | + Some systems report "amd64" (e.g. Windows and Linux), some "x86_64" (e.g. Mac OS). |
| 81 | + At this point we need the "x86_64" version, as this one is used in the ZIP. |
| 82 | + */ |
| 83 | + if (arch.equals("amd64")) |
| 84 | + arch = "x86_64"; |
| 85 | + |
| 86 | + // Path of Discord's library inside the ZIP |
| 87 | + String zipPath = "lib/" + arch + "/" + name + suffix; |
| 88 | + |
| 89 | + // Return the file if it already exists |
| 90 | + File file = new File(tempDir, name + suffix); |
| 91 | + if (file.exists()) |
| 92 | + return file; |
| 93 | + |
| 94 | + // Open the URL as a ZipInputStream |
| 95 | + URL downloadUrl = new URL("https://dl-game-sdk.discordapp.net/2.5.6/discord_game_sdk.zip"); |
| 96 | + ZipInputStream zin = new ZipInputStream(downloadUrl.openStream()); |
| 97 | + |
| 98 | + // Search for the right file inside the ZIP |
| 99 | + ZipEntry entry; |
| 100 | + while ((entry = zin.getNextEntry()) != null) { |
| 101 | + if (entry.getName().equals(zipPath)) { |
| 102 | + // Create a new temporary directory |
| 103 | + // We need to do this, because we may not change the filename on Windows |
| 104 | + if (!tempDir.mkdir()) |
| 105 | + throw new IOException("Cannot create temporary directory"); |
| 106 | + |
| 107 | + File temp = new File(tempDir, name + suffix); |
| 108 | + |
| 109 | + Files.copy(zin, temp.toPath()); |
| 110 | + |
| 111 | + zin.close(); |
| 112 | + |
| 113 | + return temp; |
| 114 | + } |
| 115 | + zin.closeEntry(); |
| 116 | + } |
| 117 | + zin.close(); |
| 118 | + return null; |
| 119 | + } |
| 120 | + |
| 121 | + public void setDetails(String details) { |
| 122 | + if (!enabled) return; |
| 123 | + activity.setDetails(details); |
| 124 | + updateActivity(); |
| 125 | + } |
| 126 | + |
| 127 | + public void setState(String state, boolean saveLastState) { |
| 128 | + if (!enabled) return; |
| 129 | + if (saveLastState) this.lastActivity = state; |
| 130 | + activity.setState(state); |
| 131 | + updateActivity(); |
| 132 | + } |
| 133 | + |
| 134 | + public void loadLastState() { |
| 135 | + if (!enabled) return; |
| 136 | + activity.setState(lastActivity); |
| 137 | + updateActivity(); |
| 138 | + } |
| 139 | + |
| 140 | + private void updateActivity() { |
| 141 | + if (!enabled) return; |
| 142 | + core.activityManager().updateActivity(activity); |
| 143 | + } |
| 144 | +} |
0 commit comments