about summary refs log tree commit diff
path: root/pkgs/games/humblebundle/bastion.nix
blob: 207dd105bf4da6815d8920f0c68c396c8ba912d8 (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
{ lib, stdenv, buildGame, fixFmodHook, fetchHumbleBundle, makeWrapper
, unzip, imagemagick, mono, SDL2, SDL2_image, openal, libvorbis, libGL
}:

buildGame rec {
  name = "bastion-${version}";
  version = "20161016";

  src = fetchHumbleBundle {
    name = "bastion-20161016.bin";
    machineName = "bastion_linux";
    downloadName = "Download";
    md5 = "19fea173ff2da0f990f60bd5e7c3b237";
  };

  unpackCmd = "${unzip}/bin/unzip -qq \"$curSrc\" 'data/*' || :";

  nativeBuildInputs = [ makeWrapper imagemagick fixFmodHook ];

  libDir =
    if stdenv.system == "i686-linux" then "lib"
    else if stdenv.system == "x86_64-linux" then "lib64"
    else throw "Unsupported architecture ${stdenv.system}.";

  buildPhase = let
    dllmap = {
      SDL2 = "${SDL2}/lib/libSDL2.so";
      SDL2_image = "${SDL2_image}/lib/libSDL2_image.so";
      soft_oal = "${openal}/lib/libopenal.so";
      libvorbisfile = "${libvorbis}/lib/libvorbisfile.so";
      MojoShader = "$out/libexec/bastion/libmojoshader.so";
    };
  in lib.concatStrings (lib.mapAttrsToList (dll: target: ''
    sed -i -e '/<dllmap.*dll="${dll}\.dll".*os="linux"/ {
      s!target="[^"]*"!target="'"${target}"'"!
    }' FNA.dll.config
  '') dllmap) + ''
    # This is not needed, so let's remove it.
    sed -i -e '/libtheoraplay/d' FNA.dll.config

    # We don't want any Steam libraries, that's why we bought the non-DRM
    # version, right?
    cc -Wall -std=c11 -shared -xc - -o "$libDir/libSteamStub.so" -fPIC <<EOF
    #include <stddef.h>
    int SteamAPI_Init(void) { return 0; }
    void SteamAPI_Shutdown(void) {}
    void SteamAPI_RunCallbacks() {}
    /* All the symbols for the global accessors for Steamworks C++ APIs */
    ${lib.concatMapStrings (sym: ''
      void *Steam${sym}(void) { return NULL; }
    '') [
      "Client" "User" "Friends" "Utils" "Matchmaking" "UserStats" "Apps"
      "Networking" "MatchmakingServers" "RemoteStorage" "Screenshots" "HTTP"
      "UnifiedMessages" "Controller" "UGC" "AppList" "Music" "MusicRemote"
      "HTMLSurface" "Inventory" "Video"
    ]}
    EOF
    patchelf --remove-needed libsteam_api.so "$libDir/libSteamWrapper.so"
    patchelf --add-needed libSteamStub.so "$libDir/libSteamWrapper.so"

    # For the Desktop icon
    convert Bastion.bmp Bastion.png
  '';

  installPhase = ''
    for lib in fmodex mojoshader SteamWrapper SteamStub; do
      install -vD "$libDir/lib$lib.so" "$out/libexec/bastion/lib$lib.so"
    done

    cp -rvt "$out/libexec/bastion" Bastion.exe FNA.dll* \
      FMOD.dll Lidgren.Network.dll MonoGame.Framework.Net.dll

    mkdir -p "$out/share"
    cp -rv Content "$out/share/bastion"
    cp -vt "$out/libexec/bastion" Bastion.bmp
    ln -s "$out/share/bastion" "$out/libexec/bastion/Content"

    makeWrapper ${lib.escapeShellArg mono}/bin/mono "$out/bin/bastion" \
      --add-flags "$out/libexec/bastion/Bastion.exe" \
      --set SDL_OPENGL_LIBRARY ${lib.escapeShellArg "${libGL}/lib/libGL.so"} \
      --run "cd '$out/libexec/bastion'"

    install -vD -m 0644 Bastion.png "$out/share/icons/bastion.png"

    mkdir -p "$out/share/applications"
    cat > "$out/share/applications/bastion.desktop" <<EOF
    [Desktop Entry]
    Name=Bastion
    Type=Application
    Version=1.1
    Exec=$out/bin/bastion
    Icon=$out/share/icons/bastion.png
    Categories=Game
    EOF
  '';

  sandbox.paths.required = [ "$XDG_DATA_HOME/Bastion" ];
}