PDA

View Full Version : Replicating Password Protected Variables


Kyranki
01-13-2008, 10:36 PM
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?wakka=ExampleEncryption

This is the reserved variables class:


// 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 (v: Reserved.(@ "reservedvars_" @ this.reservedvarsname))
// Returns true or false
return (v[0] == var);
}
// Functions used to mod vars
public function modReservedVar(var, val, encryption, key, pass) {
// 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(encryption, key) == pass) {
modvar(var, val);
this.reservedvarslog.(@ var).add({timevar2, temp.oldvar, getvar(var)});
}
}
// This is the function used to change the value for a reserved (protected) variable.
public function setReservedVar(var, val, encryption, key, pass) {
// 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(encryption, key) == pass) {
setvar(var, val);
this.reservedvarslog.(@ var).add({timevar2, temp.oldvar, getvar(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 (ve: Reserved.(@ "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 (ve: Reserved.(@ "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 (v: this.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(v, this.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":

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:


this.reservedvars_(this.reservedvarsname) = {{var, encryption code, password}, {var, encryption code, password}};


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

this.reservedvarsname = "StanMan";


Meaning in the Reserved NPC it would look like:

// Reserved NPC

this.reservedvars_StanMan = {{var, encryption code, pass}, {var, encryption code, pass}};


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);

Chompy
01-13-2008, 10:43 PM
nice script :)

But I would like to see more examples tho, like in use etc. ^^

Twinny
01-14-2008, 12:31 AM
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 :D. 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.