Little update..
This one is more based on objects instead of text based ;o
Put this in a class:
PHP Code:
function onCreated() {
this.filepath = "files/templates/";
// Filepath used when type of parsing is 2
this.at = {
"fullname", "description"
};
/* All variables in the 'at' array can be attached to existing text or
made equal something else. Also supports format (%s)
var = @text --> var @= " "@ text
var = "Lion's %s of Strength" -- > var = format("Lion's %s of Strength", var) */
this.as = {
"icon", "image", "script", "params", "clientscript"
};
/* All vars is 'this.as' can only be overwritten with an other value
var = val */
this.pm = {
"strength", "intelligence", "dexterity", "wisdom",
"luck", "attackspeed", "armor"
};
/* These variables have support for 'plus_', 'minus_' prefixes
var = plus_5 --> var += 5
var = minus_5 --> var -= 5 */
this.bl = {
"equipable", "unique"
};
/* These are booleans, can only set these to 0 or 1 (false or true) */
// For more info, check out the examples
}
public function parseobject(c, cad) {
// c = type (1 = loadvarsfromarray, 2 = loadvarsfromfile)
// cad = what to load (1 = array with vars, 2 = filename)
temp.cd = 0;
if (c == 2) cd.loadlines(this.filepath @ cad);
else cd = cad;
temp.vars.loadvarsfromarray(cd);
for(temp.cvar : vars.getdynamicvarnames()) {
if (cvar in this.at) {
temp.value = vars.(@cvar);
if (value.starts("@")) {
this.(@cvar) @= " "@ value.substring(1);
}else if (value.pos("%s") > -1) {
this.(@cvar) = format(value, this.(@cvar));
}else{
this.(@cvar) = value;
}
}else if (cvar in this.as) {
this.(@cvar) = vars.(@cvar);
}else if (cvar in this.pm) {
temp.value = vars.(@cvar);
if (value.starts("plus_")) {
this.(@cvar) += int(float(value.substring(5)));
}else if (value.starts("minus_")) {
if ((this.(@cvar)-int(float(value.substring(6)))) < 1) {
this.(@cvar) = 0;
}else{
this.(@cvar) -= int(float(value.substring(6)));
}
}else{
this.(@cvar) = int(float(value));
}
}else if (cvar in this.bl) {
if (vars.(@cvar) in <0,1>) { // true = 1, false = 0
this.(@cvar) = vars.(@cvar);
}
}
}
}
And here's an example of usage:
PHP Code:
function onCreated() {
this.item = new TStaticVar("WoodenSword");
this.item.strength = 4;
this.item.dexterity = 3;
this.item.value = 45;
this.item.equipable = 1;
this.item.unique = 0;
this.item.script = "";
this.item.params = "";
this.item.clientscript = "";
this.item.fullname = "Wooden Sword";
this.item.description = "A sword made out of wood.";
this.item.join("test_parse"); // The class that you put the function in
temp.enchant = {
"fullname=Broken %s",
"equipable=0",
"value=minus_20",
"description=@This item is broken."
};
this.item.parseobject(1, enchant);
echo(this.item.fullname); // 'Broken Wooden Sword'
}
Suggestions and ideas on how to improve are welcome :-)