summary refs log tree commit diff
path: root/pkgs/games/openra/mod.nix
blob: 8df5922a44180b1cd7c6c60dcf93d373bdbf3e96 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*  The package defintion for an OpenRA out-of-tree mod.
    It shares code with `engine.nix` by what is defined in `common.nix`.
    To build an out-of-tree mod it needs the source code of the engine available,
    and they each need to be build with a specific version or fork of the engine,
    so the engine needs to be supplied as an argument as well.
    The engine is relatively small and quick to build, so this is not much of a problem.
    Building a mod will result in a wrapper script that starts the mod inside the specified engine.
*/
{ stdenv
, packageAttrs
, patchEngine
, wrapLaunchGame
, mod
, engine
}:

with stdenv.lib;

let
  engineSourceName = engine.src.name or "engine";
  modSourceName = mod.src.name or "mod";

# Based on: https://build.opensuse.org/package/show/home:fusion809/openra-ura
in stdenv.mkDerivation (recursiveUpdate packageAttrs rec {
  name = "${pname}-${version}";
  pname = "openra-${mod.name}";
  inherit (mod) version;

  srcs = [
    mod.src
    engine.src
  ];

  sourceRoot = ".";

  postUnpack = ''
    mv ${engineSourceName} ${modSourceName}
    cd ${modSourceName}
  '';

  postPatch = ''
    cat <<'EOF' > fetch-engine.sh
    #!/bin/sh
    exit 0
    EOF

    sed -i 's/^VERSION.*/VERSION = ${version}/g' Makefile

    dos2unix *.md

    ${patchEngine engineSourceName engine.version}
  '';

  configurePhase = ''
    runHook preConfigure

    make version VERSION=${escapeShellArg version}
    make -C ${engineSourceName} version VERSION=${escapeShellArg engine.version}

    runHook postConfigure
  '';

  checkTarget = "test";

  installPhase = ''
    runHook preInstall

    make -C ${engineSourceName} install-engine install-common-mod-files DATA_INSTALL_DIR=$out/lib/${pname}

    cp -r ${engineSourceName}/mods/{${concatStringsSep "," ([ "common" "modcontent" ] ++ engine.mods)}} mods/* \
      $out/lib/${pname}/mods/

    substitute ${./mod-launch-game.sh} $out/lib/${pname}/launch-game.sh \
      --subst-var out \
      --subst-var-by name ${escapeShellArg mod.name} \
      --subst-var-by title ${escapeShellArg mod.title} \
      --subst-var-by assetsError ${escapeShellArg mod.assetsError}
    chmod +x $out/lib/${pname}/launch-game.sh

    ${wrapLaunchGame "-${mod.name}"}

    substitute ${./openra-mod.desktop} $(mkdirp $out/share/applications)/${pname}.desktop \
      --subst-var-by name ${escapeShellArg mod.name} \
      --subst-var-by title ${escapeShellArg mod.title} \
      --subst-var-by description ${escapeShellArg mod.description}

    cp README.md $(mkdirp $out/share/doc/packages/${pname})/README.md

    [[ -e mods/${mod.name}/icon.png ]] && mod_icon=mods/${mod.name}/icon.png || {
      [[ -e mods/${mod.name}/logo.png ]] && mod_icon=mods/${mod.name}/logo.png || mod_icon=packaging/linux/mod_256x256.png
    }
    cp "$mod_icon" $(mkdirp $out/share/pixmaps)/${pname}.png

    for size in 16 32 48 64 128 256; do
      size=''${size}x''${size}
      cp packaging/linux/mod_''${size}.png $(mkdirp $out/share/icons/hicolor/''${size}/apps)/${pname}.png
    done

    runHook postInstall
  '';

  meta = {
    inherit (mod) description homepage;
  };
})