Class: Esp::Mw::OpenmwConfig
- Inherits:
-
Object
- Object
- Esp::Mw::OpenmwConfig
- Defined in:
- lib/esp/mw/openmw_config.rb
Overview
Reads and edits OpenMW’s openmw.cfg. Knows the per-OS location of the user config (the one OpenMW’s launcher edits) and can parse the data=/content= lines to enumerate installed plugins.
v1 targets the single user config; OPENMW_CONFIG (or an explicit path) overrides it. The full global/local/user hierarchy and ?token? data paths are not merged/expanded yet — see roadmap/17.
Constant Summary collapse
- DEFAULT_PATH =
Computed at load from the current OS/env. Callers may pass an explicit path to OpenmwConfig.new instead.
default_path
Instance Attribute Summary collapse
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #append(line) ⇒ Object
- #backup_once_per_day(now: Time.now) ⇒ Object
-
#content_entries ⇒ Object
content= plugin filenames, in file order — this is the load order.
-
#data_dirs ⇒ Object
Absolute data= directories, in file order (relative paths resolve against the config’s directory).
- #exist? ⇒ Boolean
- #include?(line) ⇒ Boolean
-
#initialize(path = DEFAULT_PATH) ⇒ OpenmwConfig
constructor
A new instance of OpenmwConfig.
-
#installed_plugins ⇒ Object
Every plugin file found across the data dirs, annotated with whether it’s active (has a content= line) and its load-order index.
Constructor Details
#initialize(path = DEFAULT_PATH) ⇒ OpenmwConfig
Returns a new instance of OpenmwConfig.
54 55 56 |
# File 'lib/esp/mw/openmw_config.rb', line 54 def initialize(path = DEFAULT_PATH) @path = path end |
Instance Attribute Details
#path ⇒ Object (readonly)
Returns the value of attribute path.
52 53 54 |
# File 'lib/esp/mw/openmw_config.rb', line 52 def path @path end |
Class Method Details
.default_dir ⇒ Object
19 20 21 22 23 24 |
# File 'lib/esp/mw/openmw_config.rb', line 19 def default_dir env = ENV['OPENMW_CONFIG'].to_s return env.split(File::PATH_SEPARATOR).first unless env.empty? platform_config_dir end |
.default_path ⇒ Object
15 16 17 |
# File 'lib/esp/mw/openmw_config.rb', line 15 def default_path File.join(default_dir, 'openmw.cfg') end |
.host_os ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/esp/mw/openmw_config.rb', line 26 def host_os case RbConfig::CONFIG['host_os'] when /mswin|mingw|cygwin/ then :windows when /darwin/ then :macos else :linux end end |
Instance Method Details
#append(line) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/esp/mw/openmw_config.rb', line 66 def append(line) return false if include?(line) File.open(@path, 'a') { |f| f.puts(line) } true end |
#backup_once_per_day(now: Time.now) ⇒ Object
73 74 75 76 77 |
# File 'lib/esp/mw/openmw_config.rb', line 73 def backup_once_per_day(now: Time.now) backup = "#{@path}.bak.#{now.strftime('%Y%m%d')}" FileUtils.cp(@path, backup) unless File.exist?(backup) backup end |
#content_entries ⇒ Object
content= plugin filenames, in file order — this is the load order.
90 91 92 |
# File 'lib/esp/mw/openmw_config.rb', line 90 def content_entries lines.filter_map { |line| match_value(line, 'content') } end |
#data_dirs ⇒ Object
Absolute data= directories, in file order (relative paths resolve against the config’s directory). Token paths (?userdata? etc.) are passed through unexpanded and simply won’t glob.
82 83 84 85 86 87 |
# File 'lib/esp/mw/openmw_config.rb', line 82 def data_dirs lines.filter_map do |line| value = match_value(line, 'data') value && resolve_dir(value) end end |
#exist? ⇒ Boolean
58 59 60 |
# File 'lib/esp/mw/openmw_config.rb', line 58 def exist? File.exist?(@path) end |
#include?(line) ⇒ Boolean
62 63 64 |
# File 'lib/esp/mw/openmw_config.rb', line 62 def include?(line) exist? && File.readlines(@path, chomp: true).include?(line) end |
#installed_plugins ⇒ Object
Every plugin file found across the data dirs, annotated with whether it’s active (has a content= line) and its load-order index.
96 97 98 99 |
# File 'lib/esp/mw/openmw_config.rb', line 96 def installed_plugins order = content_entries.each_with_index.to_h data_dirs.flat_map { |dir| plugins_in(dir, order) } end |