Commit 612d061

HPCesia <me@hpcesia.com>
2025-12-07 18:25:20
fix: Genshin crashed after 1 minute
See: - https://github.com/ezKEa/aagl-gtk-on-nix/blob/c4a5f08a6ff74624ac576525765b06c79d2077e3/pkgs/wrapAAGL/default.nix#L107C19-L107C31 - https://github.com/an-anime-team/an-anime-game-launcher/issues/572
1 parent 606eb8f
Changed files (1)
modules
modules/game/hoyo-games.nix
@@ -37,16 +37,88 @@
 
     environment.systemPackages = let
       aagl-pkgs = inputs.aagl-nix.packages.${pkgs.stdenv.system};
-      override = {
+      override = prev: {
         extraPkgs = pkgs: [pkgs.bubblewrap]; # Use bubblewrap sandbox to set hostname to `STEAMDECK` to bypass anti-cheat
       };
-      anime-game-launcher = aagl-pkgs.anime-game-launcher.override override;
+      aagl-override-attrs = final: prev: let
+        # See https://github.com/an-anime-team/an-anime-game-launcher/issues/572#issuecomment-3476345318
+        my_wrapper = pkgs.writeShellScriptBin prev.pname ''
+          # --- Configuration ---
+          STRACE_CMD="${pkgs.strace}/bin/strace -f -e trace=process -o /dev/null ${prev.passthru.wrapper}/bin/${prev.passthru.wrapper.name} "$@""
+          ESCORT_DURATION=120
+          GAME_PROCESS_NAME="YuanShen.exe"
+          LAUNCHER_PROCESS_NAME="${prev.passthru.wrapper.name}"
+          POLLING_INTERVAL=5 # Polling interval in seconds
+
+          # --- Variables ---
+          STRACE_PID=""
+          KILLER_PID=""
+
+          # --- Cleanup Function ---
+          cleanup() {
+              echo "[Fix Script] Exit signal received, cleaning up..."
+              if [ ! -z "$KILLER_PID" ] && ps -p "$KILLER_PID" > /dev/null; then
+                  kill -9 "$KILLER_PID"
+              fi
+              if [ ! -z "$STRACE_PID" ] && ps -p "$STRACE_PID" > /dev/null; then
+                  kill -9 "$STRACE_PID"
+              fi
+              pkill -9 -f "$LAUNCHER_PROCESS_NAME"
+              echo "[Fix Script] Cleanup complete."
+          }
+          trap cleanup EXIT SIGINT SIGTERM
+
+          # 1. Start the escort and the launcher
+          echo "[Fix Script] Escort enabled..."
+          $STRACE_CMD &
+          STRACE_PID=$!
+          echo "[Fix Script] Escort process PID: $STRACE_PID"
+
+          # 2. Main monitoring loop
+          echo "[Fix Script] Waiting for you to start the game from the launcher..."
+          while ps -p $STRACE_PID > /dev/null; do
+              if pgrep -f -x $GAME_PROCESS_NAME > /dev/null; then
+                  # State: In-Game
+                  if [ -z "$KILLER_PID" ]; then
+                      echo "[Fix Script] Game process detected! Starting $ESCORT_DURATION-second separation countdown..."
+                      # Start a "timed killer" in the background, its only job is to kill strace
+                      (sleep $ESCORT_DURATION && kill -9 $STRACE_PID) &
+                      KILLER_PID=$!
+                      echo "[Fix Script] Detach program deployed, PID: $KILLER_PID"
+                  fi
+              else
+                  # State: In-Launcher (or game not started)
+                  if [ ! -z "$KILLER_PID" ]; then
+                      echo "[Fix Script] Game has been exited. Revoking detach program..."
+                      if ps -p $KILLER_PID > /dev/null; then
+                          kill -9 $KILLER_PID
+                      fi
+                      KILLER_PID=""
+                      echo "[Fix Script] Reset. Ready to relaunch the game."
+                  fi
+              fi
+              sleep $POLLING_INTERVAL
+          done
+
+          # When the strace process ends (either killed or the user closed the launcher), the script will arrive here.
+          # We perform a final, failsafe cleanup here.
+          echo "[Fix Script] Escort task finished. Performing final cleanup."
+          pkill -9 -f "$LAUNCHER_PROCESS_NAME"
+
+          exit 0
+        '';
+      in {
+        passthru = prev.passthru // {wrapper = my_wrapper;};
+        paths = with prev.passthru; [icon desktopEntry my_wrapper];
+      };
+
+      anime-game-launcher = (aagl-pkgs.anime-game-launcher.override override).overrideAttrs aagl-override-attrs;
       honkers-railway-launcher = aagl-pkgs.honkers-railway-launcher.override override;
       sleepy-launcher = aagl-pkgs.sleepy-launcher.override override;
     in [
-      anime-game-launcher # Genshin: Impact
-      honkers-railway-launcher # Honkai: Star Rail
-      sleepy-launcher # Zenless Zon Zero
+      anime-game-launcher
+      honkers-railway-launcher
+      sleepy-launcher
     ];
   };
 }