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 
 
Hooking Player Attitude Changes for pvp
Goto page Previous  1, 2, 3  Next
 
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: Thu Jul 29, 2010 22:24    Post subject: Reply with quote

Baaleos wrote:
LMAO. Lol

I did try not using the ASM, but when I used the direct call to the function, it resulted in crashes inside the nwnx_events.dll instead of inside nwnserver.exe.

I will give it a try shortly, my 'test dummies' who I test the hostility on have gone for a nap. Lol


could you run two VMs? ...if you have 2 cdkeys, that is.
Back to top
View user's profile Send private message Send e-mail MSN Messenger
Baaleos



Joined: 02 Sep 2007
Posts: 830

PostPosted: Thu Jul 29, 2010 22:27    Post subject: Reply with quote

fraid I dont have any spare cdkeys, although I did become rather adept at....erm... 'making them up' at one point in time. But thats highly unreliable.

But anyway....
I have to say, Im actually giggling quite incessently here, nwnx forums have got more replies from you two in the last 5 minutes than in the whole week. Hehe.

Sorry to keep bugging ya all.
Im only starting to get the hang of c++ and even then, im kinda needing help finishing what I start.
Back to top
View user's profile Send private message
Baaleos



Joined: 02 Sep 2007
Posts: 830

PostPosted: Thu Jul 29, 2010 22:38    Post subject: Reply with quote

Note - The reason I am trying to get this to work, is to prevent players in a faction, such as humans, from going friendly to players in a faction such as vampires.

While I suspect attempting to bypass this function will lead to 'crashes' undoubtably, because this function also gets called for things such as Party joining etc, I do think if I were to script it to 're-toggle' hostility via nwnscript it would possibly work.

But my main worry is, that the RunScript call comes before the SetLikesMeNextHook()


Meaning the Script call from nwn will come before the ToggleButton has even been processed.

Eg - Player toggles hostile
Script Runs, Sets them back to Hostile via SetDislikesMe(); in nwscript
NextHook() runs, and sets them to friends ... defeating the purpose

Does anyone know if DelayCommand() functions run from inside the event script, are blocking?
if not, then it could be a potential way to resolve this.
Eg - DelayCommand(1.00,SetDislikesMe());

hopfully this would fire after the player had successfully toggled themselves to friendly.

But if in the case that it is blocking.... im screwed and I need a different approach.
Back to top
View user's profile Send private message
Zebranky



Joined: 04 Jun 2006
Posts: 415

PostPosted: Thu Jul 29, 2010 22:39    Post subject: Reply with quote

This thread might push me to the #4 spot by post count. Wink

I think the crash is due to the *(dword *)a -- you're casting something that is very much not a pointer to a pointer, then dereferencing it. Have you tried the code in my post from 16:16 (well, according to my time zone, anyway)?
_________________
Win32 SVN builds: http://www.mercuric.net/nwn/nwnx/

<Fluffy-Kooshy> NWNx plugin is to this as nuclear warheads are to getting rid of fire ants.

<ThriWork> whenever I hear nwn extender, I think what does NWN need a penis extender for?
Back to top
View user's profile Send private message Visit poster's website
Baaleos



Joined: 02 Sep 2007
Posts: 830

PostPosted: Thu Jul 29, 2010 22:48    Post subject: tried with and without Reply with quote

I've tried with and without casts.

I think I have tried every version of code mentioned in this daisy chain of posts. Hehe.

I've tried 'a', and (*dword)*a etc


The state I currently have it in, is this
Code:

void (*EndDialogNextHook)();
void (*RemoveSanctuaryNextHook)();
void (*DevastatingNextHook)();
void (*TogglePauseNextHook)();
void (*SetLikesMeNextHook)(void *pThis, void *, unsigned long a, int b, int c);


void __fastcall SetLikesMeHookProc(void *pThis, void *, unsigned long a, int b, int c)
{
    fprintf(events.m_fFile, "Firing Event\n");
    events.FireEvent(a, 17,"SetLikesMe");
    fprintf(events.m_fFile, "Event Fired\n");
    SetLikesMeNextHook(pThis, NULL, a, b, c);
    fprintf(events.m_fFile, "After the SetLikesMeNextHook\n");
}


I cant remember for sure, but I know that the earlier versions of the code, were causing crashes in the module itself, the dll.
Then eventually, the crashes started to occur inside nwserver.
Oddly enough, one of the crashes did give me a memory offset, which was inside the SetLikesMeNextHook function (well, the internal function anyway).
Which shows that it at least did make the leap from module to nwn internal code, it is probably, as you say, the crash caused by one of the stack variables loosing its value.

Im just waiting for someone I know, with nwn to come online, then I can test these.


In the mean time.
Do you happen to know any easier or more reliable way of doing what I want to do?
Eg - Prevent people from toggling themselves friendly or hostile

OOOOH
My tester is awake...
I will report back
Back to top
View user's profile Send private message
MaxRock



Joined: 24 Jan 2008
Posts: 196

PostPosted: Thu Jul 29, 2010 22:54    Post subject: Reply with quote

Could the problem be with the Runscript hook's calling convention? Since that is what the windows crash box keeps telling me.

Maybe it has to be changed to fastcall too? Cause there's no errors if you take the runscript call out and instead print all the parameters (which are good by the way).
Back to top
View user's profile Send private message Send e-mail MSN Messenger
Baaleos



Joined: 02 Sep 2007
Posts: 830

PostPosted: Thu Jul 29, 2010 22:55    Post subject: Reply with quote

crash occurs in module nwnserver.exe

at offset d0464

In IDA, this offset does take me to a line I think, which mentions esi etc.


Note - Once again, it didnt get beyond the RunScript method call, it crashed before getting to the debug message which was below it.
Back to top
View user's profile Send private message
MaxRock



Joined: 24 Jan 2008
Posts: 196

PostPosted: Thu Jul 29, 2010 23:04    Post subject: Reply with quote

Baaleos wrote:
crash occurs in module nwnserver.exe

at offset d0464

In IDA, this offset does take me to a line I think, which mentions esi etc.


which gets the object id of "this". (v = NWSCreature->obj_id)
So I'm assuming that at that point the this pointer got broke (Yes, that is how my oop professor put it, cough) in the SetLikesMeNextHook call?

On an unrelated note: Does C# have pointers?
Back to top
View user's profile Send private message Send e-mail MSN Messenger
Baaleos



Joined: 02 Sep 2007
Posts: 830

PostPosted: Thu Jul 29, 2010 23:06    Post subject: I have absolutely no idea Reply with quote

I've never had to use c# for unsafe inmemory writing.

I do know that it is possible to write memory edits etc with c#, but it requires using an api, and having to specify the memory locations.

Since nwnx is c++ I never taught myself the c# equivelant, since it woulda been pointless and not compatible with nwnx.
Back to top
View user's profile Send private message
Baaleos



Joined: 02 Sep 2007
Posts: 830

PostPosted: Thu Jul 29, 2010 23:19    Post subject: wel...__fastcall Reply with quote

A simple modifyer of __fastcall didnt do anything.

I just added it to the front of run script etc, in declaration and definition, but made no change.

Still crashed at ...464

Why does the RunScript definition use ASM, can it be easilly updated to use a fastcall?

Would the fastcall even make a difference?
Back to top
View user's profile Send private message
Baaleos



Joined: 02 Sep 2007
Posts: 830

PostPosted: Thu Jul 29, 2010 23:22    Post subject: Reply with quote

Silly question.... but could this make a difference?

Code:

if (!scriptRun)
   {
    fprintf(events.m_fFile, "Firing Event\n");
    events.FireEvent(a, 17,"SetLikesMe");
    fprintf(events.m_fFile, "Event Fired\n");
    SetLikesMeNextHook(pThis, NULL, a, b, c);
    fprintf(events.m_fFile, "After the SetLikesMeNextHook\n");
   }


I mean, the !scriptRun variable is there for a reason in the other hooks, and the RunScript method does set it to be 1.

Also, a = unsigned long,
RunScript is expecting a const int - are these compatible?
Back to top
View user's profile Send private message
MaxRock



Joined: 24 Jan 2008
Posts: 196

PostPosted: Thu Jul 29, 2010 23:38    Post subject: Reply with quote

EUREKA!

Code:

signed int(__fastcall *RunScript_Next)(void*, void*, CExoString*, int, int);
signed int __fastcall RunScript_Hook(void *pThis, void *pVOID, CExoString *ScriptName, int a3, int a4) {
   return RunScript_Next(pThis, NULL, ScriptName, a3, a4);
}


void(__fastcall *SetPVPPlayerLikesMe_Next)(CNWSCreature*, void*, unsigned long, int, int);
void __fastcall SetPCPPlayerLikesMe_Hook(CNWSCreature* cre, void* pVoid, unsigned long objid, int a, int b) {
   __log(3, "cre: %08X\nobjid: %08X\na: %d\nb: %d\n", cre, objid, a, b);
   CExoString ScriptName("nwnx_events");
   RunScript_Hook((*NWN_VirtualMachine), NULL, &ScriptName, objid, 1);
   SetPVPPlayerLikesMe_Next(cre, NULL, objid, a, b);
}


Code:

void HookFunctions() {
   DWORD org_SetPVPPlayerLikesMe = 0x004D0450;
   DWORD org_Runscript = 0x005BF9D0;

   if (HookCode((PVOID) org_SetPVPPlayerLikesMe, SetPCPPlayerLikesMe_Hook, (PVOID*)&SetPVPPlayerLikesMe_Next)) __log(1, "* SetPCPPlayerLikesMe hooked\n");
   if (HookCode((PVOID) org_Runscript, RunScript_Hook, (PVOID*)&RunScript_Next)) __log(1, "* RunScript hooked\n");

}


nwnx_events.nss
Code:

void main() {
   PrintString(GetName(OBJECT_SELF));
}


There is of course a catch though.
Since I couldn't figure out the FireEvent stuff in nwnx_events I put this in the nwnx_funcs plugin (I would have started from scratch but I hate setting up a new project. Does anybody now how to make a project template in VS 2008 express?)

Anyways, the nwserverlog shows the name of the caller and it doesn't crash.
What's left is to save the 2nd creature involved and return it from the setstring hook.

EDIT:
And of course actually changing the PVPLikesMe to behave the way we went. ... and getting it to work with nwnx_events. Confused ... at least it doesn't crash.
Back to top
View user's profile Send private message Send e-mail MSN Messenger
Baaleos



Joined: 02 Sep 2007
Posts: 830

PostPosted: Thu Jul 29, 2010 23:42    Post subject: Reply with quote

well, the good thing is that nwnx_events has a built in oTarget variable, which is returnable via nwnscript via

GetNWNXEventTarget()

or something like that, but obviously needs to be called from within the event script.

I will have a look at adding those changes to the events script here, to see if it makes a difference.

In regards to what im trying to do, do you think this is at all pointless? I mean, putting a hook on the SetLikesMe function, would nwnscript even be able to reset the person back to the correct attitude, since the script is getting run before the hook finishes?
Back to top
View user's profile Send private message
MaxRock



Joined: 24 Jan 2008
Posts: 196

PostPosted: Thu Jul 29, 2010 23:48    Post subject: Reply with quote

Baaleos wrote:

In regards to what im trying to do, do you think this is at all pointless? I mean, putting a hook on the SetLikesMe function, would nwnscript even be able to reset the person back to the correct attitude, since the script is getting run before the hook finishes?


That's probably the way to go. If you return from the LikesMe function right away the attitude itself doesn't actually change but both parties receive the "So and so dislikes you" and the button changes too.
So reversing the attitude in the script should be the way to go.

Edit: Ohh! Yea, I didn't even think about whether nwn script can do that...
Back to top
View user's profile Send private message Send e-mail MSN Messenger
Baaleos



Joined: 02 Sep 2007
Posts: 830

PostPosted: Thu Jul 29, 2010 23:52    Post subject: Reply with quote

there is another problem.
This function doesnt only get run when a player clicks it, nwnscript uses it too in the SetDislikes() function.
Meaning the event script, will get fired, by player, and by nwnscript.
Meaning returning out of the script will happen regardless of whos doing it, unless I have some special variable set up that can do it.


Im sooo screwed and have probably wasted everyones day. Lol


There is the nBypass variable, which when set to 1, I could make it bypass the Event completely, however, it would be very complicated to determine when a valid time to use this is.
Eg - I would have set a localvariable on players I want to change their pvp status to, to say that they are allowed to, and then in the event script, when it comes to them actually changing dislike status, it reads this variable, and if they do not have it, then it sets the Bypass switch. If they do however have the 'key' variable, it will not bypass.

On top of all that,
Is there any quick and dirty way of declaring the CExoString class and the VirtualMachine class?
Can I do these via __fastcalls too?
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
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
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