Search code examples
phpparsingreplay

How do I look into a Starcraft 2 replay?


I am interested in building a parser for my own enjoyment using PHP. What do I need to know? What suggestions would you have for me? How do I even open a Starcraft 2 replay using PHP?


Solution

  • The SC replay file is actually an MPQ archive file. This MPQ archive contains a few different files (like a .zip file).

    Inside this archive are separate files for each of the types of data in the MPQ archive. (For example there is one file for game events and another for UI events).

    There is a fair amount of documentation online about how to deal with MPQ files. Now, the individual files within the MPQ is a bit trickier.

    If you want to get the information from the replay (who the players were and what map they played on) you can use these tools. (I'm assuming a Unix like web server).

    1) Download and build libmpq and mpq-tools (https://libmpq.org/ )

    2) Run the following scripts

    You can run these from a system() call and then run a few split commands to get the players and the race.

    Save this as info.sh. Run it like a command shell and pass in the replay file as an argument.

    #!/bin/bash
    
    # Save this file as info.sh
    
    # This extracts the individual files from the MPQ archive (the replay
    # file)
    
    
    mpq-extract -e $1 > /dev/null
    cat file000000.xxx | strings | ruby info.rb
    

    Here is a ruby script. Save this as info.rb

    # This *kinda* extracts the file info from a header file.  I don't
    # really know how it works yet, so I'm just extracting strings.
    #
    # Save this file as info.rb
    
    lines = STDIN.readlines
    puts "%s:%s|%s:%s" % [(lines[0].strip), (lines[1].strip), (lines[2].strip), (lines[3].strip)]
    

    Hope this helps!