JnCrMx / discord-game-sdk4j

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

Feature Request / Question #11

Closed Tomoli75 closed 3 years ago

Tomoli75 commented 3 years ago

Hi,

Would it be possible to add lobby features or networking? I believe they're documented in the Game SDK documentation and would make life a lot easier. If this would take too much time, please could you point me in the direction of adding them - I have tried to compile the library on my own using the latest Game SDK, to no avail.

Thanks! :)

JnCrMx commented 3 years ago

That is a really interesting idea, but I am afraid that it will take a lot of time as there are a lot of functions to implement.

So if you want to try it yourself, here is a short guide on how to set up a development environment:

  1. Install a C compiler: As the library relies on native code included with JNI, a C compiler is required to build it. On Windows I'd recommend using the one from Visual Studio (but not actually using Visual Studio). On Linux it's a lot easier as you can just use gcc.
  2. Install CMake (https://cmake.org): The project uses CMake to build the native libraries (which makes it possible to use mostly the same build process for Windows and Linux). Installing it should be easy on Linux, but might be a bit more difficult on Windows. Mainly make sure that it is in your %PATH%, so you can access it from anywhere.
  3. Install Maven: The project is built using Apache Maven. If you are using an IDE, you can probably skip this step tho, as it should provide its own version of Maven.
  4. Download and extract the Discord Game SDK: Download the Game SDK and extract it to ./discord-game-sdk (relative from the repository root). After this step the directory structure should look something like this: image
  5. Run mvn test: This Maven goal will build the native libraries, compile the Java code and also run a few tests. Some tests will probably fail, because I included some hard coded user IDs and stuff like that. Running mvn compile is not enough tho, as it will not copy the native libraries to the right place. But you can skip the tests by doing mvn test -DskipTests.
  6. Now you should have a development environment which should be able to compile the library. I will add a little guide on how I'd recommend getting started with including new features tomorrow.

If you encounter any problems with this setup, please tell me and I will try my best to help you revolve them. I know that this guide is pretty messy, so don't hesitate to ask if you have any questions or something isn't clear :)

Tomoli75 commented 3 years ago

I've got it building using cmd, I just couldn't really figure out how to implement new functions - haven't exactly created Java bindings for the Game SDK before.

The files (e.g. this) aren't exactly the easiest to understand at first glance, and so I was having some slight issues in trying to figure out how it was implemented so that I could add the lobby functionality.

JnCrMx commented 3 years ago

Ah, I see.

The main idea is that the "normal" Java functions call native C functions which then call the Game SDK.

For callbacks it works exactly the other way around: The SDk calls a provided C function which then calls a Java function. This for example happens in line 35.

Another thing that needs to be done by the native library is converting between C structs and Java objects. That e.g. is what create_java_relationship (line 7) is for. To do so, it first needs to find the corresponding Java classes (FindClass) and their constructors (GetMethodID(..., "<init>", ...)) and then finally create the objects (NewObject). For some objects there is a static method that is called instead of the constructor (e.g. createRelationship), but the overall procedure is similar.

Another confusing thing might be this CallbackData. Those structs are passed to the Discord SDK and then to our callback function. They provide an easy way to tell the C callback which Java method (usually of a Consumer or Predicate) it is supposed to call.

I hope this could help a bit to get a general overview. I'm sorry if this is a bit confusing, but where I am it's currently 3:45, so I might be a little sleepy already.

JnCrMx commented 3 years ago

I made a little example of how I would implement those functions: You can find it in 27a435db208dc1f0fabde47cc689a00082bcfab6.

If you want to work on it, maybe just fork and use the lobby-stub branch. I'd even welcome little changes, so if you want to, maybe just implement a few easy functions and make a pull request.

I also hope that this example is a little easier to understand than the messy relationship code. But if you have any particular questions or need help to understand it, feel free to ask :D

If you don't want to work on it or don't have time to get into my messy code, just tell me, so I will work on implementing the functions.

JnCrMx commented 3 years ago

Ok, I've implemented most lobby functions now (see https://github.com/JnCrMx/discord-game-sdk4j/tree/lobby-stub). I'm currently working on networking (which seems to be a bit complicated, but I was able to set up a minimal working example already). Sorry if you wanted to work on it, but I just couldn't resist.

JnCrMx commented 3 years ago

Sorry for closing, it was a misclick. I will keep it open until I'm done with lobby and networking functions.

Tomoli75 commented 3 years ago

Thank you for helping with this - I didn't really understand the conversion, but am incredibly grateful for your continued help in using this SDK.

JnCrMx commented 3 years ago

Update:

I'm currently working on the Javadoc for Lobby functions, but I have finished implementation for Lobby as well as Network bindings. Javadoc is usually one of the less interesting tasks, so I'm not sure how quick I will have it finished.

If you want to test/use the new functions right away, either checkout networking (or lobbies, but networking is mostly built upon lobbies, so you get both when choosing networking) and build the library yourself, or if you don't want to bother with compiling, tell me, so I build it and make some pre-releases or something like that. :D

JnCrMx commented 3 years ago

I'm done with the documentation for the Lobby functions now. It took me a lot longer than I expected though. I merged the Lobby branch into master and will hopefully be able to make a new release today.

Tomoli75 commented 3 years ago

Thank you for helping so much with this - it means a lot, and I hope I can somehow make it up to you in future!

JnCrMx commented 3 years ago

networking was merged into master by #15 and 7d91e88358fdb14f0d78421ffb0ac750b7c4cbc0. I will include it in the next release (0.5), which I'm working on at the moment.