Class: Demoman::DemoFile

Inherits:
Object
  • Object
show all
Defined in:
lib/demoman/demo_file.rb

Overview

Parses and provides data about a Half life demo file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = nil) ⇒ DemoFile

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

Parameters:

  • file (String) (defaults to: nil)

    path to a demo file



76
77
78
79
80
81
82
83
84
# 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

Instance Attribute Details

#demo_protocolInteger (readonly)

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

Returns:

  • (Integer)


39
40
41
# File 'lib/demoman/demo_file.rb', line 39

def demo_protocol
  @demo_protocol
end

#durationFloat (readonly)

This is the duration of the demo, in seconds.

Returns:

  • (Float)


54
55
56
# File 'lib/demoman/demo_file.rb', line 54

def duration
  @duration
end

#framesInteger (readonly)

The number of frames in the demo

Returns:

  • (Integer)


65
66
67
# File 'lib/demoman/demo_file.rb', line 65

def frames
  @frames
end

#game_dirString (readonly)

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

Returns:

  • (String)


33
34
35
# File 'lib/demoman/demo_file.rb', line 33

def game_dir
  @game_dir
end

#mapString (readonly)

The current map on the server

Returns:

  • (String)


27
28
29
# File 'lib/demoman/demo_file.rb', line 27

def map
  @map
end

#network_protocolInteger (readonly)

This is the network protocol, which varies by game.

Returns:

  • (Integer)


44
45
46
# File 'lib/demoman/demo_file.rb', line 44

def network_protocol
  @network_protocol
end

#player_nameString (readonly)

The name of the player recording the demo.

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

Returns:

  • (String)


22
23
24
# File 'lib/demoman/demo_file.rb', line 22

def player_name
  @player_name
end

#server_addressString (readonly)

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.

Returns:

  • (String)


15
16
17
# File 'lib/demoman/demo_file.rb', line 15

def server_address
  @server_address
end

#sign_on_lengthInteger (readonly)

Length of the signon data (Init for first frame)

Returns:

  • (Integer)


70
71
72
# File 'lib/demoman/demo_file.rb', line 70

def sign_on_length
  @sign_on_length
end

#ticksInteger (readonly)

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

Returns:

  • (Integer)


60
61
62
# File 'lib/demoman/demo_file.rb', line 60

def ticks
  @ticks
end

#typeString (readonly)

The type of demo. This is usually HL2DEMO

Returns:

  • (String)


49
50
51
# File 'lib/demoman/demo_file.rb', line 49

def type
  @type
end

Instance Method Details

#parse_data(data) ⇒ void

This method returns an undefined value.

Parses the demo file header.

Parameters:

  • data (String)

    the raw demo file header data.



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# 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?Boolean

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

Returns:

  • (Boolean)

    true

See Also:

Since:

  • 2.0.2



120
121
122
# File 'lib/demoman/demo_file.rb', line 120

def parsed?
  !@demodata.nil?
end

#valid?Boolean

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.

Returns:

  • (Boolean)

Since:

  • 2.0.2



133
134
135
136
137
138
139
140
141
# 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