-
-
Save drori200/a61284adeffc26117f087aee045620ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class HavenCoreActivity { | |
public static Core core; | |
public static Activity activity; | |
private static Thread callbackRunner; | |
public static Thread getCallbackRunner() { | |
return callbackRunner; | |
} | |
public static void start() { | |
File discordLibrary; | |
try { | |
discordLibrary = downloadDiscordLibrary(); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
if (discordLibrary == null) { | |
System.err.println("Error downloading Discord SDK."); | |
// System.exit(-1); | |
FMLCommonHandler.instance().exitJava(-1, false); | |
} | |
// Initialize the Core | |
Core.init(discordLibrary); | |
initPresence(); | |
if (callbackRunner == null) { | |
callbackRunner = new Thread( | |
() -> { | |
while (!Thread.currentThread().isInterrupted()) { | |
core.runCallbacks(); | |
try { | |
Thread.sleep(100 + new Random().nextInt(50)); | |
} catch (InterruptedException localInterruptedException) { | |
throw new RuntimeException(localInterruptedException); | |
} | |
} | |
}, | |
"RPC-Callback-Handler"); | |
} | |
callbackRunner.start(); | |
} | |
public static void initPresence() { | |
CreateParams params = new CreateParams(); | |
params.setClientID(788530816740753459L); | |
params.setFlags(CreateParams.getDefaultFlags()); | |
// Create the Core | |
core = new Core(params); | |
activity = new Activity(); | |
activity.setDetails("About to enjoy HavenCore"); | |
activity.setType(ActivityType.PLAYING); | |
// Setting a start time causes an "elapsed" field to appear | |
activity.timestamps().setStart(Instant.now()); | |
// Make a "cool" image show up | |
activity.assets().setLargeImage("havencorelogo"); | |
// Finally, update the current activity to our activity | |
core.activityManager().updateActivity(activity); | |
} | |
public static File downloadDiscordLibrary() throws IOException { | |
// Find out which name Discord's library has (.dll for Windows, .so for Linux) | |
String name = "discord_game_sdk"; | |
String suffix; | |
String osName = System.getProperty("os.name").toLowerCase(Locale.ROOT); | |
String arch = System.getProperty("os.arch").toLowerCase(Locale.ROOT); | |
if (osName.contains("windows")) { | |
suffix = ".dll"; | |
} else if (osName.contains("linux")) { | |
suffix = ".so"; | |
} else if (osName.contains("mac os")) { | |
suffix = ".dylib"; | |
} else { | |
throw new RuntimeException("cannot determine OS type: " + osName); | |
} | |
/* | |
Some systems report "amd64" (e.g. Windows and Linux), some "x86_64" (e.g. Mac OS). | |
At this point we need the "x86_64" version, as this one is used in the ZIP. | |
*/ | |
if (arch.equals("amd64")) arch = "x86_64"; | |
// Path of Discord's library inside the ZIP | |
String zipPath = "lib/" + arch + "/" + name + suffix; | |
// Open the URL as a ZipInputStream | |
URL downloadUrl = new URL("https://dl-game-sdk.discordapp.net/2.5.6/discord_game_sdk.zip"); | |
HttpURLConnection connection = (HttpURLConnection) downloadUrl.openConnection(); | |
connection.setRequestProperty( | |
"User-Agent", "discord-game-sdk4j (https://github.com/JnCrMx/discord-game-sdk4j)"); | |
ZipInputStream zin = new ZipInputStream(connection.getInputStream()); | |
// Search for the right file inside the ZIP | |
ZipEntry entry; | |
while ((entry = zin.getNextEntry()) != null) { | |
if (entry.getName().equals(zipPath)) { | |
// Create a new temporary directory | |
// We need to do this, because we may not change the filename on Windows | |
File tempDir = new File(System.getProperty("java.io.tmpdir") + "HavenCorePublic-" | |
+ "java-" + name + System.nanoTime()); | |
if (!tempDir.mkdir()) throw new IOException("Cannot create temporary directory"); | |
tempDir.deleteOnExit(); | |
// Create a temporary file inside our directory (with a "normal" name) | |
File temp = new File(tempDir, name + suffix); | |
temp.deleteOnExit(); | |
// Copy the file in the ZIP to our temporary file | |
Files.copy(zin, temp.toPath()); | |
// We are done, so close the input stream | |
zin.close(); | |
// Return our temporary file | |
return temp; | |
} | |
// next entry | |
zin.closeEntry(); | |
} | |
zin.close(); | |
// We couldn't find the library inside the ZIP | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment