Module: Lich::Main::ArgvOptions::SideEffects

Defined in:
documented/main/argv_options.rb

Overview

Apply side effects: dark mode, hosts-dir, bind-address, detachable-client

Class Method Summary collapse

Class Method Details

.announce(level, message) ⇒ Object

Surface a message on both channels every bind handler uses.



234
235
236
237
# File 'documented/main/argv_options.rb', line 234

def self.announce(level, message)
  $stdout.puts "#{level}: #{message}"
  Lich.log "#{level}: #{message}"
end

.die(message) ⇒ Object

Fatal argv problem: tell the user everywhere, then stop.



240
241
242
243
# File 'documented/main/argv_options.rb', line 240

def self.die(message)
  announce('error', message)
  exit 1
end

.execute(argv_options) ⇒ Hash

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.

Applies side effects from parsed ARGV options: theme, hosts, bind address, detachable client, etc.

Calls handlers in sequence to configure dark mode (StartupTheme), hosts directory, bind address resolution, detachable client settings, sentinel flag, frontend capabilities, and SAL file launch. Returns the modified argv_options hash.

Parameters:

  • argv_options (Hash) —

    the options hash from OptionParser.execute

Returns:

  • (Hash) —

    the same argv_options, possibly with keys added or modified (e.g., :bind_address resolved to a concrete host, :detachable_client_host set)



203
204
205
206
207
208
209
210
211
212
# File 'documented/main/argv_options.rb', line 203

def self.execute(argv_options)
  StartupTheme.apply(argv_options)
  handle_hosts_dir(argv_options)
  handle_bind_address(argv_options)
  handle_detachable_client(argv_options)
  handle_sentinel(argv_options)
  handle_frontend_caps(argv_options)
  handle_sal_launch(argv_options)
  argv_options
end

.handle_bind_address(argv_options) ⇒ Object

--bind-address shares the keyword vocabulary of --detachable-client hosts (tailscale/lan/any). Resolve it once, up front, so the frontend listener, the --game proxy, and a detachable client that inherits it all bind the same concrete address -- and so the exposure warning appears exactly once.



250
251
252
253
254
255
256
257
# File 'documented/main/argv_options.rb', line 250

def self.handle_bind_address(argv_options)
  result = BindAddressOption.apply(argv_options[:bind_address])
  die(result.error) if result.error
  return unless result.host

  argv_options[:bind_address] = result.host
  announce('warning', result.warning) if result.warning
end

.handle_detachable_client(argv_options) ⇒ void

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.

This method returns an undefined value.

Resolves detachable client bind host and port from --detachable-client= argument.

Parses the --detachable-client=TARGET argument (e.g., "tailscale", "lan", "127.0.0.1:9000") and resolves the host via BindHostResolver. Stores the resolved host in :detachable_client_host and the port in :detachable_client_port. If no host is specified, defaults to :bind_address or 127.0.0.1. Exits with an error message if parsing or resolution fails.

Parameters:

  • argv_options (Hash) —

    the options hash to modify

Raises:

  • (SystemExit) —

    exits with code 1 if target parsing or host resolution fails



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# File 'documented/main/argv_options.rb', line 295

def self.handle_detachable_client(argv_options)
  argv_options[:detachable_client_host] = argv_options[:bind_address] || '127.0.0.1'
  argv_options[:detachable_client_port] = nil
  arg = ARGV.find { |a| a.start_with?('--detachable-client=') }
  return unless arg

  begin
    target = DetachableClientTarget.parse(arg.split('=', 2).last)
    if target.host
      resolution = Lich::Common::BindHostResolver.resolve(target.host)
      argv_options[:detachable_client_host] = resolution.host
      announce('warning', resolution.warning) if resolution.warning
    end
    # (The port-only form inherits --bind-address, which
    # handle_bind_address already resolved and warned about; the
    # loopback default warrants no warning.)
    argv_options[:detachable_client_port] = target.port
  rescue DetachableClientTarget::ParseError, Lich::Common::BindHostResolver::Error => e
    die(e.message)
  end
end

.handle_frontend_caps(argv_options) ⇒ Object

--frontend-caps=LIST: the rich-stream capabilities every client gets unless it declares its own with SET_FRONTEND_CAPS, in place of the catalog's for the frontend (see Lich::Common::ClientCapabilities). An empty LIST (or one with no known name) gives every client the legacy rewrites.



225
226
227
228
229
230
231
# File 'documented/main/argv_options.rb', line 225

def self.handle_frontend_caps(argv_options)
  return unless argv_options.key?(:frontend_caps)

  capabilities, unknown = Lich::Common::ClientCapabilities.parse(argv_options[:frontend_caps])
  announce('warning', "--frontend-caps: ignoring unknown capabilities: #{unknown.join(', ')}") unless unknown.empty?
  Lich::Common::ClientCapabilities.launch_capabilities = capabilities
end

.handle_hosts_dir(argv_options) ⇒ void

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.

This method returns an undefined value.

Stores a custom hosts directory path if it exists.

Searches ARGV for --hosts-dir=PATH. If the path exists as a directory, normalizes it (convert backslashes to forward slashes, ensure trailing slash), removes the argument from ARGV, and stores the path in argv_options. If the path does not exist, logs a warning to stdout.

Parameters:

  • argv_options (Hash) —

    the options hash to modify



269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'documented/main/argv_options.rb', line 269

def self.handle_hosts_dir(argv_options)
  if (arg = ARGV.find { |a| a =~ /^--hosts-dir=(.+)$/i })
    hosts_dir = arg[/^--hosts-dir=(.+)$/i, 1]
    ARGV.delete(arg)
    if hosts_dir && File.exist?(hosts_dir)
      hosts_dir = hosts_dir.tr('\\', '/')
      hosts_dir += '/' unless hosts_dir[-1..-1] == '/'
      argv_options[:hosts_dir] = hosts_dir
    else
      $stdout.puts "warning: given hosts directory does not exist: #{hosts_dir}"
    end
  end
end

.handle_sal_launch(argv_options) ⇒ void

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.

This method returns an undefined value.

Launches a SAL file via the Simutronics launcher or system command.

If argv_options is set, verifies the file exists. If the file does not exist and a detachable client port is in use, appends a message suggesting the port may be held by another Lich. For SGE.sal, retrieves the launcher command via Lich.get_simu_launcher, substitutes the SAL path, and executes via Win32.ShellExecute (Windows), system call under Wine, or bare system call. Exits after launch.

Parameters:

  • argv_options (Hash) —

    the options hash (expects :sal, :detachable_client_host, :detachable_client_port)

Raises:

  • (SystemExit) —

    exits after launching the SAL file or on error



340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'documented/main/argv_options.rb', line 340

def self.handle_sal_launch(argv_options)
  return unless argv_options[:sal]

  unless File.exist?(argv_options[:sal])
    msg = "error: launch file does not exist: #{argv_options[:sal]}"
    # A launcher that deletes its .sal once it reaches the detachable port
    # (e.g. Saga) does so early if something else already holds that port.
    port = argv_options[:detachable_client_port]
    if port && port_in_use?(argv_options[:detachable_client_host], port)
      msg += "\n\nThe detachable client port #{port} is already in use, most likely by another Lich " \
             "that is still running. Your launcher may have connected to it and deleted the launch " \
             "file. Close that Lich or choose a different port, then try again."
    end
    Lich.log msg
    Lich.msgbox(message: msg, icon: :error)
    exit
  end
  Lich.log "info: launch file: #{argv_options[:sal]}"

  if argv_options[:sal] =~ /SGE\.sal/i
    unless (launcher_cmd = Lich.get_simu_launcher)
      $stdout.puts 'error: failed to find the Simutronics launcher'
      Lich.log 'error: failed to find the Simutronics launcher'
      exit
    end
    launcher_cmd.sub!('%1', argv_options[:sal])
    Lich.log "info: launcher_cmd: #{launcher_cmd}"
    if defined?(Win32) && launcher_cmd =~ /^"(.*?)"\s*(.*)$/
      dir_file = $1
      param = $2
      dir = dir_file.slice(/^.*[\\\/]/)
      file = dir_file.sub(/^.*[\\\/]/, '')
      operation = (Win32.isXP? ? 'open' : 'runas')
      r = Win32.ShellExecute(lpOperation: operation, lpFile: file, lpDirectory: dir, lpParameters: param)
      Lich.log "error: Win32.ShellExecute returned #{r}; Win32.GetLastError: #{Win32.GetLastError}" if r < 33
    elsif defined?(Wine)
      system("#{Wine::BIN} #{launcher_cmd}")
    else
      system(launcher_cmd)
    end
    exit
  end
end

.handle_sentinel(argv_options) ⇒ Object

--sentinel: the running session gets the origin marker whichever frontend it identifies as.



216
217
218
# File 'documented/main/argv_options.rb', line 216

def self.handle_sentinel(argv_options)
  Lich::Common::Frontend.sentinel_requested = true if argv_options[:sentinel]
end

.port_in_use?(host, port) ⇒ Boolean

Plain bind probe (no SO_REUSEADDR on Windows), so a live listener fails it.

Returns:

  • (Boolean)


318
319
320
321
322
323
324
325
# File 'documented/main/argv_options.rb', line 318

def self.port_in_use?(host, port)
  TCPServer.new(host, port).close
  false
rescue Errno::EADDRINUSE, Errno::EACCES
  true
rescue StandardError
  false
end