Skip to content

Commit

Permalink
2.2 Add QQ Music Rpc #11
Browse files Browse the repository at this point in the history
  • Loading branch information
Kxnrl committed Oct 29, 2021
1 parent 0c0c6f2 commit 7ca985d
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 55 deletions.
153 changes: 110 additions & 43 deletions NetEaseMusic-DiscordRPC/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ namespace NetEaseMusic_DiscordRPC
{
static class Program
{
private const string ApplicationId = "481562643958595594";
private const string NeteaseAppId = "481562643958595594";
private const string TencentAppId = "903485504899665990";

private static async Task Main()
{
Expand All @@ -36,16 +37,25 @@ private static async Task Main()

await GetOffsetsAsync();

_ = Task.Run(() =>
var neteaseRpc = new DiscordRpcClient(NeteaseAppId);
var tencentRpc = new DiscordRpcClient(TencentAppId);
neteaseRpc.Initialize();
tencentRpc.Initialize();

if (!neteaseRpc.IsInitialized || !tencentRpc.IsInitialized)
{
using var discord = new DiscordRpcClient(ApplicationId);
discord.Initialize();
MessageBox.Show("Failed to init rpc client.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
Environment.Exit(-1);
}

_ = Task.Run(() =>
{
var playerState = false;
var currentSong = string.Empty;
var currentSing = string.Empty;
var currentRate = 0.0;
var maxSongLens = 0.0;
var lastPlaying = -1;
while (true)
try
Expand All @@ -55,40 +65,73 @@ private static async Task Main()
GC.Collect();
GC.WaitForFullGCComplete();
Thread.Sleep(TimeSpan.FromMilliseconds(250));
string title;
int pid;
var lastRate = currentRate;
var lastLens = maxSongLens;
var skipThis = false;
if (!Win32Api.User32.GetWindowTitle("OrpheusBrowserHost", out var title, out var pid) ||
pid == 0)
if (!Win32Api.User32.GetWindowTitle("OrpheusBrowserHost", out title, out pid) && !Win32Api.User32.GetWindowTitle("QQMusic_Daemon_Wnd", out title))
{
Debug.Print($"player is not running");
playerState = false;
goto update;
}
// load memory
MemoryUtil.LoadMemory(pid, ref currentRate, ref maxSongLens);
if (pid > 0)
{
// load memory
MemoryUtil.LoadMemory(pid, ref currentRate, ref maxSongLens);
var diffRate = currentRate - lastRate;
var diffRate = currentRate - lastRate;
if (currentRate == 0.0 && maxSongLens == 0.0)
{
Debug.Print($"invalid? {currentRate} | {lastRate} | {diffRate}");
playerState = false;
}
// magic hacks? //currentRate != 0.109 &&
else if ((currentRate > 0.109 || currentRate == 0) && diffRate < 0.001 && diffRate >= 0 &&
maxSongLens == lastLens) //currentRate.Equals(lastRate)
{
Debug.Print(
$"Music pause? {currentRate} | {lastRate} | {maxSongLens} | {lastLens} | {diffRate}");
playerState = false;
if (currentRate == 0.0 && maxSongLens == 0.0)
{
Debug.Print($"invalid? {currentRate} | {lastRate} | {diffRate}");
playerState = false;
}
// magic hacks? //currentRate != 0.109 &&
else if ((currentRate > 0.109 || currentRate == 0) && diffRate < 0.001 && diffRate >= 0 &&
maxSongLens == lastLens) //currentRate.Equals(lastRate)
{
Debug.Print(
$"Music pause? {currentRate} | {lastRate} | {maxSongLens} | {lastLens} | {diffRate}");
playerState = false;
}
else if (!playerState || !maxSongLens.Equals(lastLens))
{
var match = title.Replace(" - ", "\t").Split('\t');
if (match.Length > 1)
{
currentSong = match[0];
currentSing = match[1]; // like spotify
}
else
{
currentSong = title;
currentSing = string.Empty;
}
playerState = true;
}
// check
else if (Math.Abs(diffRate) < 1.0 && neteaseRpc.CurrentPresence != null)
{
// skip playing
Debug.Print($"Skip Rpc {currentRate} | {lastRate} | {Math.Abs(diffRate)}");
skipThis = true;
}
}
else if (!playerState || !maxSongLens.Equals(lastLens))
else if (pid == 0)
{
// mark as playing and always update
playerState = true;
skipThis = false;
var match = title.Replace(" - ", "\t").Split('\t');
if (match.Length > 1)
{
Expand All @@ -100,18 +143,17 @@ private static async Task Main()
currentSong = title;
currentSing = string.Empty;
}
playerState = true;
}
// check
else if (Math.Abs(diffRate) < 1.0 && discord.CurrentPresence != null)
Debug.Print($"playerState -> {playerState} | Equals {maxSongLens} | {lastLens}");
if (lastPlaying != pid)
{
// skip playing
Debug.Print($"Skip Rpc {currentRate} | {lastRate} | {Math.Abs(diffRate)}");
skipThis = true;
// player changed
skipThis = false;
}
Debug.Print($"playerState -> {playerState} | Equals {maxSongLens} | {lastLens}");
lastPlaying = pid;
update:
// update
Expand All @@ -124,33 +166,58 @@ private static async Task Main()
{
Debug.Print(
$"Try clear Rpc {Win32Api.User32.IsFullscreenAppRunning()} | {Win32Api.User32.IsWhitelistAppRunning()}");
if (discord.CurrentPresence != null)
if (neteaseRpc.CurrentPresence != null)
{
discord.ClearPresence();
Debug.Print("Clear Rpc");
neteaseRpc.ClearPresence();
Debug.Print("Clear netease rpc");
}
if (tencentRpc.CurrentPresence != null)
{
tencentRpc.ClearPresence();
Debug.Print("Clear tencent rpc");
}
continue;
}
if (skipThis)
// skip
continue;
discord.SetPresence(new RichPresence
if (pid > 0)
{
Assets = new Assets
tencentRpc.ClearPresence();
neteaseRpc.SetPresence(new RichPresence
{
LargeImageKey = "timg",
LargeImageText = "Netease Cloud Music"
},
Timestamps = new Timestamps(
Details = $"🎵 {currentSong}",
State = $"🎤 {currentSing}",
Timestamps = new Timestamps(
DateTime.UtcNow.Subtract(TimeSpan.FromSeconds(currentRate)),
DateTime.UtcNow.Subtract(TimeSpan.FromSeconds(currentRate))
.Add(TimeSpan.FromSeconds(maxSongLens))),
Details = currentSong,
State = $"by {currentSing}"
});
Assets = new Assets
{
LargeImageKey = "timg",
LargeImageText = "Netease Cloud Music"
}
});
}
else if (pid == 0)
{
neteaseRpc.ClearPresence();
tencentRpc.SetPresence(new RichPresence
{
Details = $"🎵 {currentSong}",
State = $"🎤 {currentSing}",
Assets = new Assets
{
LargeImageKey = "qimg",
LargeImageText = "QQMusic"
}
});
}
Debug.Print("Update Rpc");
}
Expand Down
4 changes: 2 additions & 2 deletions NetEaseMusic-DiscordRPC/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
// 可以指定所有值,也可以使用以下所示的 "*" 预置版本号和修订号
// 方法是按如下所示使用“*”: :
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.1.0")]
[assembly: AssemblyFileVersion("2.1.0")]
[assembly: AssemblyVersion("2.2.0")]
[assembly: AssemblyFileVersion("2.2.0")]
14 changes: 14 additions & 0 deletions NetEaseMusic-DiscordRPC/win32Api.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,20 @@ public static bool IsWhitelistAppRunning()
return false;
}

public static bool GetWindowTitle(string match, out string text)
{
text = null;

var handle = FindWindow(match, null);

if (handle == IntPtr.Zero)
return false;

text = GetWindowTitle(handle);

return true;
}

public static bool GetWindowTitle(string match, out string text, out int pid)
{
var title = string.Empty;
Expand Down
3 changes: 2 additions & 1 deletion NetEaseMusic-DiscordRPC/windows.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Visual Studio
VisualStudioAppManagement
devenv.exe

// Visual Studio Code
CodeHelper.exe
Code.exe

// SourceEngine Game
Valve001
Expand Down
16 changes: 7 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
# Netease Cloud Music Discord Rpc


Enables Discord [Rich Presence](https://discordapp.com/rich-presence) For Netease Cloud Music.
将网易云音乐动态同步到Discord.
2.0 Major update, Rpc will be clear when the music pauses.
2.0版本更新之后, 可以在暂停时清除Rpc状态了.

Enables Discord [Rich Presence](https://discordapp.com/rich-presence) For Netease Cloud Music and Tencent QQ Music.
将网易云音乐/QQ音乐动态同步到Discord.


### Info
Expand All @@ -15,21 +12,22 @@ Enables Discord [Rich Presence](https://discordapp.com/rich-presence) For Neteas
* 要添加软件到白名单, 只需要在windows.txt新增一行输入白名单程序的lpClassName. 查看文档 [FindWindow](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633499(v=vs.85).aspx)



### Feature
* Sync Rich Presence.
* Clear presence when you are using fullscreen or whitelist Application.
* 同步动态到Discord.
* 清除动态当你运行全屏程序或者其他白名单程序. (例如你全屏游玩CSGO或者打开了VisualStudio)



### Screenshot
![Screenshot](https://img.kxnrl.com/ugc/6929F80BC24B7D4388C852F8FBC3B870CE6E0C63)

<img src="https://img.kxnrl.com/ugc/6929F80BC24B7D4388C852F8FBC3B870CE6E0C63" width="50%" height="50%" /><img src="https://img.kxnrl.com/ugc/EEBC797196D9DF6023A6504D17744D6353DE1D40" width="49%" height="49%" />


### Changes log
#### 2.2
- 添加QQ音乐Rpc
#### 2.1
- 新增在线Offset获取
#### 2.0
- 直接读取内存来获取歌曲进度和长度
- 新增->当暂停音乐时,Rpc状态会被清除
Expand Down

0 comments on commit 7ca985d

Please sign in to comment.