Page 2 of 2 FirstFirst 1 2
Results 11 to 20 of 20

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

  1. #11
    Basic Member
    Join Date
    Nov 2013
    Posts
    15
    Quote Originally Posted by MuppetMaster42 View Post
    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.
    when running

    PHP Code:
    $kv = new KeyValues
    $VDFitems $kv->load("http://cdn.dota2.com/apps/570/scripts/items/items_game.69b71364e0c236b30f4b369c64006d6815d79dcb.txt");
    var_dump($VDFitems); 
    response I have

    PHP Code:
    bool(false
    any suggestions

  2. #12
    Basic Member Netshroud's Avatar
    Join Date
    Dec 2011
    Posts
    166
    If you read the class, you'll see that it checks is_readable to see if the file exists locally before reading it. You'll need to download items_game.txt and then pass KeyValues::load a path to items_game on the local filesystem.

    I'll warn you that it's probably not the most efficient way to do it and that you may run into further issues. I couldn't actually get it to load with the following error:

    Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 6 bytes) in /private/tmp/dota_kv_php_test.php on line 87

  3. #13
    Basic Member
    Join Date
    Dec 2013
    Posts
    44
    Quote Originally Posted by soneric View Post
    when running

    PHP Code:
    $kv = new KeyValues
    $VDFitems $kv->load("http://cdn.dota2.com/apps/570/scripts/items/items_game.69b71364e0c236b30f4b369c64006d6815d79dcb.txt");
    var_dump($VDFitems); 
    response I have

    PHP Code:
    bool(false
    any suggestions
    Use google.ru - "file_get_contents return false, why?"

    Check settings php!
    Probably allow_url_fopen in your php.ini: http://php.net/manual/en/filesystem.configuration.php

    Needs to be allowed.

  4. #14
    Basic Member
    Join Date
    Dec 2013
    Posts
    44
    Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 6 bytes) in /private/tmp/dota_kv_php_test.php on line 87

    Check php settings!
    memory_limit need increase (512Mb - maybe enough)

  5. #15
    Basic Member
    Join Date
    Dec 2013
    Posts
    44
    I use KeyParser

    PHP Code:
            include('classes/kvparser/kvparser.php');
            
    $current file_get_contents($items['result']['items_game_url']);
            if (
    $current) { //Файл с описанием вещей загрузился
                   
    file_put_contents('tmp/item_vdf.txt'$current);
                   
    $kv = new KeyValues;
                   
    $VDFitems $kv->load('tmp/item_vdf.txt');
            } 
    Last edited by kpachbiu; 01-07-2014 at 04:09 AM.

  6. #16
    Basic Member Netshroud's Avatar
    Join Date
    Dec 2011
    Posts
    166
    Quote Originally Posted by kpachbiu View Post
    Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 6 bytes) in /private/tmp/dota_kv_php_test.php on line 87

    Check php settings!
    memory_limit need increase (512Mb - maybe enough)
    Yes, but do you really want to use a class that uses over 128MB of memory to parse a 6.5MB text file? Especially on a web server or similar?

  7. #17
    No, of course not. I used 512 MB and set_time_limit(0) to parse the file. VDF is a really strange data format. I don't know why it exists. You can run VDF parser one time to decode VDF data and then encode it as JSON. JSON works much faster.

  8. #18
    how i use Python

  9. #19
    Basic Member
    Join Date
    Dec 2011
    Posts
    30
    hey guys, when i am using the KeyValue parser it's returning me a blank page...

    when i tried to debug it i foung that its crashing at the load() function where it execute $tokens = token_get_all("<?php\n" . $string . "\n?>");

    die('dead'); before this line works but after the line it does not display.... any ideas???

    fyi the items txt is arounf 14mb from the GetSchemaUrl...

    helpppp

  10. #20
    Volunteer Moderator CvP's Avatar
    Join Date
    Sep 2011
    Posts
    4,334
    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']);); 


    Thank you.

Posting Permissions

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