Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   New Scripting Engine (GS2) (https://forums.graalonline.com/forums/forumdisplay.php?f=153)
-   -   Introducing Callstack Access (https://forums.graalonline.com/forums/showthread.php?t=78280)

Skyld 01-09-2008 12:28 AM

Introducing Callstack Access
 
Introduction

This is possibly my most wanted feature in GScript for some time: callstack access.

Callstack access allows you to see which objects and functions are calling other objects or functions. This means you can add better script security by, say, checking which NPCs are trying to call your bank functions, for example. It also can be used for script debugging.

Does it work on all servers yet? Probably not. Try it, and if it doesn't work, you can request your NPC-Server to be updated with it. :)

How callstack access works

First, let's examine how to get the contents of the callstack.

The getCallStack() function returns an array of links. The array that getCallStack() returns is a chronological list of the function call chain.

The important thing to recognise about these links is that they are not direct links to the calling function or NPC themselves, but instead they are an object linking to the temp. scope of the calling functions. Yes, this means that you can access temp. variables of functions earlier in the call chain. (See section below)

There is also a sub-object named "scriptcallobject", which is a link to the object that is calling the function.

Finally, there is another variable called "name" which returns the name of the calling function.

Code Example

Now, just to make the example a bit more straight-forward, we'll demonstrate this with just two NPCs; TestNPC, and TestNPC2.

Here is the code in TestNPC:
PHP Code:

function onCreated()
{
  
this.testCallStack();
}

public function 
testCallStack()
{
  
temp.callstack getCallStack();

  for (
temp.temp.callstack.size() - 1temp.>= 0temp.--)
  {
    echo(
"call stack entry " temp.": " 
      
temp.callstack[temp.i].scriptcallobject.name "." temp.callstack[temp.i].name);
  }


(You'll notice that the for (temp.foo: bar) iterator is not used here, or otherwise you might overwrite temp variables.)

And then TestNPC2:
PHP Code:

function onCreated()
{
  
TestNPC.testCallStack();


Updating the script of TestNPC will reveal this in RC:
Quote:

call stack entry 1: TestNPC.testCallStack
call stack entry 0: TestNPC.onCreated
Updating TestNPC2 instead reveals this:
Quote:

call stack entry 1: TestNPC.testCallStack
call stack entry 0: TestNPC.onCreated
This shows the order in which functions have been called in order to reach the current function.

Accessing temp variables

Instead of using scriptcallobject, you can access temp names directly, i.e.:
PHP Code:

function onCreated()
{
  
temp.foo "two";
  
  
this.testCallStack();
}

public function 
testCallStack()
{
  
temp.foo "one";
  
temp.callstack getCallStack();

  for (
temp.temp.callstack.size() - 1temp.>= 0temp.--)
  {
    echo(
"variable contents " temp.callstack[temp.i].foo);
  }


Upon updating the script, this will appear in RC:
Quote:

variable contents one
variable contents two

Twinny 01-09-2008 12:36 AM

A wicked way to secure scripts from pesky staff ^^. Although I wanted protected variables, this will definitely help ^^ <3 for Stefan!

Bank DBNPC
PHP Code:

public function deposit()
{
  
temp.stack getCallStack();
  
  if (
temp.stack[0].scriptcallobject.name != "Twinny/Test"//Could become an array of TEMPORARY allowed scripts
    
printf("Recieved illegal call from %s"temp.stack[0].scriptcallobject.name);
  else
    echo(
"Money deposited ^^");


Twinny/test and Twinny/Test2
PHP Code:

function onCreated() 
  
Bank.deposit(); 

Result!
PHP Code:

Weapon/GUI-script Twinny/Test2 added/updated by Twinny
Recieved illegal call from Twinny
/Test2
Weapon
/GUI-script Twinny/Test added/updated by Twinny
Money deposited 
^^ 


Kyranki 01-09-2008 01:13 AM

Oh, totally cool dude. I think there've been a few people awaiting the ability to view the callstack and script accordingly to improve script efficiencies. Though, I'm still with Twinny on protected/password protected variables, etc. this is an excellent addition!

Codein 01-09-2008 01:42 AM

I like this :) Great news. I'll start implementing into my MUDlib right away.

Switch 01-09-2008 02:06 AM

Nice :)

projectigi 01-12-2008 11:41 AM

uhm, may i request that triggers are traced too? :p

Inverness 01-12-2008 08:26 PM

Here are two functions, one to get the object that calls the function that calls it, and one to get the calling function in the same way.
PHP Code:

function getcallingfunction() {
  
temp.callstack getcallstack();
  return 
temp.callstack[temp.callstack.size() - 3];
}
function 
getcallingobject() {
  
temp.callstack getcallstack();
  return 
temp.callstack[temp.callstack.size() - 3].scriptcallobject;



Admins 01-14-2008 08:48 PM

Quote:

Originally Posted by projectigi (Post 1369290)
uhm, may i request that triggers are traced too? :p

Triggers are meant to be insecure, for easy script interaction. Also npcs could trigger each other endlessly. It could eventually be possible to find out who has triggered it.

projectigi 01-14-2008 09:16 PM

well i just want to make sure that e.g. a onActionServerSide can only be called from the same weapon xD

Dan 01-14-2008 09:43 PM

Very nice feature.

coreys 01-15-2008 04:46 AM

Very nice, I love it.
Now I just want to be able to use it on Maloria :)

xXziroXx 08-15-2008 05:17 AM

I missed this completely, very nice function! :D

Tigairius 08-15-2008 05:40 AM

Quote:

Originally Posted by xXziroXx (Post 1414528)
I missed this completely, very nice function! :D

Me too, thanks for bringing it to light :O

Programmer 08-15-2008 02:40 PM

Quote:

Originally Posted by xXziroXx (Post 1414528)
I missed this completely, very nice function! :D

Same, didn't even notice it o.O

xXziroXx 08-15-2008 03:54 PM

Couldn't have provided that first class bump without Dusty. :cool:


All times are GMT +2. The time now is 10:16 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.