summaryrefslogtreecommitdiff
path: root/pkgs/games/dwarf-fortress/dwarf-therapist/wrapper.nix
blob: 62f4b3bdaf9127c4ea919c9dd6262e8ce4e5e6a2 (about) (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
{
  lib,
  stdenv,
  writeText,
  dwarf-therapist,
  dwarf-fortress,
  dfhack,
  mkDfWrapper,
  replaceVars,
  coreutils,
  wrapQtAppsHook,
  expect,
  xvfb-run,
}:

let
  platformSlug =
    let
      prefix = if dwarf-fortress.baseVersion >= 50 then "-classic_" else "_";
      base = if stdenv.hostPlatform.is32bit then "linux32" else "linux64";
    in
    prefix + base;
  inifile = "linux/v0.${builtins.toString dwarf-fortress.baseVersion}.${dwarf-fortress.patchVersion}${platformSlug}.ini";
  unsupportedVersion = lib.versionOlder dwarf-therapist.maxDfVersion dwarf-fortress.dfVersion;

  # Used to run dfhack to produce a Therapist ini file for the current memory map.
  # See: http://www.bay12forums.com/smf/index.php?topic=168411.msg8532557#msg8532557
  dfHackExpectScript = writeText "dfhack.exp" ''
    spawn xvfb-run dfhack +devel/export-dt-ini
    expect "DFHack is ready. Have a nice day!"
    send "die\r"
  '';

  dfHackWrapper = mkDfWrapper {
    inherit dwarf-fortress dfhack;
    enableDFHack = true;
  };
in

stdenv.mkDerivation {
  pname = "dwarf-therapist";
  inherit (dwarf-therapist) version meta maxDfVersion;
  inherit (dwarf-fortress) dfVersion;

  wrapper = replaceVars ./dwarf-therapist.in {
    stdenv_shell = "${stdenv.shell}";
    rm = "${coreutils}/bin/rm";
    ln = "${coreutils}/bin/ln";
    cat = "${coreutils}/bin/cat";
    mkdir = "${coreutils}/bin/mkdir";
    dirname = "${coreutils}/bin/dirname";
    therapist = "${dwarf-therapist}";
    # replaced in buildCommand
    install = null;
  };

  paths = [ dwarf-therapist ];

  nativeBuildInputs =
    [ wrapQtAppsHook ]
    ++ lib.optionals unsupportedVersion [
      expect
      xvfb-run
      dfHackWrapper
    ];

  passthru = { inherit dwarf-fortress dwarf-therapist; };

  buildCommand =
    lib.optionalString unsupportedVersion ''
      fixupMemoryMaps() (
        local output="$1"
        local orig_md5="$2"
        local patched_md5="$3"
        echo "It doesn't support DF $dfVersion out of the box, so we're doing it the hard way."
        export NIXPKGS_DF_HOME="$(mktemp -dt dfhack.XXXXXX)"
        expect ${dfHackExpectScript}
        local ini="$NIXPKGS_DF_HOME/therapist.ini"
        if [ -f "$ini" ]; then
          if grep -q "$patched_md5" "$ini"; then
            cp -v "$ini" "$output"
          else
            echo "Couldn't find MD5 ($patched_md5) in $ini"
            exit 1
          fi
        else
          echo "Couldn't find $ini!"
          exit 1
        fi
      )
    ''
    + lib.optionalString (!unsupportedVersion) ''
      fixupMemoryMaps() {
        echo "It should support DF $dfVersion, but we couldn't find any memory maps."
        echo "This is a nixpkgs bug, please report it!"
        exit 1
      }
    ''
    + ''
      mkdir -p $out/bin

      install -Dm755 $wrapper $out/bin/dwarftherapist
      ln -s $out/bin/dwarftherapist $out/bin/DwarfTherapist

      substituteInPlace $out/bin/dwarftherapist \
        --subst-var-by install $out
      wrapQtApp $out/bin/dwarftherapist

      # Fix up memory layouts
      input_file="${dwarf-therapist}/share/dwarftherapist/memory_layouts/${inifile}"
      output_file="$out/share/dwarftherapist/memory_layouts/${inifile}"
      rm -f "$output_file"
      mkdir -p "$(dirname -- "$output_file")"

      orig_md5=$(cat "${dwarf-fortress}/hash.md5.orig" | cut -c1-8)
      patched_md5=$(cat "${dwarf-fortress}/hash.md5" | cut -c1-8)

      if [ -f "$input_file" ]; then
        echo "[Dwarf Therapist Wrapper] Fixing Dwarf Fortress MD5 prefix:"
        echo "  Input:   $input_file"
        echo "  Search:  $orig_md5"
        echo "  Output:  $output_file"
        echo "  Replace: $patched_md5"

        substitute "$input_file" "$output_file" --replace-fail "$orig_md5" "$patched_md5"
      else
        echo "[Dwarf Therapist Wrapper] OH NO! No memory maps found!"
        echo "This version of Therapist ($dfVersion) has max DF version $maxDfVersion."
        fixupMemoryMaps "$output_file" "$orig_md5" "$patched_md5"
      fi
    '';

  preferLocalBuild = true;
}