summary refs log tree commit diff
path: root/pkgs/games/openmw/tes3mp.nix
blob: 48454b42df2996595ba208358b6cf8c0afbbd5c7 (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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
{ lib
, stdenv
, cmake
, openmw
, fetchFromGitHub
, fetchpatch
, luajit
, makeWrapper
, symlinkJoin
}:

# revisions are taken from https://github.com/GrimKriegor/TES3MP-deploy

let
  # raknet could also be split into dev and lib outputs
  raknet = stdenv.mkDerivation {
    pname = "raknet";
    version = "unstable-2020-01-19";

    src = fetchFromGitHub {
      owner = "TES3MP";
      repo = "CrabNet";
      # usually fixed:
      # https://github.com/GrimKriegor/TES3MP-deploy/blob/d2a4a5d3acb64b16d9b8ca85906780aeea8d311b/tes3mp-deploy.sh#L589
      rev = "19e66190e83f53bcdcbcd6513238ed2e54878a21";
      sha256 = "WIaJkSQnoOm9T7GoAwmWl7fNg79coIo/ILUsWcbH+lA=";
    };

    cmakeFlags = [
      "-DCRABNET_ENABLE_DLL=OFF"
    ];

    nativeBuildInputs = [ cmake ];

    installPhase = ''
      install -Dm555 lib/libRakNetLibStatic.a $out/lib/libRakNetLibStatic.a
    '';
  };

  coreScripts = stdenv.mkDerivation {
    pname = "corescripts";
    version = "0.8.1";

    src = fetchFromGitHub {
      owner = "TES3MP";
      repo = "CoreScripts";
      # usually latest in stable branch (e.g. 0.7.1)
      rev = "6ae0a2a5d16171de3764817a7f8b1067ecde3def";
      sha256 = "8j/Sr9IRMNFPEVfFzdb42PckHS3KW7FH7x7rRxIh5gY=";
    };

    buildCommand = ''
      dir=$out/share/openmw-tes3mp
      mkdir -p $dir
      cp -r $src $dir/CoreScripts
    '';
  };

  # build an unwrapped version so we don't have to rebuild it all over again in
  # case the scripts or wrapper scripts change.
  unwrapped = openmw.overrideAttrs (oldAttrs: rec {
    pname = "openmw-tes3mp-unwrapped";
    version = "0.8.1";

    src = fetchFromGitHub {
      owner = "TES3MP";
      repo = "TES3MP";
      # usually latest in stable branch (e.g. 0.7.1)
      rev = "68954091c54d0596037c4fb54d2812313b7582a1";
      sha256 = "8/bV4sw7Q8l8bDTHGQ0t4owf6J6h9q468JFx4KegY5o=";
    };

    nativeBuildInputs = oldAttrs.nativeBuildInputs ++ [ makeWrapper ];

    buildInputs = oldAttrs.buildInputs ++ [ luajit ];

    cmakeFlags = oldAttrs.cmakeFlags ++ [
      "-DBUILD_OPENCS=OFF"
      "-DRakNet_INCLUDES=${raknet.src}/include"
      "-DRakNet_LIBRARY_RELEASE=${raknet}/lib/libRakNetLibStatic.a"
      "-DRakNet_LIBRARY_DEBUG=${raknet}/lib/libRakNetLibStatic.a"
    ];

    prePatch = ''
      substituteInPlace components/process/processinvoker.cpp \
        --replace "\"./\"" "\"$out/bin/\""
    '';

    patches = [
      (fetchpatch {
        url = "https://gitlab.com/OpenMW/openmw/-/commit/98a7d90ee258ceef9c70b0b2955d0458ec46f048.patch";
        sha256 = "sha256-RhbIGeE6GyqnipisiMTwWjcFnIiR055hUPL8IkjPgZw=";
      })

      # https://github.com/TES3MP/openmw-tes3mp/issues/552
      ./tes3mp.patch
    ];

    env.NIX_CFLAGS_COMPILE = "-fpermissive";

    preConfigure = ''
      substituteInPlace files/version.in \
        --subst-var-by OPENMW_VERSION_COMMITHASH ${src.rev}
    '';

    # move everything that we wrap out of the way
    postInstall = ''
      mkdir -p $out/libexec
      mv $out/bin/tes3mp-* $out/libexec
    '';

    meta = with lib; {
      description = "Multiplayer for TES3:Morrowind based on OpenMW";
      homepage = "https://tes3mp.com/";
      license = licenses.gpl3Only;
      maintainers = with maintainers; [ peterhoeg ];
      platforms = [ "x86_64-linux" "i686-linux" ];
    };
  });

  tes3mp-server-run = ''
    config="''${XDG_CONFIG_HOME:-''$HOME/.config}"/openmw
    data="''${XDG_DATA_HOME:-''$HOME/.local/share}"/openmw
    if [[ ! -f "$config"/tes3mp-server.cfg && ! -d "$data"/server ]]; then
      mkdir -p "$config"
      echo [Plugins] > "$config"/tes3mp-server.cfg
      echo "home = $data/server" >> "$config"/tes3mp-server.cfg
      mkdir -p "$data"
      cp -r ${coreScripts}/share/openmw-tes3mp/CoreScripts "$data"/server
      chmod -R u+w "$data"/server
    fi
  '';

in
symlinkJoin {
  name = "openmw-tes3mp-${unwrapped.version}";
  inherit (unwrapped) version meta;

  nativeBuildInputs = [ makeWrapper ];

  paths = [ unwrapped ];

  postBuild = ''
    mkdir -p $out/bin

    makeWrapper ${unwrapped}/libexec/tes3mp-browser $out/bin/tes3mp-browser \
      --chdir "$out/bin"

    makeWrapper ${unwrapped}/libexec/tes3mp-server $out/bin/tes3mp-server \
      --run '${tes3mp-server-run}' \
      --chdir "$out/bin"
  '';
}