| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | ########################################################################### |
| 4 | # fheroes2: https://github.com/ihhub/fheroes2 # |
| 5 | # Copyright (C) 2025 # |
| 6 | # # |
| 7 | # This program is free software; you can redistribute it and/or modify # |
| 8 | # it under the terms of the GNU General Public License as published by # |
| 9 | # the Free Software Foundation; either version 2 of the License, or # |
| 10 | # (at your option) any later version. # |
| 11 | # # |
| 12 | # This program is distributed in the hope that it will be useful, # |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
| 15 | # GNU General Public License for more details. # |
| 16 | # # |
| 17 | # You should have received a copy of the GNU General Public License # |
| 18 | # along with this program; if not, write to the # |
| 19 | # Free Software Foundation, Inc., # |
| 20 | # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # |
| 21 | ########################################################################### |
| 22 | |
| 23 | set -e -o pipefail |
| 24 | |
| 25 | function echo_red { |
| 26 | echo -e "\033[0;31m$*\033[0m" |
| 27 | } |
| 28 | |
| 29 | function echo_green { |
| 30 | echo -e "\033[0;32m$*\033[0m" |
| 31 | } |
| 32 | |
| 33 | function echo_stage { |
| 34 | echo |
| 35 | echo_green "$*" |
| 36 | echo |
| 37 | } |
| 38 | |
| 39 | echo_green "This script will extract and copy game resources from the original Heroes of Might and Magic II CD image." |
| 40 | |
| 41 | echo_stage "[1/4] determining the destination directory" |
| 42 | |
| 43 | DEST_PATH="" |
| 44 | |
| 45 | if [[ -n "$2" ]]; then |
| 46 | DEST_PATH="$2" |
| 47 | elif [[ -f fheroes2 && -x fheroes2 ]]; then |
| 48 | DEST_PATH="." |
| 49 | elif [[ -d ../../src ]]; then |
| 50 | # Special hack for developers running this script from the source tree |
| 51 | DEST_PATH="../.." |
| 52 | fi |
| 53 | |
| 54 | if [[ -z "$DEST_PATH" || ( -d "$DEST_PATH" && ! -w "$DEST_PATH" ) ]]; then |
| 55 | if [[ "$(uname 2> /dev/null)" == "Linux" ]]; then |
| 56 | DEST_PATH="${XDG_CONFIG_HOME:-$HOME/.local/share}/fheroes2" |
| 57 | elif [[ -z "$2" ]]; then |
| 58 | DEST_PATH="$HOME/.fheroes2" |
| 59 | fi |
| 60 | fi |
| 61 | |
| 62 | echo_green "Destination directory: $DEST_PATH" |
| 63 | |
| 64 | echo_stage "[2/4] determining the path to the HoMM2 CD image" |
| 65 | |
| 66 | if [[ "$#" -gt "0" ]]; then |
| 67 | HOMM2_CD_IMAGE_PATH="$1" |
| 68 | else |
| 69 | read -e -p "Please enter the full path to the HoMM2 CD image (e.g. /home/user/homm2.cue or /home/user/homm2.iso): " HOMM2_CD_IMAGE_PATH |
| 70 | fi |
| 71 | |
| 72 | STAGE_PATH=$(mktemp -d) |
| 73 | trap 'rm -rf "$STAGE_PATH"' EXIT |
| 74 | |
| 75 | function stage_iso_assets { |
| 76 | if [[ -z "$(command -v fuseiso)" ]]; then |
| 77 | echo_red "fuseiso was not found in your system. Unable to mount CD image. Installation aborted." |
| 78 | exit 1 |
| 79 | fi |
| 80 | if [[ -z "$(command -v fusermount)" ]]; then |
| 81 | echo_red "fusermount was not found in your system. Unable to unmount CD image. Installation aborted." |
| 82 | exit 1 |
| 83 | fi |
| 84 | |
| 85 | mkdir ROM |
| 86 | fuseiso "$1" ROM |
| 87 | trap 'fusermount -u ROM' RETURN |
| 88 | |
| 89 | HOMM2_DIR=$(find ROM -type d -iname heroes2) |
| 90 | if [[ -z "$HOMM2_DIR" ]]; then |
| 91 | echo_red "Unable to locate 'heroes2' directory in CD image." |
| 92 | exit 1 |
| 93 | fi |
| 94 | |
| 95 | copy_directory "$HOMM2_DIR" "ANIM" |
| 96 | |
| 97 | DATA_CAB_PATH=$(find "ROM" -type f -iname data1.cab) |
| 98 | if [[ -n "$DATA_CAB_PATH" ]]; then |
| 99 | if [[ -z "$(command -v unshield)" ]]; then |
| 100 | echo_red "unshield was not found in your system. Unable to extract 'data1.cab'. Installation aborted." |
| 101 | exit 1 |
| 102 | fi |
| 103 | unshield -d "CAB" x "$DATA_CAB_PATH" > /dev/null |
| 104 | |
| 105 | copy_directory "CAB" "MAPS" |
| 106 | copy_directory "CAB" "DATA" |
| 107 | |
| 108 | rm -rf CAB |
| 109 | else |
| 110 | copy_directory "$HOMM2_DIR" "MAPS" |
| 111 | copy_directory "$HOMM2_DIR" "DATA" |
| 112 | fi |
| 113 | |
| 114 | fusermount -u ROM |
| 115 | trap - RETURN |
| 116 | |
| 117 | rmdir ROM |
| 118 | } |
| 119 | |
| 120 | function copy_directory() { |
| 121 | BASE_DIR=$1 |
| 122 | DIR_NAME=$2 |
| 123 | |
| 124 | DIR_PATH=$(find "$BASE_DIR" -type d -iname "$DIR_NAME") |
| 125 | if [[ -n "$DIR_PATH" ]]; then |
| 126 | mkdir "$DIR_NAME" |
| 127 | |
| 128 | cp -r "$DIR_PATH/." "$DIR_NAME" |
| 129 | fi |
| 130 | } |
| 131 | |
| 132 | echo_stage "[3/4] staging game resources" |
| 133 | cd "$STAGE_PATH" |
| 134 | CD_IMAGE_FILE_NAME=$(basename "$HOMM2_CD_IMAGE_PATH") |
| 135 | CD_IMAGE_EXTENSION=${CD_IMAGE_FILE_NAME##*.} |
| 136 | if [[ "${CD_IMAGE_EXTENSION^^}" == "CUE" ]]; then |
| 137 | if [[ -z "$(command -v bchunk)" ]]; then |
| 138 | echo_red "bchunk was not found in your system. Unable to rip CD audio. Installation aborted." |
| 139 | exit 1 |
| 140 | fi |
| 141 | # Parse the path of the bin file directly from the CUE sheet, so that this |
| 142 | # will continue to work even if the name of the cue sheet doesn't match the |
| 143 | # name of the bin file. |
| 144 | BIN_PATH="$(dirname "$HOMM2_CD_IMAGE_PATH")/$(cat "$HOMM2_CD_IMAGE_PATH" | grep FILE | grep -oE '"([^"]+)"' | tr -d '"')" |
| 145 | bchunk -w "$BIN_PATH" "$HOMM2_CD_IMAGE_PATH" tmp_ > /dev/null |
| 146 | |
| 147 | if [[ -z "$(command -v flac)" ]]; then |
| 148 | echo_red "flac was not found in your system. Unable to reencode CD audio. Installation aborted." |
| 149 | exit 1 |
| 150 | fi |
| 151 | mkdir MUSIC |
| 152 | for WAV_PATH in *.wav; do |
| 153 | # The music ripped from the CD starts from index 2, since the first index |
| 154 | # is occupied by the ISO data. However, the engine expects the music to |
| 155 | # start from index zero, so we must subtract one from the index to meet |
| 156 | # this expectation. |
| 157 | WAV_FILE=$(basename "$WAV_PATH") |
| 158 | OLD_TRACK_NUM=$(echo "${WAV_FILE%.*}" | cut -d'_' -f2) |
| 159 | NEW_TRACK_NUM=$(printf '%02d' $((${OLD_TRACK_NUM#0}-1))) |
| 160 | NEW_FILE_NAME="homm2_$NEW_TRACK_NUM.flac" |
| 161 | # Flac generates a lot noise on the error stream, even when it's working |
| 162 | # as expected. So we redirect the error output to quiet things down. |
| 163 | flac --delete-input-file -8 "$WAV_PATH" -o "MUSIC/$NEW_FILE_NAME" 2> /dev/null |
| 164 | echo_green "Completed ripping $NEW_FILE_NAME to 'MUSIC'." |
| 165 | done |
| 166 | |
| 167 | stage_iso_assets "tmp_01.iso" |
| 168 | rm "tmp_01.iso" |
| 169 | elif [[ "${CD_IMAGE_EXTENSION^^}" == "ISO" ]]; then |
| 170 | stage_iso_assets "$HOMM2_CD_IMAGE_PATH" |
| 171 | else |
| 172 | echo_red "Unsupported CD image extension '$CD_IMAGE_EXTENSION'." |
| 173 | exit 1 |
| 174 | fi |
| 175 | |
| 176 | echo_stage "[4/4] copying game resources" |
| 177 | |
| 178 | [[ ! -d "$DEST_PATH" ]] && mkdir -p "$DEST_PATH" |
| 179 | |
| 180 | cd "$DEST_PATH" |
| 181 | cp -rf "$STAGE_PATH/." . |