Class: DemoReaderWarsow
- Inherits:
-
Object
- Object
- DemoReaderWarsow
- Defined in:
- lib/demo_reader_warsow.rb
Instance Attribute Summary collapse
-
#basegamedir ⇒ Object
readonly
Returns the value of attribute basegamedir.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#game ⇒ Object
readonly
Returns the value of attribute game.
-
#gamedir ⇒ Object
readonly
Returns the value of attribute gamedir.
-
#gamemode ⇒ Object
readonly
Returns the value of attribute gamemode.
-
#mapname ⇒ Object
readonly
Returns the value of attribute mapname.
-
#player ⇒ Object
readonly
Returns the value of attribute player.
-
#playernames ⇒ Object
readonly
Returns the value of attribute playernames.
-
#scoreboards ⇒ Object
readonly
Returns the value of attribute scoreboards.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
-
#valid ⇒ Object
readonly
Returns the value of attribute valid.
-
#version ⇒ Object
readonly
Returns the value of attribute version.
Instance Method Summary collapse
- #detect_version(file) ⇒ Object
- #gamemode_wd11(file_content) ⇒ Object
- #init ⇒ Object
-
#initialize(filename) ⇒ DemoReaderWarsow
constructor
A new instance of DemoReaderWarsow.
- #time_in_msec ⇒ Object
Constructor Details
#initialize(filename) ⇒ DemoReaderWarsow
Returns a new instance of DemoReaderWarsow.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/demo_reader_warsow.rb', line 5 def initialize(filename) @filename = filename @game = "Warsow" @version = -1 @mapname = nil @time = nil @time_in_msec = nil @playernames = [] @scoreboards = [] @gamemode = nil @player = nil @basegamedir = nil @gamedir = nil @valid = false self.init() end |
Instance Attribute Details
#basegamedir ⇒ Object (readonly)
Returns the value of attribute basegamedir.
2 3 4 |
# File 'lib/demo_reader_warsow.rb', line 2 def basegamedir @basegamedir end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
2 3 4 |
# File 'lib/demo_reader_warsow.rb', line 2 def filename @filename end |
#game ⇒ Object (readonly)
Returns the value of attribute game.
2 3 4 |
# File 'lib/demo_reader_warsow.rb', line 2 def game @game end |
#gamedir ⇒ Object (readonly)
Returns the value of attribute gamedir.
2 3 4 |
# File 'lib/demo_reader_warsow.rb', line 2 def gamedir @gamedir end |
#gamemode ⇒ Object (readonly)
Returns the value of attribute gamemode.
2 3 4 |
# File 'lib/demo_reader_warsow.rb', line 2 def gamemode @gamemode end |
#mapname ⇒ Object (readonly)
Returns the value of attribute mapname.
2 3 4 |
# File 'lib/demo_reader_warsow.rb', line 2 def mapname @mapname end |
#player ⇒ Object (readonly)
Returns the value of attribute player.
2 3 4 |
# File 'lib/demo_reader_warsow.rb', line 2 def player @player end |
#playernames ⇒ Object (readonly)
Returns the value of attribute playernames.
2 3 4 |
# File 'lib/demo_reader_warsow.rb', line 2 def playernames @playernames end |
#scoreboards ⇒ Object (readonly)
Returns the value of attribute scoreboards.
2 3 4 |
# File 'lib/demo_reader_warsow.rb', line 2 def scoreboards @scoreboards end |
#time ⇒ Object (readonly)
Returns the value of attribute time.
2 3 4 |
# File 'lib/demo_reader_warsow.rb', line 2 def time @time end |
#valid ⇒ Object (readonly)
Returns the value of attribute valid.
2 3 4 |
# File 'lib/demo_reader_warsow.rb', line 2 def valid @valid end |
#version ⇒ Object (readonly)
Returns the value of attribute version.
2 3 4 |
# File 'lib/demo_reader_warsow.rb', line 2 def version @version end |
Instance Method Details
#detect_version(file) ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/demo_reader_warsow.rb', line 179 def detect_version(file) # try to detect version 8, 9 or 10 # file.pos = 0 file.pos += 4 # skip 4 byte message length file.pos += 1 # skip 1 byte "svc_serverdata" # version # reads 4 bytes and decodes the little-endian format version = file.read(4).unpack('V')[0] return version if [8, 9, 10].include? version # try to detect version 11 # file.pos = 0 file.pos += 1 # skip 1 byte "svc_demoinfo" file.pos += 4 # skip 4 byte "initial server time" file.pos += 4 # skip 4 byte "demo duration in milliseconds" file.pos += 4 # skip 4 byte "file offset at which the final -1 was written" file.pos += 1 # skip 1 byte "svc_serverdata" file.pos += 4 # skip 4 byte "UNKNOWN! - not documented in source, but is needed to fit demos!" # version # reads 4 bytes and decodes the little-endian format version = file.read(4).unpack('V')[0] return version if version == 11 nil # could not detect any known version end |
#gamemode_wd11(file_content) ⇒ Object
229 230 231 232 233 |
# File 'lib/demo_reader_warsow.rb', line 229 def gamemode_wd11(file_content) if file_content =~ /cs 12 "(.*?)"/ $1 end end |
#init ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/demo_reader_warsow.rb', line 25 def init file = File.new(@filename, 'r') return if file.stat.size < 10 @version = detect_version(file) # just support some wd* file versions return unless [8, 9, 10, 11].include? @version # skip 4 bytes "svs.spawncount" file.pos += 4 # skip 2 bytes "cl.snapFrameTime" file.pos += 2 if [10, 11].include? @version # skip 4 bytes "cl.baseServerTime" file.pos += 4 if @version == 10 # FS_BaseGameDirectory @basegamedir = file.gets("\0").chop # FS_GameDirectory @gamedir = file.gets("\0").chop # skip short "playernum" file.pos += 2 # mapname @mapname = file.gets("\0").chop.downcase content = file.gets nil file.close # remove illegal utf8 chars that crashes ruby 1.9.x # if RUBY_VERSION >= "1.9" content.encode!("ISO-8859-1", :invalid => :replace, :undef => :replace) # iso is just fine (no other chars expected) end # detect scoreboard regex = /scb \"([^\"]+)/ matchdata = regex.match(content) while matchdata @scoreboards.push matchdata[1] matchdata = regex.match(matchdata.post_match) end # detect game mode if @version == 11 @gamemode = gamemode_wd11(content) else regex = /&([^ ]+) / gamemodes = [] @scoreboards.each do |scb| matchdata = regex.match(scb) gamemodes.push matchdata[1] end gamemodes.uniq! if gamemodes.length.zero? @gamemode = 'unknown' else if gamemodes.length == 1 @gamemode = gamemodes.first @gamemode.chop! if ['races', 'dms', 'ctfs'].include? @gamemode else @gamemode = "multiple gamemodes found: #{gamemodes.join(', ')}!" end end end # detect time by sent message string with time from server if ['race', 'unknown'].include?(@gamemode) matches = [] regex = /(server record|race(?: ?[\d#\^]*) finished\s*)(?:!.*(?:current|times\^7 ))?:[0-9:\. ]* (\d+):(\d+)\.(\d+)/im matchdata = regex.match(content) while matchdata matches.push("%02d:%02d.%03d" % [$2,$3,$4].map(&:to_i)) matchdata = regex.match(matchdata.post_match) end if matches.length > 0 @time = matches.sort.first @gamemode = 'race' if @gamemode == 'unknown' end # set gamemode to freestyle when no time exists # @gamemode = 'freestyle' if time_in_msec.to_i <= 0 end #detect all player names matches = [] regex = /cs ([0-9]+) \"\\name\\([^\0]*)\\hand/ rest_content = content matchdata = regex.match(rest_content) first_no = matchdata[1].to_i while matchdata # damit werden doppelte eintraege durch den letzten aktuellen ueberschrieben matches[matchdata[1].to_i - first_no] = matchdata[2] rest_content = matchdata.post_match matchdata = regex.match(rest_content) end if matches.length > 0 # save player names only @playernames = [] matches.each_with_index { |player, number| #playernames.push [number, player].join(': ') @playernames[number] = player } end # detect player playernames = @playernames.compact.sort.uniq if playernames.length == 1 @player = playernames.first else if @time && @gamemode == 'race' && !@scoreboards.empty? && !playernames.empty? min, sec, msec = @time.scan(/^([0-9]+):([0-9]+)\.([0-9]+)$/).flatten.map { |x| x.to_i } min = 9 if min > 9 # scoreboard does not support race times > 9:55:999; max minute value is 9 ! t = "#{min}#{sec}#{msec}" regex = Regexp.new("&p ([0-9]+) #{t}") playerids = @scoreboards.join('').scan(regex).flatten.uniq if playerids.length == 1 @player = @playernames[playerids.first.to_i] end end end @valid = true end |
#time_in_msec ⇒ Object
218 219 220 221 222 223 224 225 226 |
# File 'lib/demo_reader_warsow.rb', line 218 def time_in_msec return @time_in_msec unless @time_in_msec.nil? # time str to int if @time.kind_of? String min, sec, msec = @time.scan(/^([0-9]+):([0-9]+)\.([0-9]+)$/).flatten.map { |x| x.to_i } @time_in_msec = msec + sec * 1000 + min * 60 * 1000 end end |