btrbk: add mainProgram (#356350)
[NixPkgs.git] / pkgs / by-name / _2 / _2ship2harkinian / package.nix
blob90899330ea3d31a3375a6f6de57578293226e689
2   lib,
3   stdenv,
4   SDL2,
5   cmake,
6   copyDesktopItems,
7   fetchFromGitHub,
8   fetchpatch,
9   fetchurl,
10   imagemagick,
11   imgui,
12   libpng,
13   libpulseaudio,
14   libzip,
15   lsb-release,
16   makeDesktopItem,
17   makeWrapper,
18   ninja,
19   nlohmann_json,
20   pkg-config,
21   python3,
22   spdlog,
23   stormlib,
24   tinyxml-2,
25   writeTextFile,
26   zenity,
29 let
31   # This would get fetched at build time otherwise, see:
32   # https://github.com/HarbourMasters/2ship2harkinian/blob/1.0.2/mm/CMakeLists.txt#L708
33   gamecontrollerdb = fetchurl {
34     name = "gamecontrollerdb.txt";
35     url = "https://raw.githubusercontent.com/gabomdq/SDL_GameControllerDB/b1759cf84028aab89caa1c395e198c340b8dfd89/gamecontrollerdb.txt";
36     hash = "sha256-7C5EkqBIhLGNJuhi3832y0ffW5Ep7iuTYXb1bL5h2Js=";
37   };
39   # 2ship needs a specific imgui version
40   imgui' = imgui.overrideAttrs rec {
41     version = "1.90.6";
42     src = fetchFromGitHub {
43       owner = "ocornut";
44       repo = "imgui";
45       rev = "v${version}-docking";
46       hash = "sha256-Y8lZb1cLJF48sbuxQ3vXq6GLru/WThR78pq7LlORIzc=";
47     };
48   };
50   libgfxd = fetchFromGitHub {
51     owner = "glankk";
52     repo = "libgfxd";
53     rev = "008f73dca8ebc9151b205959b17773a19c5bd0da";
54     hash = "sha256-AmHAa3/cQdh7KAMFOtz5TQpcM6FqO9SppmDpKPTjTt8=";
55   };
57   stb_impl = writeTextFile {
58     name = "stb_impl.c";
59     text = ''
60       #define STB_IMAGE_IMPLEMENTATION
61       #include "stb_image.h"
62     '';
63   };
65   stb' = fetchurl {
66     name = "stb_image.h";
67     url = "https://raw.githubusercontent.com/nothings/stb/0bc88af4de5fb022db643c2d8e549a0927749354/stb_image.h";
68     hash = "sha256-xUsVponmofMsdeLsI6+kQuPg436JS3PBl00IZ5sg3Vw=";
69   };
71   # Apply 2ship's patch for stormlib
72   stormlib' = stormlib.overrideAttrs (prev: rec {
73     version = "9.25";
74     src = fetchFromGitHub {
75       owner = "ladislav-zezula";
76       repo = "StormLib";
77       rev = "v${version}";
78       hash = "sha256-HTi2FKzKCbRaP13XERUmHkJgw8IfKaRJvsK3+YxFFdc=";
79     };
80     nativeBuildInputs = prev.nativeBuildInputs ++ [ pkg-config ];
81     patches = (prev.patches or [ ]) ++ [
82       (fetchpatch {
83         name = "stormlib-optimizations.patch";
84         url = "https://github.com/briaguya-ai/StormLib/commit/ff338b230544f8b2bb68d2fbe075175ed2fd758c.patch";
85         hash = "sha256-Jbnsu5E6PkBifcx/yULMVC//ab7tszYgktS09Azs5+4=";
86       })
87     ];
88   });
90   thread_pool = fetchFromGitHub {
91     owner = "bshoshany";
92     repo = "thread-pool";
93     rev = "v4.1.0";
94     hash = "sha256-zhRFEmPYNFLqQCfvdAaG5VBNle9Qm8FepIIIrT9sh88=";
95   };
98 stdenv.mkDerivation (finalAttrs: {
99   pname = "2ship2harkinian";
100   version = "1.0.2";
102   src = fetchFromGitHub {
103     owner = "HarbourMasters";
104     repo = "2ship2harkinian";
105     rev = "refs/tags/${finalAttrs.version}";
106     hash = "sha256-1iSFzroKxwFpsIGNMetSlQKTKRWCy7QtgCTepFdSeY8=";
107     fetchSubmodules = true;
108   };
110   patches = [
111     # remove fetching stb as we will patch our own
112     ./0001-deps.patch
113   ];
115   nativeBuildInputs = [
116     cmake
117     copyDesktopItems
118     imagemagick
119     lsb-release
120     makeWrapper
121     ninja
122     pkg-config
123     python3
124   ];
126   buildInputs = [
127     SDL2
128     imgui'
129     libpng
130     libpulseaudio
131     libzip
132     nlohmann_json
133     spdlog
134     stormlib'
135     tinyxml-2
136     zenity
137   ];
139   cmakeFlags = [
140     (lib.cmakeBool "NON_PORTABLE" true)
141     (lib.cmakeFeature "CMAKE_INSTALL_PREFIX" "${placeholder "out"}/2s2h")
142     (lib.cmakeFeature "FETCHCONTENT_SOURCE_DIR_IMGUI" "${imgui'.src}")
143     (lib.cmakeFeature "FETCHCONTENT_SOURCE_DIR_LIBGFXD" "${libgfxd}")
144     (lib.cmakeFeature "FETCHCONTENT_SOURCE_DIR_STORMLIB" "${stormlib'}")
145     (lib.cmakeFeature "FETCHCONTENT_SOURCE_DIR_THREADPOOL" "${thread_pool}")
146   ];
148   dontAddPrefix = true;
150   # Linking fails without this
151   hardeningDisable = [ "format" ];
153   # Pie needs to be enabled or else it segfaults
154   hardeningEnable = [ "pie" ];
156   preConfigure = ''
157     # mirror 2ship's stb
158     mkdir stb
159     cp ${stb'} ./stb/${stb'.name}
160     cp ${stb_impl} ./stb/${stb_impl.name}
162     substituteInPlace libultraship/cmake/dependencies/common.cmake \
163       --replace-fail "\''${STB_DIR}" "/build/source/stb"
164   '';
166   postBuild = ''
167     cp ${gamecontrollerdb} ${gamecontrollerdb.name}
168     pushd ../OTRExporter
169     python3 ./extract_assets.py -z ../build/ZAPD/ZAPD.out --norom --xml-root ../mm/assets/xml --custom-assets-path ../mm/assets/custom --custom-otr-file 2ship.o2r --port-ver ${finalAttrs.version}
170     popd
171   '';
173   preInstall = ''
174     # Cmake likes it here for its install paths
175     cp ../OTRExporter/2ship.o2r mm/
176   '';
178   postInstall = ''
179     mkdir -p $out/bin
180     ln -s $out/2s2h/2s2h.elf $out/bin/2s2h
181     install -Dm644 ../mm/linux/2s2hIcon.png $out/share/pixmaps/2s2h.png
182   '';
184   postFixup = ''
185     wrapProgram $out/2s2h/2s2h.elf --prefix PATH ":" ${lib.makeBinPath [ zenity ]}
186   '';
188   desktopItems = [
189     (makeDesktopItem {
190       name = "2s2h";
191       icon = "2s2h";
192       exec = "2s2h";
193       comment = finalAttrs.meta.description;
194       genericName = "2 Ship 2 Harkinian";
195       desktopName = "2s2h";
196       categories = [ "Game" ];
197     })
198   ];
200   meta = {
201     homepage = "https://github.com/HarbourMasters/2ship2harkinian";
202     description = "A PC port of Majora's Mask with modern controls, widescreen, high-resolution, and more";
203     mainProgram = "2s2h";
204     platforms = [ "x86_64-linux" ];
205     maintainers = with lib.maintainers; [ qubitnano ];
206     license = with lib.licenses; [
207       # OTRExporter, OTRGui, ZAPDTR, libultraship
208       mit
209       # 2 Ship 2 Harkinian
210       cc0
211       # Reverse engineering
212       unfree
213     ];
214   };