Semi-working version

This commit is contained in:
wasabi
2025-05-30 10:43:47 -05:00
parent 19b009f8ad
commit 5c792e6529

View File

@@ -13,13 +13,14 @@ sub print_help;
sub main; sub main;
sub parse_config; sub parse_config;
sub setup_config; sub setup_config;
sub write_config; sub create_config;
sub update_config;
sub create_install; sub create_install;
sub change_install; sub change_install;
use constant { use constant {
VERSION = "v0.1.0" VERSION => "v0.1.0",
} };
my $steamtricks_prefix = $ENV{'STEAMTRICKS_PREFIX'}; my $steamtricks_prefix = $ENV{'STEAMTRICKS_PREFIX'};
my $change_install = ''; my $change_install = '';
@@ -30,11 +31,12 @@ my $game_version = '';
my $new_steam_prefix = '~/.var/app/com.valvesoftware.Steam/data/Steam/steamapps/common'; my $new_steam_prefix = '~/.var/app/com.valvesoftware.Steam/data/Steam/steamapps/common';
my $help = ''; my $help = '';
my $version = ''; 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, GetOptions (
'create-install' => \$create_install, 'create-managed-install' => \$create_install,
'change-install' => \$change_install, 'change-managed-install' => \$change_install,
'game=s' => \$game, 'game=s' => \$game,
'game-version=s' => \$game_version, 'game-version=s' => \$game_version,
'setup-config' => \$new_config, 'setup-config' => \$new_config,
@@ -42,26 +44,26 @@ GetOptions ('steam-prefix=s' => \$steam_prefix,
'help' => \$help, 'help' => \$help,
'version' => \$print_version); 'version' => \$print_version);
main; main();
sub main { sub main {
if ($help) { if ($help) {
print_help; print_help;
return 0; return 0;
} else if ($version) { } elsif ($print_version) {
print basename($0), VERSION; print basename($0),' ', VERSION, "\n";
return 0; return 0;
} else if ($new_config) { } elsif ($new_config) {
setup_config($new_steam_prefix) setup_config($new_steam_prefix);
return 0; return 0;
} else { } else {
my $steam_prefix, @managed_games = parse_config; my ($steam_prefix, @managed_games) = parse_config;
if ($change_install && $game_version && ! $create_install) { if ($change_install && $game_version && ! $create_install) {
change_install($steam_prefix, @managed_games); change_install($steam_prefix, @managed_games);
return 0; return 0;
} else if ($create_install && $game_version && ! $change_install ) { } elsif ($create_install && $game_version && ! $change_install ) {
create_install($steam_prefix, @managed_games); create_install($steam_prefix, @managed_games);
return 0; return 0;
} }
@@ -69,71 +71,106 @@ sub main {
} }
sub parse_config { sub parse_config {
my $Config;
if (-s "$config_location" ) { if (-s "$config_location" ) {
my $Config = Config::Tiny->read( "$config_location" ); $Config = Config::Tiny->read( "$config_location" );
} else { } else {
warn "config file ${config_location} not found. Creating new config with flatpak defaults\n"; 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 $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; return $steam_prefix, @managed_games;
} }
sub setup_config { sub setup_config {
my $Config;
print "Creating config with STEAM_PREFIX=${new_steam_prefix}\n" print "Creating config with steam_prefix=${new_steam_prefix}\n";
if (-s "${config_location}") { if (-s "${config_location}") {
warn "$config_location already exists, do you wish to overwrite this file? [y/N]"; warn "$config_location already exists, do you wish to overwrite this file? [y/N]";
my $ans = <STDIN>; my $ans = <STDIN>;
chomp $ans; chomp $ans;
switch($ans) { if ( $ans =~ /^[yY].?$/ ) {
case /[yY].?/ { my $Config = write_config } $Config = create_config;
else { die "Config not created. Exiting" } } else {
die "Config not created. Exiting";
} }
} else { } else {
my $Config = write_config; $Config = create_config;
} }
return $Config; return $Config;
} }
sub write_config { sub create_config {
my $Config = Config::Tiny->new({ my $Config = Config::Tiny->new({
_ => { STEAM_PREFIX => $new_steam_prefix } _ => { steam_prefix => $new_steam_prefix },
install_manager => { MANAGED_GAMES[] => ""} install_manager => { "managed_games[]" => ""}
}); });
$Config->write( ${config_location} ) or die "Config file $config_location could not be written: $!\n"; $Config->write( ${config_location} ) or die "Config file $config_location could not be written: $!\n";
return $Config; return $Config;
} }
sub create_install { sub update_config {
my $steam_prefix, @managed_games = @_; 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}"); move("${steam_prefix}/${game}", "${steam_prefix}/${game}_${game_version}");
symlink("${steam_prefix}/${game}_${game_version}", "${steam_prefix}/${game}"); 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) ){ 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; exit 1;
} else { } 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 = <STDIN>;
chomp $ans;
if ($ans =~ /^[nN].?$/ ) {
die "Config not upddated, exiting";
} else {
update_config("install_manager", "managed_games[]", $game);
}
} }
} else { } 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 { 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 { sub print_help {
print basename($0), "\n"; print basename($0), "\n";
return 0;
} }