-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDisconnectSound.cs
74 lines (69 loc) · 2.8 KB
/
DisconnectSound.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
using System;
using System.Collections;
using System.IO;
using ABI_RC.Core.Networking;
using MelonLoader;
using UnityEngine;
using UnityEngine.Networking;
using System.Net;
using System.Linq;
namespace DisconnectSound
{
public class DisconnectSound : MelonMod
{
private AudioSource _audioSource;
private void Disconnect(object sender, DarkRift.Client.DisconnectedEventArgs e)
{
if (e == null) return;
LoggerInstance.Msg("Playing Disconnected Sound");
_audioSource.Play();
}
public override void OnSceneWasLoaded(int buildIndex, string sceneName)
{
if (buildIndex == 3)
MelonCoroutines.Start(InitAudio());
}
private IEnumerator InitAudio()
{
if (!Directory.Exists(Directory.GetCurrentDirectory() + "//DisconnectedSound"))
Directory.CreateDirectory(Directory.GetCurrentDirectory() + "//DisconnectedSound");
if (Directory.GetFiles(Directory.GetCurrentDirectory() + "//DisconnectedSound").Length == 0)
{
using (WebClient wc = new WebClient())
wc.DownloadFile("https://github.com/Edward7s/DisconnectSound/raw/main/Sound/BipSound.mp3", Directory.GetCurrentDirectory() + "//DisconnectedSound//Audio.mp3");
}
string path = Directory.GetFiles(Directory.GetCurrentDirectory() + "//DisconnectedSound").FirstOrDefault();
if (path.EndsWith(".mp3"))
{
WWW webReq = new WWW("File://" + path);
yield return webReq;
GenerateAudioSource(webReq.GetAudioClip(false, false));
yield break;
}
if (path.EndsWith(".ogg"))
{
using (var uwr = UnityWebRequestMultimedia.GetAudioClip(path, AudioType.OGGVORBIS))
{
yield return uwr.SendWebRequest();
if (uwr.isNetworkError || uwr.isHttpError)
{
LoggerInstance.Error(uwr.error);
yield break;
}
AudioClip testAudio = DownloadHandlerAudioClip.GetContent(uwr);
testAudio.hideFlags |= HideFlags.DontUnloadUnusedAsset;
GenerateAudioSource(testAudio);
yield break;
}
}
LoggerInstance.Warning("Please put a dc.ogg in the UserData folder");
}
private void GenerateAudioSource(AudioClip audioClip)
{
_audioSource = GameObject.Find("SteamManager").AddComponent<AudioSource>();
_audioSource.clip = audioClip;
_audioSource.volume = 0.35f;
NetworkManager.Instance.GameNetwork.Disconnected += Disconnect;
}
}
}