2024-01-22 11:16:40 +13:00
|
|
|
import platform as pf
|
2023-09-15 00:46:26 +12:00
|
|
|
import os
|
2023-09-20 10:39:15 +12:00
|
|
|
import shlex
|
2023-09-15 04:24:53 +12:00
|
|
|
from dataclasses import dataclass
|
2024-02-13 05:30:23 +13:00
|
|
|
from enum import StrEnum
|
2023-09-20 10:39:15 +12:00
|
|
|
from hashlib import md5
|
2023-09-15 00:46:26 +12:00
|
|
|
from logging import getLogger
|
2024-02-13 05:30:23 +13:00
|
|
|
from typing import Optional, Union, List, Dict, Set
|
2023-09-15 04:24:53 +12:00
|
|
|
|
2024-01-22 11:16:40 +13:00
|
|
|
if pf.system() in {"Linux", "FreeBSD"}:
|
2024-01-22 11:38:09 +13:00
|
|
|
# noinspection PyUnresolvedReferences
|
|
|
|
import vdf # pylint: disable=E0401
|
2023-09-15 00:46:26 +12:00
|
|
|
|
|
|
|
logger = getLogger("Proton")
|
|
|
|
|
2023-09-15 04:24:53 +12:00
|
|
|
steam_compat_client_install_paths = [os.path.expanduser("~/.local/share/Steam")]
|
|
|
|
|
|
|
|
|
2024-02-23 08:19:05 +13:00
|
|
|
def find_steam() -> Optional[str]:
|
2023-09-15 04:24:53 +12:00
|
|
|
# return the first valid path
|
|
|
|
for path in steam_compat_client_install_paths:
|
|
|
|
if os.path.isdir(path) and os.path.isfile(os.path.join(path, "steam.sh")):
|
|
|
|
return path
|
2024-02-23 08:19:05 +13:00
|
|
|
return None
|
2023-09-15 04:24:53 +12:00
|
|
|
|
|
|
|
|
2024-02-13 05:30:23 +13:00
|
|
|
def find_libraries(steam_path: str) -> Set[str]:
|
2023-09-15 04:24:53 +12:00
|
|
|
vdf_path = os.path.join(steam_path, "steamapps", "libraryfolders.vdf")
|
|
|
|
with open(vdf_path, "r") as f:
|
|
|
|
libraryfolders = vdf.load(f)["libraryfolders"]
|
2024-02-13 05:30:23 +13:00
|
|
|
# libraries = [os.path.join(folder["path"], "steamapps") for key, folder in libraryfolders.items()]
|
|
|
|
libraries = {os.path.join(folder["path"], "steamapps") for key, folder in libraryfolders.items()}
|
2023-09-15 04:24:53 +12:00
|
|
|
return libraries
|
|
|
|
|
|
|
|
|
2023-09-20 10:39:15 +12:00
|
|
|
# Notes:
|
|
|
|
# Anything older than 'Proton 5.13' doesn't have the 'require_tool_appid' attribute.
|
|
|
|
# Anything older than 'Proton 7.0' doesn't have the 'compatmanager_layer_name' attribute.
|
|
|
|
# In addition to that, the 'Steam Linux Runtime 1.0 (scout)' runtime lists the
|
|
|
|
# 'Steam Linux Runtime 2.0 (soldier)' runtime as a dependency and is probably what was
|
|
|
|
# being used for any version before 5.13.
|
|
|
|
#
|
|
|
|
# As a result the following implementation will list versions from 7.0 onwards which honestly
|
|
|
|
# is a good trade-off for the amount of complexity supporting everything would ensue.
|
|
|
|
|
|
|
|
|
2024-02-13 05:30:23 +13:00
|
|
|
class SteamVerb(StrEnum):
|
|
|
|
RUN = "run"
|
|
|
|
WAIT_FOR_EXIT_AND_RUN = "waitforexitandrun"
|
|
|
|
RUN_IN_PREFIX = "runinprefix"
|
|
|
|
DESTROY_PREFIX = "destroyprefix"
|
|
|
|
GET_COMPAT_PATH = "getcompatpath"
|
|
|
|
GET_NATIVE_PATH = "getnativepath"
|
|
|
|
DEFAULT = WAIT_FOR_EXIT_AND_RUN
|
|
|
|
|
|
|
|
|
2023-09-15 04:24:53 +12:00
|
|
|
@dataclass
|
|
|
|
class SteamBase:
|
|
|
|
steam_path: str
|
|
|
|
tool_path: str
|
2023-09-20 10:39:15 +12:00
|
|
|
toolmanifest: Dict
|
2023-09-15 04:24:53 +12:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return self.tool_path == other.tool_path
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return hash(self.tool_path)
|
|
|
|
|
2023-09-20 10:39:15 +12:00
|
|
|
@property
|
|
|
|
def required_tool(self) -> Optional[str]:
|
2024-02-18 23:44:04 +13:00
|
|
|
return self.toolmanifest.get("require_tool_appid", None)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def layer(self) -> Optional[str]:
|
|
|
|
return self.toolmanifest.get("compatmanager_layer_name", None)
|
2023-09-20 10:39:15 +12:00
|
|
|
|
2024-02-13 05:30:23 +13:00
|
|
|
def command(self, verb: SteamVerb = SteamVerb.DEFAULT) -> List[str]:
|
2023-09-20 10:39:15 +12:00
|
|
|
tool_path = os.path.normpath(self.tool_path)
|
2024-02-18 23:44:04 +13:00
|
|
|
cmd = "".join([shlex.quote(tool_path), self.toolmanifest["commandline"]])
|
2023-09-15 04:24:53 +12:00
|
|
|
# NOTE: "waitforexitandrun" seems to be the verb used in by steam to execute stuff
|
2023-09-20 10:39:15 +12:00
|
|
|
# `run` is used when setting up the environment, so use that if we are setting up the prefix.
|
2024-02-13 05:30:23 +13:00
|
|
|
cmd = cmd.replace("%verb%", str(verb))
|
2023-09-20 10:39:15 +12:00
|
|
|
return shlex.split(cmd)
|
|
|
|
|
2024-02-13 05:30:23 +13:00
|
|
|
def as_str(self, verb: SteamVerb = SteamVerb.DEFAULT):
|
|
|
|
return " ".join(map(shlex.quote, self.command(verb)))
|
|
|
|
|
2023-09-20 10:39:15 +12:00
|
|
|
@property
|
|
|
|
def checksum(self) -> str:
|
2024-02-13 05:30:23 +13:00
|
|
|
return md5(self.as_str().encode("utf-8")).hexdigest()
|
2023-09-15 04:24:53 +12:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class SteamRuntime(SteamBase):
|
|
|
|
steam_library: str
|
2023-09-20 10:39:15 +12:00
|
|
|
appmanifest: Dict
|
2023-09-15 04:24:53 +12:00
|
|
|
|
2023-09-20 10:39:15 +12:00
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
2023-09-15 04:24:53 +12:00
|
|
|
return self.appmanifest["AppState"]["name"]
|
|
|
|
|
2023-09-20 10:39:15 +12:00
|
|
|
@property
|
|
|
|
def appid(self) -> str:
|
2023-09-15 04:24:53 +12:00
|
|
|
return self.appmanifest["AppState"]["appid"]
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class ProtonTool(SteamRuntime):
|
|
|
|
runtime: SteamRuntime = None
|
|
|
|
|
2023-09-20 10:39:15 +12:00
|
|
|
def __bool__(self) -> bool:
|
|
|
|
if appid := self.required_tool:
|
|
|
|
return self.runtime is not None and self.runtime.appid == appid
|
|
|
|
return True
|
2023-09-15 04:24:53 +12:00
|
|
|
|
2024-02-13 05:30:23 +13:00
|
|
|
def command(self, verb: SteamVerb = SteamVerb.DEFAULT) -> List[str]:
|
|
|
|
cmd = self.runtime.command(verb)
|
|
|
|
cmd.extend(super().command(verb))
|
2023-09-20 10:39:15 +12:00
|
|
|
return cmd
|
2023-09-15 04:24:53 +12:00
|
|
|
|
|
|
|
|
|
|
|
@dataclass
|
|
|
|
class CompatibilityTool(SteamBase):
|
2023-09-20 10:39:15 +12:00
|
|
|
compatibilitytool: Dict
|
2023-09-15 04:24:53 +12:00
|
|
|
runtime: SteamRuntime = None
|
|
|
|
|
2023-09-20 10:39:15 +12:00
|
|
|
def __bool__(self) -> bool:
|
|
|
|
if appid := self.required_tool:
|
|
|
|
return self.runtime is not None and self.runtime.appid == appid
|
|
|
|
return True
|
2023-09-15 04:24:53 +12:00
|
|
|
|
2023-09-20 10:39:15 +12:00
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
2024-02-18 23:44:04 +13:00
|
|
|
return self.compatibilitytool["display_name"]
|
2023-09-15 04:24:53 +12:00
|
|
|
|
2024-02-13 05:30:23 +13:00
|
|
|
def command(self, verb: SteamVerb = SteamVerb.DEFAULT) -> List[str]:
|
|
|
|
cmd = self.runtime.command(verb) if self.runtime is not None else []
|
|
|
|
cmd.extend(super().command(verb))
|
2023-09-20 10:39:15 +12:00
|
|
|
return cmd
|
2023-09-15 04:24:53 +12:00
|
|
|
|
|
|
|
|
|
|
|
def find_appmanifests(library: str) -> List[dict]:
|
|
|
|
appmanifests = []
|
|
|
|
for entry in os.scandir(library):
|
|
|
|
if entry.is_file() and entry.name.endswith(".acf"):
|
|
|
|
with open(os.path.join(library, entry.name), "r") as f:
|
|
|
|
appmanifest = vdf.load(f)
|
|
|
|
appmanifests.append(appmanifest)
|
|
|
|
return appmanifests
|
|
|
|
|
|
|
|
|
|
|
|
def find_protons(steam_path: str, library: str) -> List[ProtonTool]:
|
|
|
|
protons = []
|
|
|
|
appmanifests = find_appmanifests(library)
|
|
|
|
common = os.path.join(library, "common")
|
|
|
|
for appmanifest in appmanifests:
|
|
|
|
folder = appmanifest["AppState"]["installdir"]
|
|
|
|
tool_path = os.path.join(common, folder)
|
|
|
|
if os.path.isfile(vdf_file := os.path.join(tool_path, "toolmanifest.vdf")):
|
|
|
|
with open(vdf_file, "r") as f:
|
|
|
|
toolmanifest = vdf.load(f)
|
|
|
|
if toolmanifest["manifest"]["compatmanager_layer_name"] == "proton":
|
|
|
|
protons.append(
|
|
|
|
ProtonTool(
|
|
|
|
steam_path=steam_path,
|
|
|
|
steam_library=library,
|
|
|
|
appmanifest=appmanifest,
|
|
|
|
tool_path=tool_path,
|
2024-02-18 23:44:04 +13:00
|
|
|
toolmanifest=toolmanifest["manifest"],
|
2023-09-15 04:24:53 +12:00
|
|
|
)
|
|
|
|
)
|
|
|
|
return protons
|
|
|
|
|
|
|
|
|
|
|
|
def find_compatibility_tools(steam_path: str) -> List[CompatibilityTool]:
|
|
|
|
compatibilitytools_paths = {
|
|
|
|
"/usr/share/steam/compatibilitytools.d",
|
|
|
|
os.path.expanduser(os.path.join(steam_path, "compatibilitytools.d")),
|
|
|
|
os.path.expanduser("~/.steam/compatibilitytools.d"),
|
|
|
|
os.path.expanduser("~/.steam/root/compatibilitytools.d"),
|
|
|
|
}
|
|
|
|
compatibilitytools_paths = {
|
|
|
|
os.path.realpath(path) for path in compatibilitytools_paths if os.path.isdir(path)
|
|
|
|
}
|
|
|
|
tools = []
|
|
|
|
for path in compatibilitytools_paths:
|
|
|
|
for entry in os.scandir(path):
|
2024-02-18 23:44:04 +13:00
|
|
|
entry_path = os.path.join(path, entry.name)
|
2023-09-15 04:24:53 +12:00
|
|
|
if entry.is_dir():
|
2024-02-18 23:44:04 +13:00
|
|
|
tool_vdf = os.path.join(entry_path, "compatibilitytool.vdf")
|
|
|
|
elif entry.is_file() and entry.name.endswith(".vdf"):
|
|
|
|
tool_vdf = entry_path
|
|
|
|
else:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not os.path.isfile(tool_vdf):
|
|
|
|
continue
|
|
|
|
|
|
|
|
with open(tool_vdf, "r") as f:
|
|
|
|
compatibilitytool = vdf.load(f)
|
|
|
|
|
|
|
|
entry_tools = compatibilitytool["compatibilitytools"]["compat_tools"]
|
|
|
|
for entry_tool in entry_tools.values():
|
|
|
|
if entry_tool["from_oslist"] != "windows" and entry_tool["to_oslist"] != "linux":
|
|
|
|
continue
|
|
|
|
|
|
|
|
install_path = entry_tool["install_path"]
|
|
|
|
tool_path = os.path.abspath(os.path.join(os.path.dirname(tool_vdf), install_path))
|
2023-09-15 04:24:53 +12:00
|
|
|
manifest_vdf = os.path.join(tool_path, "toolmanifest.vdf")
|
2024-02-18 23:44:04 +13:00
|
|
|
|
|
|
|
if not os.path.isfile(manifest_vdf):
|
|
|
|
continue
|
|
|
|
|
|
|
|
with open(manifest_vdf, "r") as f:
|
|
|
|
manifest = vdf.load(f)
|
|
|
|
|
|
|
|
tools.append(
|
|
|
|
CompatibilityTool(
|
|
|
|
steam_path=steam_path,
|
|
|
|
tool_path=tool_path,
|
|
|
|
toolmanifest=manifest["manifest"],
|
|
|
|
compatibilitytool=entry_tool,
|
2023-09-15 04:24:53 +12:00
|
|
|
)
|
2024-02-18 23:44:04 +13:00
|
|
|
)
|
2023-09-15 04:24:53 +12:00
|
|
|
return tools
|
|
|
|
|
|
|
|
|
|
|
|
def find_runtimes(steam_path: str, library: str) -> Dict[str, SteamRuntime]:
|
|
|
|
runtimes = {}
|
|
|
|
appmanifests = find_appmanifests(library)
|
|
|
|
common = os.path.join(library, "common")
|
|
|
|
for appmanifest in appmanifests:
|
|
|
|
folder = appmanifest["AppState"]["installdir"]
|
|
|
|
tool_path = os.path.join(common, folder)
|
|
|
|
if os.path.isfile(vdf_file := os.path.join(tool_path, "toolmanifest.vdf")):
|
|
|
|
with open(vdf_file, "r") as f:
|
|
|
|
toolmanifest = vdf.load(f)
|
|
|
|
if toolmanifest["manifest"]["compatmanager_layer_name"] == "container-runtime":
|
2024-02-18 23:44:04 +13:00
|
|
|
print(toolmanifest["manifest"])
|
2023-09-15 04:24:53 +12:00
|
|
|
runtimes.update(
|
|
|
|
{
|
|
|
|
appmanifest["AppState"]["appid"]: SteamRuntime(
|
|
|
|
steam_path=steam_path,
|
|
|
|
steam_library=library,
|
|
|
|
appmanifest=appmanifest,
|
|
|
|
tool_path=tool_path,
|
2024-02-18 23:44:04 +13:00
|
|
|
toolmanifest=toolmanifest["manifest"],
|
2023-09-15 04:24:53 +12:00
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
return runtimes
|
|
|
|
|
|
|
|
|
|
|
|
def find_runtime(
|
|
|
|
tool: Union[ProtonTool, CompatibilityTool], runtimes: Dict[str, SteamRuntime]
|
|
|
|
) -> Optional[SteamRuntime]:
|
2023-09-20 10:39:15 +12:00
|
|
|
required_tool = tool.required_tool
|
2023-09-15 04:24:53 +12:00
|
|
|
if required_tool is None:
|
|
|
|
return None
|
2023-09-20 10:39:15 +12:00
|
|
|
return runtimes.get(required_tool, None)
|
2023-09-15 04:24:53 +12:00
|
|
|
|
|
|
|
|
2024-02-18 23:44:04 +13:00
|
|
|
def get_ulwgl_environment(
|
|
|
|
tool: Optional[ProtonTool] = None, compat_path: Optional[str] = None
|
|
|
|
) -> Dict:
|
|
|
|
# If the tool is unset, return all affected env variable names
|
|
|
|
# IMPORTANT: keep this in sync with the code below
|
|
|
|
environ = {"WINEPREFIX": compat_path if compat_path else ""}
|
|
|
|
if tool is None:
|
|
|
|
environ["WINEPREFIX"] = ""
|
|
|
|
environ["PROTONPATH"] = ""
|
|
|
|
environ["GAMEID"] = ""
|
|
|
|
environ["STORE"] = ""
|
|
|
|
return environ
|
|
|
|
|
|
|
|
environ["PROTONPATH"] = tool.tool_path
|
|
|
|
environ["STORE"] = "egs"
|
|
|
|
return environ
|
|
|
|
|
|
|
|
|
2023-09-20 10:39:15 +12:00
|
|
|
def get_steam_environment(
|
|
|
|
tool: Optional[Union[ProtonTool, CompatibilityTool]] = None, compat_path: Optional[str] = None
|
|
|
|
) -> Dict:
|
2023-09-15 04:24:53 +12:00
|
|
|
# If the tool is unset, return all affected env variable names
|
|
|
|
# IMPORTANT: keep this in sync with the code below
|
2023-09-20 10:39:15 +12:00
|
|
|
environ = {"STEAM_COMPAT_DATA_PATH": compat_path if compat_path else ""}
|
2023-09-15 04:24:53 +12:00
|
|
|
if tool is None:
|
2024-02-13 05:30:23 +13:00
|
|
|
environ["STEAM_COMPAT_DATA_PATH"] = ""
|
2023-09-15 04:24:53 +12:00
|
|
|
environ["STEAM_COMPAT_CLIENT_INSTALL_PATH"] = ""
|
|
|
|
environ["STEAM_COMPAT_LIBRARY_PATHS"] = ""
|
|
|
|
environ["STEAM_COMPAT_MOUNTS"] = ""
|
|
|
|
environ["STEAM_COMPAT_TOOL_PATHS"] = ""
|
|
|
|
return environ
|
|
|
|
|
|
|
|
environ["STEAM_COMPAT_CLIENT_INSTALL_PATH"] = tool.steam_path
|
|
|
|
if isinstance(tool, ProtonTool):
|
|
|
|
environ["STEAM_COMPAT_LIBRARY_PATHS"] = tool.steam_library
|
|
|
|
if tool.runtime is not None:
|
|
|
|
compat_mounts = [tool.tool_path, tool.runtime.tool_path]
|
|
|
|
environ["STEAM_COMPAT_MOUNTS"] = ":".join(compat_mounts)
|
|
|
|
tool_paths = [tool.tool_path]
|
|
|
|
if tool.runtime is not None:
|
|
|
|
tool_paths.append(tool.runtime.tool_path)
|
|
|
|
environ["STEAM_COMPAT_TOOL_PATHS"] = ":".join(tool_paths)
|
|
|
|
return environ
|
|
|
|
|
|
|
|
|
|
|
|
def find_tools() -> List[Union[ProtonTool, CompatibilityTool]]:
|
|
|
|
steam_path = find_steam()
|
2024-02-23 08:19:05 +13:00
|
|
|
if steam_path is None:
|
|
|
|
logger.info("Steam could not be found")
|
|
|
|
return []
|
|
|
|
logger.info("Found Steam in %s", steam_path)
|
|
|
|
|
2023-09-15 04:24:53 +12:00
|
|
|
steam_libraries = find_libraries(steam_path)
|
2023-09-20 10:39:15 +12:00
|
|
|
logger.debug("Searching for tools in libraries:")
|
|
|
|
logger.debug("%s", steam_libraries)
|
2023-09-15 04:24:53 +12:00
|
|
|
|
|
|
|
runtimes = {}
|
|
|
|
for library in steam_libraries:
|
|
|
|
runtimes.update(find_runtimes(steam_path, library))
|
|
|
|
|
|
|
|
tools = []
|
|
|
|
for library in steam_libraries:
|
|
|
|
tools.extend(find_protons(steam_path, library))
|
|
|
|
tools.extend(find_compatibility_tools(steam_path))
|
|
|
|
|
|
|
|
for tool in tools:
|
|
|
|
runtime = find_runtime(tool, runtimes)
|
|
|
|
tool.runtime = runtime
|
|
|
|
|
2023-09-20 10:39:15 +12:00
|
|
|
tools = list(filter(lambda t: bool(t), tools))
|
|
|
|
|
2023-09-15 04:24:53 +12:00
|
|
|
return tools
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
from pprint import pprint
|
|
|
|
|
|
|
|
_tools = find_tools()
|
|
|
|
pprint(_tools)
|
|
|
|
|
2024-02-18 23:44:04 +13:00
|
|
|
for _tool in _tools:
|
|
|
|
print(get_steam_environment(_tool))
|
|
|
|
print(_tool.name)
|
|
|
|
print(_tool.command(SteamVerb.RUN))
|
|
|
|
print(" ".join(_tool.command(SteamVerb.RUN_IN_PREFIX)))
|