Page 1 of 2 1 2 LastLast
Results 1 to 10 of 20

Thread: Parsing items_game.txt... in JSON?

  1. #1
    Basic Member
    Join Date
    Dec 2011
    Posts
    81

    Parsing items_game.txt... in JSON?

    Currently trying to parse the items_game.txt in JSON, but it appears that it's not JSON at all... I came across the following link but it did not help. json_decode is still failing to parse out the data.

    http://dev.dota2.com/showthread.php?t=87191

    Any idea how I should parse this? I am not good with regex, otherwise I would update the code.

  2. #2
    Basic Member
    Join Date
    Dec 2013
    Posts
    44
    What language?
    I have class in php, need?

  3. #3
    Basic Member Netshroud's Avatar
    Join Date
    Dec 2011
    Posts
    166
    Quote Originally Posted by Punk View Post
    Currently trying to parse the items_game.txt in JSON, but it appears that it's not JSON at all...
    No, it's VDF (Valve something something, it's called KeyValues in their code). You'll need to find a parser, write a parser, or port Valve's one to PHP. You can find Valve's in the Source SDK.

  4. #4
    Basic Member
    Join Date
    Dec 2013
    Posts
    44

    Smile

    PHP KeyValue parser

    PHP Code:
    <?php
    /**
     * Tree-based Valve KeyValues parser
     * 
     * @author GameConnect
     * @copyright (C)2007-2013 GameConnect.net.  All rights reserved.
     * @link http://www.sourcebans.net
     * 
     * @package sourcebans.components
     * @since 2.0
     */
    class KeyValues
    {
            
    /**
             * @var string Root section name
             */
            
    protected $_name;
            
            
            
    /**
             * Constructor
             * 
             * @param string $name Root section name
             * @param array $data Optional key values data
             */
            
    function __construct($name null$data null)
            {
                    
    $this->_name $name;
                    
                    if(
    is_array($data))
                    {
                            
    parent::__construct($data);
                    }
            }
            
            
    /**
             * Returns a property value
             * 
             * @param string $name The property name
             * @return mixed The property value
             */
            
    public function __get($name)
            {
                    switch(
    $name)
                    {
                            case 
    'name':
                                    return 
    $this->_name;
                    }
            }
            
            
    /**
             * Blocks setting property values
             * 
             * @param string $name The property name
             * @param string $value The property value
             */
            
    public function __set($name$value) {}
            
            
    /**
             * Returns a string representation of the data
             * 
             * @return string The string representation of the data
             */
            
    public function __toString()
            {
                    
    $ret  $this->name "\n{\n";
                    
    $ret .= $this->_build(parent::getArrayCopy());
                    
    $ret .= '}';
                    
                    return 
    $ret;
            }
            
            
            
    /**
             * Loads key values data from a string or file
             * 
             * @param string $string String or file to load
             */
            
    public function load($string)
            {
                    if(
    is_readable($string))
                    {
                            
    $string file_get_contents($string);
                    }
                    
                    
    // Use token_get_all() to easily ignore comments and whitespace
                    
    $tokens token_get_all("<?php\n" $string "\n?>");
                    
    $data   $this->_parse($tokens);
                    
    // Strip root section
                    
    $data   reset($data);
                    return 
    $data;
                    
    //parent::exchangeArray($data);
            
    }
            
            
    /**
             * Saves key values data to file
             * 
             * @param string $file File to save to
             * @return boolean Whether saving was successful
             */
            
    public function save($file)
            {
                    if((
    $fp fopen($file'w+')) === false)
                            return 
    false;
                    
                    
    fwrite($fp$this);
                    
    fclose($fp);
                    return 
    true;
            }
            
            
    /**
             * Serializes key values data and root section name
             */
            
    public function serialize()
            {
                    return 
    serialize(array(
                            
    'data' => parent::serialize(),
                            
    'name' => $this->_name,
                    ));
            }
            
            
    /**
             * Returns an array representation of the data
             * 
             * @return array The array representation of the data
             */
            
    public function toArray()
            {
                    return 
    parent::getArrayCopy();
            }
            
            
    /**
             * Unserializes key values data and root section name
             */
            
    public function unserialize($data)
            {
                    
    $data        unserialize($data);
                    
    $this->_name $data['name'];
                    
                    
    parent::unserialize($data['data']);
            }
            
            
            
    /**
             * Recursively builds key values string
             * 
             * @param array $data Key values data to write
             * @param integer $level Optional level to use for indenting
             */
            
    private function _build($data$level null)
            {
                    
    // Default level to 0
                    
    if(!is_numeric($level))
                    {
                            
    $level 0;
                    }
                    
                    
    $indent str_repeat("\t"$level 1);
                    
    $ret    '';
                    
                    foreach(
    $data as $key => $value)
                    {
                            if(
    is_array($value))
                            {
                                    
    reset($value);
                                    
    // If array is numerical, write key sub-value pairs
                                    
    if(is_int(key($value)))
                                    {
                                            foreach(
    $value as $sub_value)
                                            {
                                                    
    $ret .= sprintf("%s\"%s\"\t\"%s\"\n",
                                                            
    $indent$key$sub_value);
                                            }
                                    }
                                    
    // Otherwise, recursively write section
                                    
    else
                                    {
                                            
    $ret .= sprintf("%s\"%s\"\n%s{\n",
                                                    
    $indent$key$indent);
                                            
                                            
    $ret .= $this->_build($value$level 1);
                                            
    $ret .= $indent "}\n";
                                    }
                            }
                            
    // Write key value pair
                            
    else
                            {
                                    
    $ret .= sprintf("%s\"%s\"\t\"%s\"\n",
                                            
    $indent$key$value);
                            }
                    }
                    
                    return 
    $ret;
            }
            
            
    /**
             * Recursively parses key values data from tokens
             * 
             * @param array $tokens Tokens received from token_get_all()
             */
            
    private function _parse(&$tokens)
            {
                    
    $data = array();
                    
    $key  null;
                    
                    
    // Use each() so the array cursor is also advanced
                    // when the function is called recursively
                    
    while(list(, $token) = each($tokens))
                    {
                            
    // New section
                            
    if($token == '{')
                            {
                                    
    // Recursively parse section
                                    
    $data[$key] = $this->_parse($tokens);
                                    
    $key        null;
                            }
                            
    // End section
                            
    else if($token == '}')
                            {
                                    return 
    $data;
                            }
                            
    // Key or value
                            
    else
                            {
                                    
    $value $token[1];
                                    switch(
    $token[0])
                                    {
                                            case 
    T_CONSTANT_ENCAPSED_STRING:
                                                    
    // Strip surrounding quotes, then parse as a string
                                                    
    $value substr($value1, -1);
                                            case 
    T_STRING:
                                                    
    // If key is not set, store
                                                    
    if(is_null($key))
                                                    {
                                                            
    $key $value;
                                                    }
                                                    
    // Otherwise, it's a key value pair
                                                    
    else
                                                    {
                                                            
    // If value is already set, treat as an array
                                                            // to allow multiple values per key
                                                            
    if(isset($data[$key]))
                                                            {
                                                                    
    // If value is not an array, cast
                                                                    
    if(!is_array($data[$key]))
                                                                    {
                                                                            
    $data[$key] = (array)$data[$key];
                                                                    }
                                                                    
                                                                    
    // Add value to array
                                                                    
    $data[$key][] = $value;
                                                            }
                                                            
    // Otherwise, store key value pair
                                                            
    else
                                                            {
                                                                    
    $data[$key] = $value;
                                                            }
                                                            
                                                            
    $key null;
                                                    }
                                    }
                            }
                    }
                    
                    return 
    $data;
            }
    }
    Use

    PHP Code:
    $kv = new KeyValues;
    $VDFitems $kv->load(file_get_contents($items['result']['items_game_url']);); 

  5. #5
    Basic Member
    Join Date
    Dec 2011
    Posts
    81
    @kpachbiu: Cheers!

  6. #6
    Basic Member
    Join Date
    Nov 2013
    Posts
    15

    Question

    I could give an example of how to use it where it gets the url and keyapi.

    Sorry but I'm new at this

  7. #7
    Basic Member
    Join Date
    Nov 2013
    Posts
    15
    anyone?

  8. #8

  9. #9
    Basic Member
    Join Date
    Nov 2013
    Posts
    15

    Question

    Quote Originally Posted by kpachbiu View Post
    PHP KeyValue parser

    PHP Code:
    <?php
    /**
     * Tree-based Valve KeyValues parser
     * 
     * @author GameConnect
     * @copyright (C)2007-2013 GameConnect.net.  All rights reserved.
     * @link http://www.sourcebans.net
     * 
     * @package sourcebans.components
     * @since 2.0
     */
    class KeyValues
    {
            
    /**
             * @var string Root section name
             */
            
    protected $_name;
            
            
            
    /**
             * Constructor
             * 
             * @param string $name Root section name
             * @param array $data Optional key values data
             */
            
    function __construct($name null$data null)
            {
                    
    $this->_name $name;
                    
                    if(
    is_array($data))
                    {
                            
    parent::__construct($data);
                    }
            }
            
            
    /**
             * Returns a property value
             * 
             * @param string $name The property name
             * @return mixed The property value
             */
            
    public function __get($name)
            {
                    switch(
    $name)
                    {
                            case 
    'name':
                                    return 
    $this->_name;
                    }
            }
            
            
    /**
             * Blocks setting property values
             * 
             * @param string $name The property name
             * @param string $value The property value
             */
            
    public function __set($name$value) {}
            
            
    /**
             * Returns a string representation of the data
             * 
             * @return string The string representation of the data
             */
            
    public function __toString()
            {
                    
    $ret  $this->name "\n{\n";
                    
    $ret .= $this->_build(parent::getArrayCopy());
                    
    $ret .= '}';
                    
                    return 
    $ret;
            }
            
            
            
    /**
             * Loads key values data from a string or file
             * 
             * @param string $string String or file to load
             */
            
    public function load($string)
            {
                    if(
    is_readable($string))
                    {
                            
    $string file_get_contents($string);
                    }
                    
                    
    // Use token_get_all() to easily ignore comments and whitespace
                    
    $tokens token_get_all("<?php\n" $string "\n?>");
                    
    $data   $this->_parse($tokens);
                    
    // Strip root section
                    
    $data   reset($data);
                    return 
    $data;
                    
    //parent::exchangeArray($data);
            
    }
            
            
    /**
             * Saves key values data to file
             * 
             * @param string $file File to save to
             * @return boolean Whether saving was successful
             */
            
    public function save($file)
            {
                    if((
    $fp fopen($file'w+')) === false)
                            return 
    false;
                    
                    
    fwrite($fp$this);
                    
    fclose($fp);
                    return 
    true;
            }
            
            
    /**
             * Serializes key values data and root section name
             */
            
    public function serialize()
            {
                    return 
    serialize(array(
                            
    'data' => parent::serialize(),
                            
    'name' => $this->_name,
                    ));
            }
            
            
    /**
             * Returns an array representation of the data
             * 
             * @return array The array representation of the data
             */
            
    public function toArray()
            {
                    return 
    parent::getArrayCopy();
            }
            
            
    /**
             * Unserializes key values data and root section name
             */
            
    public function unserialize($data)
            {
                    
    $data        unserialize($data);
                    
    $this->_name $data['name'];
                    
                    
    parent::unserialize($data['data']);
            }
            
            
            
    /**
             * Recursively builds key values string
             * 
             * @param array $data Key values data to write
             * @param integer $level Optional level to use for indenting
             */
            
    private function _build($data$level null)
            {
                    
    // Default level to 0
                    
    if(!is_numeric($level))
                    {
                            
    $level 0;
                    }
                    
                    
    $indent str_repeat("\t"$level 1);
                    
    $ret    '';
                    
                    foreach(
    $data as $key => $value)
                    {
                            if(
    is_array($value))
                            {
                                    
    reset($value);
                                    
    // If array is numerical, write key sub-value pairs
                                    
    if(is_int(key($value)))
                                    {
                                            foreach(
    $value as $sub_value)
                                            {
                                                    
    $ret .= sprintf("%s\"%s\"\t\"%s\"\n",
                                                            
    $indent$key$sub_value);
                                            }
                                    }
                                    
    // Otherwise, recursively write section
                                    
    else
                                    {
                                            
    $ret .= sprintf("%s\"%s\"\n%s{\n",
                                                    
    $indent$key$indent);
                                            
                                            
    $ret .= $this->_build($value$level 1);
                                            
    $ret .= $indent "}\n";
                                    }
                            }
                            
    // Write key value pair
                            
    else
                            {
                                    
    $ret .= sprintf("%s\"%s\"\t\"%s\"\n",
                                            
    $indent$key$value);
                            }
                    }
                    
                    return 
    $ret;
            }
            
            
    /**
             * Recursively parses key values data from tokens
             * 
             * @param array $tokens Tokens received from token_get_all()
             */
            
    private function _parse(&$tokens)
            {
                    
    $data = array();
                    
    $key  null;
                    
                    
    // Use each() so the array cursor is also advanced
                    // when the function is called recursively
                    
    while(list(, $token) = each($tokens))
                    {
                            
    // New section
                            
    if($token == '{')
                            {
                                    
    // Recursively parse section
                                    
    $data[$key] = $this->_parse($tokens);
                                    
    $key        null;
                            }
                            
    // End section
                            
    else if($token == '}')
                            {
                                    return 
    $data;
                            }
                            
    // Key or value
                            
    else
                            {
                                    
    $value $token[1];
                                    switch(
    $token[0])
                                    {
                                            case 
    T_CONSTANT_ENCAPSED_STRING:
                                                    
    // Strip surrounding quotes, then parse as a string
                                                    
    $value substr($value1, -1);
                                            case 
    T_STRING:
                                                    
    // If key is not set, store
                                                    
    if(is_null($key))
                                                    {
                                                            
    $key $value;
                                                    }
                                                    
    // Otherwise, it's a key value pair
                                                    
    else
                                                    {
                                                            
    // If value is already set, treat as an array
                                                            // to allow multiple values per key
                                                            
    if(isset($data[$key]))
                                                            {
                                                                    
    // If value is not an array, cast
                                                                    
    if(!is_array($data[$key]))
                                                                    {
                                                                            
    $data[$key] = (array)$data[$key];
                                                                    }
                                                                    
                                                                    
    // Add value to array
                                                                    
    $data[$key][] = $value;
                                                            }
                                                            
    // Otherwise, store key value pair
                                                            
    else
                                                            {
                                                                    
    $data[$key] = $value;
                                                            }
                                                            
                                                            
    $key null;
                                                    }
                                    }
                            }
                    }
                    
                    return 
    $data;
            }
    }
    Use

    PHP Code:
    $kv = new KeyValues;
    $VDFitems $kv->load(file_get_contents($items['result']['items_game_url']);); 


    I'm very new and do not understand how this goes.
    anyone can help me please.

    I could make an example of results

  10. #10
    Basic Member MuppetMaster42's Avatar
    Join Date
    Nov 2011
    Location
    Australia
    Posts
    585
    the 'url' for the file is the local file in the dota folder.
    do a search in the forum and you'll find it's path pretty quickly.

    as for how it works - setup your PHP, make sure the class gets loaded, then put the following code in:

    Code:
    $kv = new KeyValues; 
    $VDFitems = $kv->load(<location of items_game.txt>);
    var_dump($VDFitems);
    run the script and bam, you have everything you need to see how it works.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •