Skip to content

Commit

Permalink
Linux support again!
Browse files Browse the repository at this point in the history
Co-authored-by: covers1624 <laughlan.cov@internode.on.net>
  • Loading branch information
Chicken-Bones and covers1624 committed May 19, 2022
1 parent ea56792 commit c06e433
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 21 deletions.
11 changes: 6 additions & 5 deletions patches/tModLoader/Terraria/ModLoader/Engine/Steam.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ internal class Steam
}

internal static void SetAppId(AppId_t appId) {
Environment.SetEnvironmentVariable("STEAMGAMEID", appId.ToString());
Environment.SetEnvironmentVariable("STEAMOVERLAYGAMEID", appId.ToString());
Environment.SetEnvironmentVariable("STEAMAPPID", appId.ToString());
//File.WriteAllText("steam_appid.txt", appId.ToString());
File.Delete("steam_appid.txt");
if (Environment.GetEnvironmentVariable("SteamClientLaunch") != "1") {
File.WriteAllText("steam_appid.txt", appId.ToString());
}
else if (Environment.GetEnvironmentVariable("SteamAppId") != appId.ToString()) {
throw new Exception("Cannot overwrite steam env. SteamAppId=" + Environment.GetEnvironmentVariable("SteamAppId"));
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using log4net;
using Steamworks;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Reflection;
using System.Threading;

namespace Terraria.ModLoader.Engine
Expand All @@ -28,20 +31,29 @@ public enum LaunchResult
}

internal static LaunchResult Launch() {
if (Environment.GetEnvironmentVariable("STEAMCLIENTLAUNCH") != "1") {
if (Environment.GetEnvironmentVariable("SteamClientLaunch") != "1") {
Logger.Debug("Disabled. Launched outside steam client.");
return LaunchResult.Ok;
}

serverPipe = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable);
var tMLName = Path.GetFileName(Assembly.GetExecutingAssembly().Location);
var proc = new Process() {
StartInfo = {
FileName = Environment.ProcessPath,
Arguments = $"tmodloader.dll -terrariasteamclient {serverPipe.GetClientHandleAsString()}",
Arguments = $"{tMLName} -terrariasteamclient {serverPipe.GetClientHandleAsString()}",
UseShellExecute = false,
RedirectStandardOutput = true
}
};

// clear steam env vars
foreach (var k in ((IEnumerable<string>)proc.StartInfo.EnvironmentVariables.Keys).ToArray()) {
if (k.StartsWith("steam", StringComparison.InvariantCultureIgnoreCase)) {
proc.StartInfo.EnvironmentVariables.Remove(k);
}
}

proc.Start();

while (true) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,7 @@ file_download() {
# Call a script setting its permission right for execution
run_script() {
chmod a+x "$1"
"$@"

# LD_PRELOAD="" fixes error messages from steams library linking polluting the stdout of all programs(in tree) run by run_script
LD_PRELOAD="" "$@"
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,12 @@ if [[ ! -f "$install_dir/dotnet" && ! -f "$install_dir/dotnet.exe" ]]; then
# *nix binaries are various and not worth detecting the required one here, always use "on-the-fly" script install
file_download dotnet-install.sh https://dot.net/v1/dotnet-install.sh

run_script ./dotnet-install.sh --channel "$channel" --install-dir "$install_dir" --runtime "dotnet" --version "$dotnet_version"
run_script ./dotnet-install.sh --channel "$channel" --install-dir "$install_dir" --runtime "dotnet" --version "$dotnet_version" --verbose
fi
fi

echo "Dotnet should be present in \"$install_dir\""
# @TODO: Re-evaluate if this needed
if [[ ! -f "$install_dir/dotnet" && ! -f "$install_dir/dotnet.exe" ]]; then
echo -e "${TXTCOLOR_RED}Download of portable dotnet runtime seems to have failed,${TXTCOLOR_NC}"
echo -e "${TXTCOLOR_RED}proceeding will probably use system wide installed runtime${TXTCOLOR_NC}"
read -p "Please press Enter to try to run the game anyway... "
echo -e "${TXTCOLOR_RED}Proceeding will probably fall back to system wide installed runtime${TXTCOLOR_NC}"
fi
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,25 @@ echo "Attempting Launch..."
# Move to the root folder
cd "$root_dir"

export STEAMGAMEID=""
export STEAMOVERLAYGAMEID=""
export STEAMAPPID=""

if [[ "$_uname" == *"_NT"* ]]; then
# Replace / with \\ in WINDIR env var to not confuse MonoMod about the current platform
# somehow busybox-w64 replaces paths in envs with normalized paths (no clue why..., maybe open an issue there?)
export WINDIR=${WINDIR////\\}

clear
sleep 1 # wait a little extra time for steam to realise that our parent process has exited
else
# Kill the Steam reaper process on Linux/Mac?
# Sed replace all null bytes(and spaces) with spaces, grep for reaper marker.
if $(sed 's/\x0/ /g' /proc/$PPID/cmdline | grep -q "reaper SteamLaunch AppId=1281930"); then
echo "Running under Steam reaper process. Killing.." 2>&1 | tee -a "$LogFile"
kill -9 $PPID # _yeet_
fi
fi

clear
if [[ -f "$install_dir/dotnet" || -f "$install_dir/dotnet.exe" ]]; then
echo "Launched Using Local Dotnet" 2>&1 | tee -a "$LogFile"
chmod a+x "$install_dir/dotnet"
[[ -f "$install_dir/dotnet" ]] && chmod a+x "$install_dir/dotnet"
exec "$install_dir/dotnet" tModLoader.dll "$@" 2>"$NativeLog"
else
echo "Launched Using System Dotnet" 2>&1 | tee -a "$LogFile"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
cd "$(dirname "$0")"

chmod a+x ./LaunchUtils/ScriptCaller.sh
./LaunchUtils/ScriptCaller.sh "$@" &
disown -r
# forward our parent process id to the child in case ScriptCaller needs to kill the parent to break free of steam's process lifetime tracker (reaper)
PPID=$PPID ./LaunchUtils/ScriptCaller.sh "$@" &
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ if [ $steam = "y" ]; then
fi

chmod +x ./LaunchUtils/ScriptCaller.sh
./start-tModLoader.sh $launch_args
./LaunchUtils/ScriptCaller.sh $launch_args

0 comments on commit c06e433

Please sign in to comment.