Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   object -> XML (https://forums.graalonline.com/forums/showthread.php?t=74337)

JkWhoSaysNi 06-01-2007 09:41 PM

object -> XML
 
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:

PHP 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("  <"@vn@">");
        for (i=0;i<=obj.(@vn).size()-1;i++) {
          temp.xml.add("    <index id=\"" @ i @ "\">" @ obj.(@vn)[i] @ "</index>");
  
        }
        temp.xml.add("  </"@vn@">");
      }
      else {
        temp.xml.add("  <"@vn@">");
        temp.tag = vn;
        temp.xml.addarray(generateNode(obj.(@vn)));
        temp.xml.add("  </"@temp.tag@">");
        
      }
    }
    else {
    temp.xml.add("  <"@vn@">"@ obj.(@vn) @"</"@vn@">");
    
    }
  }
  
  
  return temp.xml;
}
function toXML(obj,filename) {
  xml = new TStaticVar();
  xml.add("<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>");
  
xml.add("<"@obj.name@">");
  
xml.addarray(generateNode(obj));
  
xml.add("</"@obj.name@">");
  
xml.savelines(filenamefalse);
  
}

usage:

PHP Code:

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

and sample output for a player object:

PHP Code:

<?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 12:25 AM

Pretty nice!

cbk1994 06-02-2007 01:42 AM

Looks very good. Don't see any purposes though, if you can't load it.

Rapidwolve 06-02-2007 04:35 AM

I don't even know what the point of XML is, nice work though.

Chompy 06-02-2007 04:04 PM

Quote:

Originally Posted by cbkbud (Post 1314297)
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 06: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 06: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 01:05 AM

Quote:

Originally Posted by JkWhoSaysNi (Post 1314571)
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 01: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 02:13 AM

Amazing! Rep++

JkWhoSaysNi 06-03-2007 02:49 AM

Quote:

Originally Posted by Inverness (Post 1314680)
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 05:09 AM

It's a shame this doesn't save flags into it as well =[

JkWhoSaysNi 06-03-2007 07:46 PM

what do you mean by 'flags'? like clientr?

Rapidwolve 06-03-2007 07:53 PM

Nooo, I think he means flags like the american flag

just kidding <3

Deadly_Killer 06-03-2007 08:13 PM

Quote:

Originally Posted by JkWhoSaysNi (Post 1314724)
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

PHP Code:

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


Just a guess though ;).


All times are GMT +2. The time now is 09:54 AM.

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