Class: ModOrganizer

Inherits:
Object
  • Object
show all
Extended by:
Memoist
Includes:
Utils
Defined in:
lib/mod_organizer.rb,
lib/mod_organizer/mod.rb,
lib/mod_organizer/utils.rb,
lib/mod_organizer/source.rb,
lib/mod_organizer/version.rb,
lib/mod_organizer/download.rb

Overview

Handle a ModOrganizer installation: mods, esps, load order. No concept of Merges. No concept of what is actually present in the game directory (except already installed masters/plugins and load order).

Defined Under Namespace

Modules: Utils Classes: Download, Mod, Source

Constant Summary collapse

VERSION =
'1.0.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#files_glob

Constructor Details

#initialize(mo_dir, instance_name: nil, logger: Logger.new($stdout)) ⇒ ModOrganizer

Constructor

Parameters
  • mo_dir (String): Mod Organizer installation directory

  • instance_name (String or nil): Mod Organizer instance name, or nil in case of a portable installation. [default: nil]

  • logger (Logger): The logger to be used for log messages [default: Logger.new(STDOUT)]



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mod_organizer.rb', line 31

def initialize(
  mo_dir,
  instance_name: nil,
  logger: Logger.new($stdout)
)
  @mo_dir = mo_dir.gsub('\\', '/')
  @mo_instance_dir = instance_name.nil? ? @mo_dir : "#{ENV.fetch('LOCALAPPDATA')}/ModOrganizer/#{instance_name}"
  @logger = logger
  # Read MO ini file
  mo_ini_file = "#{@mo_instance_dir}/ModOrganizer.ini"
  raise "Missing ModOrganizer configuration file #{mo_ini_file}" unless File.exist?(mo_ini_file)

  mo_ini = IniFile.load(mo_ini_file)
  @selected_profile = mo_ini['General']['selected_profile']
  @selected_profile = ::Regexp.last_match(1) if @selected_profile =~ /^@ByteArray\((.+)\)$/
  @game_path = mo_ini['General']['gamePath'].gsub('\\', '/')
  @game_path = ::Regexp.last_match(1) if @game_path =~ /^@ByteArray\((.+)\)$/
  @profiles_dir = (mo_ini['Settings']['profiles_directory'] || "#{@mo_instance_dir}/profiles").gsub('\\', '/')
  @mods_dir = (mo_ini['Settings']['mod_directory'] || "#{@mo_instance_dir}/mods").gsub('\\', '/')
  @overwrite_dir = (mo_ini['Settings']['overwrite_directory'] || "#{@mo_instance_dir}/overwrite").gsub('\\', '/')
  @downloads_dir = (mo_ini['Settings']['download_directory'] || "#{@mo_instance_dir}/downloads").gsub('\\', '/')
  @logger.debug "Selected profile: #{@selected_profile}"
  @logger.debug "Mods directory: #{@mods_dir}"
  @logger.debug "Downloads directory: #{@downloads_dir}"
  @logger.debug "Game path: #{@game_path}"
end

Instance Attribute Details

#downloads_dirObject (readonly)

String: The downloads dir



23
24
25
# File 'lib/mod_organizer.rb', line 23

def downloads_dir
  @downloads_dir
end

#game_pathObject (readonly)

String: The game path



20
21
22
# File 'lib/mod_organizer.rb', line 20

def game_path
  @game_path
end

Instance Method Details

#categoriesObject

Get the categories

Result
  • Hash<Integer, String>: For each category ID, the corresponding category name



112
113
114
115
116
# File 'lib/mod_organizer.rb', line 112

def categories
  categories_file = "#{@mo_dir}/categories.dat"
  categories_file = "#{__dir__}/default_categories.dat" unless File.exist?(categories_file)
  CSV.read(categories_file, col_sep: '|').to_h { |cat_id, title, _nexus_ids, _parent_id| [cat_id.to_i, title] }
end

#download(file_name:) ⇒ Object

Return a downloaded info of file if it exists

Parameters
  • file_name (String): The base file name for which we want the download info

Result
  • Download: The downloaded information



125
126
127
128
# File 'lib/mod_organizer.rb', line 125

def download(file_name:)
  downloaded_file = "#{@downloads_dir}/#{file_name}"
  File.exist?(downloaded_file) ? Download.new(self, file_name) : nil
end

#enabled_modsObject

Return the list of enabled mods

Result
  • Array<String>: Enabled mods



99
100
101
102
103
104
105
# File 'lib/mod_organizer.rb', line 99

def enabled_mods
  cached_enabled_mods = []
  modlist.each do |(mod_name, mod_enabled)|
    cached_enabled_mods << mod_name if mod_enabled
  end
  cached_enabled_mods
end

#mod(name:) ⇒ Object

Retrieve a mod

Parameters
  • name (String): The mod name

Result
  • Mod or nil: The mod, or nil if the mod is unknown



80
81
82
83
# File 'lib/mod_organizer.rb', line 80

def mod(name:)
  mod_dir = "#{@mods_dir}/#{name}"
  File.exist?(mod_dir) ? Mod.new(self, mod_dir) : nil
end

#mod_namesObject

Get the list of mod names

Result
  • Array<String>: List of mods



69
70
71
# File 'lib/mod_organizer.rb', line 69

def mod_names
  files_glob("#{@mods_dir}/*").map { |mod_dir| File.directory?(mod_dir) ? File.basename(mod_dir) : nil }.compact
end

#mods_listObject

Get the ordered MO mods list, sorted from the first being loaded to the last (so opposite from the internal MO file)

Result
  • Array<String>: Sorted list of mod names



90
91
92
# File 'lib/mod_organizer.rb', line 90

def mods_list
  modlist.map { |mod_name, _enabled| mod_name }
end

#runObject

Run an instance of ModOrganizer



59
60
61
62
63
# File 'lib/mod_organizer.rb', line 59

def run
  Dir.chdir(@mo_dir) do
    system 'ModOrganizer.exe'
  end
end