Skip to content

Commit

Permalink
Potential fix for #5
Browse files Browse the repository at this point in the history
  • Loading branch information
T0biasCZe committed Jan 18, 2024
1 parent 8ba9a68 commit 5234b68
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Discord WMP/CurrentCommit.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8119c66
8ba9a68
62 changes: 47 additions & 15 deletions Discord WMP/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -278,32 +278,57 @@ private void update_Tick(object sender, EventArgs e) {
systemMediaControls.update(data, this);
}
if(use_rpc && !stopped) {
var mil = data.position_sec / data.lenght_sec;
var time = data.position + "/" + data.lenght;
var playbar = progressbar(mil, 10);
if(data.album == "") {
//grab string after " - " in title and put it to album
data.album = data.title.Substring(data.title.IndexOf(" - ") + 3);
}
playeddata = data.title + "\n " + data.artist + "\n " + data.album + "\n " + "\n" + time + "\n" + progressbar(mil, 21);
string albumart = AlbumManager.getalbumart(data.album, data.title);
playeddata += "\n" + albumart;
this.Refresh();

bool discord_not_running = false;
try {
int temp = client.TargetPipe;
}
catch {
discord_not_running = true;
}
if(discord_not_running) {
if(initialized) {
Deinitialize();
initialized = false;
Console.WriteLine("discord not running, deinitialized");
}
goto breakout;
}
if(!initialized) {
try {
if(client_id != null && client_id.Text != "" && client_id.Text.Length >= 10 /*&& int.TryParse(client_id.Text, out _)*/) {
Console.WriteLine("valid client id");
Initialize();
initialized = true;
bool result = Initialize();
if(result) {
Console.WriteLine("initialized");
initialized = true;
}
else {
Console.WriteLine("error initializing RPC.");
initialized = false;
goto breakout;
}
}
}
catch {
ConsoleColor old = Console.ForegroundColor;
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("error initializing RPC. Discord propably not running.\n If you do not want to use Discord RPC disable it in checkboxes below");
Console.ForegroundColor = old;
goto breakout;
}
}
var mil = data.position_sec / data.lenght_sec;
var time = data.position + "/" + data.lenght;
var playbar = progressbar(mil, 10);
if(data.album == "") {
//grab string after " - " in title and put it to album
data.album = data.title.Substring(data.title.IndexOf(" - ") + 3);
}
playeddata = data.title + "\n " + data.artist + "\n " + data.album + "\n " + "\n" + time + "\n" + progressbar(mil, 21);
string albumart = AlbumManager.getalbumart(data.album, data.title);
playeddata += "\n" + albumart;
this.Refresh();
client.SetPresence(new RichPresence() {
Details = data.title.Truncate(32),
State = data.artist.Truncate(32),
Expand All @@ -326,14 +351,20 @@ private void update_Tick(object sender, EventArgs e) {
Deinitialize();
initialized = false;
}
}
breakout:;
}

void Initialize() {
bool Initialize() {
/*
Create a Discord client
NOTE: If you are using Unity3D, you must use the full constructor and define
the pipe connection.
*/

if(client_id.Text.Length <= 8) {
Console.WriteLine("invalid client id");
return false;
}
client = new DiscordRpcClient(client_id.Text);

//Set the logger
Expand All @@ -350,6 +381,7 @@ the pipe connection.

//Connect to the RPC
client.Initialize();
return true;
}
void Deinitialize() {
client.Deinitialize();
Expand Down

0 comments on commit 5234b68

Please sign in to comment.