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

[xlcore][fix] Allow new versions of patched proton wine to work. Improve compatibility with unpatched wine. #1316

Closed
wants to merge 6 commits into from
Closed
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
36 changes: 33 additions & 3 deletions src/XIVLauncher.Common.Unix/Compatibility/CompatibilityTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,16 +253,46 @@ public Int32 GetProcessId(string executableName)
return GetProcessIds(executableName).FirstOrDefault();
}

public Int32 GetUnixProcessId(Int32 winePid)
public Int32 GetUnixProcessId(Int32 winePid, string executableName)
{
if (winePid == 0)
return GetUnixProcessIdByName(executableName);
var wineDbg = RunInPrefix("winedbg --command \"info procmap\"", redirectOutput: true);
var output = wineDbg.StandardOutput.ReadToEnd();
if (output.Contains("syntax error\n"))
return 0;
return GetUnixProcessIdByName(executableName);
var matchingLines = output.Split('\n', StringSplitOptions.RemoveEmptyEntries).Skip(1).Where(
l => int.Parse(l.Substring(1, 8), System.Globalization.NumberStyles.HexNumber) == winePid);
var unixPids = matchingLines.Select(l => int.Parse(l.Substring(10, 8), System.Globalization.NumberStyles.HexNumber)).ToArray();
return unixPids.FirstOrDefault();
var unixPid = unixPids.FirstOrDefault();
return (unixPid == 0) ? GetUnixProcessIdByName(executableName) : unixPid;
}

private Int32 GetUnixProcessIdByName(string executableName)
{
int closest = 0;
int early = 0;
var currentProcess = Process.GetCurrentProcess(); // Gets XIVLauncher.Core's process
bool nonunique = false;
foreach (var process in Process.GetProcessesByName(executableName))
{
if (process.Id < currentProcess.Id)
{
early = process.Id;
continue; // Process was launched before XIVLauncher.Core
}
// Assume that the closest PID to XIVLauncher.Core's is the correct one. But log an error if more than one is found.
if ((closest - currentProcess.Id) > (process.Id - currentProcess.Id) || closest == 0)
{
if (closest != 0) nonunique = true;
closest = process.Id;
}
if (nonunique) Log.Error($"More than one {executableName} found! Selecting the most likely match with process id {closest}.");
}
// Deal with rare edge-case where pid rollover causes the ffxiv pid to be lower than XLCore's.
if (closest == 0 && early != 0) closest = early;
if (closest != 0) Log.Verbose($"Process for {executableName} found using fallback method: {closest}. XLCore pid: {currentProcess.Id}");
return closest;
}

public string UnixToWinePath(string unixPath)
Expand Down
50 changes: 32 additions & 18 deletions src/XIVLauncher.Common.Unix/UnixDalamudRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,43 +74,57 @@ public UnixDalamudRunner(CompatibilityTools compatibility, DirectoryInfo dotnetR
launchArguments.Add(gameArgs);

var dalamudProcess = compatibility.RunInPrefix(string.Join(" ", launchArguments), environment: environment, redirectOutput: true, writeLog: true);
var output = dalamudProcess.StandardOutput.ReadLine();

if (output == null)
throw new DalamudRunnerException("An internal Dalamud error has occured");

Console.WriteLine(output);

// Proton-based wine sometimes throws a meaningless error, as does the ReShade Effects Shader Toggler (REST).
// Skip up to two errors (or any two other non-json lines). If the third line is also an error, just continue as normal and catch the error.
string output;
int dalamudErrorCount = 0;
do
{
output = dalamudProcess.StandardOutput.ReadLine();
if (output is null)
throw new DalamudRunnerException("An internal Dalamud error has occured");
Console.WriteLine("[DALAMUD] " + output);
dalamudErrorCount++;
} while (!output.StartsWith('{') && dalamudErrorCount <= 2);

new Thread(() =>
{
while (!dalamudProcess.StandardOutput.EndOfStream)
{
var output = dalamudProcess.StandardOutput.ReadLine();
if (output != null)
Console.WriteLine(output);
Console.WriteLine("[DALAMUD] " + output);
}

}).Start();

// For some reason, if there is a return statement in the try block, then any returns from the catch statment onward will
// trigger an exception when XLCore tries to get the exit code. Doing the return *after* the whole try-catch block works, however.
int unixPid = 0;
int winePid = 0;
try
{
var dalamudConsoleOutput = JsonConvert.DeserializeObject<DalamudConsoleOutput>(output);
var unixPid = compatibility.GetUnixProcessId(dalamudConsoleOutput.Pid);

if (unixPid == 0)
{
Log.Error("Could not retrive Unix process ID, this feature currently requires a patched wine version");
return null;
}

var gameProcess = Process.GetProcessById(unixPid);
Log.Verbose($"Got game process handle {gameProcess.Handle} with Unix pid {gameProcess.Id} and Wine pid {dalamudConsoleOutput.Pid}");
return gameProcess;
winePid = dalamudConsoleOutput.Pid;
unixPid = compatibility.GetUnixProcessId(winePid, gameExe.Name);
}
catch (JsonReaderException ex)
{
Log.Error(ex, $"Couldn't parse Dalamud output: {output}");
// Try to get the FFXIV process anyway. That way XIVLauncher can close when FFXIV closes.
unixPid = compatibility.GetUnixProcessId(0, gameExe.Name);
}

if (unixPid == 0)
{
Log.Error("Could not retrive Unix process ID, this feature currently requires a patched wine version.");
return null;
}

var gameProcess = Process.GetProcessById(unixPid);
var winePidInfo = (winePid == 0) ? string.Empty : $" and Wine pid {winePid}";
Log.Verbose($"Got {gameExe.Name} process handle {gameProcess.Handle} with Unix pid {gameProcess.Id}{winePidInfo}.");
return gameProcess;
}
}