Skip to content
This repository has been archived by the owner on Apr 9, 2023. It is now read-only.

Commit

Permalink
Use native createTempFile method, Fix #43
Browse files Browse the repository at this point in the history
  • Loading branch information
DeJayDev committed Feb 10, 2021
2 parents 6233ef5 + c42914a commit 1f82937
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions src/main/java/net/arikia/dev/drpc/DiscordRPC.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,38 +125,34 @@ public static void discordRespond(String userId, DiscordReply reply) {
private static void loadDLL() {
String name = System.mapLibraryName("discord-rpc");
OSUtil osUtil = new OSUtil();
File homeDir;
String finalPath;
String tempPath;
String dir;

if (osUtil.isMac()) {
homeDir = new File(System.getProperty("user.home") + File.separator + "Library" + File.separator + "Application Support" + File.separator);
dir = "darwin";
tempPath = homeDir + File.separator + "discord-rpc" + File.separator + name;
} else if (osUtil.isWindows()) {
homeDir = new File(System.getenv("TEMP"));
boolean is64bit = System.getProperty("sun.arch.data.model").equals("64");
dir = (is64bit ? "win-x64" : "win-x86");
tempPath = homeDir + File.separator + "discord-rpc" + File.separator + name;
} else {
homeDir = new File(System.getProperty("user.home"), ".discord-rpc");
dir = "linux";
tempPath = homeDir + File.separator + name;
}

finalPath = "/" + dir + "/" + name;

File f = new File(tempPath);
try {
File f = File.createTempFile("drpc", name);

try (InputStream in = DiscordRPC.class.getResourceAsStream(finalPath); OutputStream out = openOutputStream(f)) {
copyFile(in, out);
f.deleteOnExit();
} catch (IOException e) {
try (InputStream in = DiscordRPC.class.getResourceAsStream(finalPath); OutputStream out = openOutputStream(f)) {
copyFile(in, out);
f.deleteOnExit();
} catch (IOException e) {
e.printStackTrace();
}

System.load(f.getAbsolutePath());
} catch(Exception e) {
e.printStackTrace();
}

System.load(f.getAbsolutePath());
}

private static void copyFile(final InputStream input, final OutputStream output) throws IOException {
Expand Down Expand Up @@ -235,4 +231,4 @@ private interface DLL extends Library {
void Discord_ClearPresence();
void Discord_Respond(String userId, int reply);
}
}
}

0 comments on commit 1f82937

Please sign in to comment.