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 
 
Modifying existing, adding new script functions:nwscript.nss

 
Post new topic   Reply to topic    nwnx.org Forum Index -> Windows development
View previous topic :: View next topic  
Author Message
MaxRock



Joined: 24 Jan 2008
Posts: 196

PostPosted: Fri Sep 03, 2010 20:36    Post subject: Modifying existing, adding new script functions:nwscript.nss Reply with quote

I'm wondering if anybody has ever tried to add new script functions by adding them to nwscript.nss directly?
If yes, why was it abandoned? (Can't seem to find any mention of it.)
Back to top
View user's profile Send private message Send e-mail MSN Messenger
axs



Joined: 11 Feb 2005
Posts: 76

PostPosted: Fri Sep 03, 2010 22:49    Post subject: Reply with quote

I added additional arguments, new functions? I didn't try.
Back to top
View user's profile Send private message
ShaDoOoW



Joined: 20 Aug 2005
Posts: 584

PostPosted: Fri Sep 03, 2010 23:51    Post subject: Reply with quote

yes i tried but it didnt compiled, hmm new arguments are possible? didnt know/even tested this
_________________
Community Patch / NWNX Patch / NWNX Files / NWNX Connect
Back to top
View user's profile Send private message
MaxRock



Joined: 24 Jan 2008
Posts: 196

PostPosted: Sat Sep 04, 2010 1:35    Post subject: Reply with quote

I'm just asking to avoid my usual "foot-in-mouth" situations in case this has already been tried and there's a reason why it can't possibly work.
Anways...
I've been trying to implement 3.5 edition damage reduction and needed 2 new item properties / effects for damage reduction and overcoming damage reduction. Initially I was just going to "hijack" one of the existing itemproperty functions to return the new one I wanted but a closer look at CVirtualmachineCommands::ExecuteCommand revealead that the VM is actually open to new functions.
A new function at the end of nwscript.nss will actually be processed by ExecuteCommand - and of course found invalid unless you hook it - but number and types of arguments and returning values works fine.
It seems a more "elegant" way than going through SetLocalString.

Of course the catch is in how to modularize it:
- Only 1 plugin can hook CVirtualmachineCommands::ExecuteCommand
- Extending the function table
- Adding to nwscript.nss
Back to top
View user's profile Send private message Send e-mail MSN Messenger
virusman



Joined: 30 Jan 2005
Posts: 1020
Location: Russia

PostPosted: Sat Sep 04, 2010 1:38    Post subject: Reply with quote

NWNX 2.8 with its new plugin interface can solve this problem. Unfortunately, it's not available on Windows yet..
The nwscript.nss approach hasn't been used for several reasons:
1. This technique requires more effort than just using the existing SetLocalString hook
2. Sharing the hook was impossible before 2.8-dev
3. Editing nwscript.nss is generally unsafe:
* A script compiled with modified nwscript.nss, when run without NWNX, may crash the VM or the server.
* If several plugins add new functions, there is a problem with merging the file and defining their order
_________________
In Soviet Russia, NWN plays you!
Back to top
View user's profile Send private message Visit poster's website Yahoo Messenger
MaxRock



Joined: 24 Jan 2008
Posts: 196

PostPosted: Sat Sep 04, 2010 2:13    Post subject: Reply with quote

nwscript.nss is definitly the biggest hurdle. (actually it's possible to put an #include "newfunctions" at the end and those new functions will work).
But it seems that at some point the engine must enumerate the functions in nwscript.nss: the first new one is 848, the next one 849, and so on.
Still leaves the problem that nwscript.nss has to be edited somehow...
Well, maybe the idea isn't practical for a general nwnx plugin, but for one tailored to a server I think it's interesting.

EDIT:
Yea, nevermind. The compiler enumerates the functions, duh Embarassed
Back to top
View user's profile Send private message Send e-mail MSN Messenger
ShaDoOoW



Joined: 20 Aug 2005
Posts: 584

PostPosted: Sat Sep 04, 2010 7:05    Post subject: Reply with quote

virusman wrote:

* A script compiled with modified nwscript.nss, when run without NWNX, may crash the VM or the server.
Not sure if I understand here, but I do using modified nwmain since two years back when I find the article from Georg Zoeller, without any problems with NWNX both on linux and windows basically with all plugins available...

only functions rename
_________________
Community Patch / NWNX Patch / NWNX Files / NWNX Connect
Back to top
View user's profile Send private message
MaxRock



Joined: 24 Jan 2008
Posts: 196

PostPosted: Sat Sep 04, 2010 8:10    Post subject: Reply with quote

ShaDoOoW wrote:
virusman wrote:

* A script compiled with modified nwscript.nss, when run without NWNX, may crash the VM or the server.
Not sure if I understand here, but I do using modified nwmain since two years back when I find the article from Georg Zoeller, without any problems with NWNX both on linux and windows basically with all plugins available...

only functions rename


I'm using that too.
From what I gather the virtual machine doesn't really care about function names; the compiler generates a number from the position in nwscript.nss which the vm uses to determine which function to actually call. EffectHeal(), for example is 78. If you rename it to ExplodeToLittleTinyBits it'll still work cause the compiler still sees it as 78.

The VM has a big switch/case which is used to determine which function to call (I know, it's really a function pointer array, but I find switch/case easier to explain)

Code:

int CNWVirtualMachine::ExecuteCommand(function_number, ...) {
  switch (function_number) {
    case 0: ExecuteCommandRandom(...); break;
    case 1: ExecuteCommandPrintString(...); break;
    .
    .
    case 847: ExecuteCommandItemPropertyEffect(...); break;
 }
}

The nice thing is that if you add a new function at the end of nwscript.nss, the compiler will happily assign it the next number - in this case 848 - and if you hook ExecuteCommand to extend the switch/case you can use that function in any script

The problem Virusman is talking about is that bad things might happen if the script is run without the plugin hooking ExecuteCommand.
Back to top
View user's profile Send private message Send e-mail MSN Messenger
ShaDoOoW



Joined: 20 Aug 2005
Posts: 584

PostPosted: Sat Sep 04, 2010 9:05    Post subject: Reply with quote

what about constants? I ask because I added one constant (const int SAVING_THROW_TYPE_PARALYSE = 20;) into nwscript.nss for my patch project, yet didnt tested yet. I added it under last saving_throw_type...
_________________
Community Patch / NWNX Patch / NWNX Files / NWNX Connect
Back to top
View user's profile Send private message
MaxRock



Joined: 24 Jan 2008
Posts: 196

PostPosted: Sat Sep 04, 2010 18:10    Post subject: Reply with quote

Not quite sure about constants but they shouldn't matter too much, since the compiler simply substitutes them for their value. Adding them anywhere certainly doesn't change the function numbering.
Back to top
View user's profile Send private message Send e-mail MSN Messenger
Sag



Joined: 24 Sep 2009
Posts: 8

PostPosted: Thu Sep 23, 2010 15:18    Post subject: Reply with quote

The article can be found here:
http://www.gulbsoft.org/nwn/how_to_override_core_scripting_functions

I renamed GetCasterLevel to GetCasterLevelVoid and made a function in nw_i0_spells named GetCasterLevel which then uses ExecuteScript from which I retrieve the caster's level based on conditions I added in. Granted I had to compile all spell scripts but once I did that, I simply added them to the server's override folder. It works quite well and the "ExecuteScript" function allows me to edit a different script without the need to recompile everything.

I'm sure the same could be done to run sub-functions in this way. It's a bit of a hack, but it works.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    nwnx.org Forum Index -> Windows development All times are GMT + 2 Hours
Page 1 of 1

 
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