Class: Lich::Common::WizardScript

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

Overview

Class for handling wizard scripts This class extends Script to manage wizard-specific scripts.

Examples:

Starting a wizard script

WizardScript.new("wizard_script.lic")

Constant Summary

Constants inherited from Script

Script::JUMP, Script::JUMP_ERROR

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, #custom?, 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

FIXME: when modernized, ensure proper use of variables and init of parent class rubocop:disable Lint/MissingSuper rubocop:disable Lint/UselessAssignment rubocop:disable Lint/InterpolationCheck Initializes a new wizard script

Parameters:

  • file_name (String)

    The name of the script file

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

    Command line variables for the script



1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
# File 'documented/common/script.rb', line 1018

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
  @downstream_buffer.max_size = 400
  @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