Skip to content

Commit

Permalink
Prevent multiple game launches (#280, #281)
Browse files Browse the repository at this point in the history
Former-commit-id: fcb80e5
  • Loading branch information
tkashkin committed Aug 31, 2019
1 parent 5fd12c9 commit 34ded7e
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 20 deletions.
10 changes: 5 additions & 5 deletions src/data/Emulator.vala
Original file line number Diff line number Diff line change
Expand Up @@ -184,14 +184,14 @@ namespace GameHub.Data

public override async void run()
{
if(!RunnableIsLaunched && !Sources.Steam.Steam.IsAnyAppRunning && executable.query_exists())
if(can_be_launched(true) && executable.query_exists())
{
RunnableIsLaunched = is_running = true;
Runnable.IsLaunched = is_running = true;

yield Utils.run_thread(get_args(null, executable), executable.get_parent().get_path(), null, true);

Timeout.add_seconds(1, () => {
RunnableIsLaunched = is_running = false;
Runnable.IsLaunched = is_running = false;
return Source.REMOVE;
});
}
Expand All @@ -207,7 +207,7 @@ namespace GameHub.Data

if(executable.query_exists())
{
RunnableIsLaunched = is_running = true;
Runnable.IsLaunched = is_running = true;

if(game != null)
{
Expand All @@ -219,7 +219,7 @@ namespace GameHub.Data
yield Utils.run_thread(get_args(game, executable), dir.get_path(), null, true);

Timeout.add_seconds(1, () => {
RunnableIsLaunched = is_running = false;
Runnable.IsLaunched = is_running = false;
if(game != null)
{
game.is_running = false;
Expand Down
6 changes: 3 additions & 3 deletions src/data/Game.vala
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ namespace GameHub.Data

public override async void run()
{
if(!RunnableIsLaunched && !Sources.Steam.Steam.IsAnyAppRunning && executable.query_exists())
if(can_be_launched(true) && executable.query_exists())
{
RunnableIsLaunched = is_running = true;
Runnable.IsLaunched = is_running = true;
update_status();

string[] cmd = { executable.get_path() };
Expand Down Expand Up @@ -145,7 +145,7 @@ namespace GameHub.Data
save();

Timeout.add_seconds(1, () => {
RunnableIsLaunched = is_running = false;
Runnable.IsLaunched = is_running = false;
update_status();
return Source.REMOVE;
});
Expand Down
23 changes: 20 additions & 3 deletions src/data/Runnable.vala
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,25 @@ namespace GameHub.Data
public abstract async void install(Runnable.Installer.InstallMode install_mode=Runnable.Installer.InstallMode.INTERACTIVE);
public abstract async void run();

public virtual bool can_be_launched(bool is_launch_attempt=false)
{
if(Runnable.IsLaunched || Sources.Steam.Steam.IsAnyAppRunning || is_running) return false;
if(is_launch_attempt)
{
lock(Runnable.LastLaunchAttempt)
{
var launch_attempt = Runnable.LastLaunchAttempt;
var now = get_real_time();
Runnable.LastLaunchAttempt = now;
if(now - launch_attempt < 1000000) return false;
}
}
return true;
}

public virtual async void run_with_compat(bool is_opened_from_menu=false)
{
if(!RunnableIsLaunched && !Sources.Steam.Steam.IsAnyAppRunning)
if(can_be_launched(true))
{
var dlg = new UI.Dialogs.CompatRunDialog(this, is_opened_from_menu);
dlg.destroy.connect(() => {
Expand Down Expand Up @@ -318,6 +334,9 @@ namespace GameHub.Data
save();
}

public static bool IsLaunched = false;
public static int64 LastLaunchAttempt = 0;

public abstract class Installer
{
private static string NSIS_INSTALLER_DESCRIPTION = "Nullsoft Installer";
Expand Down Expand Up @@ -845,6 +864,4 @@ namespace GameHub.Data
assert_not_reached();
}
}

public static bool RunnableIsLaunched = false;
}
1 change: 1 addition & 0 deletions src/data/sources/steam/SteamGame.vala
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ namespace GameHub.Data.Sources.Steam

public override async void run()
{
if(!can_be_launched(true)) return;
last_launch = get_real_time() / 1000000;
save();
Utils.open_uri(@"steam://rungameid/$(id)");
Expand Down
6 changes: 3 additions & 3 deletions src/ui/dialogs/CompatRunDialog.vala
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ namespace GameHub.UI.Dialogs
{
if(compat_tool_picker == null || compat_tool_picker.selected == null) return;

RunnableIsLaunched = game.is_running = true;
Runnable.IsLaunched = game.is_running = true;

if(game is Emulator)
{
Expand All @@ -133,7 +133,7 @@ namespace GameHub.UI.Dialogs
compat_tool_picker.selected.run_emulator.begin(game as Emulator, emulated_game, launch_in_game_dir, (obj, res) => {
compat_tool_picker.selected.run_emulator.end(res);
Timeout.add_seconds(1, () => {
RunnableIsLaunched = game.is_running = emulated_game.is_running = false;
Runnable.IsLaunched = game.is_running = emulated_game.is_running = false;
emulated_game.update_status();
return Source.REMOVE;
});
Expand All @@ -148,7 +148,7 @@ namespace GameHub.UI.Dialogs
compat_tool_picker.selected.run.begin(game, (obj, res) => {
compat_tool_picker.selected.run.end(res);
Timeout.add_seconds(1, () => {
RunnableIsLaunched = game.is_running = false;
Runnable.IsLaunched = game.is_running = false;
game.update_status();
return Source.REMOVE;
});
Expand Down
4 changes: 2 additions & 2 deletions src/ui/views/GameDetailsView/GameDetailsPage.vala
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,9 @@ namespace GameHub.UI.Views.GameDetailsView
action_install.visible = s.state != Game.State.INSTALLED;
action_install.sensitive = s.state == Game.State.UNINSTALLED && game.is_installable;
action_run_with_compat.visible = s.state == Game.State.INSTALLED && game.use_compat;
action_run_with_compat.sensitive = !game.is_running && !RunnableIsLaunched && !GameHub.Data.Sources.Steam.Steam.IsAnyAppRunning;
action_run_with_compat.sensitive = game.can_be_launched();
action_run.visible = s.state == Game.State.INSTALLED && !action_run_with_compat.visible;
action_run.sensitive = !game.is_running && !RunnableIsLaunched && !GameHub.Data.Sources.Steam.Steam.IsAnyAppRunning;
action_run.sensitive = game.can_be_launched();
action_open_directory.visible = s.state == Game.State.INSTALLED && game.install_dir != null && game.install_dir.query_exists();
action_open_installer_collection_directory.visible = game.installers_dir != null && game.installers_dir.query_exists();
action_open_bonus_collection_directory.visible = game is GameHub.Data.Sources.GOG.GOGGame && (game as GameHub.Data.Sources.GOG.GOGGame).bonus_content_dir != null && (game as GameHub.Data.Sources.GOG.GOGGame).bonus_content_dir.query_exists();
Expand Down
4 changes: 2 additions & 2 deletions src/ui/views/GamesView/GameContextMenu.vala
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ namespace GameHub.UI.Views.GamesView
if(game.use_compat)
{
var run_with_compat = new Gtk.MenuItem.with_label(_("Run with compatibility layer"));
run_with_compat.sensitive = !game.is_running && !RunnableIsLaunched && !GameHub.Data.Sources.Steam.Steam.IsAnyAppRunning;
run_with_compat.sensitive = game.can_be_launched();
run_with_compat.activate.connect(() => game.run_with_compat.begin(true));
add(run_with_compat);
}
else
{
var run = new Gtk.MenuItem.with_label(_("Run"));
run.sensitive = !game.is_running && !RunnableIsLaunched && !GameHub.Data.Sources.Steam.Steam.IsAnyAppRunning;
run.sensitive = game.can_be_launched();
run.activate.connect(() => game.run.begin());
add(run);
}
Expand Down
4 changes: 2 additions & 2 deletions src/ui/views/GamesView/GamesView.vala
Original file line number Diff line number Diff line change
Expand Up @@ -911,14 +911,14 @@ namespace GameHub.UI.Views.GamesView
var b = Gamepad.Buttons.get(btn);
b.emit_key_event(press);

if(GameHub.Application.log_verbose && !RunnableIsLaunched)
if(GameHub.Application.log_verbose && !Runnable.IsLaunched && !Sources.Steam.Steam.IsAnyAppRunning)
{
debug("[Gamepad] Button %s: %s (%s) [%d]", (press ? "pressed" : "released"), b.name, b.long_name, btn);
}

ui_update_gamepad_mode();

if(controller_settings.focus_window && !press && b == Gamepad.BTN_GUIDE && !window.has_focus && !RunnableIsLaunched && !Sources.Steam.Steam.IsAnyAppRunning)
if(controller_settings.focus_window && !press && b == Gamepad.BTN_GUIDE && !window.has_focus && !Runnable.IsLaunched && !Sources.Steam.Steam.IsAnyAppRunning)
{
window.get_window().focus(Gdk.CURRENT_TIME);
}
Expand Down

0 comments on commit 34ded7e

Please sign in to comment.