logo logo

 Back to main page

The NWNX Community Forum

 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
nwnx_jvm - native JVM integration
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8, 9  Next
 
Post new topic   Reply to topic    nwnx.org Forum Index -> Linux development
View previous topic :: View next topic  
Author Message
elven



Joined: 28 Jul 2006
Posts: 259
Location: Germany

PostPosted: Sun Jan 18, 2015 3:09    Post subject: Reply with quote

No problem!

Have fun and let us know what cool things you build!
Back to top
View user's profile Send private message
Zunath



Joined: 06 Jul 2006
Posts: 183

PostPosted: Sun Jan 18, 2015 18:43    Post subject: Reply with quote

Hey elven,

This is working pretty great. I'm a little confused about how to handle delay commands and assign actions. I know there was a little discussion before about it, but I'm not sure I'm understanding. Smile

Here's some sample code of what I'm doing, based on what I read in previous posts.


Code:



            Scheduler.assign(oPC, new Runnable() {
               public void run() {
                  NWObject waypoint = NWScript.getObjectByTag(
                        "AFK_AREA_Spawn", 0);
                  NWScript.jumpToObject(waypoint, false);
               }
            });




Am I on the right track? What's the correct way to do an AssignCommand in java?
Back to top
View user's profile Send private message
elven



Joined: 28 Jul 2006
Posts: 259
Location: Germany

PostPosted: Sun Jan 18, 2015 19:27    Post subject: Reply with quote

Zunath wrote:
Code:

            Scheduler.assign(oPC, new Runnable() {
               public void run() {
                  NWObject waypoint = NWScript.getObjectByTag(
                        "AFK_AREA_Spawn", 0);
                  NWScript.jumpToObject(waypoint, false);
               }
            });




Am I on the right track? What's the correct way to do an AssignCommand in java?


That's the right way to go. AssignCommand() in nwscript actually takes a precompiled bytecode snippet - a closure. This isn't possible in Java (or any other external language), so we workaround:

We store a local code closure natively (the Runnable), and tell nwserver to ExecuteScript() it on the object we want Assign/Delay to run on. That's what Scheduler.assign does - it keeps track of your closures and then executes a specially-prepared nwscript that does the object switch and yields back to the VM.

Flow:

your code -> Scheduler.assign()/delay()/etc https://github.com/NWNX/nwnx2-linux/blob/master/plugins/jvm/java/src/org/nwnx/nwnx2/jvm/Scheduler.java#L54

-> Scheduler.flushQueues() [you need to call this whenever you want all delay/assign commands to be queued. Ideally, this is just before yielding back to nwserver]

-> flushQueues yields to https://github.com/NWNX/nwnx2-linux/blob/master/plugins/jvm/nwn/jvm_token.nss - it groups assigns by objectId to be as efficient as possible

-> token.nss returns to the jvm, which routes to Scheduler.token() https://github.com/NWNX/nwnx2-linux/blob/master/plugins/jvm/java/src/org/nwnx/nwnx2/jvm/Scheduler.java#L54

-> scheduler.token retrieves your stored closure and runs it with OBJECT_SELF being the correct object so native commands that always act on OBJECT_SELF work transparently
Back to top
View user's profile Send private message
Zunath



Joined: 06 Jul 2006
Posts: 183

PostPosted: Sun Jan 18, 2015 20:55    Post subject: Reply with quote

That helps - thanks elven.

In the interest of helping future readers, this is what my sample code ended up looking like:

Code:

Scheduler.assign(oPC, new Runnable() {
               public void run() {
                  NWObject waypoint = NWScript.getObjectByTag(
                        "AFK_AREA_Spawn", 0);
                  NWScript.jumpToObject(waypoint, false);
               }
            });

      Scheduler.flushQueues();



Note the addition of Scheduler.flushQueuest(); at the bottom.


On another topic: Have you considered a Windows port? Or has there not been enough demand?
Back to top
View user's profile Send private message
elven



Joined: 28 Jul 2006
Posts: 259
Location: Germany

PostPosted: Sun Jan 18, 2015 21:19    Post subject: Reply with quote

Note that it's best to call flushQueues() at the very end of the script you are running, not after each assign. More efficient that way since it then gets to group all assigns by Object ID, and doesn't context switch multiple times.

There is a windows port somewhere on these forums. I have never done nwnx windows development, so I can't help there. :)
Back to top
View user's profile Send private message
Zunath



Joined: 06 Jul 2006
Posts: 183

PostPosted: Mon Jan 19, 2015 3:53    Post subject: Reply with quote

Ah ok, cool. Thanks again.

I've started posting some of my stuff on Github in case anyone is interested to see what I'm playing around with.

Right now, I'm just moving some of our server's main scripts over to Java without much change to the logic. Later, I'll dig into making it more efficient and cleaning it up a bit. Gotta start somewhere though!

Here's the Github link: https://github.com/zunath/MZS_JVM
Back to top
View user's profile Send private message
Zunath



Joined: 06 Jul 2006
Posts: 183

PostPosted: Mon Jan 19, 2015 5:04    Post subject: Reply with quote

Elven,

Hate to keep bombarding you with questions but something isn't clear to me.

When I'm doing a Scheduler.delay, how do I get the object being acted upon (I.E: OBJECT_SELF)?

For example, in this code:

Code:


        Scheduler.delay(oPC, 1, new Runnable() {
            public void run() {
                NWScript.deleteLocalInt(OBJECT_SELF, "PC_ENTERING_MOD");
            }
        });


I would use oPC, but because it's not available in this scope (separate thread, I think?).

Any help you can provide would be great. Thanks man.
Back to top
View user's profile Send private message
elven



Joined: 28 Jul 2006
Posts: 259
Location: Germany

PostPosted: Mon Jan 19, 2015 13:44    Post subject: Reply with quote

Zunath wrote:
Elven,

Hate to keep bombarding you with questions but something isn't clear to me.

When I'm doing a Scheduler.delay, how do I get the object being acted upon (I.E: OBJECT_SELF)?

For example, in this code:

Code:


        Scheduler.delay(oPC, 1, new Runnable() {
            public void run() {
                NWScript.deleteLocalInt(OBJECT_SELF, "PC_ENTERING_MOD");
            }
        });


I would use oPC, but because it's not available in this scope (separate thread, I think?).

Any help you can provide would be great. Thanks man.


To pass objects into closures you need to mark them as final. Try "final NWObject oPC", then use that instead of OBJECT_SELF.

OBJECT_SELF is just a constant itself btw. I think it was 0x7F000000 but I may misremember.
Back to top
View user's profile Send private message
Zunath



Joined: 06 Jul 2006
Posts: 183

PostPosted: Mon Jan 19, 2015 15:06    Post subject: Reply with quote

Oh, ok. I guess that makes sense Smile Thank you again.


For what it's worth: I come from C# .NET land, so I'm learning the "quirks" with Java. (Otherwise known as my limited experience with the language haha).
Back to top
View user's profile Send private message
Zunath



Joined: 06 Jul 2006
Posts: 183

PostPosted: Tue Jan 20, 2015 3:43    Post subject: Reply with quote

Elven,

Tried to get buy-in from the guys maintaining my server for implementing this. They raised some valid concerns that I think would be remedied if we used the Windows version.

I tried to track it down and only came across this post: http://www.nwnx.org/phpBB2/viewtopic.php?t=1772

It looks promising but appears to be for NWN2 - is this the project you were referring to? Or is there another one I missed that's for NWN1?
Back to top
View user's profile Send private message
elven



Joined: 28 Jul 2006
Posts: 259
Location: Germany

PostPosted: Tue Jan 20, 2015 13:42    Post subject: Reply with quote

Zunath wrote:
Elven,

Tried to get buy-in from the guys maintaining my server for implementing this. They raised some valid concerns that I think would be remedied if we used the Windows version.

I tried to track it down and only came across this post: http://www.nwnx.org/phpBB2/viewtopic.php?t=1772

It looks promising but appears to be for NWN2 - is this the project you were referring to? Or is there another one I missed that's for NWN1?


That's odd. May I ask what concerns a serveradmin has about nwnx2/nwnx_jvm on Linux, but not on Windows? I'm curious.

Yeah, I guess you are right. Was the NWN2 port I was remembering. Sorry. :)
Back to top
View user's profile Send private message
Zunath



Joined: 06 Jul 2006
Posts: 183

PostPosted: Tue Jan 20, 2015 15:14    Post subject: Reply with quote

I'm actually the server admin, so I'm totally on board.

There were a few points brought up:

1.) If I get hit by a bus or someone has to take over maintaining the server, no one would know how to set it up.
2.) If the module was distributed via the vault in the future it would be difficult for anyone to load it up without prior knowledge of Linux.
3.) The people writing scripts now don't know any other languages and are hesitant to take up Java - they aren't sure how the transition really benefits them. (I've expressed source control, better IDEs, etc to clear this up but they're still scared Smile )
4.) The debugging process is difficult. The devs either need a Linux VM or use one I set up and remotely connect to it for testing.

Basically it boils down to lack of technical knowledge and a slight hesitation to learn anything new. Any pointers you can give (particularly on #4) would help - I really want to implement it. It's so much better than NWScript Smile
Back to top
View user's profile Send private message
elven



Joined: 28 Jul 2006
Posts: 259
Location: Germany

PostPosted: Tue Jan 20, 2015 15:24    Post subject: Reply with quote

Zunath wrote:
I'm actually the server admin, so I'm totally on board.

There were a few points brought up:

1.) If I get hit by a bus or someone has to take over maintaining the server, no one would know how to set it up.
2.) If the module was distributed via the vault in the future it would be difficult for anyone to load it up without prior knowledge of Linux.
3.) The people writing scripts now don't know any other languages and are hesitant to take up Java - they aren't sure how the transition really benefits them. (I've expressed source control, better IDEs, etc to clear this up but they're still scared :) )
4.) The debugging process is difficult. The devs either need a Linux VM or use one I set up and remotely connect to it for testing.

Basically it boils down to lack of technical knowledge and a slight hesitation to learn anything new. Any pointers you can give (particularly on #4) would help - I really want to implement it. It's so much better than NWScript :)


TBH, I don't see any of your points to be a particular showstopper w.r.t. future development. Once you decide to run nwnx2 you might as well just run the whole things on Linux. It has MUCH better plugin support, more features, and I daresay is more used, so more people can help you if you need stuff done.

1) Write documentation. Also, try not to get hit by a bus please.

2) That's the case on windows+nwnx too. I would advise anyways to keep a canonical set of binary files that it has been developed/tested against alongside your module and store them.

3) (?) Not sure what to say. Once you decide on using addons, you're effectively limiting development to your own PW, especially if you go esoteric like nwnx_jvm.

4) You might want to check into JVM remote debugging, where you attach Eclipse/IntelliJ to the actual gameserver over a tcp debug channel and can step through code, put breakpoints, and so on. Might sway some people. ;)

If you are feeling fancy, you can prepackage a server VM with all the stuff you need to run your PW and just link a disk image to that. Vagrant helps with this.

If you are a .NET person, there is a plugin for that too. Somewhere. I think. </vague>
Back to top
View user's profile Send private message
Zunath



Joined: 06 Jul 2006
Posts: 183

PostPosted: Tue Jan 20, 2015 16:19    Post subject: Reply with quote

Quote:


TBH, I don't see any of your points to be a particular showstopper w.r.t. future development. Once you decide to run nwnx2 you might as well just run the whole things on Linux. It has MUCH better plugin support, more features, and I daresay is more used, so more people can help you if you need stuff done.



You're preaching to the choir - I'm 100% for it. Smile


Quote:


1) Write documentation. Also, try not to get hit by a bus please.

2) That's the case on windows+nwnx too. I would advise anyways to keep a canonical set of binary files that it has been developed/tested against alongside your module and store them.

3) (?) Not sure what to say. Once you decide on using addons, you're effectively limiting development to your own PW, especially if you go esoteric like nwnx_jvm.

4) You might want to check into JVM remote debugging, where you attach Eclipse/IntelliJ to the actual gameserver over a tcp debug channel and can step through code, put breakpoints, and so on. Might sway some people. Wink



Good points - I saw the functionality for remote debugging which is awesome.

I'm thinking more along the lines of deploying the jar file over to the VM automatically. I.E: You build the jar and it gets deployed without having to copy and paste the file. Do you do this using a shared folder, dropbox, or some other mechanism?

Quote:

If you are feeling fancy, you can prepackage a server VM with all the stuff you need to run your PW and just link a disk image to that. Vagrant helps with this.


I wasn't aware of Vagrant - that's pretty cool. So essentially people would set up VirtualBox, point to our pre-built image and they'd be good to go?

Quote:

If you are a .NET person, there is a plugin for that too. Somewhere. I think. </vague>


I actually ran across that plugin and used it first. The problem is that it doesn't have a way to execute NWScript commands directly. The NWN2 solution is more robust, but not backwards compatible with NWN1. I talked to Skywing and he offered a lot of insight on how you'd go about porting it, but I really think the code is a little over my head at this point. Smile
Back to top
View user's profile Send private message
elven



Joined: 28 Jul 2006
Posts: 259
Location: Germany

PostPosted: Wed Jan 21, 2015 11:38    Post subject: Reply with quote

Zunath wrote:


Good points - I saw the functionality for remote debugging which is awesome.

I'm thinking more along the lines of deploying the jar file over to the VM automatically. I.E: You build the jar and it gets deployed without having to copy and paste the file. Do you do this using a shared folder, dropbox, or some other mechanism?

[..]

I wasn't aware of Vagrant - that's pretty cool. So essentially people would set up VirtualBox, point to our pre-built image and they'd be good to go?


As written, I dont use nwnx_jvm anymore. But generally speaking there are various ways to deploy stuff to servers. Using dropbox is not one of them. ;)

scp will do fine. Keep it simple. Alternatively, if you want to have everything tracked that is deployed, write a small script that builds from source serverside and deploys. Or something like Jenkins, which is overkill in nearly all cases.

..

You don't send around Vagrant images, instead you send something akin to a Makefile that builds the image and runs all neccessary shell comands to get up and running. Check out the Docs on the page, it's pretty simple. The only downside to Vagrant is that it runs on VirtualBox, which kind of sucks, especially for NWN development (it had a tick timer bug in the past, not sure if that was fixed meanwhile). YMMV.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    nwnx.org Forum Index -> Linux development All times are GMT + 2 Hours
Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8, 9  Next
Page 4 of 9

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group