Module: Lich::GameBase::XMLCleaner

Defined in:
documented/games.rb

Overview

XML string cleaner module

Class Method Summary collapse

Class Method Details

.clean_nested_quotes(server_string) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Repairs nested quotes inside XML attribute values.

The server occasionally sends attributes with nested quotes (e.g., title='Tsetem's Items', or the settingsInfo space-not-found bug) that break XML parsers. This method escapes the inner quotes as ' or " and logs the repair.

Modifies server_string in place.

Parameters:

  • server_string (String) —

    the malformed server line

Returns:

  • (String) —

    the repaired line



386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'documented/games.rb', line 386

def clean_nested_quotes(server_string)
  # Fix nested single quotes
  unless (matches = server_string.scan(/'([^=>]*'[^=>]*)'/)).empty?
    Lich.log "Invalid nested single quotes XML tags detected: #{server_string.inspect}"
    matches.flatten.each do |match|
      server_string.gsub!(match, match.gsub(/'/, '''))
    end
    Lich.log "Invalid nested single quotes XML tags fixed to: #{server_string.inspect}"
  end

  # Fix nested double quotes
  unless (matches = server_string.scan(/"([^=>]*"[^=>]*)"/)).empty?
    Lich.log "Invalid nested double quotes XML tags detected: #{server_string.inspect}"
    matches.flatten.each do |match|
      server_string.gsub!(match, match.gsub(/"/, '"'))
    end
    Lich.log "Invalid nested double quotes XML tags fixed to: #{server_string.inspect}"
  end

  server_string
end

.fix_invalid_characters(server_string) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Strips invalid characters that break XML parsing.

Removes bell characters (\a) and other encodings that Ox cannot tolerate. Logs each detected and repaired instance.

Modifies server_string in place.

Parameters:

  • server_string (String) —

    the server line

Returns:

  • (String) —

    the cleaned line



418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'documented/games.rb', line 418

def fix_invalid_characters(server_string)
  # Note: a bare '&' is intentionally not escaped here. REXML raised on it
  # (hence the old escaping); Ox tolerates it (convert_special: false emits
  # it verbatim with no error), so escaping is no longer needed for parsing.

  # Fix bell character
  if server_string.include?("\a")
    Lich.log "Invalid \\a detected: #{server_string.inspect}"
    server_string.gsub!("\a", '')
    Lich.log "Invalid \\a stripped out: #{server_string.inspect}"
  end

  server_string
end

.fix_xml_tags(server_string) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Repairs common XML tag malformations from the game stream.

Fixes open-ended dynaStream and component tags, removes dangling closing tags, and strips unclosed tags in long empath-appraisal strings. Logs each repair.

Modifies server_string in place.

Parameters:

  • server_string (String) —

    the server line

Returns:

  • (String) —

    the repaired line



443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'documented/games.rb', line 443

def fix_xml_tags(server_string)
  # Fix open-ended XML tags
  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 fixed to: #{server_string.inspect}"
  end

  # Remove dangling closing tags
  if server_string =~ /^(?:(\"|<compass><\/compass>))?<\/(dynaStream|component)>\r\n/
    Lich.log "Extraneous closing tag detected and deleted: #{server_string.inspect}"
    server_string = ""
  end

  # Remove unclosed tag in long strings from empath appraisals
  if server_string =~ / and <d cmd=\"transfer .+? nerves\">a/
    Lich.log "Unclosed wound (nerves) tag detected and deleted: #{server_string.inspect}"
    server_string.sub!(/ and <d cmd=\"transfer .+? nerves\">a.+?$/, " and more.")
  end

  server_string
end