Skip to content

Commit

Permalink
Add timer and state to warframe finder service (#322)
Browse files Browse the repository at this point in the history
* Add timer and state to warframe fnder service

* Migrate to threadpool based timer

* Change sequence of FindProcess

* Enable WFM socket for GFN

* Remove debug log

* Revert GFN checks back

* Rename local variable based on review, remove unnecessary checks

* Revert check for null back

* Actually proper check

* process switch cleanup, cache GameIsStreamed value (#1)

---------

Co-authored-by: D1firehail <[email protected]>
  • Loading branch information
dimon222 and D1firehail authored Sep 25, 2024
1 parent 11496b3 commit 6172e2b
Showing 1 changed file with 82 additions and 57 deletions.
139 changes: 82 additions & 57 deletions WFInfo/Services/WarframeProcess/WarframeProcessFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using WFInfo.Settings;
using System.Windows.Threading;

namespace WFInfo.Services.WarframeProcess
{
Expand All @@ -12,71 +13,90 @@ public Process Warframe
{
get
{
FindProcess();
return _warframe;
}
private set
{
if (_warframe == value)
{
// nothing to do if process didn't change
return;
}

if (_warframe != null)
{
// Old warframe process is still cached, lets dispose it
Main.dataBase.DisableLogCapture();
_warframe.Dispose();
_warframe = null;
}

// actually switching process
_warframe = value;
// cache new GameIsStreamed value. No need to constantly re-check title
GameIsStreamed = _warframe?.MainWindowTitle.Contains("GeForce NOW") ?? false;

if (_warframe != null)
{
// New process found
if (Main.dataBase.GetSocketAliveStatus())
{
Debug.WriteLine("Socket was open in verify warframe");
}
Task.Run(async () =>
{
await Main.dataBase.SetWebsocketStatus("in game");
});

if (_settings.Auto && !GameIsStreamed)
{
Main.dataBase.EnableLogCapture();
}
}

// Invoking action
OnProcessChanged?.Invoke(_warframe);
}
}

public HandleRef HandleRef => IsRunning ? new HandleRef(Warframe, Warframe.MainWindowHandle) : new HandleRef();
public bool IsRunning => Warframe != null && !Warframe.HasExited;
public bool GameIsStreamed => Warframe?.MainWindowTitle.Contains("GeForce NOW") ?? false;
public bool GameIsStreamed { get; private set; }
public event ProcessChangedArgs OnProcessChanged;

private Process _warframe;

private readonly IReadOnlyApplicationSettings _settings;
private System.Threading.Timer find_process_timer;

public WarframeProcessFinder(IReadOnlyApplicationSettings settings)
{
_settings = settings;
find_process_timer = new System.Threading.Timer(FindProcess, null, 0, 40000);
}

private void FindProcess()
private void FindProcess(Object stateInfo)
{
// process already found
// Process was already found previously
if (_warframe != null)
{
if (_warframe.HasExited)
{ //reset warframe process variables, and reset LogCapture so new game process gets noticed
Main.dataBase.DisableLogCapture();
_warframe.Dispose();
_warframe = null;

if (_settings.Auto)
Main.dataBase.EnableLogCapture();
if (!_warframe.HasExited)
{
return; // Current process is still good
}
else return; // Current process is still good
}

//Main.AddLog("FindProcess have been triggered");

Process identified_process = null;
// Search for Warframe related process
foreach (Process process in Process.GetProcesses())
{
if (process.ProcessName == "Warframe.x64" && process.MainWindowTitle == "Warframe")
{
_warframe = process;

if (Main.dataBase.GetSocketAliveStatus())
Debug.WriteLine("Socket was open in verify warframe");
Task.Run(async () =>
{
await Main.dataBase.SetWebsocketStatus("in game");
});
identified_process = process;
Main.AddLog("Found Warframe Process: ID - " + process.Id + ", MainTitle - " + process.MainWindowTitle + ", Process Name - " + process.ProcessName);

//try and catch any UAC related issues
try
{
bool _ = _warframe.HasExited;
}
catch (System.ComponentModel.Win32Exception e)
{
_warframe = null;

Main.AddLog($"Failed to get Warframe process due to: {e.Message}");
Main.StatusUpdate("Restart Warframe without admin privileges", 1);

}
OnProcessChanged?.Invoke(_warframe);
return;
break;
}
else if (process.MainWindowTitle.Contains("Warframe") && process.MainWindowTitle.Contains("GeForce NOW"))
{
Expand All @@ -85,32 +105,37 @@ private void FindProcess()
Main.SpawnGFNWarning();
});
Main.AddLog("GFN -- Found Warframe Process: ID - " + process.Id + ", MainTitle - " + process.MainWindowTitle + ", Process Name - " + process.ProcessName);
identified_process = process;
break;
}
}

_warframe = process;

//try and catch any UAC related issues
try
{
bool _ = _warframe.HasExited;
}
catch (System.ComponentModel.Win32Exception e)
{
_warframe = null;

Main.AddLog($"Failed to get Warframe process due to: {e.Message}");
Main.StatusUpdate("Restart Warframe without admin privileges, or WFInfo with admin privileges", 1);
// Try and catch any UAC related issues
if (identified_process != null)
{
try
{
bool _ = identified_process.HasExited;
}
catch (System.ComponentModel.Win32Exception e)
{
identified_process = null;

}
OnProcessChanged?.Invoke(_warframe);
return;
Main.AddLog($"Failed to get Warframe process due to: {e.Message}");
Main.StatusUpdate("Restart Warframe without admin privileges, or WFInfo with admin privileges", 1);
}
}

if (!_settings.Debug)
else
{
Main.AddLog("Did Not Detect Warframe Process");
Main.StatusUpdate("Unable to Detect Warframe Process", 1);
if (!_settings.Debug)
{
Main.AddLog("Did Not Detect Warframe Process");
Main.StatusUpdate("Unable to Detect Warframe Process", 1);
}
}

// set new process, or null if none found
Warframe = identified_process;
}
}
}

0 comments on commit 6172e2b

Please sign in to comment.