vcmibuilder 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. # --download) download=true ; shift 1 ;;
  30. --validate) validate=true ; shift 1 ;;
  31. *) print_help=true ; shift 1 ;;
  32. esac
  33. done
  34. if [[ -n "$print_help" ]]
  35. then
  36. echo "VCMI data builder utility"
  37. echo "Usage: vcmibuilder <options>"
  38. echo "Options:"
  39. echo " --cd1 DIRECTORY " "Path to mounted first CD with Heroes 3 install files"
  40. echo " " "Requires unshield"
  41. echo
  42. echo " --cd2 DIRECTORY " "Path to second CD with Heroes 3 data files."
  43. echo
  44. echo " --gog EXECUTABLE " "Path to gog.com executable"
  45. echo " " "Requires innoextract ( http://constexpr.org/innoextract/ )"
  46. echo
  47. echo " --data DIRECTORY " "Path to installed Heroes 3 data"
  48. echo
  49. echo " --convertMP3 " "Convert all mp3 files into ogg/vorbis"
  50. echo " " "Requires ffmpeg or avconv"
  51. echo
  52. # echo " --download " "Automatically download optional package using wget"
  53. # echo " " "Requires wget and Internet connection"
  54. # echo
  55. echo " --dest DIRECTORY " "Path where resulting data will be placed. Default is ~/.local/share/vcmi"
  56. echo
  57. echo " --validate " "Run basic validness checks"
  58. exit 0
  59. fi
  60. # test presence of program $1, $2 will be passed as parameters to test presence
  61. test_utility ()
  62. {
  63. $1 $2 > /dev/null 2>&1 || { echo "$1 was not found. Please install it" 1>&2 ; exit 1; }
  64. }
  65. #print error message and exit
  66. fail ()
  67. {
  68. $2
  69. echo "$1" 1>&2
  70. rm -rf "$temp_dir"
  71. exit 1
  72. }
  73. # print warning to stderr.
  74. warning ()
  75. {
  76. echo "$1" 1>&2
  77. warn_user=true
  78. }
  79. # check if selected options are correct.
  80. if [[ -n "$data_dir" ]]
  81. then
  82. if [[ -n "$gog_file" ]] || [[ -n "$cd1_dir" ]] || [[ -n "$cd2_dir" ]]
  83. then
  84. warning "Warning: Installed data dir was specified. Both gog and cd options will be ignored"
  85. unset gog_file cd1_dir cd2_dir
  86. fi
  87. fi
  88. if [[ -n "$gog_file" ]]
  89. then
  90. test_utility "innoextract"
  91. if [[ -n "$cd1_dir" ]] || [[ -n "$cd2_dir" ]]
  92. then
  93. warning "Warning: Both gog and cd options were specified. cd options will be ignored"
  94. unset cd1_dir cd2_dir
  95. fi
  96. fi
  97. if [[ -n "$useffmpeg" ]]
  98. then
  99. ffmpeg -version > /dev/null 2>&1 && AUDIO_CONV=ffmpeg
  100. avconv -version > /dev/null 2>&1 && AUDIO_CONV=avconv
  101. if [[ -z "$AUDIO_CONV" ]]
  102. then
  103. fail "Error: neither ffmpeg nor avconv were found. Please install it."
  104. fi
  105. fi
  106. if [[ -n "$cd1_dir" ]]
  107. then
  108. test_utility "unshield" "-V"
  109. fi
  110. if [[ -n "$download" ]]
  111. then
  112. test_utility "unzip"
  113. fi
  114. if [[ -z "$gog_file" ]] && [[ -z "$data_dir" ]] && ( [[ -z "$cd1_dir" ]] || [[ -z "$cd2_dir" ]] )
  115. then
  116. warning "Warning: Selected options will not create complete Heroes 3 data!"
  117. fi
  118. # if at least one warning has been printed - ask for confirmation
  119. if [[ -n "$warn_user" ]]
  120. then
  121. read -p "Do you wish to continue? (y/n) " -n 1
  122. echo #print eol
  123. if [[ ! $REPLY =~ ^[Yy]$ ]]
  124. then
  125. exit 1
  126. fi
  127. fi
  128. if [[ -z "$dest_dir" ]]
  129. then
  130. if [[ -z "$XDG_DATA_HOME" ]]
  131. then
  132. dest_dir="$HOME/.local/share/vcmi"
  133. else
  134. dest_dir="$XDG_DATA_HOME/vcmi"
  135. fi
  136. fi
  137. temp_dir="$dest_dir"/buildertmp
  138. # start installation
  139. mkdir -p "$dest_dir"
  140. mkdir -p "$temp_dir"
  141. if [[ -n "$gog_file" ]]
  142. then
  143. # innoextract always reports error (iconv 84 error). Just test file for presence
  144. test -f "$gog_file" || fail "Error: gog.com executable was not found!"
  145. gog_file="$(cd "$(dirname "$gog_file")"; pwd)/$(basename "$gog_file")"
  146. cd "$temp_dir" && innoextract "$gog_file"
  147. data_dir="$temp_dir"/app
  148. fi
  149. if [[ -n "$cd1_dir" ]]
  150. then
  151. data_dir="$temp_dir"/cddir
  152. mkdir -p "$data_dir"
  153. unshield -d "$data_dir" x "$cd1_dir"/_setup/data1.cab || fail "Error: failed to extract from Install Shield installer!" "rm -rf $data_dir"
  154. # a bit tricky - different releases have different root directory. Move extracted files to data_dir
  155. if [ -d "$data_dir"/Heroes3 ]
  156. then
  157. mv "$data_dir"/Heroes3/* "$data_dir"
  158. elif [ -d "$data_dir""/Program_Files" ]
  159. then
  160. mv "$data_dir"/Program_Files/* "$data_dir"
  161. elif [ -d "$data_dir""/LangInde_Program_Files" ]
  162. then
  163. mv "$data_dir"/LangInde_Program_Files/* "$data_dir"
  164. else
  165. echo "Error: failed to find extracted game files!"
  166. echo "Extracted directories are: "
  167. ls -la "$data_dir"
  168. echo "Please report this on vcmi.eu"
  169. exit 1;
  170. fi
  171. fi
  172. if [[ -n "$cd2_dir" ]]
  173. then
  174. mkdir -p "$dest_dir"/Data
  175. if [ -d "$cd2_dir"/heroes3 ]
  176. then
  177. cp "$cd2_dir"/heroes3/Data/Heroes3.vid "$dest_dir"/Data/Heroes3.vid
  178. cp "$cd2_dir"/heroes3/Data/Heroes3.snd "$dest_dir"/Data/Heroes3-cd2.snd
  179. else
  180. cp "$cd2_dir"/Heroes3/Data/Heroes3.vid "$dest_dir"/Data/Heroes3.vid
  181. cp "$cd2_dir"/Heroes3/Data/Heroes3.snd "$dest_dir"/Data/Heroes3-cd2.snd
  182. fi
  183. fi
  184. if [[ -n "$data_dir" ]]
  185. then
  186. # this folder is named differently from time to time
  187. # bash also has `shopt -s nocaseglob` but we don't want this to
  188. # accidentally influence other parts of this script
  189. # since the directory names are short, we use this pattern matching
  190. cp -r "$data_dir"/[Dd][Aa][Tt][Aa] "$dest_dir"
  191. cp -r "$data_dir"/[Mm][Aa][Pp][Ss] "$dest_dir"
  192. cp -r "$data_dir"/[Mm][Pp]3 "$dest_dir"
  193. fi
  194. if [[ -n "$download" ]]
  195. then
  196. wget "http://download.vcmi.eu/core.zip" -O "$temp_dir"/core.zip || fail "Error: failed to download VCMI archive!" "rm -f core.zip"
  197. vcmi_archive="$temp_dir"/core.zip
  198. fi
  199. if [[ -n "$vcmi_archive" ]]
  200. then
  201. echo "decompressing $vcmi_archive"
  202. unzip -qo "$vcmi_archive" -d "$dest_dir" || fail "Error: failed to extract VCMI archive!"
  203. fi
  204. if [[ -n "$useffmpeg" ]]
  205. then
  206. # now when all music files were installed convert them to ogg
  207. echo "Converting mp3 files..."
  208. OIFS="$IFS"
  209. IFS=$'\n'
  210. for file in `find "$dest_dir" -type f -iname "*.mp3"`
  211. do
  212. echo "Converting $file"
  213. ogg=${file%.*}
  214. $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"
  215. rm -f $file
  216. done
  217. IFS="$OIFS"
  218. fi
  219. if [[ -n "$validate" ]]
  220. then
  221. test -f "$dest_dir"/Data/H3bitmap.lod || fail "Error: Heroes 3 data files are missing!"
  222. test -f "$dest_dir"/Data/H3sprite.lod || fail "Error: Heroes 3 data files are missing!"
  223. test -f "$dest_dir"/Data/VIDEO.VID || fail "Error: Heroes 3 data files (CD2) are missing!"
  224. test -d "$dest_dir"/Mods/vcmi/Data || fail "Error: VCMI data files are missing!"
  225. fi
  226. rm -rf "$temp_dir"