diff --git a/steamtricks/bin/steamtricks b/steamtricks/bin/steamtricks index 99ca189..8bfcc92 100644 --- a/steamtricks/bin/steamtricks +++ b/steamtricks/bin/steamtricks @@ -13,13 +13,14 @@ sub print_help; sub main; sub parse_config; sub setup_config; -sub write_config; +sub create_config; +sub update_config; sub create_install; sub change_install; use constant { - VERSION = "v0.1.0" -} + VERSION => "v0.1.0", +}; my $steamtricks_prefix = $ENV{'STEAMTRICKS_PREFIX'}; my $change_install = ''; @@ -30,11 +31,12 @@ my $game_version = ''; my $new_steam_prefix = '~/.var/app/com.valvesoftware.Steam/data/Steam/steamapps/common'; my $help = ''; my $version = ''; -my $config_location = "${steamtricks_prefix}/steamtricks.conf" +my $print_version = ''; +my $config_location = "${steamtricks_prefix}/steamtricks.conf"; -GetOptions ('steam-prefix=s' => \$steam_prefix, - 'create-install' => \$create_install, - 'change-install' => \$change_install, +GetOptions ( + 'create-managed-install' => \$create_install, + 'change-managed-install' => \$change_install, 'game=s' => \$game, 'game-version=s' => \$game_version, 'setup-config' => \$new_config, @@ -42,26 +44,26 @@ GetOptions ('steam-prefix=s' => \$steam_prefix, 'help' => \$help, 'version' => \$print_version); -main; +main(); sub main { if ($help) { print_help; return 0; - } else if ($version) { - print basename($0), VERSION; + } elsif ($print_version) { + print basename($0),' ', VERSION, "\n"; return 0; - } else if ($new_config) { - setup_config($new_steam_prefix) + } elsif ($new_config) { + setup_config($new_steam_prefix); return 0; } else { - my $steam_prefix, @managed_games = parse_config; + my ($steam_prefix, @managed_games) = parse_config; if ($change_install && $game_version && ! $create_install) { change_install($steam_prefix, @managed_games); return 0; - } else if ($create_install && $game_version && ! $change_install ) { + } elsif ($create_install && $game_version && ! $change_install ) { create_install($steam_prefix, @managed_games); return 0; } @@ -69,71 +71,106 @@ sub main { } sub parse_config { - + my $Config; if (-s "$config_location" ) { - my $Config = Config::Tiny->read( "$config_location" ); + $Config = Config::Tiny->read( "$config_location" ); } else { warn "config file ${config_location} not found. Creating new config with flatpak defaults\n"; - my $Config = setup_config; + $Config = setup_config; } my $steam_prefix = Dumper($Config->{steam_prefix}); - my @managed_games = Dumper($Config->{install_manager}->{managed_games}) + my @managed_games = Dumper($Config->{install_manager}->{managed_games}); return $steam_prefix, @managed_games; } sub setup_config { - - print "Creating config with STEAM_PREFIX=${new_steam_prefix}\n" + my $Config; + print "Creating config with steam_prefix=${new_steam_prefix}\n"; if (-s "${config_location}") { warn "$config_location already exists, do you wish to overwrite this file? [y/N]"; my $ans = ; chomp $ans; - switch($ans) { - case /[yY].?/ { my $Config = write_config } - else { die "Config not created. Exiting" } + if ( $ans =~ /^[yY].?$/ ) { + $Config = create_config; + } else { + die "Config not created. Exiting"; } } else { - my $Config = write_config; + $Config = create_config; } return $Config; } -sub write_config { +sub create_config { my $Config = Config::Tiny->new({ - _ => { STEAM_PREFIX => $new_steam_prefix } - install_manager => { MANAGED_GAMES[] => ""} + _ => { steam_prefix => $new_steam_prefix }, + install_manager => { "managed_games[]" => ""} }); $Config->write( ${config_location} ) or die "Config file $config_location could not be written: $!\n"; return $Config; } -sub create_install { - my $steam_prefix, @managed_games = @_; +sub update_config { + my ($section, $key, $value) = @_; + my $Config = Config::Tiny->read($config_location); + if ($section) { + $Config->{$section}->{$key}= $value; + } else { + $Config->{_}->{$key} = $value; + } + $Config->write($config_location) or die "Config file $config_location could not be written: $!\n"; +} - if (-d "${steam_prefix}/${game}") { +sub create_install { + my ($steam_prefix, @managed_games) = @_; + + if (-d "${steam_prefix}/${game}" ) { move("${steam_prefix}/${game}", "${steam_prefix}/${game}_${game_version}"); symlink("${steam_prefix}/${game}_${game_version}", "${steam_prefix}/${game}"); - } else if ( -l "${steam_prefix}/${game}") { + print "You may now restart Steam to apply effects\n"; + } elsif ( -l "${steam_prefix}/${game}") { if ( grep( /^$game$/, @managed_games) ){ - warn "This game is already managed, are you looking for --change-install ?"; + warn "This game is already managed, are you looking for --change-managed-install ?"; exit 1; } else { - + warn "This game is symlinked but not in the managed_games[] field in the config"; + print "Would you like to add it to the config? [Y/n]: "; + my $ans = ; + chomp $ans; + if ($ans =~ /^[nN].?$/ ) { + die "Config not upddated, exiting"; + } else { + update_config("install_manager", "managed_games[]", $game); + } } - - } else { - die "Game not found at ${steam_prefix}/${game} please check your steam prefix and your game name"; + die "Game not found at ${steam_prefix}/${game} please check your steam_prefix and your game name"; } } sub change_install { - + my ($steam_prefix, $managed_games) = @_; + + if ( -l "${steam_prefix}/${game}" && grep /^$game$/, $managed_games) { + unlink "${steam_prefix}/${game}" or die "Could not unlink $steam_prefix/$game: $!"; + symlink "${steam_prefix}/${$game}_${game_version}", "${steam_prefix}/${game}" or die "Could not create symlink ${steam_prefix}/${game}: $!"; + print "You may now change any beta settings and restart Steam to apply your changes\n"; + } elsif ( -l "${steam_prefix}/${game}" && ! grep /^$game$/, $managed_games) { + warn "$game not in config as a managed_game[], adding to config"; + update_config("install_manager", "managed_games[]", $game); + + unlink "${steam_prefix}/${game}" or die "Could not unlink $steam_prefix/$game: $!"; + symlink "${steam_prefix}/${$game}_${game_version}", "${steam_prefix}/${game}" or die "Could not create symlink ${steam_prefix}/${game}: $!"; + print "You may now change any beta settings and restart Steam to apply your changes\n"; + } else { + die "${steam_prefix}/${game} is not a symlink, please run with --create-managed-install"; + } } sub print_help { print basename($0), "\n"; + return 0; } \ No newline at end of file