JnCrMx / discord-game-sdk4j

Java bindings for Discord's Game SDK
MIT License
123 stars 23 forks source link

Can't create instance of Core with SDK as resource #34

Closed thebrightspark closed 3 years ago

thebrightspark commented 3 years ago

So I'm packaging the Discord Game SDK within the resources of a Minecraft mod so that I can have custom rich presence for the game. However, creating an instance of Core requires a File, which is not possible since one cannot be created for a JAR resource. It's fine when testing within the IDE, but not for a built JAR.

It'd be great if this could be changed to accommodate both a file on the file system or a JAR resource if possible please.

The exception I see is (omitting irrelevant lines):

java.lang.IllegalArgumentException: URI scheme is not "file"
    at java.io.File.<init>(File.java:421) ~[?:1.8.0_51]

With code that looks like this:

URL url = this.getClass().getClassLoader().getResource("discord_game_sdk/lib/x86_64/discord_game_sdk.dll");
try {
    Core.init(new File(url.toURI()));
} catch (Exception e) {
    e.printStackTrace();
    return;
}

And the exception would be thrown at the line: Core.init(new File(url.toURI()));.

JnCrMx commented 3 years ago

The general problem with loading it from an arbitrary URL is that the operating system usually requires native libraries to be loaded from a real file.

So the native library needs to be unpacked/copied in any case. I'd recommend doing that manually for now (you can look at examples/DownloadNativeLibrary.java for reference and use Files.copy(...) for copying the resource to a temporary file).

But I will add a method to load it from a URL soon, as it would probably be very useful feature!

thebrightspark commented 3 years ago

Ah okay then.

Yes, for now I've gotten around it by copying the file to a temporary location, inspired by your download example.

Thanks a lot for your response!

JnCrMx commented 3 years ago

Hopefully fixed in a2db44e1bee7d25f0fe07d96a17ef7589b718b77