games-mods.eclass 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. # Copyright 1999-2011 Gentoo Foundation
  2. # Distributed under the terms of the GNU General Public License v2
  3. # Variables to specify in an ebuild which uses this eclass:
  4. # GAME - (doom3, quake4 or ut2004, etc), unless ${PN} starts with e.g. "doom3-"
  5. # MOD_DESC - Description for the mod
  6. # MOD_NAME - Creates a command-line wrapper and desktop icon for the mod
  7. # MOD_DIR - Subdirectory name for the mod, if applicable
  8. # MOD_ICON - Custom icon for the mod, instead of the default
  9. inherit eutils games
  10. EXPORT_FUNCTIONS src_install pkg_postinst
  11. [[ -z ${GAME} ]] && GAME=${PN%%-*}
  12. case ${GAME} in
  13. doom3)
  14. GAME_PKGS="games-fps/doom3"
  15. GAME_DIRS=( "${GAMES_PREFIX_OPT}"/doom3 )
  16. GAME_NAME="Doom 3"
  17. GAME_BIN="doom3"
  18. GAME_ICON="doom3"
  19. DED_PKGS=""
  20. DED_BIN="doom3-ded"
  21. DED_OPTS="+set dedicated 1 +exec server.cfg"
  22. DED_CFG_DIR=".doom3"
  23. SELECT_MOD="+set fs_game "
  24. ;;
  25. enemy-territory)
  26. GAME_PKGS="games-fps/enemy-territory"
  27. GAME_DIRS=( "${GAMES_PREFIX_OPT}"/enemy-territory )
  28. GAME_NAME="Enemy Territory"
  29. GAME_BIN="et"
  30. GAME_ICON="ET"
  31. DED_PKGS=""
  32. DED_BIN="et-ded"
  33. DED_OPTS="+set dedicated 1 +exec server.cfg"
  34. DED_CFG_DIR=".etwolf"
  35. SELECT_MOD="+set fs_game "
  36. ;;
  37. quake3)
  38. GAME_PKGS="games-fps/quake3 games-fps/quake3-bin"
  39. GAME_DIRS=( "${GAMES_DATADIR}"/quake3 "${GAMES_PREFIX_OPT}"/quake3 )
  40. GAME_NAME="Quake III"
  41. GAME_BIN="quake3"
  42. GAME_ICON="quake3"
  43. DED_PKGS=""
  44. DED_BIN="quake3-ded"
  45. DED_OPTS="+set dedicated 1 +exec server.cfg"
  46. DED_CFG_DIR=".q3a"
  47. SELECT_MOD="+set fs_game "
  48. ;;
  49. quake4)
  50. GAME_PKGS="games-fps/quake4-bin"
  51. GAME_DIRS=( "${GAMES_PREFIX_OPT}"/quake4 )
  52. GAME_NAME="Quake 4"
  53. GAME_BIN="quake4"
  54. GAME_ICON="/usr/share/pixmaps/quake4.bmp"
  55. DED_PKGS=""
  56. DED_BIN="quake4-ded"
  57. DED_OPTS="+set dedicated 1 +exec server.cfg"
  58. DED_CFG_DIR=".quake4"
  59. SELECT_MOD="+set fs_game "
  60. ;;
  61. ut2003)
  62. GAME_PKGS="games-fps/ut2003"
  63. GAME_DIRS=( "${GAMES_PREFIX_OPT}"/ut2003 )
  64. GAME_NAME="UT2003"
  65. GAME_BIN="ut2003"
  66. GAME_ICON="ut2003"
  67. DED_PKGS=""
  68. DED_BIN="ucc"
  69. DED_OPTS=""
  70. DED_CFG_DIR=""
  71. SELECT_MOD="-mod="
  72. ;;
  73. ut2004)
  74. GAME_PKGS="games-fps/ut2004"
  75. GAME_DIRS=( "${GAMES_PREFIX_OPT}"/{ut2004,ut2004-ded} )
  76. GAME_NAME="UT2004"
  77. GAME_BIN="ut2004"
  78. GAME_ICON="ut2004"
  79. DED_PKGS="games-server/ut2004-ded"
  80. DED_BIN="ut2004-ded"
  81. DED_OPTS=""
  82. DED_CFG_DIR=""
  83. SELECT_MOD="-mod="
  84. ;;
  85. *)
  86. eerror "This game is either not supported or you must set the GAME"
  87. eerror "variable to the proper game."
  88. die "games-mods.eclass: unsupported GAME"
  89. ;;
  90. esac
  91. MOD_BIN="${GAME_BIN}-${PN/${GAME}-}"
  92. MOD_DED_BIN="${MOD_BIN}-ded"
  93. games-mods_get_rdepend() {
  94. local pkgs
  95. if [[ ${1} == "--ded" ]] ; then
  96. pkgs=( ${DED_PKGS} ${GAME_PKGS} )
  97. else
  98. pkgs=( ${GAME_PKGS} )
  99. fi
  100. [[ ${#pkgs[@]} -gt 1 ]] && echo -n "|| ( "
  101. case ${EAPI:-0} in
  102. 0|1) echo -n "${pkgs[@]}" ;;
  103. [23456])
  104. local p
  105. if [[ ${1} == "--ded" ]] ; then
  106. echo -n "${DED_PKGS}"
  107. for p in ${GAME_PKGS} ; do
  108. echo -n " ${p}[dedicated]"
  109. done
  110. else
  111. for p in ${GAME_PKGS} ; do
  112. echo -n " || ( ${p}[opengl] ${p}[-dedicated] )"
  113. done
  114. fi
  115. ;;
  116. *) die "EAPI ${EAPI} not supported"
  117. esac
  118. [[ ${#pkgs[@]} -gt 1 ]] && echo -n " )"
  119. }
  120. DESCRIPTION="${GAME_NAME} ${MOD_NAME} - ${MOD_DESC}"
  121. SLOT="0"
  122. IUSE="dedicated opengl"
  123. RESTRICT="mirror strip"
  124. DEPEND="app-arch/unzip"
  125. RDEPEND="dedicated? ( $(games-mods_get_rdepend --ded) )
  126. opengl? ( $(games-mods_get_rdepend) )
  127. !dedicated? ( !opengl? ( $(games-mods_get_rdepend) ) )"
  128. S=${WORKDIR}
  129. INS_DIR=${GAMES_DATADIR}/${GAME}
  130. games-mods_use_opengl() {
  131. [[ -z ${MOD_DIR} ]] && return 1
  132. if use opengl || ! use dedicated ; then
  133. # Use opengl by default
  134. return 0
  135. fi
  136. return 1
  137. }
  138. games-mods_use_dedicated() {
  139. [[ -z ${MOD_DIR} ]] && return 1
  140. use dedicated && return 0 || return 1
  141. }
  142. games-mods_dosyms() {
  143. # We are installing everything for these mods into ${INS_DIR},
  144. # ${GAMES_DATADIR}/${GAME} in most cases, and symlinking it
  145. # into ${GAMES_PREFIX_OPT}/${GAME} for each game. This should
  146. # allow us to support both binary and source-based games easily.
  147. local dir
  148. for dir in "${GAME_DIRS[@]}" ; do
  149. [[ -z ${dir} || ${INS_DIR} == ${dir} ]] && continue
  150. pushd "${D}/${INS_DIR}" > /dev/null || die "pushd failed"
  151. local i
  152. for i in * ; do
  153. if [[ -d ${i} ]] ; then
  154. if [[ ${i} == ${MOD_DIR} ]] ; then
  155. dosym "${INS_DIR}/${i}" "${dir}/${i}" \
  156. || die "dosym ${i} failed"
  157. else
  158. local f
  159. while read f ; do
  160. dosym "${INS_DIR}/${f}" "${dir}/${f}" \
  161. || die "dosym ${f} failed"
  162. done < <(find "${i}" -type f)
  163. fi
  164. elif [[ -f ${i} ]] ; then
  165. dosym "${INS_DIR}/${i}" "${dir}/${i}" \
  166. || die "dosym ${i} failed"
  167. else
  168. die "${i} shouldn't be there"
  169. fi
  170. done
  171. popd > /dev/null || die "popd failed"
  172. done
  173. }
  174. games-mods_make_initd() {
  175. cat <<EOF > "${T}"/${MOD_DED_BIN}
  176. #!/sbin/openrc-run
  177. $(head -n 2 ${PORTDIR}/header.txt)
  178. # Generated by games-mods.eclass
  179. depend() {
  180. need net
  181. }
  182. start() {
  183. ebegin "Starting ${MOD_DED_BIN}"
  184. start-stop-daemon --start --quiet --background --make-pidfile \\
  185. --pidfile /var/run/${MOD_DED_BIN}.pid \\
  186. --chuid \${${MOD_DED_BIN//-/_}_user}:\${${MOD_DED_BIN//-/_}_group} \\
  187. --env HOME="\${${MOD_DED_BIN//-/_}_home}" \\
  188. --exec "${GAMES_BINDIR}/${MOD_DED_BIN}" \\
  189. -- \${${MOD_DED_BIN//-/_}_opts}
  190. eend \$?
  191. }
  192. stop() {
  193. ebegin "Stopping ${MOD_DED_BIN}"
  194. start-stop-daemon --stop \\
  195. --pidfile /var/run/${MOD_DED_BIN}.pid
  196. eend \$?
  197. }
  198. EOF
  199. doinitd "${T}"/${MOD_DED_BIN} || die "doinitd failed"
  200. }
  201. games-mods_make_confd() {
  202. cat <<-EOF > "${T}"/${MOD_DED_BIN}
  203. # User and group the server should run as
  204. ${MOD_DED_BIN//-/_}_user="${GAMES_USER_DED}"
  205. ${MOD_DED_BIN//-/_}_group="${GAMES_GROUP}"
  206. # Directory to use for HOME
  207. ${MOD_DED_BIN//-/_}_home="${GAMES_PREFIX}"
  208. # Any extra options you want to pass to the dedicated server
  209. ${MOD_DED_BIN//-/_}_opts=""
  210. EOF
  211. doconfd "${T}"/${MOD_DED_BIN} || die "doconfd failed"
  212. }
  213. games-mods_src_install() {
  214. if games-mods_use_opengl ; then
  215. if [[ -n ${MOD_ICON} ]] ; then
  216. # Install custom icon
  217. local ext=${MOD_ICON##*.}
  218. if [[ -f ${MOD_ICON} ]] ; then
  219. newicon "${MOD_ICON}" ${PN}.${ext} || die "newicon failed"
  220. else
  221. newicon ${MOD_DIR}/"${MOD_ICON}" ${PN}.${ext} \
  222. || die "newicon failed"
  223. fi
  224. case ${ext} in
  225. bmp|ico)
  226. MOD_ICON=/usr/share/pixmaps/${PN}.${ext}
  227. ;;
  228. *)
  229. MOD_ICON=${PN}
  230. ;;
  231. esac
  232. else
  233. # Use the game's standard icon
  234. MOD_ICON=${GAME_ICON}
  235. fi
  236. games_make_wrapper ${MOD_BIN} "${GAME_BIN} ${SELECT_MOD}${MOD_DIR}"
  237. make_desktop_entry ${MOD_BIN} "${GAME_NAME} - ${MOD_NAME}" "${MOD_ICON}"
  238. # Since only quake3 has both a binary and a source-based install,
  239. # we only look for quake3 here.
  240. case ${GAME} in
  241. quake3)
  242. if has_version games-fps/quake3-bin ; then
  243. games_make_wrapper ${GAME_BIN}-bin-${PN/${GAME}-} \
  244. "${GAME_BIN}-bin ${SELECT_MOD}${MOD_DIR}"
  245. fi
  246. make_desktop_entry ${GAME_BIN}-bin-${PN/${GAME}-} \
  247. "${GAME_NAME} - ${MOD_NAME} (binary)" "${MOD_ICON}"
  248. ;;
  249. esac
  250. fi
  251. # We expect anything not wanted to have been deleted by the ebuild
  252. insinto "${INS_DIR}"
  253. doins -r * || die "doins -r failed"
  254. games-mods_dosyms
  255. if games-mods_use_dedicated ; then
  256. if [[ -f ${FILESDIR}/server.cfg ]] ; then
  257. insinto "${GAMES_SYSCONFDIR}"/${GAME}/${MOD_DIR}
  258. doins "${FILESDIR}"/server.cfg || die "doins server.cfg failed"
  259. dosym "${GAMES_SYSCONFDIR}"/${GAME}/${MOD_DIR}/server.cfg \
  260. "${GAMES_PREFIX}"/${DED_CFG_DIR}/${MOD_DIR}/server.cfg \
  261. || die "dosym server.cfg failed"
  262. fi
  263. games_make_wrapper ${MOD_DED_BIN} \
  264. "\"${GAMES_BINDIR}/${DED_BIN}\" ${SELECT_MOD}${MOD_DIR} ${DED_OPTS}"
  265. games-mods_make_initd
  266. games-mods_make_confd
  267. fi
  268. prepgamesdirs
  269. }
  270. games-mods_pkg_postinst() {
  271. games_pkg_postinst
  272. if games-mods_use_opengl ; then
  273. elog "To play this mod run:"
  274. elog " ${MOD_BIN}"
  275. fi
  276. if games-mods_use_dedicated ; then
  277. elog "To launch a dedicated server run:"
  278. elog " ${MOD_DED_BIN}"
  279. elog "To launch the server at startup run:"
  280. elog " rc-update add ${MOD_DED_BIN} default"
  281. fi
  282. }