-
Notifications
You must be signed in to change notification settings - Fork 746
Launching & Closing Custom Integrations based on specific games #4032
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
Open
CubesterYT
wants to merge
5
commits into
bloxstraplabs:main
Choose a base branch
from
CubesterYT:integrations-upgrade
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+218
−23
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9318813
Custom Integrations Upgrade: Allow launching and closing based on spe…
CubesterYT c9dabd6
Merge branch 'main' into integrations-upgrade
CubesterYT 9700c61
Readd "Dispose"
CubesterYT c78b04d
Merge branch 'bloxstraplabs:main' into integrations-upgrade
CubesterYT d7e5912
Merge branch 'main' into integrations-upgrade
CubesterYT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
namespace Bloxstrap.Integrations | ||
{ | ||
public class IntegrationWatcher : IDisposable | ||
{ | ||
private readonly ActivityWatcher _activityWatcher; | ||
private readonly Dictionary<int, CustomIntegration> _activeIntegrations = new(); | ||
|
||
public IntegrationWatcher(ActivityWatcher activityWatcher) | ||
{ | ||
_activityWatcher = activityWatcher; | ||
|
||
_activityWatcher.OnGameJoin += OnGameJoin; | ||
_activityWatcher.OnGameLeave += OnGameLeave; | ||
} | ||
|
||
private void OnGameJoin(object? sender, EventArgs e) | ||
{ | ||
if (!_activityWatcher.InGame) | ||
return; | ||
|
||
long currentGameId = _activityWatcher.Data.PlaceId; | ||
|
||
foreach (var integration in App.Settings.Prop.CustomIntegrations) | ||
{ | ||
if (!integration.SpecifyGame || integration.GameID != currentGameId.ToString()) | ||
continue; | ||
|
||
LaunchIntegration(integration); | ||
} | ||
} | ||
|
||
private void OnGameLeave(object? sender, EventArgs e) | ||
{ | ||
foreach (var pid in _activeIntegrations.Keys.ToList()) | ||
{ | ||
var integration = _activeIntegrations[pid]; | ||
if (integration.AutoCloseOnGame) | ||
{ | ||
TerminateProcess(pid); | ||
_activeIntegrations.Remove(pid); | ||
} | ||
} | ||
} | ||
|
||
private void LaunchIntegration(CustomIntegration integration) | ||
{ | ||
const string LOG_IDENT = "IntegrationWatcher::LaunchIntegration"; | ||
|
||
try | ||
{ | ||
var process = Process.Start(new ProcessStartInfo | ||
{ | ||
FileName = integration.Location, | ||
Arguments = integration.LaunchArgs.Replace("\r\n", " "), | ||
WorkingDirectory = Path.GetDirectoryName(integration.Location), | ||
UseShellExecute = true | ||
}); | ||
|
||
if (process != null) | ||
{ | ||
App.Logger.WriteLine(LOG_IDENT, $"Integration '{integration.Name}' launched for game ID '{integration.GameID}' (PID {process.Id})."); | ||
_activeIntegrations[process.Id] = integration; | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
App.Logger.WriteLine(LOG_IDENT, $"Failed to launch integration '{integration.Name}': {ex.Message}"); | ||
} | ||
} | ||
|
||
private void TerminateProcess(int pid) | ||
{ | ||
const string LOG_IDENT = "IntegrationWatcher::TerminateProcess"; | ||
|
||
try | ||
{ | ||
var process = Process.GetProcessById(pid); | ||
process.Kill(); | ||
|
||
App.Logger.WriteLine(LOG_IDENT, $"Terminated integration process (PID {pid})."); | ||
} | ||
catch (Exception) | ||
{ | ||
App.Logger.WriteLine(LOG_IDENT, $"Failed to terminate process (PID {pid}), likely already exited."); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
foreach (var pid in _activeIntegrations.Keys) | ||
{ | ||
TerminateProcess(pid); | ||
} | ||
|
||
_activeIntegrations.Clear(); | ||
_activityWatcher.Dispose(); | ||
GC.SuppressFinalize(this); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System.Windows; | ||
using System.Windows.Data; | ||
|
||
namespace Bloxstrap.UI.Converters | ||
{ | ||
public class BooleanToVisibilityConverter : IValueConverter | ||
{ | ||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) | ||
{ | ||
if (value is bool boolValue) | ||
return boolValue ? Visibility.Visible : Visibility.Collapsed; | ||
|
||
return Visibility.Collapsed; | ||
} | ||
|
||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
Bloxstrap/UI/Converters/InverseBooleanToVisibilityConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using System.Windows; | ||
using System.Windows.Data; | ||
|
||
namespace Bloxstrap.UI.Converters | ||
{ | ||
public class InverseBooleanToVisibilityConverter : IValueConverter | ||
{ | ||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) | ||
{ | ||
if (value is bool boolValue) | ||
return boolValue ? Visibility.Collapsed : Visibility.Visible; | ||
|
||
return Visibility.Visible; | ||
} | ||
|
||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.