Class: Factorix::InstalledMOD

Inherits:
Data
  • Object
show all
Extended by:
Enumerable
Includes:
Comparable
Defined in:
lib/factorix/installed_mod.rb,
lib/factorix/installed_mod.rb

Overview

Represents a MOD installed in the MOD directory or data directory

InstalledMOD represents an actual MOD package found in either:

  • The MOD directory (user-installed MODs as ZIP files or directories)

  • The data directory (base and expansion MODs bundled with the game)

This is distinct from MOD (which is just a name identifier) and MODState (which represents desired state in mod-list.json).

Constant Summary collapse

ZIP_FORM =

Form constants

:zip
DIRECTORY_FORM =
:directory

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#formObject (readonly)

Returns the value of attribute form

Returns:

  • (Object)

    the current value of form



8
9
10
# File 'lib/factorix/installed_mod.rb', line 8

def form
  @form
end

#infoObject (readonly)

Returns the value of attribute info

Returns:

  • (Object)

    the current value of info



8
9
10
# File 'lib/factorix/installed_mod.rb', line 8

def info
  @info
end

#modObject (readonly)

Returns the value of attribute mod

Returns:

  • (Object)

    the current value of mod



8
9
10
# File 'lib/factorix/installed_mod.rb', line 8

def mod
  @mod
end

#pathObject (readonly)

Returns the value of attribute path

Returns:

  • (Object)

    the current value of path



8
9
10
# File 'lib/factorix/installed_mod.rb', line 8

def path
  @path
end

#versionObject (readonly)

Returns the value of attribute version

Returns:

  • (Object)

    the current value of version



8
9
10
# File 'lib/factorix/installed_mod.rb', line 8

def version
  @version
end

Class Method Details

.all(handler: nil) ⇒ Array<InstalledMOD>

Get all installed MODs

Parameters:

  • handler (Progress::ScanHandler, nil) (defaults to: nil)

    optional event handler for progress tracking

Returns:



45
46
47
48
49
50
51
# File 'lib/factorix/installed_mod.rb', line 45

def self.all(handler: nil)
  scanner = Scanner.new
  scanner.subscribe(handler) if handler
  result = scanner.scan
  scanner.unsubscribe(handler) if handler
  result
end

.each {|mod| ... } ⇒ Enumerator, Array

Enumerate over all installed MODs

Yield Parameters:

Returns:

  • (Enumerator, Array)

    Enumerator if no block given, otherwise the result of the block



57
# File 'lib/factorix/installed_mod.rb', line 57

def self.each(&) = all.each(&)

.from_directory(path) ⇒ InstalledMOD

Create InstalledMOD from a directory

Parameters:

  • path (Pathname)

    Path to the directory

Returns:

Raises:



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/factorix/installed_mod.rb', line 82

def self.from_directory(path)
  info_path = path + "info.json"
  raise FileFormatError, "Missing info.json" unless info_path.file?

  info = InfoJSON.from_json(info_path.read)

  dirname = path.basename.to_s
  expected_unversioned = info.name
  expected_versioned = "#{info.name}_#{info.version}"

  unless dirname == expected_unversioned || dirname == expected_versioned
    raise FileFormatError, "Directory name mismatch: expected #{expected_unversioned} or #{expected_versioned}, got #{dirname}"
  end

  new(mod: MOD[name: info.name], version: info.version, form: DIRECTORY_FORM, path:, info:)
end

.from_zip(path) ⇒ InstalledMOD

Create InstalledMOD from a ZIP file

Parameters:

  • path (Pathname)

    Path to the ZIP file

Returns:

Raises:



64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/factorix/installed_mod.rb', line 64

def self.from_zip(path)
  info = InfoJSON.from_zip(path)

  expected_filename = "#{info.name}_#{info.version}.zip"
  actual_filename = path.basename.to_s

  unless actual_filename == expected_filename
    raise FileFormatError, "Filename mismatch: expected #{expected_filename}, got #{actual_filename}"
  end

  new(mod: MOD[name: info.name], version: info.version, form: ZIP_FORM, path:, info:)
end

Instance Method Details

#<=>(other) ⇒ Integer?

Compare with another InstalledMOD

Comparison is by version (ascending), then by form (directory > ZIP)

Parameters:

Returns:

  • (Integer, nil)

    -1, 0, 1 for less than, equal to, greater than; nil if not comparable



213
214
215
216
217
218
219
# File 'lib/factorix/installed_mod.rb', line 213

def <=>(other)
  return nil unless other.is_a?(InstalledMOD)
  return nil unless mod == other.mod

  # Compare by version (ascending), then by form priority (directory > ZIP)
  (version <=> other.version).nonzero? || form_priority(form) <=> form_priority(other.form)
end

#base?Boolean

Check if this is the base MOD

Returns:

  • (Boolean)

    true if this is the base MOD



224
# File 'lib/factorix/installed_mod.rb', line 224

def base? = mod.base?

#expansion?Boolean

Check if this is an expansion MOD

Returns:

  • (Boolean)

    true if this is an expansion MOD



229
# File 'lib/factorix/installed_mod.rb', line 229

def expansion? = mod.expansion?