Class: FactorioMods::Install

Inherits:
Object
  • Object
show all
Defined in:
lib/factorio_mods/install.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Install

Returns a new instance of Install.



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/factorio_mods/install.rb', line 28

def initialize(path)
  @base_path = File.expand_path path
  return unless valid?

  info = JSON.parse(File.read(File.join(mod_path('base'), 'info.json')),
                    symbolize_names: true)

  @version = info[:version]
  @is_steam = !(path.tr('\\', '/') =~ %r{/steamapps/common/}i).nil?
  @architecture = Dir.entries(File.join(base_path, 'bin'))
                     .reject { |e| e.start_with? '.' }
                     .first
end

Instance Attribute Details

#architectureObject (readonly)

Returns the value of attribute architecture.



5
6
7
# File 'lib/factorio_mods/install.rb', line 5

def architecture
  @architecture
end

#base_pathObject (readonly)

Returns the value of attribute base_path.



5
6
7
# File 'lib/factorio_mods/install.rb', line 5

def base_path
  @base_path
end

#is_steamObject (readonly)

Returns the value of attribute is_steam.



5
6
7
# File 'lib/factorio_mods/install.rb', line 5

def is_steam
  @is_steam
end

#versionObject (readonly)

Returns the value of attribute version.



5
6
7
# File 'lib/factorio_mods/install.rb', line 5

def version
  @version
end

Class Method Details

.discoverObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/factorio_mods/install.rb', line 7

def self.discover
  to_scan = if FactorioMods::OS.windows?
              [
                'C:\Program Files (x86)\Steam\steamapps\ccommon\Factorio',
                'C:\Program Files\Factorio'
              ]
            elsif FactorioMods::OS.mac?
              [
                '~/Library/Application Support/Steam/steamapps/common/Factorio/factorio.app/Contents',
                '/Applications/factorio.app/Contents'
              ]
            elsif FactorioMods::OS.linux?
              [
                '~/.steam/steam/steamapps/common/Factorio',
                '~/.factorio'
              ]
            end

  to_scan.map { |path| Install.new path }.select(&:valid?)
end

Instance Method Details

#bin_pathObject



76
77
78
# File 'lib/factorio_mods/install.rb', line 76

def bin_path
  File.join base_path, 'bin', architecture
end

#binaryObject



42
43
44
45
46
47
48
# File 'lib/factorio_mods/install.rb', line 42

def binary
  if OS.windows?
    File.join bin_path, 'factorio.exe'
  else
    File.join bin_path, 'factorio'
  end
end

#data_pathObject



80
81
82
# File 'lib/factorio_mods/install.rb', line 80

def data_path
  File.join base_path, 'data'
end

#mod_path(mod) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/factorio_mods/install.rb', line 88

def mod_path(mod)
  if %w[base core].include? mod.to_s
    File.join data_path, mod
  else
    matching = Dir.entries(mods_path).select { |entry| entry.start_with?(mod) }
    return nil unless matching.any?
    raise 'More than one mod matches' if matching.count > 1

    File.join(mods_path, matching.first)
  end
end

#mods_pathObject



84
85
86
# File 'lib/factorio_mods/install.rb', line 84

def mods_path
  File.join read_path, 'mods'
end

#read_pathObject



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/factorio_mods/install.rb', line 50

def read_path
  @read_path ||= begin
    if uses_system_paths
      config = IniFile.load(File.join(system_path, 'config', 'config.ini'))

      config['path']['read-data']
        .gsub '__PATH__system-read-data__', system_path
    else
      base_path
    end
  end
end

#valid?Boolean

Returns:

  • (Boolean)


100
101
102
103
# File 'lib/factorio_mods/install.rb', line 100

def valid?
  Dir.exist?(base_path) &&
    File.exist?(File.join(mod_path('base'), 'info.json'))
end

#write_pathObject



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/factorio_mods/install.rb', line 63

def write_path
  @write_path ||= begin
    if uses_system_paths
      config = IniFile.load(File.join(system_path, 'config', 'config.ini'))

      config['path']['write-data']
        .gsub '__PATH__system-write-data__', system_path
    else
      base_path
    end
  end
end