-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.cs
More file actions
141 lines (123 loc) · 5.53 KB
/
Copy pathPlugin.cs
File metadata and controls
141 lines (123 loc) · 5.53 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
using BepInEx;
using BepInEx.Logging;
using HarmonyLib;
using System;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Audio;
using UnityEngine.Networking;
namespace bepinex_soundmod
{
[BepInPlugin("org.bepinex.plugins.NOSoundMod", "NOSoundMod", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
internal static new ManualLogSource Logger;
public static readonly Dictionary<string, AudioClip> replacedClips = [];
private void Awake()
{
// Plugin startup logic
Logger = base.Logger;
Logger.LogInfo($"Plugin NOSoundMod is loaded!");
string soundsPath = Path.Combine(Path.GetDirectoryName(Info.Location), "sounds");
Logger.LogInfo($"Sounds path: \"{soundsPath}\"");
if (!Directory.Exists(soundsPath)) Directory.CreateDirectory(soundsPath);
foreach (string file in Directory.GetFiles(soundsPath, "*", SearchOption.AllDirectories))
{
string format = Path.GetExtension(file).Substring(1);
string name = Path.GetFileNameWithoutExtension(file);
string fullName = $"{name}.{format}";
Logger.LogInfo($"Loading file \"{fullName}\"...");
AudioClip clip = LoadClip(Path.Combine(soundsPath, file), format.ToLower());
if (clip != null)
{
Logger.LogInfo($"Loaded file \"{fullName}\"!");
clip.name = name;
replacedClips.Add(name, clip);
}
}
string keys = "";
foreach (string key in replacedClips.Keys) keys += $"\"{key}\", ";
Logger.LogInfo($"{replacedClips.Count} sound replacements: {keys.Substring(0, keys.Length - 2)}");
Harmony.CreateAndPatchAll(typeof(AudioSourcePatch));
}
[HarmonyPatch(typeof(AudioSource))]
internal class AudioSourcePatch
{
[HarmonyPatch(nameof(AudioSource.Play), [])]
[HarmonyPrefix]
public static void Play_Patch(AudioSource __instance) => ReplaceClip(__instance);
[HarmonyPatch(nameof(AudioSource.Play), [typeof(ulong)])]
[HarmonyPrefix]
public static void Play_UlongPatch(AudioSource __instance) => ReplaceClip(__instance);
[HarmonyPatch(nameof(AudioSource.Play), [typeof(double)])]
[HarmonyPrefix]
public static void Play_DoublePatch(AudioSource __instance) => ReplaceClip(__instance);
[HarmonyPatch(nameof(AudioSource.PlayDelayed), [typeof(float)])]
[HarmonyPrefix]
public static void PlayDelayed_Patch(AudioSource __instance) => ReplaceClip(__instance);
[HarmonyPatch(nameof(AudioSource.PlayOneShot), new Type[] { typeof(AudioClip), typeof(float) })]
[HarmonyPrefix]
public static void PlayOneShot_Prefix(AudioSource __instance, ref AudioClip clip)
{
clip = ReplaceClip(clip, __instance);
}
public static void PlayOneShotHelper_Patch(AudioSource source, ref AudioClip clip, float volumeScale) => clip = ReplaceClip(clip, source);
public static void ReplaceClip(AudioSource source)
{
if (source != null && source.clip != null)
source.clip = ReplaceClip(source.clip, source);
}
public static AudioClip ReplaceClip(AudioClip clip, AudioSource source)
{
string clipName = clip != null ? clip.name : "null";
string sourceName = source != null ? source.name : "null";
bool replaced = clip != null && replacedClips.ContainsKey(clip.name);
Logger.LogDebug($"Audio \"{clipName}\", source \"{sourceName}\", replaced: {replaced}");
if (clip != null && replacedClips.ContainsKey(clip.name))
return replacedClips[clip.name];
return clip;
}
}
public static AudioClip LoadClip(string path, string format)
{
AudioType type = AudioType.UNKNOWN;
switch (format)
{
case "ogg":
type = AudioType.OGGVORBIS;
break;
case "wav":
type = AudioType.WAV;
break;
case "aif":
case "aiff":
type = AudioType.AIFF;
break;
case "acc":
type = AudioType.ACC;
break;
case "mpg":
case "mpeg":
type = AudioType.MPEG;
break;
}
using (UnityWebRequest uwr = UnityWebRequestMultimedia.GetAudioClip(path, type))
{
uwr.SendWebRequest();
try
{
while (!uwr.isDone) { }
;
if (uwr.result == UnityWebRequest.Result.Success) return DownloadHandlerAudioClip.GetContent(uwr);
else Logger.LogWarning($"Failed to load clip: {uwr.error}");
}
catch (Exception err)
{
Logger.LogError($"Caught error while loading clip: {err.Message}, {err.StackTrace}");
}
}
return null;
}
}
}