Skip to content

Commit

Permalink
New: Added GameStarting event to extension API #574
Browse files Browse the repository at this point in the history
  • Loading branch information
JosefNemec committed Jun 4, 2018
1 parent 4f89083 commit 4343c95
Show file tree
Hide file tree
Showing 27 changed files with 625 additions and 29 deletions.
1 change: 1 addition & 0 deletions doc/tutorials/scriptingEvents.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Available Events
|PowerShell Name | Python Name | Event | Passed Arguments |
| - | - | - | - |
| OnScriptLoaded | on_script_loaded | Script is loaded by Playnite. | None |
| OnGameStarting | on_game_starting | Before game is started. | [Game](xref:Playnite.SDK.Models.Game) |
| OnGameStarted | on_game_started | Game started running. | [Game](xref:Playnite.SDK.Models.Game) |
| OnGameStopped | on_game_stopped | Game stopped running. | [Game](xref:Playnite.SDK.Models.Game) and session length in seconds |
| OnGameInstalled | on_game_installed | Game is installed. | [Game](xref:Playnite.SDK.Models.Game) |
Expand Down
10 changes: 10 additions & 0 deletions doc/tutorials/scriptingTemplates.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ function global:OnScriptLoaded()
{
}
function global:OnGameStarting()
{
param(
$game
)
}
function global:OnGameStarted()
{
param(
Expand Down Expand Up @@ -72,6 +79,9 @@ __exports = [
def on_script_loaded():
pass

def on_game_starting(game):
pass

def on_game_started(game):
pass

Expand Down
47 changes: 38 additions & 9 deletions source/Playnite/API/PlayniteAPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public PlayniteAPI(GameDatabase database, GameControllerFactory controllers, IDi
LoadScripts();
LoadPlugins();
controllers.Installed += Controllers_Installed;
controllers.Starting += Controllers_Starting;
controllers.Started += Controllers_Started;
controllers.Stopped += Controllers_Stopped;
controllers.Uninstalled += Controllers_Uninstalled;
Expand All @@ -106,6 +107,7 @@ public void Dispose()
DisposeScripts();
DisposePlugins();
controllers.Installed -= Controllers_Installed;
controllers.Starting -= Controllers_Starting;
controllers.Started -= Controllers_Installed;
controllers.Stopped -= Controllers_Installed;
controllers.Uninstalled -= Controllers_Installed;
Expand Down Expand Up @@ -280,6 +282,33 @@ private void Controllers_Stopped(object sender, GameControllerEventArgs args)
}
}

private void Controllers_Starting(object sender, GameControllerEventArgs args)
{
foreach (var script in scripts)
{
try
{
script.OnGameStarting(database.GetGame(args.Controller.Game.Id));
}
catch (Exception e)
{
logger.Error(e, $"Failed to load execute OnGameStarting method from {script.Name} script.");
}
}

foreach (var plugin in plugins)
{
try
{
plugin.OnGameStarting(database.GetGame(args.Controller.Game.Id));
}
catch (Exception e)
{
logger.Error(e, $"Failed to load execute OnGameStarting method from {plugin.Properties.PluginName} plugin.");
}
}
}

private void Controllers_Started(object sender, GameControllerEventArgs args)
{
foreach (var script in scripts)
Expand Down Expand Up @@ -347,17 +376,17 @@ private void Database_DatabaseOpened(object sender, EventArgs args)
logger.Error(e, $"Failed to load execute OnScriptLoaded method from {script.Name} script.");
continue;
}
}

foreach (var plugin in plugins)
foreach (var plugin in plugins)
{
try
{
try
{
plugin.OnLoaded();
}
catch (Exception e)
{
logger.Error(e, $"Failed to load execute OnLoaded method from {plugin.Properties.PluginName} plugin.");
}
plugin.OnLoaded();
}
catch (Exception e)
{
logger.Error(e, $"Failed to load execute OnLoaded method from {plugin.Properties.PluginName} plugin.");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,22 @@ public BattleNetGameController(Game game) : base(game)

public override void Play(List<Emulator> emulators)
{
Dispose();
ReleaseResources();
stopWatch = Stopwatch.StartNew();
procMon = new ProcessMonitor();
procMon.TreeDestroyed += Monitor_TreeDestroyed;
var app = BattleNetLibrary.GetAppDefinition(Game.ProviderId);

if (Game.PlayTask.Type == GameTaskType.URL && Game.PlayTask.Path.StartsWith("battlenet", StringComparison.InvariantCultureIgnoreCase))
{
OnStarting(this, new GameControllerEventArgs(this, 0));
GameHandler.ActivateTask(Game.PlayTask, Game, emulators);
procMon.TreeStarted += ProcMon_TreeStarted;
procMon.WatchDirectoryProcesses(Game.InstallDirectory, false);
}
else if (app.Type == BattleNetLibrary.BNetAppType.Classic && Game.PlayTask.Path.Contains(app.ClassicExecutable))
{
OnStarting(this, new GameControllerEventArgs(this, 0));
var proc = GameHandler.ActivateTask(Game.PlayTask, Game, emulators);
procMon.WatchDirectoryProcesses(Game.InstallDirectory, true);
OnStarted(this, new GameControllerEventArgs(this, 0));
Expand All @@ -53,7 +55,7 @@ public override void Play(List<Emulator> emulators)

public override void Install()
{
Dispose();
ReleaseResources();
var product = BattleNetLibrary.GetAppDefinition(Game.ProviderId);
if (product.Type == BattleNetLibrary.BNetAppType.Classic)
{
Expand All @@ -69,7 +71,7 @@ public override void Install()

public override void Uninstall()
{
Dispose();
ReleaseResources();
var product = BattleNetLibrary.GetAppDefinition(Game.ProviderId);
var entry = BattleNetLibrary.GetUninstallEntry(product);
if (entry != null)
Expand Down
11 changes: 6 additions & 5 deletions source/Playnite/Providers/GOG/GogGameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ public GogGameController(Game game, Settings settings) : this(game)
this.settings = settings;
}

public override void Dispose()
public override void ReleaseResources()
{
base.Dispose();
base.ReleaseResources();
fileWatcher?.Dispose();
}

public override void Play(List<Emulator> emulators)
{
Dispose();
ReleaseResources();
if (settings?.GOGSettings.RunViaGalaxy == true)
{
OnStarting(this, new GameControllerEventArgs(this, 0));
stopWatch = Stopwatch.StartNew();
procMon = new ProcessMonitor();
procMon.TreeStarted += ProcMon_TreeStarted;
Expand All @@ -57,14 +58,14 @@ public override void Play(List<Emulator> emulators)

public override void Install()
{
Dispose();
ReleaseResources();
Process.Start(@"goggalaxy://openGameView/" + Game.ProviderId);
StartInstallWatcher();
}

public override void Uninstall()
{
Dispose();
ReleaseResources();
var uninstaller = Path.Combine(Game.InstallDirectory, "unins000.exe");
if (!File.Exists(uninstaller))
{
Expand Down
10 changes: 9 additions & 1 deletion source/Playnite/Providers/GameControllerFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public List<IGameController> Controllers
{
get; private set;
}


public event GameControllerEventHandler Starting;
public event GameControllerEventHandler Started;
public event GameControllerEventHandler Stopped;
public event GameControllerEventHandler Uninstalled;
Expand Down Expand Up @@ -50,6 +51,7 @@ public void AddController(IGameController controller)
controller.Installed += Controller_Installed;
controller.Uninstalled += Controller_Uninstalled;
controller.Started += Controller_Started;
controller.Starting += Controller_Starting;
controller.Stopped += Controller_Stopped;
Controllers.Add(controller);
database?.AddActiveController(controller);
Expand All @@ -76,6 +78,7 @@ public void DisposeController(IGameController controller)
controller.Installed -= Controller_Installed;
controller.Uninstalled -= Controller_Uninstalled;
controller.Started -= Controller_Started;
controller.Starting -= Controller_Starting;
controller.Stopped -= Controller_Stopped;
controller.Dispose();
}
Expand All @@ -90,6 +93,11 @@ private void Controller_Stopped(object sender, GameControllerEventArgs e)
Stopped?.Invoke(this, e);
}

private void Controller_Starting(object sender, GameControllerEventArgs e)
{
Starting?.Invoke(this, e);
}

private void Controller_Started(object sender, GameControllerEventArgs e)
{
Started?.Invoke(this, e);
Expand Down
16 changes: 16 additions & 0 deletions source/Playnite/Providers/IGameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ Game Game

void ActivateAction(GameTask action);

event GameControllerEventHandler Starting;

event GameControllerEventHandler Started;

event GameControllerEventHandler Stopped;
Expand All @@ -82,6 +84,7 @@ public Game Game
get; private set;
}

public event GameControllerEventHandler Starting;
public event GameControllerEventHandler Started;
public event GameControllerEventHandler Stopped;
public event GameControllerEventHandler Uninstalled;
Expand All @@ -105,6 +108,8 @@ public virtual void Play(List<Emulator> emulators)
throw new Exception("Cannot start game without play task");
}

Dispose();
OnStarting(this, new GameControllerEventArgs(this, 0));
var proc = GameHandler.ActivateTask(Game.PlayTask, Game, emulators);
OnStarted(this, new GameControllerEventArgs(this, 0));

Expand Down Expand Up @@ -137,9 +142,20 @@ public virtual void Play(List<Emulator> emulators)
public abstract void Uninstall();

public virtual void Dispose()
{
ReleaseResources();
}

public virtual void ReleaseResources()
{
watcherToken?.Cancel();
procMon?.Dispose();

}

public virtual void OnStarting(object sender, GameControllerEventArgs args)
{
execContext.Post((a) => Starting?.Invoke(sender, args), null);
}

public virtual void OnStarted(object sender, GameControllerEventArgs args)
Expand Down
7 changes: 4 additions & 3 deletions source/Playnite/Providers/Origin/OriginGameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public OriginGameController(Game game) : base(game)

public override void Play(List<Emulator> emulators)
{
Dispose();
ReleaseResources();
OnStarting(this, new GameControllerEventArgs(this, 0));
stopWatch = Stopwatch.StartNew();
procMon = new ProcessMonitor();
procMon.TreeDestroyed += ProcMon_TreeDestroyed;
Expand All @@ -37,14 +38,14 @@ public override void Play(List<Emulator> emulators)

public override void Install()
{
Dispose();
ReleaseResources();
ProcessStarter.StartUrl($"origin2://game/launch?offerIds={Game.ProviderId}&autoDownload=true");
StartInstallWatcher();
}

public override void Uninstall()
{
Dispose();
ReleaseResources();
ProcessStarter.StartProcess("appwiz.cpl", string.Empty);
StartUninstallWatcher();
}
Expand Down
7 changes: 4 additions & 3 deletions source/Playnite/Providers/Steam/SteamGameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ public SteamGameController(Game game) : base(game)

public override void Play(List<Emulator> emulators)
{
Dispose();
ReleaseResources();
if (Game.PlayTask.Type == GameTaskType.URL && Game.PlayTask.Path.StartsWith("steam", StringComparison.InvariantCultureIgnoreCase))
{
OnStarting(this, new GameControllerEventArgs(this, 0));
GameHandler.ActivateTask(Game.PlayTask, Game);
StartRunningWatcher();
}
Expand All @@ -42,14 +43,14 @@ public override void Play(List<Emulator> emulators)

public override void Install()
{
Dispose();
ReleaseResources();
Process.Start(@"steam://install/" + Game.ProviderId);
StartInstallWatcher();
}

public override void Uninstall()
{
Dispose();
ReleaseResources();
Process.Start(@"steam://uninstall/" + Game.ProviderId);
StartUninstallWatcher();
}
Expand Down
7 changes: 4 additions & 3 deletions source/Playnite/Providers/Uplay/UplayGameController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ public UplayGameController(Game game) : base(game)

public override void Play(List<Emulator> emulators)
{
Dispose();
ReleaseResources();
if (Game.PlayTask.Type == GameTaskType.URL && Game.PlayTask.Path.StartsWith("uplay", StringComparison.InvariantCultureIgnoreCase))
{
OnStarting(this, new GameControllerEventArgs(this, 0));
stopWatch = Stopwatch.StartNew();
procMon = new ProcessMonitor();
procMon.TreeStarted += ProcMon_TreeStarted;
Expand All @@ -45,14 +46,14 @@ public override void Play(List<Emulator> emulators)

public override void Install()
{
Dispose();
ReleaseResources();
ProcessStarter.StartUrl("uplay://install/" + Game.ProviderId);
StartInstallWatcher();
}

public override void Uninstall()
{
Dispose();
ReleaseResources();
ProcessStarter.StartUrl("uplay://uninstall/" + Game.ProviderId);
StartUninstallWatcher();
}
Expand Down
11 changes: 11 additions & 0 deletions source/Playnite/Scripting/IronPython/IronPythonScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@ public override void OnScriptLoaded()
}
}

public override void OnGameStarting(Game game)
{
if (Runtime.GetFunctionExits("on_game_starting"))
{
Runtime.Execute("on_game_starting(__game)", new Dictionary<string, object>()
{
{ "__game", game }
});
}
}

public override void OnGameStarted(Game game)
{
if (Runtime.GetFunctionExits("on_game_started"))
Expand Down
1 change: 1 addition & 0 deletions source/Playnite/Scripting/PlayniteScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public virtual void Dispose()
public abstract void InvokeExportedFunction(ScriptFunctionExport function);
public abstract void SetVariable(string name, object value);
public abstract void OnScriptLoaded();
public abstract void OnGameStarting(Game game);
public abstract void OnGameStarted(Game game);
public abstract void OnGameStopped(Game game, long ellapsedSeconds);
public abstract void OnGameInstalled(Game game);
Expand Down
Loading

0 comments on commit 4343c95

Please sign in to comment.