Module: Lich::Common::ClientCommands Private
- Defined in:
- documented/common/client_commands.rb,
documented/common/client_commands/builtins.rb
Overview
This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.
Registration and implementation of all built-in ;commands.
Commands are registered in match order: patterns that overlap are ordered
so that narrower patterns are tested first and remain reachable. The ;kill,
;pause, and ;unpause commands support both bare forms (;k, ;ka, ;kd) and
argument forms (;kill
Display toggles persist settings to the database before announcing changes, and use ClientCommands.toggle_value to apply user overrides or defaults. Database contention on write is retried once with a 100ms pause; failed reads raise immediately.
The ;trust, ;distrust, and ;list trusted commands are retained verbatim for Ruby 2.0-2.2 compatibility; all other Ruby versions report the feature as unavailable.
Defined Under Namespace
Classes: Command
Constant Summary collapse
- LOCK =
Serializes define. Registration mutates one shared staging slot; dispatch only reads the frozen published array and so needs no lock.
Mutex.new
- DISPLAY_TOGGLES =
The uniform display toggles: read the current value, negate it, let an explicit true/false argument override, report, write back. Six branches that differed only in accessor and message, so they are a table rather than six near-identical bodies.
Ordering note: these are registered in the same relative order the if/elsif chain had them. None of the six overlap each other, but ;display roomid (DR) sits between uid and exits in the original and is registered separately below to keep that position.
[ ['lichid', :display_lichid, 'Lich ID#s'], ['uid', :display_uid, 'RealID#s'], ['exits?', :display_exits, 'Room Exits of non-StringProc/Obvious exits'], ['stringprocs?', :display_stringprocs, 'Room Exits of StringProcs'], ['roomlinks?', :display_room_links, 'room exits as clickable command links'], ['roommono', :display_room_mono, 'room information in monospace font'] ].freeze
Class Method Summary collapse
-
.command(pattern, game: nil) {|match, cmd| ... } ⇒ void
Registers one command.
-
.commands ⇒ Array<Command>
Every registered command, in match order.
-
.define { ... } ⇒ void
Registers a table of commands, replacing whatever was published before.
-
.define_unlocked { ... } ⇒ void
The staging/publish half of ClientCommands.define, with the lock already held.
-
.dispatch(cmd) ⇒ Boolean
Runs the first command whose pattern matches and whose game gate passes.
- .game_matches?(gate) ⇒ Boolean
-
.help ⇒ Object
The built-in help listing.
-
.toggle_value(current, argument) ⇒ Boolean
Reads a toggle's next value: the negation of its current value, unless an explicit true/false argument overrides it.
-
.user_regexp(text, prefix: '', options: 0) ⇒ Regexp?
Compiles a pattern the user typed after a ;command, or tells them it is not a valid regex.
Class Method Details
.command(pattern, game: nil) {|match, cmd| ... } ⇒ void
159 160 161 162 163 |
# File 'documented/common/client_commands.rb', line 159 def command(pattern, game: nil, &handler) raise 'ClientCommands.command must be called inside ClientCommands.define' if @staging.nil? @staging << Command.new(pattern, game, handler).freeze end |
.commands ⇒ Array<Command>
Every registered command, in match order.
This is the PUBLISHED table. define replaces it wholesale; nothing else mutates it, so a dispatch never sees a half-built table and a reload never leaves stale handlers in front of new ones.
93 94 95 |
# File 'documented/common/client_commands.rb', line 93 def commands @commands ||= [].freeze end |
.define { ... } ⇒ void
This method returns an undefined value.
Registers a table of commands, replacing whatever was published before.
Built-ins are registered inside one of these blocks so that
re-running the file -- which is exactly what ;hmr client_commands
does, since HMR calls load() and load() re-executes the body --
rebuilds the table instead of appending a second copy behind the
first. Appending was the original behavior and it silently kept the
OLD handlers live: they matched first, so an edited built-in was
loaded, doubled the table, and never ran.
The new table is published only once the block finishes. A raise part-way through leaves the previous table in place rather than a truncated one.
Serialized, because @staging is one class-level slot and command re-reads it on every call. Two threads registering at once -- two frontends each running ";hmr client_commands", since main.rb runs a thread per attached detachable client -- would otherwise have the second define's @staging = [] redirect BOTH threads' command calls onto its array, and the later publish would drop the other thread's registrations with no exception raised. Worse, one interleaving publishes an EMPTY table: the inner ensure restores then falls through to Script.start as a script name until the next reload. Both reproduced; DetachableClientRegistry next door guards the same threading model the same way.
Reentrant by hand rather than with a plain Mutex: define nests
(that is what previous is for), and Ruby's Mutex is not
reentrant, so a nested define on one thread would deadlock.
131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'documented/common/client_commands.rb', line 131 def define(&block) return define_unlocked(&block) if @define_owner == Thread.current LOCK.synchronize do @define_owner = Thread.current begin define_unlocked(&block) ensure @define_owner = nil end end end |
.define_unlocked { ... } ⇒ void
This method returns an undefined value.
The staging/publish half of define, with the lock already held.
186 187 188 189 190 191 192 193 |
# File 'documented/common/client_commands.rb', line 186 def define_unlocked previous = @staging @staging = [] yield @commands = @staging.freeze ensure @staging = previous end |
.dispatch(cmd) ⇒ Boolean
Runs the first command whose pattern matches and whose game gate passes.
171 172 173 174 175 176 177 178 179 180 |
# File 'documented/common/client_commands.rb', line 171 def dispatch(cmd) commands.each do |c| next unless game_matches?(c.game) next unless (match = c.pattern.match(cmd)) c.handler.call(match, cmd) return true end false end |
.game_matches?(gate) ⇒ Boolean
198 199 200 201 202 203 204 205 |
# File 'documented/common/client_commands.rb', line 198 def game_matches?(gate) case gate when nil then true when :gs then XMLData.game.to_s.start_with?('GS') when :dr then XMLData.game.to_s.start_with?('DR') else false end end |
.help ⇒ Object
The built-in help listing. Long, but it is one flat sequence of respond calls with two game-dependent sections; leaving it inline in a handler would have buried the rest of the table.
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 |
# File 'documented/common/client_commands.rb', line 210 def help respond respond "Lich v#{LICH_VERSION}" respond respond 'built-in commands:' respond " #{$clean_lich_char}<script name> start a script" respond " #{$clean_lich_char}force <script name> start a script even if it's already running" respond " #{$clean_lich_char}pause <script name> pause a script" respond " #{$clean_lich_char}p <script name> ''" respond " #{$clean_lich_char}unpause <script name> unpause a script" respond " #{$clean_lich_char}u <script name> ''" respond " #{$clean_lich_char}kill <script name> kill a script" respond " #{$clean_lich_char}k <script name> ''" respond " #{$clean_lich_char}pause pause the most recently started script that isn't aready paused" respond " #{$clean_lich_char}p ''" respond " #{$clean_lich_char}unpause unpause the most recently started script that is paused" respond " #{$clean_lich_char}u ''" respond " #{$clean_lich_char}kill kill the most recently started script" respond " #{$clean_lich_char}k ''" respond " #{$clean_lich_char}list show running scripts (except hidden ones)" respond " #{$clean_lich_char}l ''" respond " #{$clean_lich_char}pause all pause all scripts" respond " #{$clean_lich_char}pa ''" respond " #{$clean_lich_char}unpause all unpause all scripts" respond " #{$clean_lich_char}ua ''" respond " #{$clean_lich_char}kill all kill all scripts" respond " #{$clean_lich_char}ka ''" respond " #{$clean_lich_char}kd kill all scripts, including protected and hidden scripts" respond " #{$clean_lich_char}list all show all running scripts" respond " #{$clean_lich_char}la ''" respond respond " #{$clean_lich_char}exec <code> executes the code as if it was in a script" respond " #{$clean_lich_char}e <code> ''" respond " #{$clean_lich_char}execq <code> same as #{$clean_lich_char}exec but without the script active and exited messages" respond " #{$clean_lich_char}eq <code> ''" respond " #{$clean_lich_char}execname <name> <code> creates named exec (name#) and then executes the code as if it was in a script" respond if (RUBY_VERSION =~ /^2\.[012]\./) respond " #{$clean_lich_char}trust <script name> let the script do whatever it wants" respond " #{$clean_lich_char}distrust <script name> restrict the script from doing things that might harm your computer" respond " #{$clean_lich_char}list trusted show what scripts are trusted" respond " #{$clean_lich_char}lt ''" respond end respond " #{$clean_lich_char}send <line> send a line to all scripts as if it came from the game" respond " #{$clean_lich_char}send to <script> <line> send a line to a specific script" respond respond " #{$clean_lich_char}set <variable> [on|off] set a global toggle variable on or off" respond " #{$clean_lich_char}debuglogs show debug log retention setting" respond " #{$clean_lich_char}debuglogs <number> set how many debug logs to keep (default: #{Lich::MAX_DEBUG_LOGS_DEFAULT})" respond respond " #{$clean_lich_char}lich5-update --<command> Lich5 ecosystem management " respond " see #{$clean_lich_char}lich5-update --help" respond " #{$clean_lich_char}hmr <regex filepath> Hot module reload a Ruby or Lich5 file without relogging, uses Regular Expression matching (files from Ruby itself or an installed gem are skipped)" if XMLData.game =~ /^GS/ respond respond " #{$clean_lich_char}infomon sync sends all the various commands to resync character data for infomon (fixskill)" respond " #{$clean_lich_char}infomon reset resets entire character infomon db table and then syncs data (fixprof)" respond " #{$clean_lich_char}infomon effects toggle display of effect durations" respond " #{$clean_lich_char}infomon show shows current Infomon values for character" respond " #{$clean_lich_char}infomon show full same, including values that are zero" respond " #{$clean_lich_char}sk help show information on modifying self-knowledge spells to be known" elsif XMLData.game =~ /^DR/ respond " #{$clean_lich_char}display flaguid toggle hiding the game's inline RealID in the Room Title (now optional; UIDs come from <nav>)" respond " #{$clean_lich_char}display roomid <where> where to show room id/RealID: title (room name line), line (below-room line), or both" respond " #{$clean_lich_char}display descflag [on|off] toggle hiding the room description while FLAG DESCRIPTION is OFF (the game sends it anyway)" end respond " #{$clean_lich_char}display lichid toggle display of Lich Map# when displaying room information" respond " #{$clean_lich_char}display uid toggle display of RealID Map# when displaying room information" respond " #{$clean_lich_char}display exits toggle display of non-StringProc/Obvious exits known for room in mapdb" respond " #{$clean_lich_char}display stringprocs toggle display of StringProc exits known for room in mapdb if timeto is valid" respond " #{$clean_lich_char}display roomlinks toggle rendering of room exits as clickable command links vs plain text" respond " #{$clean_lich_char}display roommono toggle rendering of Lich room information in monospace font vs game font" if XMLData.game =~ /^DR/ respond " #{$clean_lich_char}display expgains toggle real-time experience gain reporting (DragonRealms only)" respond " #{$clean_lich_char}display inlineexp toggle inline exp display in EXP window (DragonRealms only)" respond " #{$clean_lich_char}display exp-status show experience monitor status (DragonRealms only)" respond " #{$clean_lich_char}banks show your bank balances (DragonRealms only)" respond " #{$clean_lich_char}banks all show bank balances for all characters (DragonRealms only)" respond " #{$clean_lich_char}banks reset clear your bank data (DragonRealms only)" respond " #{$clean_lich_char}banks reset all clear all characters' bank data (DragonRealms only)" end respond respond 'If you liked this help message, you might also enjoy:' respond " #{$clean_lich_char}lnet help" if defined?(LNet) respond " #{$clean_lich_char}go2 help" respond " #{$clean_lich_char}repository help" respond " #{$clean_lich_char}alias help" respond " #{$clean_lich_char}vars help" respond " #{$clean_lich_char}autostart help" respond end |
.toggle_value(current, argument) ⇒ Boolean
Reads a toggle's next value: the negation of its current value, unless an explicit true/false argument overrides it.
The comparison is case-INSENSITIVE, which the original branches were not. Their patterns carry /i, so an uppercase argument was captured and then matched none of the lowercase literals, falling through to the negation: ";display lichid TRUE" turned the flag OFF when it was already on. An explicit argument now means what it says whatever its case.
316 317 318 319 320 321 322 |
# File 'documented/common/client_commands.rb', line 316 def toggle_value(current, argument) case argument&.downcase when 'true', 'on' then true when 'false', 'off' then false else !current end end |
.user_regexp(text, prefix: '', options: 0) ⇒ Regexp?
Compiles a pattern the user typed after a ;command, or tells them it is not a valid regex.
Regexp.new raises RegexpError for bad escapes too, unlike an interpolated literal (/#text/), which raises ArgumentError.
335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'documented/common/client_commands.rb', line 335 def user_regexp(text, prefix: '', options: 0) Regexp.new("#{prefix}#{text}", ) rescue RegexpError => e # The message ends with ": /<compiled source>/<flags>"; drop it so # the user sees their own text, not the prefix and flags. The text # is quoted as typed, not with String#inspect, which would double # every backslash, and a bad escape is the usual reason it failed. reason = e..sub(%r{: /.*\z}m, '') respond "--- Lich: error: invalid pattern \"#{text}\": #{reason}" respond '--- Lich: \\u is unicode escape, did you mean to use a / instead?' if reason == 'invalid Unicode escape' nil end |