Class: Lich::Common::WizardScript

Inherits:
Script
  • Object
show all
Defined in:
lib/common/script.rb

Instance Attribute Summary

Attributes inherited from Script

#at_exit_procs, #command_line, #current_label, #die_with, #downstream_buffer, #file_name, #hidden, #ignore_pause, #jump_label, #label_order, #match_stack_labels, #match_stack_strings, #name, #no_echo, #no_kill_all, #no_pause_all, #paused, #quiet, #safe, #silent, #unique_buffer, #upstream_buffer, #vars, #want_downstream, #want_downstream_xml, #want_script_output, #want_upstream, #watchfor

Instance Method Summary collapse

Methods inherited from Script

#at_exit, at_exit, #clear, clear_exit_procs, #clear_exit_procs, current, db, distrust, exists?, #exit, #exit!, exit!, #feedme_upstream, #get_next_label, #gets, #gets?, #has_thread?, hidden, index, #instance_eval, #instance_variable_get, kill, #kill, #labels, list, list_trusted, log, #match_stack_add, #match_stack_clear, namescript_incoming, new_downstream, new_downstream_xml, new_script_output, new_upstream, open_file, pause, #pause, #paused?, paused?, run, running, running?, #safe?, self, start, #thread_group, #to_s, trust, #unique_gets, #unique_gets?, unpause, #unpause, #upstream_gets, #upstream_gets?, version

Constructor Details

#initialize(file_name, cli_vars = []) ⇒ WizardScript

Note:

This method contains a FIXME comment indicating that proper use of variables and initialization of the parent class should be ensured when modernized.

Initializes a new instance of WizardScript.

rubocop:disable Lint/MissingSuper rubocop:disable Lint/UselessAssignment rubocop:disable Lint/InterpolationCheck

Examples:

wizard_script = WizardScript.new("path/to/script.txt", ["var1", "var2"])

Parameters:

  • file_name (String)

    the name of the script file to be processed.

  • cli_vars (Array<String>, String) (defaults to: [])

    optional command line variables to initialize the script.

Raises:

  • (StandardError)

    if there is an error reading the script file.



1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
# File 'lib/common/script.rb', line 1455

def initialize(file_name, cli_vars = [])
  @name = /.*[\/\\]+([^\.]+)\./.match(file_name).captures.first
  @file_name = file_name
  @vars = Array.new
  @killer_mutex = Mutex.new
  unless cli_vars.empty?
    if cli_vars.is_a?(String)
      cli_vars = cli_vars.split(' ')
    end
    cli_vars.each_index { |idx| @vars[idx + 1] = cli_vars[idx] }
    @vars[0] = @vars[1..-1].join(' ')
    cli_vars = nil
  end
  if @vars.first =~ /^quiet$/i
    @quiet = true
    @vars.shift
  else
    @quiet = false
  end
  @downstream_buffer = LimitedArray.new
  @want_downstream = true
  @want_downstream_xml = false
  @upstream_buffer = LimitedArray.new
  @want_upstream = false
  @unique_buffer = LimitedArray.new
  @at_exit_procs = Array.new
  @patchfor = Hash.new
  @die_with = Array.new
  @paused = false
  @hidden = false
  @no_pause_all = false
  @no_kill_all = false
  @silent = false
  @safe = false
  @no_echo = false
  @match_stack_labels = Array.new
  @match_stack_strings = Array.new
  @label_order = Array.new
  @labels = Hash.new
  data = nil
  begin
    Zlib::GzipReader.open(file_name) { |f| data = f.readlines.collect { |line| line.chomp } }
  rescue
    begin
      File.open(file_name) { |f| data = f.readlines.collect { |line| line.chomp } }
    rescue
      respond "--- Lich: error reading script file (#{file_name}): #{$!}"
      # return nil
    end
  end
  @quiet = true if data[0] =~ /^[\t\s]*#?[\t\s]*(?:quiet|hush)$/i

  counter_action = {
    'add'      => '+',
    'sub'      => '-',
    'subtract' => '-',
    'multiply' => '*',
    'divide'   => '/',
    'set'      => ''
  }

  setvars = Array.new
  data.each { |line| setvars.push($1) if line =~ /[\s\t]*setvariable\s+([^\s\t]+)[\s\t]/i and not setvars.include?($1) }
  has_counter = data.find { |line| line =~ /%c/i }
  has_save = data.find { |line| line =~ /%s/i }
  has_nextroom = data.find { |line| line =~ /nextroom/i }

  fixstring = proc { |str|
    while not setvars.empty? and str =~ /%(#{setvars.join('|')})%/io
      str.gsub!('%' + $1 + '%', '#{' + $1.downcase + '}')
    end
    str.gsub!(/%c(?:%)?/i, '#{c}')
    str.gsub!(/%s(?:%)?/i, '#{sav}')
    while str =~ /%([0-9])(?:%)?/
      str.gsub!(/%#{$1}(?:%)?/, '#{script.vars[' + $1 + ']}')
    end
    str
  }

  fixline = proc { |line|
    if line =~ /^[\s\t]*[A-Za-z0-9_\-']+:/i
      line = line.downcase.strip
    elsif line =~ /^([\s\t]*)counter\s+(add|sub|subtract|divide|multiply|set)\s+([0-9]+)/i
      line = "#{$1}c #{counter_action[$2]}= #{$3}"
    elsif line =~ /^([\s\t]*)counter\s+(add|sub|subtract|divide|multiply|set)\s+(.*)/i
      indent, action, arg = $1, $2, $3
      line = "#{indent}c #{counter_action[action]}= #{fixstring.call(arg.inspect)}.to_i"
    elsif line =~ /^([\s\t]*)save[\s\t]+"?(.*?)"?[\s\t]*$/i
      indent, arg = $1, $2
      line = "#{indent}sav = #{fixstring.call(arg.inspect)}"
    elsif line =~ /^([\s\t]*)echo[\s\t]+(.+)/i
      indent, arg = $1, $2
      line = "#{indent}echo #{fixstring.call(arg.inspect)}"
    elsif line =~ /^([\s\t]*)waitfor[\s\t]+(.+)/i
      indent, arg = $1, $2
      line = "#{indent}waitfor #{fixstring.call(Regexp.escape(arg).inspect.gsub("\\\\ ", ' '))}"
    elsif line =~ /^([\s\t]*)put[\s\t]+\.(.+)$/i
      indent, arg = $1, $2
      if arg.include?(' ')
        line = "#{indent}start_script(#{Regexp.escape(fixstring.call(arg.split[0].inspect))}, #{fixstring.call(arg.split[1..-1].join(' ').scan(/"[^"]+"|[^"\s]+/).inspect)})\n#{indent}exit"
      else
        line = "#{indent}start_script(#{Regexp.escape(fixstring.call(arg.inspect))})\n#{indent}exit"
      end
    elsif line =~ /^([\s\t]*)put[\s\t]+;(.+)$/i
      indent, arg = $1, $2
      if arg.include?(' ')
        line = "#{indent}start_script(#{Regexp.escape(fixstring.call(arg.split[0].inspect))}, #{fixstring.call(arg.split[1..-1].join(' ').scan(/"[^"]+"|[^"\s]+/).inspect)})"
      else
        line = "#{indent}start_script(#{Regexp.escape(fixstring.call(arg.inspect))})"
      end
    elsif line =~ /^([\s\t]*)(put|move)[\s\t]+(.+)/i
      indent, cmd, arg = $1, $2, $3
      line = "#{indent}waitrt?\n#{indent}clear\n#{indent}#{cmd.downcase} #{fixstring.call(arg.inspect)}"
    elsif line =~ /^([\s\t]*)goto[\s\t]+(.+)/i
      indent, arg = $1, $2
      line = "#{indent}goto #{fixstring.call(arg.inspect).downcase}"
    elsif line =~ /^([\s\t]*)waitforre[\s\t]+(.+)/i
      indent, arg = $1, $2
      line = "#{indent}waitforre #{arg}"
    elsif line =~ /^([\s\t]*)pause[\s\t]*(.*)/i
      indent, arg = $1, $2
      arg = '1' if arg.empty?
      arg = '0' + arg.strip if arg.strip =~ /^\.[0-9]+$/
      line = "#{indent}pause #{arg}"
    elsif line =~ /^([\s\t]*)match[\s\t]+([^\s\t]+)[\s\t]+(.+)/i
      indent, label, arg = $1, $2, $3
      line = "#{indent}match #{fixstring.call(label.inspect).downcase}, #{fixstring.call(Regexp.escape(arg).inspect.gsub("\\\\ ", ' '))}"
    elsif line =~ /^([\s\t]*)matchre[\s\t]+([^\s\t]+)[\s\t]+(.+)/i
      indent, label, regex = $1, $2, $3
      line = "#{indent}matchre #{fixstring.call(label.inspect).downcase}, #{regex}"
    elsif line =~ /^([\s\t]*)setvariable[\s\t]+([^\s\t]+)[\s\t]+(.+)/i
      indent, var, arg = $1, $2, $3
      line = "#{indent}#{var.downcase} = #{fixstring.call(arg.inspect)}"
    elsif line =~ /^([\s\t]*)deletevariable[\s\t]+(.+)/i
      line = "#{$1}#{$2.downcase} = nil"
    elsif line =~ /^([\s\t]*)(wait|nextroom|exit|echo)\b/i
      line = "#{$1}#{$2.downcase}"
    elsif line =~ /^([\s\t]*)matchwait\b/i
      line = "#{$1}matchwait"
    elsif line =~ /^([\s\t]*)if_([0-9])[\s\t]+(.*)/i
      indent, num, stuff = $1, $2, $3
      line = "#{indent}if script.vars[#{num}]\n#{indent}\t#{fixline.call($3)}\n#{indent}end"
    elsif line =~ /^([\s\t]*)shift\b/i
      line = "#{$1}script.vars.shift"
    else
      respond "--- Lich: unknown line: #{line}"
      line = '#' + line
    end
  }

  lich_block = false

  data.each_index { |idx|
    if lich_block
      if data[idx] =~ /\}[\s\t]*LICH[\s\t]*$/
        data[idx] = data[idx].sub(/\}[\s\t]*LICH[\s\t]*$/, '')
        lich_block = false
      else
        next
      end
    elsif data[idx] =~ /^[\s\t]*#|^[\s\t]*$/
      next
    elsif data[idx] =~ /^[\s\t]*LICH[\s\t]*\{/
      data[idx] = data[idx].sub(/LICH[\s\t]*\{/, '')
      if data[idx] =~ /\}[\s\t]*LICH[\s\t]*$/
        data[idx] = data[idx].sub(/\}[\s\t]*LICH[\s\t]*$/, '')
      else
        lich_block = true
      end
    else
      data[idx] = fixline.call(data[idx])
    end
  }

  if has_counter or has_save or has_nextroom
    data.each_index { |idx|
      next if data[idx] =~ /^[\s\t]*#/

      data.insert(idx, '')
      data.insert(idx, 'c = 0') if has_counter
      data.insert(idx, "sav = Settings['sav'] || String.new\nbefore_dying { Settings['sav'] = sav }") if has_save
      data.insert(idx, "def nextroom\n\troom_count = XMLData.room_count\n\twait_while { room_count == XMLData.room_count }\nend") if has_nextroom
      data.insert(idx, '')
      break
    }
  end

  @current_label = '~start'
  @labels[@current_label] = String.new
  @label_order.push(@current_label)
  for line in data
    if line =~ /^([\d_\w]+):$/
      @current_label = $1
      @label_order.push(@current_label)
      @labels[@current_label] = String.new
    else
      @labels[@current_label] += "#{line}\n"
    end
  end
  data = nil
  @current_label = @label_order[0]
  @thread_group = ThreadGroup.new
  @@running.push(self)
  # return self
end