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 Ruby
Goto page 1, 2  Next
 
Post new topic   Reply to topic    nwnx.org Forum Index -> Development
View previous topic :: View next topic  
Author Message
virusman



Joined: 30 Jan 2005
Posts: 1020
Location: Russia

PostPosted: Sat Jan 02, 2010 4:03    Post subject: NWNX Ruby Reply with quote

I've ported NWNX Ruby to NWNX4.
You can download it here:
http://nwn.virusman.ru/trac/nwnx4/downloads
NWN2 1.23.1765 ONLY at the moment.
And it requires the new NWNX Core (also available at the above link).

No documentation yet.
NSS library: http://nwn.virusman.ru/trac/nwnx4/browser/trunk/bin/dist/nss/nwnx_ruby.nss?rev=82
An example:
Code:
# In NWNX Ruby, you can use almost all NWScript functions
# for example:
oPC = GetFirstPC()
SendMessageToPC(oPC, "This is a test message")

# also, you can define custom functions
def HappyNewYear
    oPC = GetFirstPC()
    while GetIsObjectValid(oPC)
        SendMessageToPC(oPC, "Happy new year!")
        oPC = GetNextPC()
    end
end

# all other features (like OO, arrays, blocks, etc.) are also available
# you can use any of the Ruby libraries, including Sequel (ORM library for databases)
# and nwn-lib (it can open and edit GFF, ERF, TLK, BIF files - complete replacement for LetoScript)


One of the main advantages is that Ruby code can be updated/generated without updating the module or restarting the server.
_________________
In Soviet Russia, NWN plays you!
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
PNJ



Joined: 30 Jun 2007
Posts: 67

PostPosted: Sat Jan 02, 2010 20:04    Post subject: Reply with quote

Nice, i hope to found some documentation for bic editing
Back to top
View user's profile Send private message
virusman



Joined: 30 Jan 2005
Posts: 1020
Location: Russia

PostPosted: Sat Jan 02, 2010 20:45    Post subject: Reply with quote

nwn-lib docs:
http://nwn-lib.elv.es/
_________________
In Soviet Russia, NWN plays you!
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
kuscotopia



Joined: 10 Aug 2009
Posts: 11

PostPosted: Wed Apr 07, 2010 1:38    Post subject: Reply with quote

You probably could of guessed I would be back with more questions Wink.

First, thank you virusman for pointing me towards your ruby plugin & nwn-lib. The proper bits are installed and xp-ruby.txt reports that the plugin is successfully initialized.

Onto the question, I would like to verify some assumptions (as I haven't found any documentation yet). In the example below:


Code:

string NWNXRuby_Eval(string sCode);
   
string NWNXRuby_Eval(string sCode)
{
    SetLocalString(GetModule(), "NWNX!RUBY!EVAL", sCode);
    return GetLocalString(GetModule(), "NWNX!RUBY!EVAL");
}


What is sCode? A line of ruby code to be interpreted? Name of a ruby script? If it is a segment of ruby, does nwnx4-ruby cause the script to block until ruby has finished interpreting the code? Or is there a race condition between evaluating the ruby expression and checking its return value?
Back to top
View user's profile Send private message
kuscotopia



Joined: 10 Aug 2009
Posts: 11

PostPosted: Wed Apr 07, 2010 1:55    Post subject: Reply with quote

Found similar thread here:

http://www.nwnx.org/phpBB2/viewtopic.php?t=1114&postdays=0&postorder=asc&start=0

Gives a little more insight into sCode. Still curious as to whether the calling script blocks or if a race condition exists. Thanks!
Back to top
View user's profile Send private message
Hialmar



Joined: 15 Jun 2005
Posts: 32

PostPosted: Sat Nov 27, 2010 16:41    Post subject: Reply with quote

ALFA is currently trying to have the ruby plugin working with the last version of Ruby (1.9.2-p0).

We have some crashes in the NWN2 server when initializing ruby.

It happens only in the NWN2server.exe. If I try to load the plugin in another program everything works and I can run ruby code in it (but obviously no NWScript code).

Anyone have ideas how to debug this problem ?

I have tried debugging nwn2server.exe before it crashes but it's a pain in the neck without the PDB.

If nothing works I will try to have the ruby interpreter in another process and use some kind of RPC over shared memory to make it work.
- The plugin would call Eval(ruby_code) in the Ruby interpreter process ;
- The Ruby interpreter process would then call back NWScript functions in the plugin so that they are executed in the nwn2server.exe.

It's quite complex and might have efficiency issues so debugging the plugin with the last version of Ruby is my priority here.
_________________
Hialmar, A Land Far Away Infrastructure Administrator
Back to top
View user's profile Send private message
virusman



Joined: 30 Jan 2005
Posts: 1020
Location: Russia

PostPosted: Sat Nov 27, 2010 17:41    Post subject: Reply with quote

What do you need 1.9 for?
_________________
In Soviet Russia, NWN plays you!
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
Hialmar



Joined: 15 Jun 2005
Posts: 32

PostPosted: Sat Nov 27, 2010 19:49    Post subject: Reply with quote

Regalis (one of our members) woul like some stuff from Ruby on Rails that seem only available in recent versions.

What is the last version of Ruby that works with the plugin ?

Thanks
_________________
Hialmar, A Land Far Away Infrastructure Administrator
Back to top
View user's profile Send private message
virusman



Joined: 30 Jan 2005
Posts: 1020
Location: Russia

PostPosted: Sun Nov 28, 2010 19:26    Post subject: Reply with quote

The only version I've compiled and tested it with was 1.8.6.
A couple of weeks ago Basileus contacted me and asked to switch NWNX4 Ruby to 1.9 or 1.8.7-mingw32. I recompiled with 1.8.7, but it didn't work on his server.
Some header editing is required for 1.9 upgrade, and obviously testing/debugging will be necessary.
If you've successfully compiled the plugin with 1.9 and need help with debugging, branch the existing code into /branches/xp_ruby-1.9 and upload your code there.
Unfortunately, I won't have enough time to do full testing for at least 2 weeks, but I might look at the code and suggest something.
_________________
In Soviet Russia, NWN plays you!
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
Hialmar



Joined: 15 Jun 2005
Posts: 32

PostPosted: Sun Nov 28, 2010 22:31    Post subject: Reply with quote

Did it.
You'll find everything in /branches/xp_ruby-1.9 as you requested.

In order to link with ruby1.9.2-p0 you'll need to comment out the DLLMain function in the ruby.c file.
I have included the ruby.c file I modified.
If you want the static lib and its pdb please ask and I'll put it on one of my websites.

Thanks a lot for your help.

I'll continue to debug it. I have found a way to debug it with vs2008 by generating an exception right before I want to debug and attaching to nwn2server.exe

Edit: Basileus and Regalis are the same person.
_________________
Hialmar, A Land Far Away Infrastructure Administrator
Back to top
View user's profile Send private message
Hialmar



Joined: 15 Jun 2005
Posts: 32

PostPosted: Sun Nov 28, 2010 23:25    Post subject: Reply with quote

I have found the issues I think.

Here is the code that causes problem:
In io.c:
Code:

    rb_define_variable("$stdin", &rb_stdin);
    rb_stdin = prep_stdio(stdin, FMODE_READABLE, rb_cIO, "<STDIN>");

The prep_stdio does:
Code:

static VALUE
prep_stdio(FILE *f, int fmode, VALUE klass, const char *path)
{
    rb_io_t *fptr;
    VALUE io = prep_io(fileno(f), fmode|FMODE_PREP, klass, path);

    GetOpenFile(io, fptr);
    fptr->stdio_file = f;

    return io;
}


GetOpenFile does the following:
Code:
void
rb_io_check_closed(rb_io_t *fptr)
{
    rb_io_check_initialized(fptr);
    if (fptr->fd < 0) {
   rb_raise(rb_eIOError, "closed stream");
    }
}


Which raises the exception as stdin is obviously closed in nwn2server.exe.

I will try to modify this behavior.
_________________
Hialmar, A Land Far Away Infrastructure Administrator
Back to top
View user's profile Send private message
Hialmar



Joined: 15 Jun 2005
Posts: 32

PostPosted: Sun Dec 05, 2010 15:05    Post subject: Reply with quote

It works now.

I disabled stdin/stdout/stderr in io.c using a global int variable.
The two files that do that are ruby.c and io.c and they are in the ruby1.9modifications folder.
_________________
Hialmar, A Land Far Away Infrastructure Administrator
Back to top
View user's profile Send private message
Hialmar



Joined: 15 Jun 2005
Posts: 32

PostPosted: Sat Dec 11, 2010 16:23    Post subject: Reply with quote

We found a bug with strings like the following,
"SendMessageToPC(GetFirstPC(), %q'Hello World')"
which crashed the plugin because of the %q and the fact that xp_ruby.dll logs strings to eval to the xp_ruby.txt log file using fprintf which interprets all % commands and crashes on the %q.

I have made a log method that outputs code to eval with a fputs in order not to interpret the % commands.

I have committed the files the svn as well.
_________________
Hialmar, A Land Far Away Infrastructure Administrator


Last edited by Hialmar on Sat Jan 08, 2011 23:02; edited 1 time in total
Back to top
View user's profile Send private message
Hialmar



Joined: 15 Jun 2005
Posts: 32

PostPosted: Sat Jan 08, 2011 23:02    Post subject: Reply with quote

I have corrected a bug that caused crashes when loading ruby files.

It's in the SVN now.

There is one more file (win32.c in the win32 sub-directory) that needs to be modified in the ruby distribution.
_________________
Hialmar, A Land Far Away Infrastructure Administrator
Back to top
View user's profile Send private message
Dethia



Joined: 01 Oct 2009
Posts: 23

PostPosted: Sun Mar 06, 2011 20:51    Post subject: Reply with quote

Is anyone using this for nwn2 along with elven's nwn-lib?

I am trying to do so but the first trouble I come across is loading the required libraries. Note I am not too familiar with ruby syntax though I did read some of the book.

In short I need to do:

require 'rubygems'
require 'nwn/all'

to load the libraries but get the following error:

Code:
NWNX4 Ruby Plugin V.0.1.0
(c) 2010 by virusman (virusman@virusman.ru)
visit us at http://www.nwnx.org

* Module loaded successfully.

(S) Request: "EVAL"

(S) Params:  "require 'rubygems'"

Error 6 while evaluating a Ruby expression: require 'rubygems'

(S) Request: "EVAL"

(S) Params:  "require 'nwn/all'"

Error 6 while evaluating a Ruby expression: require 'nwn/all'

* Plugin unloaded.


My guess is there is a pathing issue, as in the plug-in doesn't know where the ruby bin directory is, but how do I specify it? any help is appreciated.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    nwnx.org Forum Index -> Development All times are GMT + 2 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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