Skip to content

Commit

Permalink
Fix weird time tracking event behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
suchmememanyskill committed Feb 7, 2023
1 parent e605fd5 commit 600ce4d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
34 changes: 23 additions & 11 deletions Launcher/Launcher/LauncherConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public async Task GetProfiles()
{
Profiles = new()
{
new NativeWindowsProfile(),
new NativeLinuxProfile()
new ProfileWrapper<NativeWindowsProfile>(),
new ProfileWrapper<NativeLinuxProfile>()
};

foreach (var appGameSource in _app.GameSources)
Expand All @@ -41,6 +41,12 @@ public async Task GetProfiles()
Profiles.AddRange(_app.Config.CustomProfiles);

Profiles.RemoveAll(x => x.CompatiblePlatform != PlatformExtensions.CurrentPlatform);

Profiles.ForEach(x =>
{
x.OnGameLaunch += OnGameLaunch;
x.OnGameClose += OnGameClose;
});

string windowsDefault = _app.Config.WindowsDefaultProfile;
string linuxDefault = _app.Config.LinuxDefaultProfile;
Expand Down Expand Up @@ -152,9 +158,7 @@ public void Launch(LaunchParams launchParams)
_app.Logger.Log($"Launching {launchParams.Executable} using {profile.Name}");

_activeSessions[launchParams] = (new(), profile);

profile.OnGameLaunch += OnGameLaunch;
profile.OnGameClose += OnGameClose;

profile.Launch(launchParams);
}

Expand All @@ -175,17 +179,25 @@ private void OnGameClose(LaunchParams x)
{
_app.Logger.Log($"{x.Game.Name} closed");
x.InvokeOnGameClose();

if (!_activeSessions.ContainsKey(x))
return;

GameSession session = _activeSessions[x].Item1;

_activeSessions[x].Item1.EndTime = DateTime.Now;
_activeSessions[x].Item1.CalcTimeSpent();
_app.Config.GetGameConfig(x.Game).Sessions.Add(_activeSessions[x].Item1);
session.EndTime = DateTime.Now;

if (x.ExecutionTime == null)
session.CalcTimeSpent();
else
session.TimeSpent = x.ExecutionTime.Value;

_app.Config.GetGameConfig(x.Game).Sessions.Add(session);
_app.Config.Save(_app);

x.Game.IsRunning = false;
x.Game.InvokeOnUpdate();

_activeSessions[x].Item2.OnGameLaunch -= OnGameLaunch;
_activeSessions[x].Item2.OnGameClose -= OnGameClose;

_activeSessions.Remove(x);
}
}
30 changes: 30 additions & 0 deletions Launcher/Launcher/ProfileWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using LauncherGamePlugin.Enums;
using LauncherGamePlugin.Launcher;

namespace Launcher.Launcher;

public class ProfileWrapper<T> : IBootProfile where T : IBootProfile, new()
{
public string Name { get; }
public Platform CompatiblePlatform { get; }
public Platform CompatibleExecutable { get; }
public void Launch(LaunchParams launchParams)
{
T instance = new T();
instance.OnGameLaunch += x => OnGameLaunch?.Invoke(x);
instance.OnGameClose += x => OnGameClose?.Invoke(x);
instance.Launch(launchParams);
}

public event Action<LaunchParams>? OnGameLaunch;
public event Action<LaunchParams>? OnGameClose;

public ProfileWrapper()
{
T instance = new T();
Name = instance.Name;
CompatiblePlatform = instance.CompatiblePlatform;
CompatibleExecutable = instance.CompatibleExecutable;
}
}
1 change: 1 addition & 0 deletions LauncherGamePlugin/Launcher/LaunchParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class LaunchParams
public string WorkingDirectory { get; }
public Platform Platform { get; }
public IGame Game { get; }
public TimeSpan? ExecutionTime { get; set; }
public bool UsingListArgs { get; } = false;

private string _args = "";
Expand Down
3 changes: 2 additions & 1 deletion LauncherGamePlugin/Launcher/NativeProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ public void Launch(LaunchParams args)
Thread t = new(() =>
{
p.WaitForExit();
// Until i figure this out, this will stay commented out :(
//args.ExecutionTime = p.UserProcessorTime; // TODO: This is a hack but i don't really want to change this event chain right now
OnGameClose?.Invoke(args);
OnGameClose = null;
});
t.Start();
}
Expand Down

0 comments on commit 600ce4d

Please sign in to comment.