Module: Lich::GameBase::XMLCleaner

Defined in:
documented/games.rb

Overview

XML string cleaner module XML string cleaner module Provides methods to clean and fix XML strings

Examples:

Using XMLCleaner

cleaned_string = XMLCleaner.clean_nested_quotes(raw_string)

Class Method Summary collapse

Class Method Details

.clean_nested_quotes(server_string) ⇒ String

Cleans nested quotes in the server string

Examples:

cleaned_string = XMLCleaner.clean_nested_quotes(raw_string)

Parameters:

  • server_string (String)

    The server string to clean

Returns:

  • (String)

    The cleaned server string



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'documented/games.rb', line 146

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

Fixes invalid characters in the server string

Examples:

fixed_string = XMLCleaner.fix_invalid_characters(raw_string)

Parameters:

  • server_string (String)

    The server string to fix

Returns:

  • (String)

    The fixed server string



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'documented/games.rb', line 173

def fix_invalid_characters(server_string)
  # Fix ampersands
  if server_string.include?('&') && !server_string.include?('&') && !server_string.include?('>') && !server_string.include?('<') && !server_string.include?(''') && !server_string.include?('"')
    Lich.log "Invalid & detected: #{server_string.inspect}"
    server_string.gsub!('&', '&')
    Lich.log "Invalid & fixed to: #{server_string.inspect}"
  end

  # 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

  # Fix poorly encoded apostrophes
  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

  server_string
end

.fix_xml_tags(server_string) ⇒ String

Fixes open-ended XML tags in the server string

Examples:

fixed_string = XMLCleaner.fix_xml_tags(raw_string)

Parameters:

  • server_string (String)

    The server string to fix

Returns:

  • (String)

    The fixed server string



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'documented/games.rb', line 203

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