This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
DiscordMute.cs
147 lines (113 loc) · 5.11 KB
/
DiscordMute.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
#region
using MelonLoader;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UIExpansionKit.API;
using UnityEngine;
using UnityEngine.UI;
using Keys = System.Windows.Forms.Keys;
using VRC.UI.Elements.Controls;
using VRC.UI.Elements.Tooltips;
#endregion
namespace DiscordMute
{
public static class BuildInfo
{
public const string Name = "DiscordMute";
public const string Description = "Mod for mute/unmute Discord directly in-game";
public const string Author = "Rafa";
public const string Company = "RBX";
public const string Version = "1.2.0";
public const string DownloadLink = null;
}
public class DiscordMute : MelonMod
{
#region DllImport
[DllImport("user32.dll", SetLastError = true)]
private static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
private static readonly int KEYEVENTF_KEYUP = 0x0002;
#endregion
private string MuteKey;
public override void OnApplicationStart()
{
MelonPreferences.CreateCategory("DiscordMute");
MelonPreferences.CreateEntry("DiscordMute", nameof(MuteKey), "", is_hidden: true);
OnPreferencesSaved();
MelonCoroutines.Start(FindMePls());
}
public override void OnPreferencesSaved()
{
MuteKey = MelonPreferences.GetEntryValue<string>("DiscordMute", nameof(MuteKey));
}
private static Transform UserInterface;
private static Transform QuickMenu;
private IEnumerator FindMePls()
{
MelonLogger.Msg("Waiting for VRChat UI...");
// Thank u, xKiraiChan
// https://github.com/xKiraiChan/WingAPI/blob/master/WingAPI.cs#L30-L34
// And sorry for late response, I'm busy working on other projects
while ((UserInterface = GameObject.Find("UserInterface")?.transform) is null)
yield return null;
while ((QuickMenu = UserInterface.Find("Canvas_QuickMenu(Clone)")) is null)
yield return null;
MelonCoroutines.Start(InitializeUI());
}
private GameObject DiscordButton;
public IEnumerator InitializeUI()
{
MelonLogger.Msg("Initializing DiscordMute UI...");
var parentObject = QuickMenu.Find("Container").Find("Window").transform;
var originalMic = parentObject.Find("MicButton").gameObject;
DiscordButton = UnityEngine.Object.Instantiate(originalMic, originalMic.transform);
DiscordButton.name = "DiscordButton";
DiscordButton.transform.SetParent(parentObject);
DiscordButton.SetActive(true);
DiscordButton.transform.localPosition += new Vector3(30, 97, 0);
MelonLogger.Msg("Initializing Bind Manager");
BindManager.Initialize();
void ShowBindManager()
{
BindManager.Show("Press your mute key in keyboard", new Action<List<Keys>>(selectedKeys =>
{
string stringKeys = "";
if (selectedKeys.Count > 0) stringKeys = string.Join(",", selectedKeys);
MelonPreferences.SetEntryValue("DiscordMute", nameof(MuteKey), stringKeys);
MelonPreferences.Save();
}), null);
}
ExpansionKitApi.GetExpandedMenu(ExpandedMenu.SettingsMenu).AddSimpleButton("Discord Bind", () => ShowBindManager());
UnityEngine.Object.Destroy(DiscordButton.GetComponent<MicToggle>());
UnityEngine.Object.Destroy(DiscordButton.GetComponent<Toggle>());
yield return null; // Wait object destroy
var tooltip = DiscordButton.GetComponent<UiToggleTooltip>();
tooltip.field_Public_String_0 = "Discord";
tooltip.field_Public_String_1 = "Discord";
var toggle = DiscordButton.AddComponent<Toggle>();
yield return null; // Wait component
toggle.onValueChanged.RemoveAllListeners();
toggle.isOn = true;
toggle.onValueChanged.AddListener(new Action<bool>(value =>
{
if (!string.IsNullOrEmpty(MuteKey))
{
List<Keys> selectedKeys = new List<Keys>();
if (!string.IsNullOrEmpty(MuteKey))
{
string[] stringKeys = MuteKey.Split(',');
foreach (var stringKey in stringKeys) selectedKeys.Add((Keys)Enum.Parse(typeof(Keys), stringKey));
}
// Hold and Release the selected keys
foreach (var key in selectedKeys) HoldKey(key);
foreach (var key in selectedKeys) ReleaseKey(key);
}
else ShowBindManager();
}));
yield break;
}
private void HoldKey(Keys key) => keybd_event((byte)key, (byte)key, 0, 0);
private void ReleaseKey(Keys key) => keybd_event((byte)key, (byte)key, KEYEVENTF_KEYUP, 0);
}
}