about summary refs log tree commit diff
path: root/pkgs/games/shipwright/default.nix
blob: 2ee250a703f4614e22825c14b66b8ca09a7e9314 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
{ stdenv
, cmake
, lsb-release
, ninja
, lib
, fetchFromGitHub
, fetchurl
, copyDesktopItems
, makeDesktopItem
, python3
, libX11
, libXrandr
, libXinerama
, libXcursor
, libXi
, libXext
, glew
, boost
, SDL2
, SDL2_net
, pkg-config
, libpulseaudio
, libpng
, imagemagick
, zenity
, makeWrapper
, darwin
, libicns
}:
let
  inherit (darwin.apple_sdk_11_0.frameworks)
    IOSurface Metal QuartzCore Cocoa AVFoundation;
in
stdenv.mkDerivation (finalAttrs: {
  pname = "shipwright";
  version = "8.0.5";

  src = fetchFromGitHub {
    owner = "harbourmasters";
    repo = "shipwright";
    rev = finalAttrs.version;
    hash = "sha256-o2VwOF46Iq4pwpumOau3bDXJ/CArx6NWBi00s3E4PnE=";
    fetchSubmodules = true;
  };

  patches = [
    ./darwin-fixes.patch
  ];

  # This would get fetched at build time otherwise, see:
  # https://github.com/HarbourMasters/Shipwright/blob/e46c60a7a1396374e23f7a1f7122ddf9efcadff7/soh/CMakeLists.txt#L736
  gamecontrollerdb = fetchurl {
    name = "gamecontrollerdb.txt";
    url = "https://raw.githubusercontent.com/gabomdq/SDL_GameControllerDB/b7933e43ca2f8d26d8b668ea8ea52b736221af1e/gamecontrollerdb.txt";
    hash = "sha256-XIuS9BkWkM9d+SgT1OYTfWtcmzqSUDbMrMLoVnPgidE=";
  };

  nativeBuildInputs = [
    cmake
    ninja
    pkg-config
    python3
    imagemagick
    makeWrapper
  ] ++ lib.optionals stdenv.isLinux [
    lsb-release
    copyDesktopItems
  ] ++ lib.optionals stdenv.isDarwin [
    libicns
    darwin.sigtool
  ];

  buildInputs = [
    boost
    glew
    SDL2
    SDL2_net
    libpng
  ] ++ lib.optionals stdenv.isLinux [
    libX11
    libXrandr
    libXinerama
    libXcursor
    libXi
    libXext
    libpulseaudio
    zenity
  ] ++ lib.optionals stdenv.isDarwin [
    IOSurface
    Metal
    QuartzCore
    Cocoa
    AVFoundation
    darwin.apple_sdk_11_0.libs.simd
  ];

  cmakeFlags = [
    "-DCMAKE_INSTALL_PREFIX=${placeholder "out"}/lib"
    (lib.cmakeBool "NON_PORTABLE" true)
  ];

  env.NIX_CFLAGS_COMPILE =
    lib.optionalString stdenv.isDarwin "-Wno-int-conversion -Wno-implicit-int";

  dontAddPrefix = true;

  # Linking fails without this
  hardeningDisable = [ "format" ];

  postBuild = ''
    cp ${finalAttrs.gamecontrollerdb} ${finalAttrs.gamecontrollerdb.name}
    pushd ../OTRExporter
    python3 ./extract_assets.py -z ../build/ZAPD/ZAPD.out --norom --xml-root ../soh/assets/xml --custom-assets-path ../soh/assets/custom --custom-otr-file soh.otr --port-ver ${finalAttrs.version}
    popd
  '';

  preInstall = lib.optionalString stdenv.isLinux ''
    # Cmake likes it here for its install paths
    cp ../OTRExporter/soh.otr ..
  '' + lib.optionalString stdenv.isDarwin ''
    cp ../OTRExporter/soh.otr soh/soh.otr
  '';

  postInstall = lib.optionalString stdenv.isLinux ''
    mkdir -p $out/bin
    ln -s $out/lib/soh.elf $out/bin/soh
    install -Dm644 ../soh/macosx/sohIcon.png $out/share/pixmaps/soh.png
  '' + lib.optionalString stdenv.isDarwin ''
    # Recreate the macOS bundle (without using cpack)
    # We mirror the structure of the bundle distributed by the project

    mkdir -p $out/Applications/soh.app/Contents
    cp $src/soh/macosx/Info.plist.in $out/Applications/soh.app/Contents/Info.plist
    substituteInPlace $out/Applications/soh.app/Contents/Info.plist \
      --replace-fail "@CMAKE_PROJECT_VERSION@" "${finalAttrs.version}"

    mv $out/MacOS $out/Applications/soh.app/Contents/MacOS

    # Wrapper
    cp $src/soh/macosx/soh-macos.sh.in $out/Applications/soh.app/Contents/MacOS/soh
    chmod +x $out/Applications/soh.app/Contents/MacOS/soh
    patchShebangs $out/Applications/soh.app/Contents/MacOS/soh

    # "lib" contains all resources that are in "Resources" in the official bundle.
    # We move them to the right place and symlink them back to $out/lib,
    # as that's where the game expects them.
    mv $out/Resources $out/Applications/soh.app/Contents/Resources
    mv $out/lib/** $out/Applications/soh.app/Contents/Resources
    rm -rf $out/lib
    ln -s $out/Applications/soh.app/Contents/Resources $out/lib

    # Copy icons
    cp -r ../build/macosx/soh.icns $out/Applications/soh.app/Contents/Resources/soh.icns

    # Fix executable
    install_name_tool -change @executable_path/../Frameworks/libSDL2-2.0.0.dylib \
                      ${SDL2}/lib/libSDL2-2.0.0.dylib \
                      $out/Applications/soh.app/Contents/Resources/soh-macos
    install_name_tool -change @executable_path/../Frameworks/libGLEW.2.2.0.dylib \
                      ${glew}/lib/libGLEW.2.2.0.dylib \
                      $out/Applications/soh.app/Contents/Resources/soh-macos
    install_name_tool -change @executable_path/../Frameworks/libpng16.16.dylib \
                      ${libpng}/lib/libpng16.16.dylib \
                      $out/Applications/soh.app/Contents/Resources/soh-macos

    # Codesign (ad-hoc)
    codesign -f -s - $out/Applications/soh.app/Contents/Resources/soh-macos
  '';

  fixupPhase = lib.optionalString stdenv.isLinux ''
    wrapProgram $out/lib/soh.elf --prefix PATH ":" ${lib.makeBinPath [ zenity ]}
  '';

  desktopItems = [
    (makeDesktopItem {
      name = "soh";
      icon = "soh";
      exec = "soh";
      comment = finalAttrs.meta.description;
      genericName = "Ship of Harkinian";
      desktopName = "soh";
      categories = [ "Game" ];
    })
  ];

  meta = {
    homepage = "https://github.com/HarbourMasters/Shipwright";
    description = "A PC port of Ocarina of Time with modern controls, widescreen, high-resolution, and more";
    mainProgram = "soh";
    platforms = [ "x86_64-linux" ] ++ lib.platforms.darwin;
    maintainers = with lib.maintainers; [ j0lol matteopacini ];
    license = with lib.licenses; [
      # OTRExporter, OTRGui, ZAPDTR, libultraship
      mit
      # Ship of Harkinian itself
      unfree
    ];
  };
})