Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 01-13-2008, 10:36 PM
Kyranki Kyranki is offline
Freelance Coder
Join Date: Aug 2007
Location: At the end of the rainbow, in the pot of gold.
Posts: 202
Kyranki is on a distinguished road
Send a message via AIM to Kyranki Send a message via MSN to Kyranki
Replicating Password Protected Variables

A while ago, Twinny (among others) requested the use of password protected variables. I've been thinking about whether or not this was possible to replicate using script functions etc. for a while since then and attempted to try it out so bear with me on this.

For testing purposes I used Skyld's simple encryption/decryption class for the encoding of passwords:

http://skyld.vip.graal.net/wikka.php...mpleEncryption

This is the reserved variables class:

PHP Code:
// Function used to determine if the variable is reserves
public function isReservedVar(var) {
  
// Runs a for loop through the list of reserved vars in the "Reserved" NPC and checks if the var param is a reserved variable
  
for (vReserved.(@ "reservedvars_" this.reservedvarsname))
    
// Returns true or false
    
return (v[0] == var);
}
// Functions used to mod vars
public function modReservedVar(var, valencryptionkeypass) {
  
// Checks if the variable is indeed reserved
  
if (!isReservedVar(var))
    return 
false;

    
// Gets the value of the variable before changing it
    
temp.oldval getvar(var);
    
// Checks if the encryption code, when decoded, reveals the password given, if so, mods the var and updates the log
    
if (simpledecrypt(encryptionkey) == pass) {
      
modvar(var, val);    
      
this.reservedvarslog.(@ var).add({timevar2temp.oldvargetvar(var)});
    }
}
// This is the function used to change the value for a reserved (protected) variable.
public function setReservedVar(var, valencryptionkeypass) {
  
// Checks if the variable is indeed a reserved one
  
if (!isReservedVar(var))
    return 
false;

    
// Gets the value of the variable you're about to change before it is changed
    
temp.oldval getvar(var);
    
// Checks if the encryption code, when decoded, reveals the password given, if so changes the var and updates the log
    
if (simpledecrypt(encryptionkey) == pass) {
      
setvar(var, val);
      
this.reservedvarslog.(@ var).add({timevar2temp.oldvargetvar(var)});    
    }
}
// Gets the encryption code from the array in the "Reserved" NPC holding the protected variables information for this npc.
public function getEncrypt(var) {
  for (
veReserved.(@ "reservedvars_" this.reservedvarsname)) {
    if (
ve[0] == var)
      return 
ve[1];
  }
}
// Gets the password from the array in the "Reserved" NPC holding the protected variables information for this npc.
public function getPassword(var) {
  for (
veReserved.(@ "reservedvars_" this.reservedvarsname)) {
    if (
ve[0] == var)
      return 
ve[2];
  }  

public function 
fixrv() {
  
// Runs a loop through all of the variables of the "this.reservedvarslog" object. Each of these vars is an array containing information on when that variable was updated the old value and the new value.
  
for (vthis.reservedvarslog.getDynamicVarNames()) {
    
// Checks if the value doesn't match the last logged new value, if it doesn't that means someone changed the variable without using the "setReservedVar" function, so this will change the variable back to the last logged "new" value...making it impossible to make a definite change to a variable without using that function.
    
if (this.(@ v) != this.reservedvarslog.(@ v)[this.reservedvarslog.(@ v).size() - 1][2])
      
setvar(vthis.reservedvarslog.(@ v)[this.reservedvarslog.(@ v).size() - 1][2]);
  }
}
// Setup for fixing the reserved variables
public function onFixReservedVars() {
  
// Calls the function which does the actual fixing of the vars
  
fixrv();
  
// Schedules the FixReservedVars event every 60 seconds
  
scheduleevent(60"FixReservedVars"null);

This is the list of reserved variables NPC DB aptly named: "Reserved":

PHP Code:
function onCreated() {
  
// Protected Variables, Encoding, Password Encoding reveals when decoded
  
this.reservedvars__StanTest2 = {{"variable""scTV16in1sY=""Password"}};

Now, for the reserved variables lists which go in the "Reserved" NPC DB, they can are listed as such:

PHP Code:
this.reservedvars_(this.reservedvarsname) = {{var, encryption codepassword}, {var, encryption codepassword}}; 
The variable "this.reservedvarsname" is a variable which is set inside the object of which you're using these functions. So "this.reservedvarsname" could be like:

// Test Weapon 1
PHP Code:
this.reservedvarsname "StanMan"
Meaning in the Reserved NPC it would look like:

// Reserved NPC
PHP Code:
this.reservedvars_StanMan = {{var, encryption codepass}, {var, encryption codepass}}; 
At best this class, is a simple way to make it so certain variables can only be changed a certain way. This won't be the best example you'll find of this by far...but I did try and I hope someone could learn something from this at the very least.

P.S. On a side note, there were a few function in use that did not belong to me...Those would be:

setvar(var, val);
modvar(var, val);
getvar(var);

Those all belong to Inverness and are of his creation only which is why I did not post the code, for them.

And easy replacement for each:

setvar(var, val):
this.(@ var) = val;

modvar(var, val):
this.(@ var) += val;

getvar(var):
return this.(@ var);
__________________
Stan.
Reply With Quote
  #2  
Old 01-13-2008, 10:43 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
nice script

But I would like to see more examples tho, like in use etc.
__________________
Reply With Quote
  #3  
Old 01-14-2008, 12:31 AM
Twinny Twinny is offline
My empire of dirt
Twinny's Avatar
Join Date: Mar 2006
Location: Australia
Posts: 2,422
Twinny is just really niceTwinny is just really nice
Send a message via AIM to Twinny
Quote:
Originally Posted by Kyranki View Post
A while ago, Twinny (among others) requested the use of password protected variables.
There is a major MAJOR difference between "protected" variables and "password protected" variables. Protected variables are variables that only the local object can read/access. In other languages you can specifiy what other objects can access the variable but that's not as needed. Since this is not available, I've been doing what you just made.

Though I added call stack access the second it came out . Now, the script needs to request the variable from the db. The db then checks the source object against a hard-coded (no variable to alter) array of allowed sources. If listed, the db will return the decrypted variable. I'm planning on releasing a nifty little script which shows a few of these features.

Last edited by Twinny; 01-14-2008 at 12:42 AM..
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 03:40 AM.


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