Class: Esp::Mw::OpenmwConfig

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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

#pathObject (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_dirObject



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_pathObject



15
16
17
# File 'lib/esp/mw/openmw_config.rb', line 15

def default_path
  File.join(default_dir, 'openmw.cfg')
end

.host_osObject



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_entriesObject

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_dirsObject

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

Returns:

  • (Boolean)


58
59
60
# File 'lib/esp/mw/openmw_config.rb', line 58

def exist?
  File.exist?(@path)
end

#include?(line) ⇒ Boolean

Returns:

  • (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_pluginsObject

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