class Demoman::DemoFile

Parses and provides data about a Half life demo file.

@see developer.valvesoftware.com/wiki/DEM_Format

Attributes

demo_protocol[R]

This is the demo protocol version. This is almost always 3

@return [Integer]

duration[R]

This is the duration of the demo, in seconds.

@return [Float]

frames[R]

The number of frames in the demo

@return [Integer]

game_dir[R]

The game directory as reported by the server. Examples: tf, dod, etc.

@return [String]

map[R]

The current map on the server

@return [String]

network_protocol[R]

This is the network protocol, which varies by game.

@return [Integer]

player_name[R]

The name of the player recording the demo.

If recorded by SourceTV it will be the name of the SourceTV player.

@return [String]

server_address[R]

If recorded by a client, then this will be the IPAddress:Port of the server.

If recorded by SourceTV then this will be the hostname of the server.

@return [String]

sign_on_length[R]

Length of the signon data (Init for first frame)

@return [Integer]

ticks[R]

This is the number of server ticks that occurred. This is roughly the tick rate of the server multiplied by the duration.

@return [Integer]

type[R]

The type of demo. This is usually HL2DEMO

@return [String]

Public Class Methods

new(file=nil) click to toggle source

Initialize a {DemoFile} object. If the file parameter is provided, then the file is read and parsed.

@param file [String] path to a demo file

# File lib/demoman/demo_file.rb, line 76
def initialize(file=nil)
  @demodata = nil
  unless file.nil?
    io = File.new(file, "r")
    data = io.sysread(4096)
    parse_data data
  end

end

Public Instance Methods

parse_data(data) click to toggle source

Parses the demo file header.

@param data [String] the raw demo file header data.

@return [void]

# File lib/demoman/demo_file.rb, line 91
def parse_data(data)
  @demodata = nil
  #                        0 1 2 3    4    5    6    7 8 9 10
  @demodata = data.unpack("A8/I/I/A260/A260/A260/A260/f/I/I/I/")
  @type = @demodata[0]
  @demo_protocol = @demodata[1]
  @network_protocol = @demodata[2]
  @server_address = @demodata[3]
  @player_name = @demodata[4]
  @map = @demodata[5]
  @game_dir = @demodata[6]

  @duration = @demodata[7]
  @ticks = @demodata[8]
  @frames = @demodata[9]
  @sign_on_length = @demodata[10]
  
  return self
end
parsed?() click to toggle source

Returns if the demo has been parsed. Note: This does not check if the demo was valid.

@see valid?

@since 2.0.2

@return [Boolean] true

# File lib/demoman/demo_file.rb, line 120
def parsed?
  !@demodata.nil?
end
valid?() click to toggle source

Returns whether or not the parsed demo is considered valid.

Note: This is a somewhat educated guess. As long as the values appear to be within a specific range, then the demo is assumed to be valid.

@since 2.0.2

@return [Boolean]

# File lib/demoman/demo_file.rb, line 133
def valid?
  return false unless self.parsed?

  return false unless @demodata.compact.size == 11

  return false unless @type.eql?('HL2DEMO')

  true
end