-
Notifications
You must be signed in to change notification settings - Fork 2
/
UERP.cs
123 lines (105 loc) · 3.12 KB
/
UERP.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
#if UNITY_EDITOR
using System;
using UnityEngine;
using UnityEditor.SceneManagement;
using UnityEditor;
using System.Threading.Tasks;
using Discord;
using Debug = UnityEngine.Debug;
using System.Diagnostics;
// With help of MarshMello0's code : https://github.com/MarshMello0/Editor-Rich-Presence
[InitializeOnLoad]
public static class UERP
{
private const string applicationId = "846826015904497714";
private static Discord.Discord discord;
private static long startTimestamp;
private static bool playMode = false;
#region Initialization
static UERP()
{
DelayStart();
}
public static async void DelayStart(int delay = 1000)
{
await Task.Delay(delay);
if (DiscordRunning())
{
Init();
}
}
public static void Init()
{
// Start Discord plugin
try
{
discord = new Discord.Discord(long.Parse(applicationId), (long)CreateFlags.Default);
}
catch (Exception e)
{
Debug.LogError(e.ToString());
return;
}
// Get start timestamp
TimeSpan timeSpan = TimeSpan.FromMilliseconds(EditorAnalyticsSessionInfo.elapsedTime);
startTimestamp = DateTimeOffset.Now.Add(timeSpan).ToUnixTimeSeconds();
// Update activity
EditorApplication.update += Update;
EditorApplication.playModeStateChanged += PlayModeChanged;
UpdateActivity();
}
#endregion
#region Update
private static void Update()
{
if(discord != null) discord.RunCallbacks();
}
private static void PlayModeChanged (PlayModeStateChange state)
{
if (EditorApplication.isPlaying != playMode)
{
playMode = EditorApplication.isPlaying;
UpdateActivity();
}
}
public static void UpdateActivity()
{
if (discord == null)
{
Init();
return;
}
Activity activity = new Activity
{
State = EditorSceneManager.GetActiveScene().name + " scene",
Details = Application.productName,
Timestamps = { Start = startTimestamp },
Assets =
{
LargeImage = "unity-icon",
LargeText = "Unity " + Application.unityVersion,
SmallImage = EditorApplication.isPlaying ? "play-mode" : "edit-mode",
SmallText = EditorApplication.isPlaying ? "Play mode" : "Edit mode",
},
};
discord.GetActivityManager().UpdateActivity(activity, result =>
{
if (result != Result.Ok) Debug.LogError(result.ToString());
});
}
#endregion
private static bool DiscordRunning()
{
Process[] processes = Process.GetProcessesByName("Discord");
if (processes.Length == 0)
{
processes = Process.GetProcessesByName("DiscordPTB");
if (processes.Length == 0)
{
processes = Process.GetProcessesByName("DiscordCanary");
}
}
return processes.Length != 0;
}
}
#endif