forked from clash098/CustomSounds
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cs
More file actions
160 lines (125 loc) · 5.47 KB
/
Main.cs
File metadata and controls
160 lines (125 loc) · 5.47 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.IO;
using CustomSounds.Patches;
using BepInEx;
using BepInEx.Configuration;
using UnityEngine;
using System.Threading.Tasks;
using UnityEngine.Networking;
namespace CustomSounds;
[BepInPlugin(Constants.Guid, Constants.Name, Constants.Version)]
public class Main : BaseUnityPlugin
{
public static Main? Instance;
public static AudioClip? customTagSound;
public static AudioClip? customRoundEndSound;
static string soundFolder = @"C:\Program Files (x86)\Steam\steamapps\common\Gorilla Tag\BepInEx\CustomSounds\";
static ConfigEntry<string> _tagSoundPath = null!;
static ConfigEntry<string> _roundEndSoundPath = null!;
public static float roomJoinedTime;
public static string TagSoundPath
{
get => _tagSoundPath.Value;
set => _tagSoundPath.Value = value;
}
public static string RoundEndSoundPath
{
get => _roundEndSoundPath.Value;
set => _roundEndSoundPath.Value = value;
}
private bool CheckSoundExists(string path) => File.Exists(path);
private void OnJoined() => roomJoinedTime = Time.time;
private void CreateSoundsFolder()
{
{
if (!Directory.Exists(soundFolder))
{
Directory.CreateDirectory(soundFolder);
Logger.LogDebug("Created Custom Sounds Folder");
}
else
{
Logger.LogDebug("Custom Sounds Folder Already Exists");
}
}
}
private void CreateTutorialFile()
{
const string content = @"This is the super awesome tutorial for Custom Sounds by @uhclash!
=================================================================
Here's how to add a new sound to the sounds folder:
1. Download your sound file.
2. Make sure the sound file is either a .mp3, .wav, or .ogg.
3. Then move your sound file to the following folder: ""C:\Program Files (x86)\Steam\steamapps\common\Gorilla Tag\BepInEx\CustomSounds"", or your ""BepInEx\CustomSounds"" folder.
Now, here's how to select the sound file for the mod:
1. Open up the following config file: ""C:\Program Files (x86)\Steam\steamapps\common\Gorilla Tag\BepInEx\config\com.uhclash.gorillatag.CustomSounds.cfg""
2. Under the [Sound Paths] section there is two configs, ""Tag Sound Path"" and ""Round End Sound Path"".
3. There will be a default path, so add the file name of the sound in the ""BepInEx\CustomSounds"" folder.
4. Press CTRL + S to save the config.
Then you can go ahead and play, the Custom Sounds won't play if it hasn't been atleast 1 seconds since joining the lobby to avoid issues, but from then on it should work!
=================================================================
Thank you for using my mod,
Clash.";
File.WriteAllText(Path.Combine(soundFolder, "README.txt"), content);
}
private AudioType DetectAudioType(string path)
{
string extension = Path.GetExtension(path).ToLower();
AudioType audioType = extension switch
{
".wav" => AudioType.WAV,
".mp3" => AudioType.MPEG,
".ogg" => AudioType.OGGVORBIS,
_ => AudioType.UNKNOWN
};
if (audioType == AudioType.UNKNOWN)
{
Logger.LogError($"Sound file {path} is not a supported audio type! (Must be .wav, .mp3, or .ogg)");
}
return audioType;
}
private async Task<AudioClip?> LoadAudioClipAsync(string path, AudioType audioType)
{
if (!CheckSoundExists(path)) Logger.LogInfo($"{path} does not exist!");
string fixedPath = path.Replace("\\", "/");
using UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip($"file://{fixedPath}", audioType);
var operation = request.SendWebRequest();
while (!operation.isDone) await Task.Yield();
if (request.result != UnityWebRequest.Result.Success)
{
Debug.Log($"{Path.GetFileName(path)} failed to load!");
return null;
}
return DownloadHandlerAudioClip.GetContent(request);
}
private void Awake()
{
_tagSoundPath = Config.Bind("Sound Paths", "Tag Sound Path", soundFolder, new ConfigDescription("What is the path to the tag sound file? (Must be .wav or .mp3)"));
_roundEndSoundPath = Config.Bind("Sound Paths", "Round End Sound Path", soundFolder, new ConfigDescription("What is the path to the round end sound file? (Must be .wav or .mp3)"));
GorillaTagger.OnPlayerSpawned(() => NetworkSystem.Instance.OnJoinedRoomEvent.Add(OnJoined));
if (TagSoundPath == soundFolder || RoundEndSoundPath == soundFolder)
{
Logger.LogError("The file path for one of the sounds has not been set, please set the file path for both sounds.");
}
CreateSoundsFolder();
CreateTutorialFile();
}
private async void Start()
{
Instance ??= this;
Logger.LogInfo("Custom Sounds Started!");
customTagSound = await LoadAudioClipAsync(TagSoundPath, DetectAudioType(TagSoundPath));
customRoundEndSound = await LoadAudioClipAsync(RoundEndSoundPath, DetectAudioType(RoundEndSoundPath));
HarmonyPatches.Patch();
}
public void OnEnable()
{
HarmonyPatches.Patch();
Logger.LogDebug("Custom Sounds Patched!");
}
public void OnDisable()
{
HarmonyPatches.Unpatch();
Logger.LogDebug("Custom Sounds Unpatched!");
}
}