-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackToGame.cs
106 lines (84 loc) · 3.41 KB
/
BackToGame.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using Playnite.SDK;
using Playnite.SDK.Events;
using Playnite.SDK.Plugins;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Controls;
using System.IO;
using System.Reflection;
using BackToGame.Controls;
namespace BackToGame
{
using wrefBackToGameControl = WeakReference<BackToGameControl>;
public class BackToGame : GenericPlugin
{
private static readonly ILogger logger = LogManager.GetLogger();
private static readonly string PluginFolder = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
static public BackToGameControl Control { get; private set; }
public override Guid Id { get; } = Guid.Parse("c05dfa72-e302-44cf-9612-af1f7caa07f7");
static public List<wrefBackToGameControl> controlWeakRefs = new List<wrefBackToGameControl>();
static IPlayniteAPI PlayniteAPI;
public BackToGame(IPlayniteAPI api) : base(api)
{
PlayniteAPI = api;
Control = new BackToGameControl(PlayniteAPI);
Injector.InjectBackToGameCommmand(api, Control);
Properties = new GenericPluginProperties
{
HasSettings = false
};
Localization.SetPluginLanguage(PluginFolder, PlayniteAPI.ApplicationSettings.Language);
controlWeakRefs.Add(new wrefBackToGameControl(Control));
#region Control constructor
AddCustomElementSupport(new AddCustomElementSupportArgs
{
SourceName = "BackToGame",
ElementList = new List<string> { "Control" }
});
AddSettingsSupport(new AddSettingsSupportArgs
{
SourceName = "BackToGame",
SettingsRoot = $"{nameof(Control)}"
});
#endregion
}
private void CleanControlsRefs()
{
controlWeakRefs.RemoveAll(wr => wr.TryGetTarget(out BackToGameControl c) == false);
}
public override Control GetGameViewControl(GetGameViewControlArgs args)
{
var strArgs = args.Name.Split('_');
var controlType = strArgs[0];
switch (controlType)
{
case "Control":
var control = new BackToGameControl(PlayniteAPI);
controlWeakRefs.Add(new wrefBackToGameControl(control));
CleanControlsRefs();
return control;
default:
throw new ArgumentException($"Unrecognized controlType '{controlType}' for request '{args.Name}'");
}
}
public override void OnGameStarted(OnGameStartedEventArgs args)
{
CleanControlsRefs();
foreach (wrefBackToGameControl c in controlWeakRefs)
{
if (c.TryGetTarget(out BackToGameControl control))
control.OnGameStarted(args.Game, args.StartedProcessId);
}
}
public override void OnGameStopped(OnGameStoppedEventArgs args)
{
CleanControlsRefs();
foreach (wrefBackToGameControl c in controlWeakRefs)
{
if (c.TryGetTarget(out BackToGameControl control))
control.OnGameStopped(args.Game);
}
}
}
}