Module: Lich::Gemstone::Game
- Defined in:
- lib/games.rb
Constant Summary collapse
- @@socket =
nil
- @@mutex =
Mutex.new
- @@last_recv =
nil
- @@thread =
nil
- @@buffer =
Lich::Common::SharedBuffer.new
- @@_buffer =
Lich::Common::SharedBuffer.new
- @@autostarted =
false
- @@cli_scripts =
false
- @@infomon_loaded =
false
- @@room_number_after_ready =
false
- @@last_id_shown_room_window =
0
Class Method Summary collapse
-
._buffer ⇒ Buffer
Returns the internal buffer.
-
._gets ⇒ String
Reads a line from the internal buffer.
-
._puts(str) ⇒ nil
Sends a string to the game socket in a thread-safe manner.
-
.buffer ⇒ Buffer
Returns the game buffer.
-
.clean_dr_serverstring(server_string) ⇒ String
Cleans the server string for DragonRealms by removing superfluous tags and fixing encoding issues.
-
.clean_gs_serverstring(server_string) ⇒ String
Cleans the server string for Gemstone by replacing specific tags.
-
.close ⇒ nil
Closes the game socket and kills the associated thread if they exist.
-
.closed? ⇒ Boolean
Checks if the game socket is closed.
-
.gets ⇒ String
Reads a line from the game buffer.
-
.open(host, port) ⇒ void
Opens a connection to the game server.
-
.puts(str) ⇒ nil
Sends a formatted string to the game socket and updates the last upstream message.
-
.thread ⇒ Thread
Returns the current thread associated with the Game.
Class Method Details
._buffer ⇒ Buffer
Returns the internal buffer.
511 512 513 |
# File 'lib/games.rb', line 511 def Game._buffer @@_buffer end |
._gets ⇒ String
Reads a line from the internal buffer.
504 505 506 |
# File 'lib/games.rb', line 504 def Game._gets @@_buffer.gets end |
._puts(str) ⇒ nil
Sends a string to the game socket in a thread-safe manner.
460 461 462 463 464 |
# File 'lib/games.rb', line 460 def Game._puts(str) @@mutex.synchronize { @@socket.puts(str) } end |
.buffer ⇒ Buffer
Returns the game buffer.
497 498 499 |
# File 'lib/games.rb', line 497 def Game.buffer @@buffer end |
.clean_dr_serverstring(server_string) ⇒ String
This method modifies the input string in place.
Cleans the server string for DragonRealms by removing superfluous tags and fixing encoding issues.
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 |
# File 'lib/games.rb', line 58 def self.clean_dr_serverstring(server_string) ## Clear out superfluous tags server_string = server_string.gsub("<pushStream id=\"combat\" /><popStream id=\"combat\" />", "") server_string = server_string.gsub("<popStream id=\"combat\" /><pushStream id=\"combat\" />", "") # DR occasionally has poor encoding in text, which causes parsing errors. # One example of this is in the discern text for the spell Membrach's Greed # which gets sent as Membrach\x92s Greed. This fixes the bad encoding until # Simu fixes it. if server_string =~ /\\x92/ Lich.log "Detected poorly encoded apostrophe: #{server_string.inspect}" server_string.gsub!("\x92", "'") Lich.log "Changed poorly encoded apostrophe to: #{server_string.inspect}" end ## Fix combat wrapping components - Why, DR, Why? server_string = server_string.gsub("<pushStream id=\"combat\" /><component id=", "<component id=") # Fixes xml with \r\n in the middle of it like: # We close the first line and in the next segment, we remove the trailing bits # <component id='room objs'> You also see a granite altar with several candles and a water jug on it, and a granite font.\r\n # <component id='room extra'>Placed around the interior, you see: some furniture and other bits of interest.\r\n # <component id='room exits'>Obvious paths: clockwise, widdershins.\r\n # Followed by in a closing line such as one of these: # </component>\r\n # <compass></compass></component>\r\n # If the pattern is on the left of the =~ the named capture gets assigned as a variable if /^<(?<xmltag>dynaStream|component) id='.*'>[^<]*(?!<\/\k<xmltag>>)\r\n$/ =~ server_string Lich.log "Open-ended #{xmltag} tag: #{server_string.inspect}" server_string.gsub!("\r\n", "</#{xmltag}>") Lich.log "Open-ended #{xmltag} tag tag fixed to: #{server_string.inspect}" end # Remove the now dangling closing tag if server_string =~ /^(?:(\"|<compass><\/compass>))?<\/(dynaStream|component)>\r\n/ Lich.log "Extraneous closing tag detected and deleted: #{server_string.inspect}" server_string = "" end ## Fix duplicate pushStrings while server_string.include?("<pushStream id=\"combat\" /><pushStream id=\"combat\" />") server_string = server_string.gsub("<pushStream id=\"combat\" /><pushStream id=\"combat\" />", "<pushStream id=\"combat\" />") end if @combat_count > 0 @end_combat_tags.each do |tag| # server_string = "<!-- looking for tag: #{tag}" + server_string if server_string.include?(tag) server_string = server_string.gsub(tag, "<popStream id=\"combat\" />" + tag) unless server_string.include?("<popStream id=\"combat\" />") @combat_count -= 1 end if server_string.include?("<pushStream id=\"combat\" />") server_string = server_string.gsub("<pushStream id=\"combat\" />", "") end end end @combat_count += server_string.scan("<pushStream id=\"combat\" />").length @combat_count -= server_string.scan("<popStream id=\"combat\" />").length @combat_count = 0 if @combat_count < 0 if @atmospherics @atmospherics = false server_string.prepend('<popStream id="atmospherics" />') unless server_string =~ /<popStream id="atmospherics" \/>/ end if server_string =~ /<pushStream id="familiar" \/><prompt time="[0-9]+">><\/prompt>/ # Cry For Help spell is broken... server_string.sub!('<pushStream id="familiar" />', '') elsif server_string =~ /<pushStream id="atmospherics" \/><prompt time="[0-9]+">><\/prompt>/ # pet pigs in DragonRealms are broken... server_string.sub!('<pushStream id="atmospherics" />', '') elsif (server_string =~ /<pushStream id="atmospherics" \/>/) @atmospherics = true end return server_string end |
.clean_gs_serverstring(server_string) ⇒ String
Cleans the server string for Gemstone by replacing specific tags.
38 39 40 41 42 43 44 |
# File 'lib/games.rb', line 38 def self.clean_gs_serverstring(server_string) # The Rift, Scatter is broken... if server_string =~ /<compDef id='room text'><\/compDef>/ server_string.sub!(/(.*)\s\s<compDef id='room text'><\/compDef>/) { "<compDef id='room desc'>#{$1}</compDef>" } end return server_string end |
.close ⇒ nil
Closes the game socket and kills the associated thread if they exist.
449 450 451 452 453 454 |
# File 'lib/games.rb', line 449 def Game.close if @@socket @@socket.close rescue nil @@thread.kill rescue nil end end |
.closed? ⇒ Boolean
Checks if the game socket is closed.
437 438 439 440 441 442 443 |
# File 'lib/games.rb', line 437 def Game.closed? if @@socket.nil? true else @@socket.closed? end end |
.gets ⇒ String
Reads a line from the game buffer.
490 491 492 |
# File 'lib/games.rb', line 490 def Game.gets @@buffer.gets end |
.open(host, port) ⇒ void
This method returns an undefined value.
Opens a connection to the game server.
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 176 177 178 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 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/games.rb', line 144 def Game.open(host, port) @@socket = TCPSocket.open(host, port) begin @@socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_KEEPALIVE, true) rescue Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}" rescue StandardError Lich.log "error: #{$!}\n\t#{$!.backtrace.join("\n\t")}" end @@socket.sync = true # Add check to determine if the game server hung at initial response @@wrap_thread = Thread.new { @last_recv = Time.now while !@@autostarted && (Time.now - @last_recv < 6) break if @@autostarted sleep 0.2 end puts 'look' if !@@autostarted } @@thread = Thread.new { begin while ($_SERVERSTRING_ = @@socket.gets) @@last_recv = Time.now @@_buffer.update($_SERVERSTRING_) if TESTING begin $cmd_prefix = String.new if $_SERVERSTRING_ =~ /^\034GSw/ unless (XMLData.game.nil? or XMLData.game.empty?) unless Module.const_defined?(:GameLoader) require_relative 'common/game-loader' GameLoader.load! end end if XMLData.game =~ /^GS/ $_SERVERSTRING_ = self.clean_gs_serverstring($_SERVERSTRING_) else $_SERVERSTRING_ = self.clean_dr_serverstring($_SERVERSTRING_) end pp $_SERVERSTRING_ if $deep_debug # retain for deep troubleshooting $_SERVERBUFFER_.push($_SERVERSTRING_) if !@@autostarted and $_SERVERSTRING_ =~ /<app char/ if Gem::Version.new(LICH_VERSION) > Gem::Version.new(Lich.core_updated_with_lich_version) Lich::Messaging.mono(Lich::Messaging.monsterbold("New installation or updated version of Lich5 detected!")) Lich::Messaging.mono(Lich::Messaging.monsterbold("Installing newest core scripts available to ensure you're up-to-date!")) Lich::Messaging.mono("") Lich::Util::Update.update_core_data_and_scripts end Script.start('autostart') if Script.exists?('autostart') @@autostarted = true if Gem::Version.new(RUBY_VERSION) < Gem::Version.new(RECOMMENDED_RUBY) ruby_warning = Terminal::Table.new ruby_warning.title = "Ruby Recommended Version Warning" ruby_warning.add_row(["Please update your Ruby installation."]) ruby_warning.add_row(["You're currently running Ruby v#{Gem::Version.new(RUBY_VERSION)}!"]) ruby_warning.add_row(["It's recommended to run Ruby v#{Gem::Version.new(RECOMMENDED_RUBY)} or higher!"]) ruby_warning.add_row(["Future Lich5 releases will soon require this newer version."]) ruby_warning.add_row([" "]) ruby_warning.add_row(["Visit the following link for info on updating:"]) if XMLData.game =~ /^GS/ ruby_warning.add_row(["https://gswiki.play.net/Lich:Software/Installation"]) elsif XMLData.game =~ /^DR/ ruby_warning.add_row(["https://github.com/elanthia-online/lich-5/wiki/Documentation-for-Installing-and-Upgrading-Lich"]) else ruby_warning.add_row(["Unknown game type #{XMLData.game} detected."]) ruby_warning.add_row(["Unsure of proper documentation, please seek assistance via discord!"]) end ruby_warning.to_s.split("\n").each { |row| Lich::Messaging.mono(Lich::Messaging.monsterbold(row)) } end end if !@@infomon_loaded && (defined?(Infomon) || !$DRINFOMON_VERSION.nil?) && !XMLData.name.nil? && !XMLData.name.empty? && !XMLData.dialogs.empty? ExecScript.start("Infomon.redo!", { :quiet => true, :name => "infomon_reset" }) if XMLData.game !~ /^DR/ && Infomon.db_refresh_needed? @@infomon_loaded = true end if !@@cli_scripts && @@autostarted && !XMLData.name.nil? && !XMLData.name.empty? if (arg = ARGV.find { |a| a =~ /^\-\-start\-scripts=/ }) for script_name in arg.sub('--start-scripts=', '').split(',') Script.start(script_name) end end @@cli_scripts = true Lich.log("info: logged in as #{XMLData.game}:#{XMLData.name}") end unless $_SERVERSTRING_ =~ /^<settings / begin # Check for valid XML prior to sending to client, corrects double and single nested quotes REXML::Document.parse_stream("<root>#{$_SERVERSTRING_}</root>", XMLData) rescue unless $!.to_s =~ /invalid byte sequence/ # Fixed invalid xml such as: # <mode id="GAME"/><settingsInfo space not found crc='0' instance='DR'/> # <settingsInfo space not found crc='0' instance='DR'/> if $_SERVERSTRING_ =~ /<settingsInfo .*?space not found / Lich.log "Invalid settingsInfo XML tags detected: #{$_SERVERSTRING_.inspect}" $_SERVERSTRING_.sub!('space not found', '') Lich.log "Invalid settingsInfo XML tags fixed to: #{$_SERVERSTRING_.inspect}" retry end # Illegal character "&" in raw string " You also see a large bin labeled \"Lost & Found\", a hastily scrawled notice, a brightly painted sign, a silver bell, the Registrar's Office and " if $_SERVERSTRING_ =~ /\&/ Lich.log "Invalid \& detected: #{$_SERVERSTRING_.inspect}" $_SERVERSTRING_.gsub!("&", '&') Lich.log "Invalid \& stripped out: #{$_SERVERSTRING_.inspect}" retry end # Illegal character "\a" in raw string "\aYOU HAVE BEEN IDLE TOO LONG. PLEASE RESPOND.\a\n" if $_SERVERSTRING_ =~ /\a/ Lich.log "Invalid \a detected: #{$_SERVERSTRING_.inspect}" $_SERVERSTRING_.gsub!("\a", '') Lich.log "Invalid \a stripped out: #{$_SERVERSTRING_.inspect}" retry end # Fixes invalid XML with nested single quotes in it such as: # From DR intro tips # <link id='2' value='Ever wondered about the time you've spent in Elanthia? Check the PLAYED verb!' cmd='played' echo='played' /> # From GS # <d cmd='forage Imaera's Lace'>Imaera's Lace</d>, <d cmd='forage stalk burdock'>stalk of burdock</d> unless (matches = $_SERVERSTRING_.scan(/'([^=>]*'[^=>]*)'/)).empty? Lich.log "Invalid nested single quotes XML tags detected: #{$_SERVERSTRING_.inspect}" matches.flatten.each do |match| $_SERVERSTRING_.gsub!(match, match.gsub(/'/, ''')) end Lich.log "Invalid nested single quotes XML tags fixed to: #{$_SERVERSTRING_.inspect}" retry end # Fixes invalid XML with nested double quotes in it such as: # <subtitle=" - [Avlea's Bows, "The Straight and Arrow"]"> unless (matches = $_SERVERSTRING_.scan(/"([^=]*"[^=]*)"/)).empty? Lich.log "Invalid nested double quotes XML tags detected: #{$_SERVERSTRING_.inspect}" matches.flatten.each do |match| $_SERVERSTRING_.gsub!(match, match.gsub(/"/, '"')) end Lich.log "Invalid nested double quotes XML tags fixed to: #{$_SERVERSTRING_.inspect}" retry end $stdout.puts "error: server_thread: #{$!}\n\t#{$!.backtrace.join("\n\t")}" Lich.log "Invalid XML detected - please report this: #{$_SERVERSTRING_.inspect}" Lich.log "error: server_thread: #{$!}\n\t#{$!.backtrace.join("\n\t")}" end XMLData.reset end if Module.const_defined?(:GameLoader) infomon_serverstring = $_SERVERSTRING_.dup if XMLData.game =~ /^GS/ Infomon::XMLParser.parse(infomon_serverstring) stripped_infomon_serverstring = strip_xml(infomon_serverstring, type: 'infomon') stripped_infomon_serverstring.split("\r\n").each { |line| unless line.empty? Infomon::Parser.parse(line) end } elsif XMLData.game =~ /^DR/ DRParser.parse(infomon_serverstring) end end Script.new_downstream_xml($_SERVERSTRING_) stripped_server = strip_xml($_SERVERSTRING_, type: 'main') stripped_server.split("\r\n").each { |line| @@buffer.update(line) if TESTING Script.new_downstream(line) if !line.empty? } end if (alt_string = DownstreamHook.run($_SERVERSTRING_)) # Buffer.update(alt_string, Buffer::DOWNSTREAM_MOD) if alt_string =~ /^(?:<resource picture="\d+"\/>|<popBold\/>)?<style id="roomName"\s+\/>/ if (Lich.display_lichid == true || Lich.display_uid == true) if XMLData.game =~ /^GS/ if (Lich.display_lichid == true && Lich.display_uid == true) alt_string.sub!(/] \(\d+\)/) { "]" } alt_string.sub!(']') { " - #{Map.current.id}] (u#{(XMLData.room_id == 0 || XMLData.room_id > 4294967296) ? "nknown" : XMLData.room_id})" } elsif Lich.display_lichid == true alt_string.sub!(']') { " - #{Map.current.id}]" } elsif Lich.display_uid == true alt_string.sub!(/] \(\d+\)/) { "]" } alt_string.sub!(']') { "] (u#{(XMLData.room_id == 0 || XMLData.room_id > 4294967296) ? "nknown" : XMLData.room_id})" } end elsif XMLData.game =~ /^DR/ if Lich.display_uid == true alt_string.sub!(/] \((?:\d+|\*\*)\)/) { "]" } elsif Lich.hide_uid_flag == true alt_string.sub!(/] \((?:\d+|\*\*)\)/) { "]" } end end end @@room_number_after_ready = true end if $frontend =~ /genie/i && alt_string =~ /^<streamWindow id='room' title='Room' subtitle=" - \[.*\] \((?:\d+|\*\*)\)"/ alt_string.sub!(/] \((?:\d+|\*\*)\)/) { "]" } end if @@room_number_after_ready && alt_string =~ /<prompt / if Lich.display_stringprocs == true room_exits = [] Map.current.wayto.each do |key, value| # Don't include cardinals / up/down/out (usually just climb/go) if value.class == Proc if Map.current.timeto[key].is_a?(Numeric) || (Map.current.timeto[key].is_a?(StringProc) && Map.current.timeto[key].call.is_a?(Numeric)) room_exits << "<d cmd=';go2 #{key}'>#{Map[key].title.first.gsub(/\[|\]/, '')}#{Lich.display_lichid ? ('(' + Map[key].id.to_s + ')') : ''}</d>" end end end alt_string = "StringProcs: #{room_exits.join(', ')}\r\n#{alt_string}" unless room_exits.empty? end if Lich.display_exits == true room_exits = [] Map.current.wayto.each do |_key, value| # Don't include cardinals / up/down/out (usually just climb/go) next if value.to_s =~ /^(?:o|d|u|n|ne|e|se|s|sw|w|nw|out|down|up|north|northeast|east|southeast|south|southwest|west|northwest)$/ if value.class != Proc room_exits << "<d cmd='#{value.dump[1..-2]}'>#{value.dump[1..-2]}</d>" end end unless room_exits.empty? alt_string = "Room Exits: #{room_exits.join(', ')}\r\n#{alt_string}" if XMLData.game =~ /^GS/ && ['wrayth', 'stormfront'].include?($frontend) && Map.current.id != @@last_id_shown_room_window alt_string = "#{alt_string}<pushStream id='room' ifClosedStyle='watching'/>Room Exits: #{room_exits.join(', ')}\r\n<popStream/>\r\n" @@last_id_shown_room_window = Map.current.id end end end if XMLData.game =~ /^DR/ room_number = "" room_number += "#{Map.current.id}" if Lich.display_lichid room_number += " - " if Lich.display_lichid && Lich.display_uid room_number += "(#{XMLData.room_id == 0 ? "**" : "u#{XMLData.room_id}"})" if Lich.display_uid unless room_number.empty? alt_string = "Room Number: #{room_number}\r\n#{alt_string}" if ['wrayth', 'stormfront'].include?($frontend) alt_string = "<streamWindow id='main' title='Story' subtitle=\" - [#{XMLData.room_title[2..-3]} - #{room_number}]\" location='center' target='drop'/>\r\n#{alt_string}" alt_string = "<streamWindow id='room' title='Room' subtitle=\" - [#{XMLData.room_title[2..-3]} - #{room_number}]\" location='center' target='drop' ifClosed='' resident='true'/>#{alt_string}" end end end @@room_number_after_ready = false end if $frontend =~ /^(?:wizard|avalon)$/ alt_string = sf_to_wiz(alt_string) end if $_DETACHABLE_CLIENT_ begin $_DETACHABLE_CLIENT_.write(alt_string) rescue $_DETACHABLE_CLIENT_.close rescue nil $_DETACHABLE_CLIENT_ = nil respond "--- Lich: error: client_thread: #{$!}" respond $!.backtrace.first Lich.log "error: client_thread: #{$!}\n\t#{$!.backtrace.join("\n\t")}" end else $_CLIENT_.write(alt_string) end end rescue $stdout.puts "error: server_thread: #{$!}\n\t#{$!.backtrace.join("\n\t")}" Lich.log "error: server_thread: #{$!}\n\t#{$!.backtrace.join("\n\t")}" end end rescue StandardError Lich.log "error: server_thread: #{$!}\n\t#{$!.backtrace.join("\n\t")}" $stdout.puts "error: server_thread: #{$!}\n\t#{$!.backtrace.slice(0..10).join("\n\t")}" sleep 0.2 retry unless $_CLIENT_.closed? or @@socket.closed? or ($!.to_s =~ /invalid argument|A connection attempt failed|An existing connection was forcibly closed|An established connection was aborted by the software in your host machine./i) rescue Lich.log "error: server_thread: #{$!}\n\t#{$!.backtrace.join("\n\t")}" $stdout.puts "error: server_thread: #{$!}\n\t#{$!.backtrace.slice(0..10).join("\n\t")}" sleep 0.2 retry unless $_CLIENT_.closed? or @@socket.closed? or ($!.to_s =~ /invalid argument|A connection attempt failed|An existing connection was forcibly closed|An established connection was aborted by the software in your host machine./i) end } @@thread.priority = 4 $_SERVER_ = @@socket # deprecated end |
.puts(str) ⇒ nil
Sends a formatted string to the game socket and updates the last upstream message.
472 473 474 475 476 477 478 479 480 481 482 483 484 485 |
# File 'lib/games.rb', line 472 def Game.puts(str) $_SCRIPTIDLETIMESTAMP_ = Time.now if (script = Script.current) script_name = script.name else script_name = '(unknown script)' end $_CLIENTBUFFER_.push "[#{script_name}]#{$SEND_CHARACTER}#{$cmd_prefix}#{str}\r\n" if script.nil? or not script.silent respond "[#{script_name}]#{$SEND_CHARACTER}#{str}\r\n" end Game._puts "#{$cmd_prefix}#{str}" $_LASTUPSTREAM_ = "[#{script_name}]#{$SEND_CHARACTER}#{str}" end |
.thread ⇒ Thread
Returns the current thread associated with the Game.
430 431 432 |
# File 'lib/games.rb', line 430 def Game.thread @@thread end |