PDA

View Full Version : object -> XML


JkWhoSaysNi
06-01-2007, 10:41 PM
Ok, i made this and figured it would be useful for others. It takes any object and converts it to xml.

It uses getStaticVarNames() because any vars in getVarNames() that aren't in getStaticVarNames() don't seem to work (such as clientr and client). I don't know why.

Anyway, the code:


function generateNode(obj) {

temp.xml = new TStaticVar();
temp.xml = "";

for (vn: obj.getStaticVarNames()) {
if (obj.(@vn).objecttype() != 0) {
if (obj.(@vn).size() > 0) {
temp.xml.add(" <"@[email protected]">");
for (i=0;i<=obj.(@vn).size()-1;i++) {
temp.xml.add(" <index id=\"" @ i @ "\">" @ obj.(@vn)[i] @ "</index>");

}
temp.xml.add(" </"@[email protected]">");
}
else {
temp.xml.add(" <"@[email protected]">");
temp.tag = vn;
temp.xml.addarray(generateNode(obj.(@vn)));
temp.xml.add(" </"@[email protected]">");

}
}
else {
temp.xml.add(" <"@[email protected]">"@ obj.(@vn) @"</"@[email protected]">");

}
}


return temp.xml;
}
function toXML(obj,filename) {
xml = new TStaticVar();
xml.add("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>");
xml.add("<"@[email protected]">");
xml.addarray(generateNode(obj));
xml.add("</"@[email protected]">");
xml.savelines(filename, false);

}


usage:


toXML(object,filename);
e.g.
toXML(player,player.account @ ".xml");


and sample output for a player object:


<?xml version="1.0" encoding="iso-8859-1"?>
<JkWhoSaysNi>
<account>JkWhoSaysNi</account>
<ani>hoverboardidle</ani>
<aniparams></aniparams>
<ap>100</ap>
<arrows>5</arrows>
<attached>false</attached>
<attachid>0</attachid>
<attachtype>0</attachtype>
<attr>
<index id="0"></index>
<index id="1"></index>
<index id="2"></index>
<index id="3"></index>
<index id="4"></index>
<index id="5"></index>
<index id="6"></index>
<index id="7">-1</index>
<index id="8">0</index>
<index id="9">dn_space_name3.gani</index>
<index id="10">-6.2681853</index>
<index id="11">0</index>
<index id="12">0</index>
<index id="13">0</index>
<index id="14"></index>
<index id="15"></index>
<index id="16"></index>
<index id="17"></index>
<index id="18"></index>
<index id="19"></index>
<index id="20">1</index>
<index id="21">1</index>
<index id="22"></index>
<index id="23"></index>
<index id="24"></index>
<index id="25"></index>
<index id="26"></index>
<index id="27"></index>
<index id="28"></index>
<index id="29"></index>
<index id="30"></index>
</attr>
<body>body10.png</body>
<bodyimg>body10.png</bodyimg>
<bombs>10</bombs>
<canspin>false</canspin>
<carriesblackstone>false</carriesblackstone>
<carriesbush>false</carriesbush>
<carriesnpc>false</carriesnpc>
<carriessign>false</carriessign>
<carriesstone>false</carriesstone>
<carriesvase>false</carriesvase>
<chat></chat>
<colors>
<index id="0">darkblue</index>
<index id="1">black</index>
<index id="2">darkblue</index>
<index id="3">darkblue</index>
<index id="4">darkblue</index>
<index id="5">black</index>
</colors>
<computerfirstlogin>0</computerfirstlogin>
<darts>5</darts>
<deaths>4</deaths>
<dir>2</dir>
<fullhearts>3</fullhearts>
<glovepower>1</glovepower>
<graalversion>4.2</graalversion>
<gralats>0</gralats>
<guild>scriptering</guild>
<head>head86.png</head>
<headimg>head86.png</headimg>
<headset>-1</headset>
<hearts>3</hearts>
<horseimg></horseimg>
<hp>3</hp>
<id>6</id>
<initialized>false</initialized>
<isfemale>false</isfemale>
<ismale>true</ismale>
<ispaused>false</ispaused>
<joinedclasses></joinedclasses>
<kills>0</kills>
<language>English</language>
<languagedomain>en</languagedomain>
<lastdead>39935339</lastdead>
<level>
<board></board>
<height>64</height>
<initialized>false</initialized>
<isnopkzone>false</isnopkzone>
<ispaused>false</ispaused>
<issparringzone>false</issparringzone>
<joinedclasses></joinedclasses>
<maxlooplimit>10000</maxlooplimit>
<name>osl3.nw</name>
<npcs>
<index id="0">Knight/Test</index>
</npcs>
<players>
<index id="0">JkWhoSaysNi</index>
</players>
<projectiles>
<initialized>false</initialized>
<ispaused>false</ispaused>
<joinedclasses></joinedclasses>
<maxlooplimit>10000</maxlooplimit>
<name>projectiles</name>
<scriptlogmissingfunctions>false</scriptlogmissingfunctions>
<timeout>0</timeout>
</projectiles>
<scriptlogmissingfunctions>false</scriptlogmissingfunctions>
<tilelayercount>1</tilelayercount>
<tilelayers>
<index id="0"></index>
</tilelayers>
<tiles>
<initialized>false</initialized>
<ispaused>false</ispaused>
<joinedclasses></joinedclasses>
<maxlooplimit>10000</maxlooplimit>
<name>obj</name>
<scriptlogmissingfunctions>false</scriptlogmissingfunctions>
<timeout>0</timeout>
</tiles>
<timeout>0</timeout>
<width>64</width>
</level>
<logintime>39935339</logintime>
<maxhp>3</maxhp>
<maxlooplimit>10000</maxlooplimit>
<movementlimit>10001</movementlimit>
<mp>0</mp>
<name>JkWhoSaysNi</name>
<nick>Knight (scriptering)</nick>
<onlinetime>106524</onlinetime>
<pause>false</pause>
<paused>false</paused>
<platform>win</platform>
<playersindex>0</playersindex>
<rating>1500</rating>
<ratingd>350</ratingd>
<rupees>0</rupees>
<scriptlogmissingfunctions>false</scriptlogmissingfunctions>
<shield>no-shield.gif</shield>
<shieldimg>no-shield.gif</shieldimg>
<shieldpower>1</shieldpower>
<sprite>0</sprite>
<sword>sword1.gif</sword>
<swordimg>sword1.gif</swordimg>
<swordpower>1</swordpower>
<timeout>0</timeout>
<trial>false</trial>
<upgradestatus>vip</upgradestatus>
<version>4.2</version>
<weapons>
<index id="0"></index>
<index id="1">ServerConnect</index>
<index id="2">-Player System</index>
<index id="3">*Player/Tailor</index>
<index id="4">*Staff/Admin Tool</index>
<index id="5">*Staff/Boots</index>
<index id="6">*Staff/Tag</index>
<index id="7">-lavadamage</index>
<index id="8">Hoverboard</index>
<index id="9">*Staff/Summon</index>
<index id="10">*ServerConnect</index>
<index id="11">-MessageSys</index>
<index id="12">flamethrower</index>
<index id="13">wavemaker</index>
<index id="14">Knight/Test</index>
</weapons>
<x>35.5</x>
<y>21</y>
<z>0</z>
</JkWhoSaysNi>


I intend to make a XML->Object function too. :)

Chompy
06-02-2007, 01:25 AM
Pretty nice!

cbk1994
06-02-2007, 02:42 AM
Looks very good. Don't see any purposes though, if you can't load it.

Rapidwolve
06-02-2007, 05:35 AM
I don't even know what the point of XML is, nice work though.

Chompy
06-02-2007, 05:04 PM
Looks very good. Don't see any purposes though, if you can't load it.

You can load/read xml through loadlines I've heard/seen.

And there is functions for xml released in the engine..
or check out PFA's XML Parser :o

cbk1994
06-02-2007, 07:48 PM
I suppose you could do this to load + save player atts every 30 minutes or whatever, and then you could look through an accounts file, and then look through the times, and see if they did something. Could be used to check for glitch abuse or so.

JkWhoSaysNi
06-02-2007, 07:52 PM
actually I'm using it to back up my DB NPCs, otherwise if they get reset, all the objects created on them are lost.

I'm still working on the XML import, i have it reading the simple data types. I just need to get the recursive part done so it recreates the arrays and objects :)

Chompy
06-03-2007, 02:05 AM
actually I'm using it to back up my DB NPCs, otherwise if they get reset, all the objects created on them are lost.

I'm still working on the XML import, i have it reading the simple data types. I just need to get the recursive part done so it recreates the arrays and objects :)

Still good :)
Waiting on XML import :D

Inverness
06-03-2007, 02:09 AM
That seems like an awfully large format to use. When you have to write down the name of the variable twice and use index for each member of an array.

Deadly_Killer
06-03-2007, 03:13 AM
Amazing! Rep++

JkWhoSaysNi
06-03-2007, 03:49 AM
That seems like an awfully large format to use. When you have to write down the name of the variable twice and use index for each member of an array.

but try to store objects recursivley in a format such as INI and you'll have problems.

Of course It is possible using a custom format but XML offers the ability of easily transporting the data and put it somewhere else like a website because there are plenty of parsers available.


As for "index", it could be replaced with CSV so it's just

array = {"a","b","c","d");

is represented as a,b,c,d

but that wouldn't account for objects stored in arrays or multi dimensional arrays.

Deadly_Killer
06-03-2007, 06:09 AM
It's a shame this doesn't save flags into it as well =[

JkWhoSaysNi
06-03-2007, 08:46 PM
what do you mean by 'flags'? like clientr?

Rapidwolve
06-03-2007, 08:53 PM
Nooo, I think he means flags like the american flag

just kidding <3

Deadly_Killer
06-03-2007, 09:13 PM
what do you mean by 'flags'? like clientr?

Yeah, and I just read your first message. You could make another thing to receive data by using

for (temp.var : getdynamicvarnames())
{
for (temp.var2 : temp.var.getdynamicvarnames())
{
// format stuff herez
}
}

Just a guess though ;).

Inverness
06-04-2007, 12:07 AM
With the introduction of GS2 its not good to put dots in clientr flags since GS2 its like changing scope I guess you could say, the engine reads it as in different object.

Deadly_Killer
06-04-2007, 08:16 AM
With the introduction of GS2 its not good to put dots in clientr flags since GS2 its like changing scope I guess you could say, the engine reads it as in different object.

First one returns client / clientr. Second one digs one level deeper to return clientr.something = hi

Skyld
06-04-2007, 10:06 AM
and sample output for a player object:


<?xml version="1.0" encoding="iso-8859-1"?>
<JkWhoSaysNi>
<account>JkWhoSaysNi</account>
<ani>hoverboardidle</ani>
<aniparams></aniparams>
<ap>100</ap>
<arrows>5</arrows>
<attached>false</attached>
<attachid>0</attachid>
<attachtype>0</attachtype>
<attr>
<index id="0"></index>
<index id="1"></index>
<index id="2"></index>
<index id="3"></index>
<index id="4"></index>
<index id="5"></index>
<index id="6"></index>
<index id="7">-1</index>
<index id="8">0</index>
<index id="9">dn_space_name3.gani</index>
<index id="10">-6.2681853</index>
<index id="11">0</index>
<index id="12">0</index>
<index id="13">0</index>
<index id="14"></index>
<index id="15"></index>
<index id="16"></index>
<index id="17"></index>
<index id="18"></index>
<index id="19"></index>
<index id="20">1</index>
<index id="21">1</index>
<index id="22"></index>
<index id="23"></index>
<index id="24"></index>
<index id="25"></index>
<index id="26"></index>
<index id="27"></index>
<index id="28"></index>
<index id="29"></index>
<index id="30"></index>
</attr>
<body>body10.png</body>
<bodyimg>body10.png</bodyimg>
<bombs>10</bombs>
<canspin>false</canspin>
<carriesblackstone>false</carriesblackstone>
<carriesbush>false</carriesbush>
<carriesnpc>false</carriesnpc>
<carriessign>false</carriessign>
<carriesstone>false</carriesstone>
<carriesvase>false</carriesvase>
<chat></chat>
<colors>
<index id="0">darkblue</index>
<index id="1">black</index>
<index id="2">darkblue</index>
<index id="3">darkblue</index>
<index id="4">darkblue</index>
<index id="5">black</index>
</colors>
<computerfirstlogin>0</computerfirstlogin>
<darts>5</darts>
<deaths>4</deaths>
<dir>2</dir>
<fullhearts>3</fullhearts>
<glovepower>1</glovepower>
<graalversion>4.2</graalversion>
<gralats>0</gralats>
<guild>scriptering</guild>
<head>head86.png</head>
<headimg>head86.png</headimg>
<headset>-1</headset>
<hearts>3</hearts>
<horseimg></horseimg>
<hp>3</hp>
<id>6</id>
<initialized>false</initialized>
<isfemale>false</isfemale>
<ismale>true</ismale>
<ispaused>false</ispaused>
<joinedclasses></joinedclasses>
<kills>0</kills>
<language>English</language>
<languagedomain>en</languagedomain>
<lastdead>39935339</lastdead>
<level>
<board></board>
<height>64</height>
<initialized>false</initialized>
<isnopkzone>false</isnopkzone>
<ispaused>false</ispaused>
<issparringzone>false</issparringzone>
<joinedclasses></joinedclasses>
<maxlooplimit>10000</maxlooplimit>
<name>osl3.nw</name>
<npcs>
<index id="0">Knight/Test</index>
</npcs>
<players>
<index id="0">JkWhoSaysNi</index>
</players>
<projectiles>
<initialized>false</initialized>
<ispaused>false</ispaused>
<joinedclasses></joinedclasses>
<maxlooplimit>10000</maxlooplimit>
<name>projectiles</name>
<scriptlogmissingfunctions>false</scriptlogmissingfunctions>
<timeout>0</timeout>
</projectiles>
<scriptlogmissingfunctions>false</scriptlogmissingfunctions>
<tilelayercount>1</tilelayercount>
<tilelayers>
<index id="0"></index>
</tilelayers>
<tiles>
<initialized>false</initialized>
<ispaused>false</ispaused>
<joinedclasses></joinedclasses>
<maxlooplimit>10000</maxlooplimit>
<name>obj</name>
<scriptlogmissingfunctions>false</scriptlogmissingfunctions>
<timeout>0</timeout>
</tiles>
<timeout>0</timeout>
<width>64</width>
</level>
<logintime>39935339</logintime>
<maxhp>3</maxhp>
<maxlooplimit>10000</maxlooplimit>
<movementlimit>10001</movementlimit>
<mp>0</mp>
<name>JkWhoSaysNi</name>
<nick>Knight (scriptering)</nick>
<onlinetime>106524</onlinetime>
<pause>false</pause>
<paused>false</paused>
<platform>win</platform>
<playersindex>0</playersindex>
<rating>1500</rating>
<ratingd>350</ratingd>
<rupees>0</rupees>
<scriptlogmissingfunctions>false</scriptlogmissingfunctions>
<shield>no-shield.gif</shield>
<shieldimg>no-shield.gif</shieldimg>
<shieldpower>1</shieldpower>
<sprite>0</sprite>
<sword>sword1.gif</sword>
<swordimg>sword1.gif</swordimg>
<swordpower>1</swordpower>
<timeout>0</timeout>
<trial>false</trial>
<upgradestatus>vip</upgradestatus>
<version>4.2</version>
<weapons>
<index id="0"></index>
<index id="1">ServerConnect</index>
<index id="2">-Player System</index>
<index id="3">*Player/Tailor</index>
<index id="4">*Staff/Admin Tool</index>
<index id="5">*Staff/Boots</index>
<index id="6">*Staff/Tag</index>
<index id="7">-lavadamage</index>
<index id="8">Hoverboard</index>
<index id="9">*Staff/Summon</index>
<index id="10">*ServerConnect</index>
<index id="11">-MessageSys</index>
<index id="12">flamethrower</index>
<index id="13">wavemaker</index>
<index id="14">Knight/Test</index>
</weapons>
<x>35.5</x>
<y>21</y>
<z>0</z>
</JkWhoSaysNi>


I intend to make a XML->Object function too. :)
In valid XML, normally you should have a root container key (for example, let's say <objects>[...]</objects>) to store all your objects in.

Secondly, it doesn't make sense to have your keys named <JkWhoSaysNi>[...]</JkWhoSaysNi>, etc; that just makes it more difficult for an interpreter. Just use a uniform tag name for each object like <object>[...]</object> (the data needed to identify the key is already in your XML tree, and it sometimes makes it easier for interpreters, also makes it possible to render your data in a web browser using XSL or such).

Other than those minor tweaks, it's a nice script.

JkWhoSaysNi
06-04-2007, 04:12 PM
<JkWhoSaysNi> is the root key :P

But you're probably right, just using <object> would be better.

I actually wanted to get the object in-game name for that. Such as "player" so it would be <player>...</player>

but I don't know how to get "player" from the object.. and as far as I know there's no way of getting it.

Skyld
06-04-2007, 05:02 PM
If TServerPlayer is sufficient, then use obj.objecttype().