Class: Factorix::CLI::Commands::MOD::Sync

Inherits:
Base
  • Object
show all
Includes:
DownloadSupport, PortalSupport
Defined in:
lib/factorix/cli/commands/mod/sync.rb

Overview

Sync MOD states and startup settings from a save file

Instance Method Summary collapse

Methods inherited from Base

backup_support!, confirmable!, inherited, require_game_stopped!

Instance Method Details

#call(save_file:, jobs: "4") ⇒ void

This method returns an undefined value.

Execute the sync command

Parameters:

  • save_file (String)

    Path to save file

  • jobs (Integer) (defaults to: "4")

    Number of parallel downloads

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/factorix/cli/commands/mod/sync.rb', line 41

def call(save_file:, jobs: "4", **)
  jobs = Integer(jobs)
  # Load save file
  say "Loading save file: #{save_file}", prefix: :info
  save_data = SaveFile.load(Pathname(save_file))
  say "Loaded save file (version: #{save_data.version}, MOD(s): #{save_data.mods.size})", prefix: :info

  # Load current state
  mod_list = MODList.load
  presenter = Progress::Presenter.new(title: "\u{1F50D}\u{FE0E} Scanning MOD(s)", output: err)
  handler = Progress::ScanHandler.new(presenter)
  installed_mods = InstalledMOD.all(handler:)
  graph = Dependency::Graph::Builder.build(installed_mods:, mod_list:)

  raise DirectoryNotFoundError, "MOD directory does not exist: #{runtime.mod_dir}" unless runtime.mod_dir.exist?

  # Find MODs that need to be installed
  mods_to_install = find_mods_to_install(save_data.mods, installed_mods)

  if mods_to_install.any?
    say "#{mods_to_install.size} MOD(s) need to be installed", prefix: :info

    # Plan installation
    install_targets = plan_installation(mods_to_install, graph, jobs)

    # Show plan
    show_install_plan(install_targets)
    return unless confirm?("Do you want to install these MOD(s)?")

    # Execute installation
    execute_installation(install_targets, jobs)
    say "Installed #{install_targets.size} MOD(s)", prefix: :success
  else
    say "All MOD(s) from save file are already installed", prefix: :info
  end

  # Resolve conflicts: disable existing MODs that conflict with new ones
  resolve_conflicts(mod_list, save_data.mods, graph)

  # Update mod-list.json
  update_mod_list(mod_list, save_data.mods)
  backup_if_exists(runtime.mod_list_path)
  mod_list.save
  say "Updated mod-list.json", prefix: :success

  # Update mod-settings.dat
  update_mod_settings(save_data.startup_settings, save_data.version)
  say "Updated mod-settings.dat", prefix: :success

  say "Sync completed successfully", prefix: :success
end