Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved Flatpak compatibility #146

Merged
merged 3 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.cache/
.vscode/
wemod_bin/
wemod_data/
wemod_venv/
Expand Down
5 changes: 4 additions & 1 deletion mainutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,6 +609,9 @@ def unpack_files() -> None:
window.close()


def is_flatpak() -> bool:
return "FLATPAK_ID" in os.environ or os.path.exists("/.flatpak-info")

def flatpakrunner():
import subprocess
import time
Expand Down Expand Up @@ -650,7 +653,7 @@ def flatpakrunner():
try:
if os.getenv("SteamCompatDataPath") == None:
wserver = subprocess.run(
["wineserver", "--wait"],
["flatpak-spawn", "--host","wineserver", "--wait"],
bufsize=1,
capture_output=True,
text=True,
Expand Down
110 changes: 64 additions & 46 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from mainutils import (
download_progress,
is_flatpak,
)

from typing import (
Expand Down Expand Up @@ -262,22 +263,40 @@ def self_update(path: List[Optional[str]]) -> List[Optional[str]]:
log("Self update skipped, because it was requested in the config")
return path

flatpak_cmd = ["flatpak-spawn", "--host"] if is_flatpak() else []

original_cwd = os.getcwd()
try:
os.chdir(SCRIPT_PATH)

# Check if we're in the main branch
curr_branch = subprocess.run(
flatpak_cmd + ["git", "branch", "--show-current"],
stdout=subprocess.PIPE,
text=True
).stdout.strip()
if curr_branch != "main":
log("Currently not in main branch. Aborting update")
if not path:
path = [sys.executable]
return path

# Fetch latest changes
subprocess.run(["git", "fetch"], text=True)
subprocess.run(flatpak_cmd + ["git", "fetch"], text=True)

# Get local and remote commit hashes
local_hash = subprocess.run(
["git", "rev-parse", "@"], stdout=subprocess.PIPE, text=True
flatpak_cmd + ["git", "rev-parse", "@"],
stdout=subprocess.PIPE,
text=True
).stdout.strip()
remote_hash = subprocess.run(
["git", "rev-parse", "@{u}"], stdout=subprocess.PIPE, text=True
flatpak_cmd + ["git", "rev-parse", "@{u}"],
stdout=subprocess.PIPE,
text=True
).stdout.strip()
base_hash = subprocess.run(
["git", "merge-base", "@", "@{u}"],
flatpak_cmd + ["git", "merge-base", "@", "@{u}"],
stdout=subprocess.PIPE,
text=True,
).stdout.strip()
Expand All @@ -293,11 +312,11 @@ def self_update(path: List[Optional[str]]) -> List[Optional[str]]:
"Warning: Local changes detected, updating from remote anyway."
)

subprocess.run(["git", "reset", "--hard", "origin"], text=True)
subprocess.run(["git", "pull"], text=True)
subprocess.run(flatpak_cmd + ["git", "reset", "--hard", "origin"], text=True)
subprocess.run(flatpak_cmd + ["git", "pull"], text=True)

# Set executable permissions (replace with specific file names if needed)
subprocess.run(["chmod", "-R", "ug+x", "."], text=True)
subprocess.run(flatpak_cmd + ["chmod", "-R", "ug+x", "*.py", "wemod{,.bat}"], text=True)

# Optionally update the path to include the executable if not already set
if not path:
Expand All @@ -313,46 +332,45 @@ def self_update(path: List[Optional[str]]) -> List[Optional[str]]:

def check_flatpak(flatpak_cmd: Optional[List[str]]) -> List[str]:
if flatpak_cmd == None:
if "FLATPAK_ID" in os.environ or os.path.exists("/.flatpak-info"):
return ["True"]
if is_flatpak():
return ["python3"]
return []
else:
if "FLATPAK_ID" in os.environ or os.path.exists("/.flatpak-info"):
flatpak_start = [
"flatpak-spawn",
"--host",
]

envlist = [
"STEAM_COMPAT_TOOL_PATHS",
"STEAM_COMPAT_DATA_PATH",
"WINE_PREFIX_PATH",
"WINEPREFIX",
"WINE",
"SCANFOLDER",
"TROUBLESHOOT",
"WEMOD_LOG",
"WAIT_ON_GAMECLOSE",
"SELF_UPDATE",
"FORCE_UPDATE_WEMOD",
"REPO_STRING",
]
for env in envlist:
if env in os.environ:
flatpak_start.append(f"--env={env}={os.environ[env]}")
infpr = os.getenv("WeModInfProtect", "1")
infpr = str(int(infpr) + 1)

flatpak_start.append("--env=FROM_FLATPAK=true")
flatpak_start.append(f"--env=WeModInfProtect={infpr}")
flatpak_start.append("--") # Isolate command from command args

if bool(flatpak_cmd): # if venv is set use it
flatpak_cmd = flatpak_start + flatpak_cmd
else: # if not use python executable
flatpak_cmd = flatpak_start + [sys.executable]

return flatpak_cmd
elif is_flatpak():
flatpak_start = [
"flatpak-spawn",
"--host",
]

envlist = [
"STEAM_COMPAT_TOOL_PATHS",
"STEAM_COMPAT_DATA_PATH",
"WINE_PREFIX_PATH",
"WINEPREFIX",
"WINE",
"SCANFOLDER",
"TROUBLESHOOT",
"WEMOD_LOG",
"WAIT_ON_GAMECLOSE",
"SELF_UPDATE",
"FORCE_UPDATE_WEMOD",
"REPO_STRING",
]
for env in envlist:
if env in os.environ:
flatpak_start.append(f"--env={env}={os.environ[env]}")
infpr = os.getenv("WeModInfProtect", "1")
infpr = str(int(infpr) + 1)

flatpak_start.append("--env=FROM_FLATPAK=true")
flatpak_start.append(f"--env=WeModInfProtect={infpr}")
flatpak_start.append("--") # Isolate command from command args

if bool(flatpak_cmd): # if venv is set use it
flatpak_cmd = flatpak_start + flatpak_cmd
else: # if not use python executable
flatpak_cmd = flatpak_start + [sys.executable]

return flatpak_cmd


def setup_main() -> None:
Expand Down
5 changes: 4 additions & 1 deletion wemod
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ if __name__ == "__main__":
if_flatpak_list = check_flatpak(python_venv)

# On venv path restart the script
if bool(if_flatpak_list):
if 0 < len(if_flatpak_list):
# Use environment variable to protect the script from re-running forever
inf_protect = os.getenv("WeModInfProtect", "1")
if int(inf_protect) > 4:
Expand Down Expand Up @@ -749,6 +749,8 @@ def run(skip_init: bool = False) -> str:
if fromflat:
import time

resp = 0

log("Using flatpak mode")

cachedir = os.path.join(SCRIPT_PATH, ".cache")
Expand Down Expand Up @@ -782,6 +784,7 @@ def run(skip_init: bool = False) -> str:
error = fef.read()
os.remove(errorfile)
if error:
resp = 1
raise Exception(str(error))

log("Any warnings from the command the logged")
Expand Down
Loading