Class: Demoman::DemoFile
- Inherits:
-
Object
- Object
- Demoman::DemoFile
- Defined in:
- lib/demoman/demo_file.rb
Overview
Parses and provides data about a Half life demo file.
Instance Attribute Summary collapse
-
#demo_protocol ⇒ Integer
readonly
This is the demo protocol version.
-
#duration ⇒ Float
readonly
This is the duration of the demo, in seconds.
-
#frames ⇒ Integer
readonly
The number of frames in the demo.
-
#game_dir ⇒ String
readonly
The game directory as reported by the server.
-
#map ⇒ String
readonly
The current map on the server.
-
#network_protocol ⇒ Integer
readonly
This is the network protocol, which varies by game.
-
#player_name ⇒ String
readonly
The name of the player recording the demo.
-
#server_address ⇒ String
readonly
If recorded by a client, then this will be the
IPAddress:Port
of the server. -
#sign_on_length ⇒ Integer
readonly
Length of the signon data (Init for first frame).
-
#ticks ⇒ Integer
readonly
This is the number of server ticks that occurred.
-
#type ⇒ String
readonly
The type of demo.
Instance Method Summary collapse
-
#initialize(file = nil) ⇒ DemoFile
constructor
Initialize a DemoFile object.
-
#parse_data(data) ⇒ void
Parses the demo file header.
-
#parsed? ⇒ Boolean
Returns if the demo has been parsed.
-
#valid? ⇒ Boolean
Returns whether or not the parsed demo is considered valid.
Constructor Details
#initialize(file = nil) ⇒ DemoFile
Initialize a Demoman::DemoFile object. If the file
parameter is provided, then the file is read and parsed.
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_protocol ⇒ Integer (readonly)
This is the demo protocol version. This is almost always 3
39 40 41 |
# File 'lib/demoman/demo_file.rb', line 39 def demo_protocol @demo_protocol end |
#duration ⇒ Float (readonly)
This is the duration of the demo, in seconds.
54 55 56 |
# File 'lib/demoman/demo_file.rb', line 54 def duration @duration end |
#frames ⇒ Integer (readonly)
The number of frames in the demo
65 66 67 |
# File 'lib/demoman/demo_file.rb', line 65 def frames @frames end |
#game_dir ⇒ String (readonly)
The game directory as reported by the server. Examples: tf
, dod
, etc.
33 34 35 |
# File 'lib/demoman/demo_file.rb', line 33 def game_dir @game_dir end |
#map ⇒ String (readonly)
The current map on the server
27 28 29 |
# File 'lib/demoman/demo_file.rb', line 27 def map @map end |
#network_protocol ⇒ Integer (readonly)
This is the network protocol, which varies by game.
44 45 46 |
# File 'lib/demoman/demo_file.rb', line 44 def network_protocol @network_protocol end |
#player_name ⇒ String (readonly)
The name of the player recording the demo.
If recorded by SourceTV it will be the name of the SourceTV player.
22 23 24 |
# File 'lib/demoman/demo_file.rb', line 22 def player_name @player_name end |
#server_address ⇒ String (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.
15 16 17 |
# File 'lib/demoman/demo_file.rb', line 15 def server_address @server_address end |
#sign_on_length ⇒ Integer (readonly)
Length of the signon data (Init for first frame)
70 71 72 |
# File 'lib/demoman/demo_file.rb', line 70 def sign_on_length @sign_on_length end |
#ticks ⇒ Integer (readonly)
This is the number of server ticks that occurred. This is roughly the tick rate of the server multiplied by the duration.
60 61 62 |
# File 'lib/demoman/demo_file.rb', line 60 def ticks @ticks end |
#type ⇒ String (readonly)
The type of demo. This is usually HL2DEMO
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.
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.
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.
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 |