Debian Codegraph @

script/homm2/extract_homm2_resources.sh

raw symbols SCIP
1 #!/usr/bin/env bash
2
3 ###########################################################################
4 # fheroes2: https://github.com/ihhub/fheroes2 #
5 # Copyright (C) 2021 - 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_yellow {
34 echo -e "\033[0;33m$*\033[0m"
35 }
36
37 function echo_stage {
38 echo
39 echo_green "$*"
40 echo
41 }
42
43 function verify_homm2_path {
44 [[ -f "$1/DATA/HEROES2.AGG" && -d "$1/MAPS" ]]
45 }
46
47 echo_green "This script will extract and copy game resources from the original distribution of Heroes of Might and Magic II"
48
49 echo_stage "[1/3] determining the destination directory"
50
51 DEST_PATH=""
52
53 if [[ -n "$2" ]]; then
54 DEST_PATH="$2"
55 elif [[ -f fheroes2 && -x fheroes2 ]]; then
56 DEST_PATH="."
57 elif [[ -d ../../src ]]; then
58 # Special hack for developers running this script from the source tree
59 DEST_PATH="../.."
60 fi
61
62 if [[ -z "$DEST_PATH" || ! -d "$DEST_PATH" || ! -w "$DEST_PATH" ]]; then
63 if [[ "$(uname 2> /dev/null)" == "Linux" ]]; then
64 DEST_PATH="${XDG_CONFIG_HOME:-$HOME/.local/share}/fheroes2"
65 elif [[ -z "$2" ]]; then
66 DEST_PATH="$HOME/.fheroes2"
67 fi
68 fi
69
70 echo_green "Destination directory: $DEST_PATH"
71
72 echo_stage "[2/3] determining the HoMM2 directory or installer package"
73
74 if [[ "$#" -gt "0" ]]; then
75 HOMM2_PATH="$1"
76 else
77 read -e -p "Please enter the full path to the HoMM2 directory or installer package (e.g. /home/user/homm2 or /tmp/installer.exe): " HOMM2_PATH
78 fi
79
80 if [[ -f "$HOMM2_PATH" ]]; then
81 if [[ -z "$(command -v innoextract)" ]]; then
82 echo_red "innoextract was not found in your system. Unable to extract the installer package. Installation aborted."
83 exit 1
84 fi
85
86 EXTRACT_DIR="$(mktemp -d)"
87
88 echo_green "Verifying the installer package, please wait..."
89
90 innoextract -e -s -d "$EXTRACT_DIR" -- "$HOMM2_PATH"
91
92 for ITEM in "$EXTRACT_DIR" "$EXTRACT_DIR"/*; do
93 if verify_homm2_path "$ITEM"; then
94 echo_green "HoMM2 installer package: $HOMM2_PATH"
95
96 HOMM2_PATH="$ITEM"
97
98 break
99 fi
100 done
101
102 if [[ -f "$HOMM2_PATH" ]]; then
103 echo_red "Unable to find HoMM2 files in this installer package. Installation aborted."
104 exit 1
105 fi
106 elif verify_homm2_path "$HOMM2_PATH"; then
107 echo_green "HoMM2 directory: $HOMM2_PATH"
108 else
109 echo_red "Unable to find the HoMM2 directory or installer package. Installation aborted."
110 exit 1
111 fi
112
113 echo_stage "[3/3] copying game resources"
114
115 [[ ! -d "$DEST_PATH" ]] && mkdir -p "$DEST_PATH"
116
117 cd "$DEST_PATH"
118
119 [[ ! -d anim ]] && mkdir anim
120 [[ ! -d data ]] && mkdir data
121 [[ ! -d maps ]] && mkdir maps
122 [[ ! -d music ]] && mkdir music
123
124 [[ -d "$HOMM2_PATH/HEROES2/ANIM" ]] && cp -r "$HOMM2_PATH/HEROES2/ANIM"/* anim
125 [[ -d "$HOMM2_PATH/ANIM" ]] && cp -r "$HOMM2_PATH/ANIM"/* anim
126 [[ -d "$HOMM2_PATH/DATA" ]] && cp -r "$HOMM2_PATH/DATA"/* data
127 [[ -d "$HOMM2_PATH/MAPS" ]] && cp -r "$HOMM2_PATH/MAPS"/* maps
128 [[ -d "$HOMM2_PATH/MUSIC" ]] && cp -r "$HOMM2_PATH/MUSIC"/* music
129
130 if [[ ! -f "$HOMM2_PATH/homm2.gog" ]]; then
131 exit 0
132 fi
133
134 # Special case - CD image from GOG
135
136 if [[ -n "$(command -v python3)" ]]; then
137 PYTHON="python3"
138 elif [[ -n "$(command -v python2)" ]]; then
139 PYTHON="python2"
140 elif [[ -n "$(command -v python)" ]]; then
141 PYTHON="python"
142 else
143 echo_yellow "python was not found in your system. Please install it and re-run this script to extract animation resources."
144 exit 0
145 fi
146
147 if [[ -z "$(command -v bsdtar)" ]]; then
148 echo_yellow "bsdtar was not found in your system. Please install it and re-run this script to extract animation resources."
149 exit 0
150 fi
151
152 echo_green "Extracting animation resources, please wait..."
153
154 "$PYTHON" - << EOF
155 with open("$HOMM2_PATH/homm2.gog", "rb") as raw_file:
156 with open("homm2.iso", "wb") as iso_file:
157 while True:
158 buf = raw_file.read(2352)
159 if not buf:
160 break
161 if buf[15] == 2:
162 iso_file.write(buf[24:2072])
163 else:
164 iso_file.write(buf[16:2064])
165 EOF
166
167 bsdtar -x -f homm2.iso -C anim --include "HEROES2/ANIM/*" --strip-components=2
168
169 rm -f homm2.iso