vcmibuilder 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #!/bin/bash -e
  2. #
  3. # VCMI data builder script
  4. # Extracts game data from various sources and creates a tree with the right files
  5. #
  6. # Authors: listed in file AUTHORS in main folder
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # no console arguments - print help
  14. if [ $# -eq 0 ] ; then
  15. print_help=true
  16. fi
  17. # command line parsing
  18. # can't use system getopt which is not cross-platform (BSD/Mac)
  19. # can't use built-in getopts which can't parse long options (too difficult to avoid - e.g. CD1/CD2)
  20. while [ $# -gt 0 ]
  21. do
  22. case $1 in
  23. --cd1) cd1_dir=$2 ; shift 2 ;;
  24. --cd2) cd2_dir=$2 ; shift 2 ;;
  25. --gog) gog_file=$2 ; shift 2 ;;
  26. --data) data_dir=$2 ; shift 2 ;;
  27. --dest) dest_dir=$2 ; shift 2 ;;
  28. --convertMP3) useffmpeg=true; shift 1 ;;
  29. --validate) validate=true ; shift 1 ;;
  30. *) print_help=true ; shift 1 ;;
  31. esac
  32. done
  33. if [[ -n "$print_help" ]]
  34. then
  35. echo "VCMI data builder utility"
  36. echo "Usage: vcmibuilder <options>"
  37. echo "Options:"
  38. echo " --cd1 DIRECTORY " "Path to mounted first CD with Heroes 3 install files"
  39. echo " " "Requires unshield"
  40. echo
  41. echo " --cd2 DIRECTORY " "Path to second CD with Heroes 3 data files."
  42. echo
  43. echo " --gog EXECUTABLE " "Path to gog.com executable"
  44. echo " " "Requires innoextract ( http://constexpr.org/innoextract/ )"
  45. echo
  46. echo " --data DIRECTORY " "Path to installed Heroes 3 data"
  47. echo
  48. echo " --convertMP3 " "Convert all mp3 files into ogg/vorbis"
  49. echo " " "Requires ffmpeg or avconv"
  50. echo
  51. echo " --dest DIRECTORY " "Path where resulting data will be placed. Default is ~/.local/share/vcmi"
  52. echo
  53. echo " --validate " "Run basic validness checks"
  54. exit 0
  55. fi
  56. # test if a program version ($1) is less than the required one ($2)
  57. verlt() {
  58. [ "$1" = "$2" ] && return 1 || \
  59. [ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
  60. }
  61. # test presence of program $1, $2 will be passed as parameters to test presence
  62. test_utility ()
  63. {
  64. $1 $2 > /dev/null 2>&1 || { echo "$1 was not found. Please install it" 1>&2 ; exit 1; }
  65. }
  66. # print error message and exit
  67. fail ()
  68. {
  69. $2
  70. echo "$1" 1>&2
  71. rm -rf "$temp_dir"
  72. exit 1
  73. }
  74. # print warning to stderr.
  75. warning ()
  76. {
  77. echo "$1" 1>&2
  78. warn_user=true
  79. }
  80. #checks whether specified directory exists. Also works with globs
  81. dir_exists() {
  82. [ -d "$1" ]
  83. }
  84. # check if selected options are correct.
  85. if [[ -n "$data_dir" ]]
  86. then
  87. if [[ -n "$gog_file" ]] || [[ -n "$cd1_dir" ]] || [[ -n "$cd2_dir" ]]
  88. then
  89. warning "Warning: Installed data dir was specified. Both gog and cd options will be ignored"
  90. unset gog_file cd1_dir cd2_dir
  91. fi
  92. fi
  93. if [[ -n "$gog_file" ]]
  94. then
  95. test_utility "innoextract"
  96. if [[ -n "$cd1_dir" ]] || [[ -n "$cd2_dir" ]]
  97. then
  98. warning "Warning: Both gog and cd options were specified. cd options will be ignored"
  99. unset cd1_dir cd2_dir
  100. fi
  101. fi
  102. if [[ -n "$gog_file" ]]
  103. then
  104. currentver="$(innoextract --version | head -n1 | cut -d ' ' -f2)"
  105. requiredver="1.6"
  106. if verlt $currentver $requiredver;
  107. then
  108. fail "innoextract version is $currentver, update it to version $requiredver or higher"
  109. fi
  110. fi
  111. if [[ -n "$useffmpeg" ]]
  112. then
  113. ffmpeg -version > /dev/null 2>&1 && AUDIO_CONV=ffmpeg
  114. avconv -version > /dev/null 2>&1 && AUDIO_CONV=avconv
  115. if [[ -z "$AUDIO_CONV" ]]
  116. then
  117. fail "Error: neither ffmpeg nor avconv were found. Please install it."
  118. fi
  119. fi
  120. if [[ -n "$cd1_dir" ]]
  121. then
  122. test_utility "unshield" "-V"
  123. fi
  124. if [[ -z "$gog_file" ]] && [[ -z "$data_dir" ]] && ( [[ -z "$cd1_dir" ]] || [[ -z "$cd2_dir" ]] )
  125. then
  126. warning "Warning: Selected options will not create complete Heroes 3 data!"
  127. fi
  128. # if at least one warning has been printed - ask for confirmation
  129. if [[ -n "$warn_user" ]]
  130. then
  131. read -p "Do you wish to continue? (y/n) " -n 1
  132. echo #print eol
  133. if [[ ! $REPLY =~ ^[Yy]$ ]]
  134. then
  135. exit 1
  136. fi
  137. fi
  138. if [[ -z "$dest_dir" ]]
  139. then
  140. if [[ "$(uname)" == Darwin ]]
  141. then
  142. dest_dir="$HOME/Library/Application Support/vcmi"
  143. elif [[ -z "$XDG_DATA_HOME" ]]
  144. then
  145. dest_dir="$HOME/.local/share/vcmi"
  146. else
  147. dest_dir="$XDG_DATA_HOME/vcmi"
  148. fi
  149. fi
  150. temp_dir="$dest_dir"/buildertmp
  151. # start installation
  152. mkdir -p "$dest_dir"
  153. mkdir -p "$temp_dir"
  154. if [[ -n "$gog_file" ]]
  155. then
  156. data_dir="$temp_dir"/gog
  157. mkdir -p "$data_dir"
  158. # innoextract always reports error (iconv 84 error). Just test file for presence
  159. test -f "$gog_file" || fail "Error: gog.com executable was not found!"
  160. gog_file="$(cd "$(dirname "$gog_file")"; pwd)/$(basename "$gog_file")"
  161. cd "$data_dir" && innoextract "$gog_file"
  162. # some versions of gog.com installer (or innoextract tool?) place game files inside /app directory
  163. if dir_exists "$data_dir"/app/[Dd][Aa][Tt][Aa]
  164. then
  165. data_dir="$data_dir"/app
  166. fi
  167. fi
  168. if [[ -n "$cd1_dir" ]]
  169. then
  170. data_dir="$temp_dir"/cddir
  171. mkdir -p "$data_dir"
  172. unshield -d "$data_dir" x "$cd1_dir"/_setup/data1.cab || fail "Error: failed to extract from Install Shield installer!" "rm -rf $data_dir"
  173. # a bit tricky - different releases have different root directory. Move extracted files to data_dir
  174. if [ -d "$data_dir"/Heroes3 ]
  175. then
  176. mv "$data_dir"/Heroes3/* "$data_dir"
  177. elif [ -d "$data_dir""/Program_Files" ]
  178. then
  179. mv "$data_dir"/Program_Files/* "$data_dir"
  180. elif [ -d "$data_dir""/LangInde_Program_Files" ]
  181. then
  182. mv "$data_dir"/LangInde_Program_Files/* "$data_dir"
  183. else
  184. echo "Error: failed to find extracted game files!"
  185. echo "Extracted directories are: "
  186. ls -la "$data_dir"
  187. echo "Please report this on vcmi.eu"
  188. exit 1;
  189. fi
  190. fi
  191. if [[ -n "$cd2_dir" ]]
  192. then
  193. mkdir -p "$dest_dir"/Data
  194. if [ -d "$cd2_dir"/heroes3 ]
  195. then
  196. cp "$cd2_dir"/heroes3/Data/Heroes3.vid "$dest_dir"/Data/Heroes3.vid
  197. cp "$cd2_dir"/heroes3/Data/Heroes3.snd "$dest_dir"/Data/Heroes3-cd2.snd
  198. else
  199. cp "$cd2_dir"/Heroes3/Data/Heroes3.vid "$dest_dir"/Data/Heroes3.vid
  200. cp "$cd2_dir"/Heroes3/Data/Heroes3.snd "$dest_dir"/Data/Heroes3-cd2.snd
  201. fi
  202. fi
  203. if [[ -n "$data_dir" ]]
  204. then
  205. # this folder is named differently from time to time
  206. # bash also has `shopt -s nocaseglob` but we don't want this to
  207. # accidentally influence other parts of this script
  208. # since the directory names are short, we use this pattern matching
  209. cp -r "$data_dir"/[Dd][Aa][Tt][Aa] "$dest_dir"
  210. cp -r "$data_dir"/[Mm][Aa][Pp][Ss] "$dest_dir"
  211. cp -r "$data_dir"/[Mm][Pp]3 "$dest_dir"
  212. fi
  213. if [[ -n "$useffmpeg" ]]
  214. then
  215. # now when all music files were installed convert them to ogg
  216. echo "Converting mp3 files..."
  217. OIFS="$IFS"
  218. IFS=$'\n'
  219. for file in `find "$dest_dir" -type f -iname "*.mp3"`
  220. do
  221. echo "Converting $file"
  222. ogg=${file%.*}
  223. $AUDIO_CONV -y -i "$file" -acodec libvorbis "$ogg".ogg 2>/dev/null || fail "Error: failed to convert $file to ogg/vorbis using $AUDIO_CONV" "rm -f "$ogg".ogg"
  224. rm -f $file
  225. done
  226. IFS="$OIFS"
  227. fi
  228. if [[ -n "$validate" ]]
  229. then
  230. test -f "$dest_dir"/[Dd][Aa][Tt][Aa]/H3bitmap.lod || fail "Error: Heroes 3 data files are missing!"
  231. test -f "$dest_dir"/[Dd][Aa][Tt][Aa]/H3sprite.lod || fail "Error: Heroes 3 data files are missing!"
  232. test -f "$dest_dir"/[Dd][Aa][Tt][Aa]/VIDEO.VID || fail "Error: Heroes 3 data files (CD2) are missing!"
  233. #test -d "$dest_dir"/Mods/vcmi/Data || fail "Error: VCMI data files are missing!"
  234. fi
  235. rm -rf "$temp_dir"