-
Notifications
You must be signed in to change notification settings - Fork 4
/
App.xaml.cs
166 lines (130 loc) · 5.79 KB
/
App.xaml.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
using System.Diagnostics;
using System.Reflection;
using System.IO;
using System.Windows;
using DiscordRPC;
using DiscordRPC.Logging;
using Microsoft.Extensions.Logging;
using Plex.ServerApi.Api;
using Plex.ServerApi.Clients;
using Plex.ServerApi.Clients.Interfaces;
using Plex.ServerApi.PlexModels.Account;
using Plex.ServerApi.PlexModels.OAuth;
using DyviniaUtils;
using DyviniaUtils.Dialogs;
using PlexampRPC.Data;
namespace PlexampRPC
{
[GlobalConfig]
public class Config : SettingsManager<Config> {
public bool UpdateChecker { get; set; } = true;
public bool LocalAddress { get; set; } = false;
public bool OwnedOnly { get; set; } = true;
public string SelectedServer { get; set; } = string.Empty;
public int ArtResolution { get; set; } = 128;
public double RefreshInterval { get; set; } = 2.5;
public int SessionTimeout { get; set; } = 30;
public string TemplateL1 { get; set; } = "{title}";
public string TemplateL2 { get; set; } = "by {artist}";
public string TemplateL3 { get; set; } = "{album}";
public string DiscordListeningTo { get; set; } = "Plexamp";
public string DiscordCustomClientID { get; set; } = "1100233636491563069";
public string PlexAddress { get; set; } = string.Empty;
}
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application {
public static readonly string Version = "v" + Assembly.GetExecutingAssembly().GetName()?.Version?.ToString()[..5];
public static string ClientID {
get {
if (Config.Settings.DiscordListeningTo == "Plexamp")
return "1100233636491563069";
else if (Config.Settings.DiscordListeningTo == "Music")
return "1116438265680109598";
else
return Config.Settings.DiscordCustomClientID;
}
}
public static DiscordRpcClient DiscordClient { get; } = new(ClientID);
public static IPlexAccountClient AccountClient { get; } = new PlexAccountClient(new() {
Product = "PlexampRPC",
DeviceName = Environment.MachineName,
Platform = "Desktop",
Version = "v2"
}, new ApiService(new PlexRequestsHttpClient(), new Logger<ApiService>(LoggerFactory.Create(builder => { builder.AddConsole(); }))));
public static string? Token { get; set; }
public static PlexAccount? Account { get; set; }
public static PlexResourceData[]? PlexResources { get; set; }
public static LogWriter? Log { get; set; }
public App() {
Config.Load();
Log = new LogWriter();
DiscordInit();
Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;
AppDomain.CurrentDomain.ProcessExit += (_, _) => DiscordClient.Dispose();
DispatcherUnhandledException += ExceptionDialog.UnhandledException;
}
protected override async void OnStartup(StartupEventArgs e) {
MainWindow window = new();
window.Show();
await PlexSignIn();
window.UpdateAccountIcon();
PlexResources = await window.GetAccountResources();
window.WindowState = WindowState.Normal;
window.Activate();
window.GetAccountInfo();
window.StartPolling();
foreach (Process existingProcess in Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName))
if (existingProcess.Id != Environment.ProcessId)
existingProcess.Kill();
if (Config.Settings.UpdateChecker)
await GitHub.CheckAndInstall("Dyvinia", "PlexampRPC");
}
protected override void OnExit(ExitEventArgs e) {
DiscordClient.Dispose();
try {
File.WriteAllText(Path.Combine(Path.GetDirectoryName(Config.FilePath)!, "log.txt"), Log?.ToString());
}
catch { }
}
private static void DiscordInit() {
DiscordClient.Logger = new ConsoleLogger() { Level = DiscordRPC.Logging.LogLevel.Warning };
DiscordClient.OnReady += (_, e) => Console.WriteLine($"Connected to {e.User.Username}'s Discord Client");
DiscordClient.OnPresenceUpdate += (_, e) => {
if (e.Presence != null)
Console.WriteLine($"Updated Presence to [{e.Presence?.Details} | {e.Presence?.State}]");
else
Console.WriteLine($"Cleared Presence");
};
DiscordClient.Initialize();
}
private static async Task PlexSignIn(bool resignIn = false) {
string authFile = Path.Combine(Path.GetDirectoryName(Config.FilePath)!, "auth.token");
if (File.Exists(authFile) && !resignIn) {
Token = File.ReadAllText(authFile);
}
else {
Token = await PlexOAuth();
File.WriteAllText(authFile, Token);
}
try {
Account = await AccountClient.GetPlexAccountAsync(Token);
}
catch {
_ = PlexSignIn(true);
}
}
private static async Task<string> PlexOAuth() {
OAuthPin plexPin;
OAuthPin? oauthUrl = await AccountClient.CreateOAuthPinAsync("");
Process.Start(new ProcessStartInfo(oauthUrl.Url) { UseShellExecute = true });
while (true) {
plexPin = await AccountClient.GetAuthTokenFromOAuthPinAsync(oauthUrl.Id.ToString());
if (!string.IsNullOrEmpty(plexPin.AuthToken)) break;
await Task.Delay(1000);
}
return plexPin.AuthToken;
}
}
}