fix: enable/disable logic

This commit is contained in:
v4n
2025-02-03 01:49:41 +02:00
parent 23cb0646bf
commit 1a6a13a621
4 changed files with 329 additions and 322 deletions
+38 -37
View File
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
VERSION="0.3.2"
VERSION="0.3.3"
# --- Globals ---
@@ -25,11 +25,6 @@ function get_version_major() {
echo "$1" | awk -F. '{print $2}'
}
BREAKING_CHANGES_DB_FILE_PATCHES=(
["2"]='sed -i "s/^\([0-9]\+\),/\1,ENABLED,/" $1'
["3"]='sed -i "1 i\\3" "$1"'
)
function get_filename_without_path() {
echo "$1" | awk -F/ '{print $NF}'
}
@@ -50,6 +45,14 @@ function disable_all_modpacks() {
sed -i 's/ENABLED/DISABLED/' "$MODPACKS_DB_FILE"
}
function remove_disabled_prefix() {
local _file="$1"
while [[ "$_file" == disabled_* ]]; do
_file=$(echo "$_file" | sed 's/^disabled_//')
done
echo "$_file"
}
function get_mod_name_and_index() {
if [[ -n "$mod_index" ]]; then # if mod index exists
entry=$(grep "^${mod_index}," "$DB_FILE")
@@ -375,7 +378,7 @@ function downgrade_mods() {
echo -e "Downgraded ${ORANGE}$mod${NC} to ${GREEN}\$MODS_DIR/$new_patch${NC}." >&2
# save changes in database as well
sed -i "s/^$mod$/$new_patch/" "$DB_FILE"
sed -i "s/\(\b$mod\b\)/$new_patch/" "$DB_FILE"
fi
done
done
@@ -388,6 +391,9 @@ function upgrade_mods() {
# opposite of downgrade_mods
for file in $files; do
# remove disabled_ prefix if it exists
file=$(remove_disabled_prefix "$file")
current_version=$(echo "$file" | grep -oP '(?<=patch_)\d+')
base_name=$(get_basename "$file")
@@ -415,7 +421,7 @@ function upgrade_mods() {
[[ $? -ne 0 ]] && { echo -e "${RED}Error${NC}: Could not upgrade mod file $mod." >&2; exit 1; }
echo -e "Upgraded ${ORANGE}$mod${NC} to ${GREEN}\$MODS_DIR/$new_patch${NC}." >&2
sed -i "s/^$mod$/$new_patch/" "$DB_FILE"
sed -i "s/\(\b$mod\b\)/$new_patch/" "$DB_FILE"
fi
done
done
@@ -460,16 +466,23 @@ function mod_disable() {
# disable each mod file by adding disabled_ to the start of the filename
files=$(get_files_by_entry_from_db "$entry")
for file in $files; do
if [[ ! -f "$MODS_DIR/$file" ]]; then
echo -e "${RED}Error${NC}: Mod file $file does not exist." >&2
exit 1
else
[[ ! -f "$MODS_DIR/$file" ]] && { echo -e "${RED}Error${NC}: Mod file $file does not exist." >&2; exit 1; }
disabled_file="disabled_$file"
mv "$MODS_DIR/$file" "$MODS_DIR/$disabled_file"
echo -e "Disabled ${ORANGE}$file${NC} (changed to ${GREEN}\$MODS_DIR/$disabled_file${NC})." >&2
fi
while [[ -f "$MODS_DIR/$disabled_file" ]]; do
disabled_file="disabled_$disabled_file"
done
mv "$MODS_DIR/$file" "$MODS_DIR/$disabled_file"
[[ $? -ne 0 ]] && { echo -e "${RED}Error${NC}: Could not disable mod file $file." >&2; exit 1; }
echo -e "Disabled ${ORANGE}$file${NC} (changed to ${GREEN}\$MODS_DIR/$disabled_file${NC})." >&2
# save change to db
sed -i "/^$mod_index,/ s/\(\b$file\b\)/$disabled_file/" "$DB_FILE"
done
# downgrade mods with greater version number
downgrade_mods "$files"
# update the database
@@ -514,16 +527,19 @@ function mod_enable() {
# enable each mod file by removing disabled_ from the start of the filename
for file in $files; do
disabled_file="disabled_$file"
enabled_file=$(remove_disabled_prefix "$file")
# check if the files exists
[[ -f "$MODS_DIR/$disabled_file" ]] || { echo -e "${RED}Error${NC}: Mod file $file does not exist." >&2; exit 1; }
[[ -f "$MODS_DIR/$file" ]] || { echo -e "${RED}Error${NC}: Mod file $file does not exist." >&2; exit 1; }
mv "$MODS_DIR/$disabled_file" "$MODS_DIR/$file"
mv "$MODS_DIR/$file" "$MODS_DIR/$enabled_file"
# check if the file was moved successfully
[[ $? -ne 0 ]] && { echo -e "${RED}Error${NC}: Could not enable mod file $disabled_file." >&2; exit 1; }
echo -e "Enabled ${ORANGE}$disabled_file${NC} (changed to ${GREEN}\$MODS_DIR/$file${NC})." >&2
[[ $? -ne 0 ]] && { echo -e "${RED}Error${NC}: Could not enable mod file $file." >&2; exit 1; }
echo -e "Enabled ${ORANGE}$file${NC} (changed to ${GREEN}\$MODS_DIR/$enabled_file${NC})." >&2
# save change to db
sed -i "/^$mod_index,/ s/\(\b$file\b\)/$enabled_file/" "$DB_FILE"
done
# update the database
@@ -735,8 +751,8 @@ function mod_uninstall() {
[[ $? -ne 0 ]] && { echo -e "${RED}Error${NC}: Could not remove mod file $file." >&2; exit 1; }
done
# downgrade mods with greater version number
downgrade_mods "$files"
# downgrade mods with greater version number, only if the mod is enabled
[[ $disabled == false ]] && downgrade_mods "$files"
# remove entry from database
sed -i "/^$mod_index,/d" "$DB_FILE"
@@ -830,21 +846,6 @@ function mod_import() {
[[ $? -ne 0 ]] && { echo -e "${RED}Error${NC}: Failed to import mods." >&2; exit 1; }
# handle breaking chanegs
# version 0.2.x has no version number in the database file, so if there's no version number, add it
if [[ $(head -n 1 "$DB_FILE") =~ "[0-9]+," ]]; then
mods_db_current_version=$(head -n 1 "$DB_FILE")
else
mods_db_current_version="2"
fi
mods_db_new_version=$(get_version_major $VERSION)
for ((i = mods_db_current_version + 1; i <= mods_db_new_version; i++)); do
eval $(echo "${BREAKING_CHANGES_DB_FILE_PATCHES[$i]}" | sed "s:\$1:$DB_FILE:")
[[ $? -ne 0 ]] && { echo -e "${RED}Error${NC}: Failed to apply breaking changes patch $i." >&2; exit 1; }
done
echo -e "Mods imported ${GREEN}successfully${NC}." >&2
}
+7 -1
View File
@@ -105,7 +105,13 @@ fi
# Install
IFS= read -ep "Install the script to $DESTINATION_PATH or specify another path (must be included in \$PATH)? (Y/path): " response
# if installing on steam deck, prompt the user, set flag
is_steam_deck=false
IFS= read -ep "Are you installing on a Steam Deck? (y/N): " response_steam_deck
[[ "$is_steam_deck" == "y" || "$is_steam_deck" == "Y" ]] && is_steam_deck=true
# other path if needed
[[ $is_steam_deck == false ]] && IFS= read -ep "Install the script to $DESTINATION_PATH or specify another path (must be included in \$PATH)? (Y/path): " response
if [[ "$response" != "y" && "$response" != "Y" && -n "$response" ]]; then
DESTINATION_PATH="$response"
+1 -1
View File
@@ -1 +1 @@
0.3.2
0.3.3