-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPresence.cs
136 lines (128 loc) · 4.65 KB
/
Presence.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
using UnityEngine;
using DiscordRPC;
using UnityEngine.SceneManagement;
using System.Collections;
using System;
using Devdog.General.UI;
using HarmonyLib;
namespace DiscordRichPresence
{
internal class Presence : MonoBehaviour
{
internal string Area = "Intro";
internal Locations AreaText;
internal string RichPresenceMessage = "State.Menu";
internal DiscordRpcClient Client;
internal DateTime StartTime = DateTime.UtcNow;
internal static bool InUI = false;
internal static string UIName = "INVENTORY";
internal void Awake()
{
Client = new DiscordRpcClient("1185413607165530142");
Client.Initialize();
Client.OnReady += Client_OnReady;
Client.OnConnectionEstablished += Client_OnConnectionEstablished;
Application.quitting += DisableRichPresence;
Harmony harmony = new Harmony("vaproxy.richpresence");
harmony.PatchAll();
}
private void Client_OnConnectionEstablished(object sender, DiscordRPC.Message.ConnectionEstablishedMessage args)
{
Plugin.Log.LogInfo("Connection established.");
}
private void Client_OnReady(object sender, DiscordRPC.Message.ReadyMessage args)
{
Plugin.Log.LogInfo("RichPresence ready.");
StartCoroutine(RichPresenceUpdate());
}
[HarmonyPatch(typeof(UIWindowPage))]
static class WindowPagePatches
{
[HarmonyPatch("DoShow")]
[HarmonyPostfix]
static void Postfix(ref UIWindowPage __instance)
{
UIName = __instance.name.Replace(" ", "_").Replace("'", "").Replace("\"", "").ToUpper();
}
}
internal IEnumerator RichPresenceUpdate()
{
float timer = 0f;
const float updateInterval = 0.5f; // Update interval in seconds
while (true)
{
// Manually track time
timer += Time.unscaledDeltaTime;
// Check if it's time to update the presence based on the interval
if (timer >= updateInterval)
{
// Reset the timer
timer = 0f;
//Plugin.Log.LogInfo($"Setting presence to Playing VA Proxy - {RichPresenceMessage}");
RichPresence Presence = new RichPresence();
Presence.WithDetails(RichPresenceMessage);
TimeSpan dur = DateTime.UtcNow - StartTime;
string hours = "";
if (dur.Hours != 0)
{
hours = $"{dur.Hours}h ";
}
string minutes = "";
if (dur.Minutes != 0)
{
minutes = $"{dur.Minutes}m ";
}
string seconds = "";
if (dur.Seconds != 0)
{
seconds = $"{dur.Seconds}s ";
}
Presence.WithState($"{hours}{minutes}{seconds} elapsed");
Assets assets = new Assets
{
LargeImageKey = "va_proxy",
LargeImageText = "VA Proxy"
};
Presence.WithAssets(assets);
Client.SetPresence(Presence);
}
yield return null;
}
}
internal void Update()
{
GameObject Pages = GameObject.Find("MAINMENU/Canvas/Pages");
UIWindow window = Pages.GetComponent<UIWindow>();
InUI = window.isVisible;
if (!InUI)
{
if (!AreaText || !AreaText.transform.parent)
{
AreaText = GameObject.Find("UI/ui/Area").GetComponent<Locations>();
}
if (AreaText)
{
Area = AreaText.ID;
}
Scene current = SceneManager.GetActiveScene();
if (current.name == "Menu")
{
RichPresenceMessage = "State.Menu";
}
else
{
RichPresenceMessage = $"Location.{Area.Replace(" ", "_").Replace("'", "").Replace("\"", "").ToUpper()}";
}
}
else
{
RichPresenceMessage = $"State.{UIName}";
}
}
internal void DisableRichPresence()
{
Client.ClearPresence();
Client.Dispose();
}
}
}