-
Notifications
You must be signed in to change notification settings - Fork 0
/
AudioLoader.cs
198 lines (179 loc) · 6.99 KB
/
AudioLoader.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using System.Collections.Generic;
using BepInEx;
using HarmonyLib;
using UnityEngine;
using System.Linq;
using UnityEngine.Assertions;
using UnityEngine.Networking;
namespace CardboardBoxMod;
class AudioLoader
{
public static List<SoundData> soundDataList = new List<SoundData>();
public static void Init()
{
Plugin.ModLog("Initializing AudioLoader");
// Load all audio files from the Sound directory
string directoryPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "Sound");
List<string> audioPaths = System.IO.Directory.GetFiles(directoryPath, "*.*", System.IO.SearchOption.AllDirectories)
.Where(s => s.EndsWith(".mp3") || s.EndsWith(".wav"))
.Select(s => "file://" + s.Replace("\\", "/"))
.ToList();
// Add all audio files to soundDataList
foreach (string path in audioPaths)
{
AudioClip clip = LoadFromDiskToAudioClip(path, AudioType.UNKNOWN);
if (clip != null)
{
Plugin.ModLog($"Loaded audio file: {path}");
soundDataList.Add(CreateSoundData(System.IO.Path.GetFileNameWithoutExtension(path), clip));
}
}
Plugin.ModLog("AudioLoader initialized");
}
public static SoundData CreateSoundData(string fullName, AudioClip audioClip)
{
SoundData soundData = ScriptableObject.CreateInstance<SoundData>();
// fullName has an optional postfix like "_1.0" to indicate the volume
// soundData.name will have a "custom_" prefix to indicate that this is a custom sound
if (fullName.Contains("_"))
{
string[] split = fullName.Split('_');
soundData.name = $"custom_{split[0]}";
if (split.Length > 1 && float.TryParse(split[1], out _))
soundData.volume = float.Parse(split[1]);
else
soundData.volume = 0.5f;
}
else
{
soundData.name = $"custom_{fullName}";
soundData.volume = 0.5f;
}
// Rest of the fields are copied from "ride" sound data
soundData.clip = audioClip;
soundData.loop = 0;
soundData.allowMultiple = false;
soundData.skipIfPlaying = false;
soundData.important = false;
soundData.alwaysPlay = false;
soundData.fadeAtStart = false;
soundData.delay = 0;
soundData.startAt = 0;
soundData.fadeLength = 0;
soundData.type = SoundData.Type.Default;
soundData.spatial = 0.5f;
soundData.pitch = 1;
soundData.randomPitch = 0;
soundData.chance = 1;
soundData.reverbMix = 1;
soundData.minInterval = 0;
soundData.variationPlayMethod = SoundData.VariationPlayMethod.Sequence;
soundData.noSameSound = false;
soundData.volumeAsMTP = false;
soundData.variations = null;
soundData.extraData = null;
soundData.variationIndex = 0;
soundData.lastVariation = null;
soundData.lastPlayed = 0;
soundData.altLastPlayed = 0;
return soundData;
}
// Porting from https://github.com/susy-bakaa/LCSoundTool/blob/main/Utilities/AudioUtility.cs
public static AudioClip LoadFromDiskToAudioClip(string path, AudioType type)
{
AudioClip clip = null;
using (UnityWebRequest uwr = UnityWebRequestMultimedia.GetAudioClip(path, type))
{
uwr.SendWebRequest();
try
{
while (!uwr.isDone)
{
}
if (uwr.result != UnityWebRequest.Result.Success)
Plugin.ModLog($"Failed to load WAV AudioClip from path: {path} Full error: {uwr.error}", CardboardBoxLogLevel.Error);
else
{
clip = DownloadHandlerAudioClip.GetContent(uwr);
}
}
catch (Exception err)
{
Plugin.ModLog($"{err.Message}, {err.StackTrace}", CardboardBoxLogLevel.Error);
}
}
return clip;
}
public static void DumpSoundData(SoundData __result)
{
// Print every field of SoundData
Plugin.ModLog($"\tclip :{__result.clip}");
Plugin.ModLog($"\tname :{__result.name}");
Plugin.ModLog($"\tloop :{__result.loop}");
Plugin.ModLog($"\tallowMultiple :{__result.allowMultiple}");
Plugin.ModLog($"\tskipIfPlaying :{__result.skipIfPlaying}");
Plugin.ModLog($"\timportant :{__result.important}");
Plugin.ModLog($"\talwaysPlay :{__result.alwaysPlay}");
Plugin.ModLog($"\tfadeAtStart :{__result.fadeAtStart}");
Plugin.ModLog($"\tdelay :{__result.delay}");
Plugin.ModLog($"\tstartAt :{__result.startAt}");
Plugin.ModLog($"\tfadeLength :{__result.fadeLength}");
Plugin.ModLog($"\ttype :{__result.type}");
Plugin.ModLog($"\tvolume :{__result.volume}");
Plugin.ModLog($"\tspatial :{__result.spatial}");
Plugin.ModLog($"\tpitch :{__result.pitch}");
Plugin.ModLog($"\trandomPitch :{__result.randomPitch}");
Plugin.ModLog($"\tchance :{__result.chance}");
Plugin.ModLog($"\treverbMix :{__result.reverbMix}");
Plugin.ModLog($"\tminInterval :{__result.minInterval}");
Plugin.ModLog($"\tvariationPlayMethod :{__result.variationPlayMethod}");
Plugin.ModLog($"\tnoSameSound :{__result.noSameSound}");
Plugin.ModLog($"\tvolumeAsMTP :{__result.volumeAsMTP}");
Plugin.ModLog($"\tvariations :{__result.variations}");
Plugin.ModLog($"\textraData :{__result.extraData}");
Plugin.ModLog($"\tvariationIndex :{__result.variationIndex}");
Plugin.ModLog($"\tlastVariation :{__result.lastVariation}");
Plugin.ModLog($"\tlastPlayed :{__result.lastPlayed}");
Plugin.ModLog($"\taltLastPlayed :{__result.altLastPlayed}");
}
}
// SoundManager.GetData
[HarmonyPatch(typeof(SoundManager))]
class SoundManagerPatch
{
[HarmonyPrefix]
[HarmonyPatch(nameof(SoundManager.GetData))]
static bool GetDataPrefix(SoundManager __instance, string id, ref SoundData __result)
{
// If the sound is a custom sound, return the custom sound data
if (id != null && id.Contains("custom_"))
{
Plugin.ModLog($"Getting custom sound data: {id}", CardboardBoxLogLevel.Debug);
foreach (SoundData soundData in AudioLoader.soundDataList)
{
if (soundData.name == id)
{
__result = soundData;
return false;
}
}
}
return true;
}
}
/*
An example
// Point.CallGuard
[HarmonyPatch(typeof(Point), nameof(Point.CallGuard))]
class CallGuardPatch
{
static void Prefix(Point __instance, Chara criminal, Chara caller)
{
// if (Plugin.IsUsingCardboardBox(criminal))
{
caller.PlaySound("custom_MGSAlarm", 1f, true);
}
}
}
*/