Class: Lich::Common::SetupFiles
- Inherits:
-
Object
- Object
- Lich::Common::SetupFiles
- Includes:
- MonitorMixin
- Defined in:
- documented/common/setup_files.rb
Overview
Manages YAML configuration file loading, caching, and cascading merge for Lich scripts.
Supports:
- Base YAML files (base.yaml, base-empty.yaml)
- Character-specific YAML files (character-setup.yaml)
- Include files with recursive resolution and circular dependency protection
- Automatic caching with content-hash (SHA256) freshness checking
- Deep-clone protection against in-memory mutation
Caching decisions are based on file content digest, not mtime, to handle rapid rewrites and source-path changes correctly. Files are deep-cloned on access to prevent scripts from mutating cached settings.
Defined Under Namespace
Classes: FileInfo
Class Method Summary collapse
-
.safe_log(text) ⇒ void
Guarded logging -- safe to call before Lich.log is defined.
Instance Method Summary collapse
-
#get_data(type) ⇒ OpenStruct
Returns the config in a 'scripts/data/base-type.yaml' file.
-
#get_settings(character_suffixes = []) ⇒ OpenStruct
Returns your character's settings.
-
#initialize(debug = false) ⇒ void
constructor
Creates a SetupFiles instance with optional debug logging.
-
#reload ⇒ Object
Reloads cached files that have changed on disk.
-
#safe_load_yaml(filepath) ⇒ Hash
Loads a YAML file and returns its content as a Hash, with error recovery.
Constructor Details
Class Method Details
.safe_log(text) ⇒ void
This method returns an undefined value.
Guarded logging -- safe to call before Lich.log is defined. Public and class-level so FileInfo can share it.
210 211 212 213 214 215 216 |
# File 'documented/common/setup_files.rb', line 210 def self.safe_log(text) if defined?(Lich) && Lich.respond_to?(:log) Lich.log(text) else $stderr.puts(text) end end |
Instance Method Details
#get_data(type) ⇒ OpenStruct
Returns the config in a 'scripts/data/base-type.yaml' file.
193 194 195 196 197 |
# File 'documented/common/setup_files.rb', line 193 def get_data(type) filename = to_base_filename(type) reload_data([filename]) transform_data(cache_get_by_filename(filename)&.data) end |
#get_settings(character_suffixes = []) ⇒ OpenStruct
Returns your character's settings.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'documented/common/setup_files.rb', line 152 def get_settings(character_suffixes = []) character_suffixes = ['setup', character_suffixes].flatten.compact.uniq character_filenames = character_suffixes_to_filenames(character_suffixes) reload_profiles(character_filenames) initial_include_suffixes = character_filenames.reduce([]) do |result, filename| result + (cache_get_by_filename(filename)&.peek('include') || []) end initial_include_filenames = initial_include_suffixes.map { |suffix| to_include_filename(suffix) } include_filenames = resolve_includes_recursively(initial_include_filenames) safe_log "#{self.class}::#{__callee__} resolved include_filenames=#{include_filenames}" if @debug all_files = ['base.yaml', 'base-empty.yaml', include_filenames, character_filenames].flatten # Peek pass: collect union_keys from all files (always unioned across files). # Keys listed in union_keys get array union instead of overwrite during merge, # allowing includes and character files to contribute to shared lists. union_keys = all_files.reduce([]) do |keys, filename| file_keys = cache_get_by_filename(filename)&.peek('union_keys') || [] (keys + file_keys).uniq end # Merge pass: union specified keys, overwrite everything else settings = all_files.reduce({}) do |result, filename| file_info = cache_get_by_filename(filename) result.merge(file_info ? file_info.data : {}) do |key, old_val, new_val| if union_keys.include?(key.to_s) && old_val.is_a?(Array) && new_val.is_a?(Array) (old_val + new_val).uniq else new_val end end end transform_settings(settings) end |
#reload ⇒ Object
Reloads cached files that have changed on disk.
200 201 202 203 |
# File 'documented/common/setup_files.rb', line 200 def reload reload_profiles(character_suffixes_to_filenames(['setup'])) reload_data end |
#safe_load_yaml(filepath) ⇒ Hash
Errors are logged but do not interrupt processing
Loads a YAML file and returns its content as a Hash, with error recovery.
Uses unsafe_load to support Ruby objects, game-specific classes, and symbols in YAML. Logs parsing errors and returns an empty Hash on failure rather than raising.
140 141 142 143 144 145 146 |
# File 'documented/common/setup_files.rb', line 140 def safe_load_yaml(filepath) OpenStruct.new(YAML.unsafe_load_file(filepath)).to_h rescue => e ("bold", "*** ERROR PARSING YAML FILE ***") ("bold", e.) {} end |