Module: Lich::Common::UserDefs Private

Included in:
DragonRealms::CustomSubstitutions, Gemstone::Combat::Definitions::Supplements
Defined in:
documented/common/user_defs.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.

Shared machinery for merging player-supplied definitions onto core Lich's frozen defaults: lenient per-entry validation with player-facing diagnostics, timeout-bounded regex compilation, guarded matching, and a memo with reset!.

Extend it onto a module that owns one family of user definitions. The extending module supplies the reading (where entries come from) and any shape checks specific to its domain; everything below is domain-neutral. Extracted from DragonRealms::CustomSubstitutions so the GemStone combat definition supplements can share it without duplicating the validation, timeout and reporting rules.

Contract for every player entry:

  • Invalid entries are dropped individually. One bad entry never disables the rest of its list.
  • Every rejection is reported through Messaging with the exact key, index, offending value, reason, and consequence.
  • Regexes compile with REGEX_TIMEOUT_SECONDS so a pathological pattern raises Regexp::TimeoutError instead of hanging Lich.

Examples:

module MyDefs
  extend Lich::Common::UserDefs
  MESSAGE_PREFIX = '[MyDefs]'

  def self.patterns
    memoize(:patterns) do
      validate_entries(raw_list, :patterns) { |entry, i| validate_regex(entry, :patterns, i) }
    end
  end
end

Constant Summary collapse

REGEX_TIMEOUT_SECONDS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Per-regex evaluation budget (seconds) applied to every user-supplied pattern. See #timeout_bounded and #apply_regexes.

1.0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object

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.

Sets up the per-module state: a lock guarding the caches (any number of concurrently running scripts may call the extending module on shared game text), the memo, and the once-only timeout report set.



47
48
49
50
51
# File 'documented/common/user_defs.rb', line 47

def self.extended(base)
  base.instance_variable_set(:@lock, Mutex.new)
  base.instance_variable_set(:@cache, {})
  base.instance_variable_set(:@reported_timeouts, [])
end

Instance Method Details

#apply_regexes(text, patterns) ⇒ String

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.

Folds patterns over text as successive String#sub(pattern, '') deletions, guarding each against Regexp::TimeoutError. A pattern that times out is skipped for this input and reported once (deduplicated by pattern source), never hanging the caller.

Parameters:

  • text (String) —

    the text to strip

  • patterns (Array<Regexp>) —

    validated, timeout-bounded patterns

Returns:

  • (String) —

    text with every applicable pattern removed



73
74
75
76
77
78
79
80
# File 'documented/common/user_defs.rb', line 73

def apply_regexes(text, patterns)
  patterns.reduce(text) do |current, pattern|
    current.sub(pattern, '')
  rescue Regexp::TimeoutError
    report_timeout(pattern)
    current
  end
end

#reset! ⇒ 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.

Clears the memoized results and the per-pattern timeout-report dedup set so the next lookup re-reads and re-validates. Call this whenever the underlying source is reloaded.



58
59
60
61
62
63
# File 'documented/common/user_defs.rb', line 58

def reset!
  @lock.synchronize do
    @cache = {}
    @reported_timeouts = []
  end
end