From 53aff5397f546b09e93aff90788f3464e47907ff Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mathis=20Dr=C3=B6ge?= <mathis.droege@ewe.net>
Date: Tue, 2 Aug 2022 18:42:00 +0200
Subject: [PATCH] [core] Save path resolution: Fallback to reading wine prefix
 from env vars

This is mainly something for Heroic, since we don't touch config.ini
---
 legendary/core.py | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/legendary/core.py b/legendary/core.py
index 63689cb5..025a8b13 100644
--- a/legendary/core.py
+++ b/legendary/core.py
@@ -852,6 +852,18 @@ def get_save_path(self, app_name, platform='Windows'):
                 wine_pfx = self.lgd.config.get('default.env', 'WINEPREFIX', fallback=None)
                 wine_pfx = self.lgd.config.get('default', 'wine_prefix', fallback=wine_pfx)
 
+            # If we still didn't find anything, try to read the prefix from the environment variables of this process
+            if not wine_pfx and sys_platform == 'darwin':
+                cx_bottle = os.getenv('CX_BOTTLE')
+                if cx_bottle and mac_is_valid_bottle(cx_bottle):
+                    wine_pfx = mac_get_bottle_path(cx_bottle)
+
+            if not wine_pfx:
+                proton_pfx = os.getenv('STEAM_COMPAT_DATA_PATH')
+                if proton_pfx:
+                    wine_pfx = f'{proton_pfx}/pfx'
+                wine_pfx = os.getenv('WINEPREFIX', wine_pfx)
+
             # if all else fails, use the WINE default
             if not wine_pfx:
                 wine_pfx = os.path.expanduser('~/.wine')