-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCustomMusic.cs
196 lines (153 loc) · 6.49 KB
/
CustomMusic.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
using HarmonyLib;
using BepInEx;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.Networking;
using static Seasons.Seasons;
namespace Seasons
{
internal static class CustomMusic
{
internal class MusicSettings
{
public float m_volume = 1f;
public float m_fadeInTime = 3f;
public bool m_alwaysFadeout = false;
public bool m_loop = true;
public bool m_resume = true;
public bool m_enabled = true;
public bool m_ambientMusic = true;
}
public const string subdirectory = "Custom music";
public static readonly Dictionary<string, AudioClip> audioClips = new Dictionary<string, AudioClip>();
public static readonly Dictionary<string, MusicSettings> clipSettings = new Dictionary<string, MusicSettings>();
internal static void SetupConfigWatcher()
{
string filter = $"*.*";
FileSystemWatcher fileSystemWatcher = new FileSystemWatcher(GetSubdirectory(), filter);
fileSystemWatcher.Changed += new FileSystemEventHandler(UpdateClipOnChange);
fileSystemWatcher.Created += new FileSystemEventHandler(UpdateClipOnChange);
fileSystemWatcher.Renamed += new RenamedEventHandler(UpdateClipOnChange);
fileSystemWatcher.Deleted += new FileSystemEventHandler(UpdateClipOnChange);
fileSystemWatcher.IncludeSubdirectories = true;
fileSystemWatcher.SynchronizingObject = ThreadingHelper.SynchronizingObject;
fileSystemWatcher.EnableRaisingEvents = true;
UpdateCustomMusic();
CheckMusicList();
SeasonEnvironment.ClearCachedObjects();
}
internal static void CheckMusicList()
{
if (!MusicMan.instance)
return;
foreach (KeyValuePair<string, AudioClip> clip in audioClips)
(MusicMan.instance.m_music.Find(music => music.m_name == clip.Key) ?? GetNewMusic(clip.Key)).m_clips = new AudioClip[1] { clip.Value };
}
private static MusicMan.NamedMusic GetNewMusic(string name)
{
MusicSettings musicSettings = clipSettings.GetValueSafe(name) ?? new MusicSettings();
MusicMan.NamedMusic music = new MusicMan.NamedMusic()
{
m_name = name,
m_ambientMusic = musicSettings.m_ambientMusic,
m_resume = musicSettings.m_resume,
m_alwaysFadeout = musicSettings.m_alwaysFadeout,
m_enabled = musicSettings.m_enabled,
m_fadeInTime = musicSettings.m_fadeInTime,
m_loop = musicSettings.m_loop,
m_volume = musicSettings.m_volume,
};
MusicMan.instance.m_music.Add(music);
return music;
}
private static string GetSubdirectory()
{
string folder = Path.Combine(configDirectory, subdirectory);
Directory.CreateDirectory(folder);
return folder;
}
private static void UpdateCustomMusic()
{
string path = GetSubdirectory();
if (!Directory.Exists(path))
return;
foreach (FileInfo file in new DirectoryInfo(path).EnumerateFiles("*.*", SearchOption.AllDirectories).OrderBy(file => file.Extension.ToLower() != ".json"))
UpdateFile(file.Name, file.FullName);
}
private static void UpdateClipOnChange(object sender, FileSystemEventArgs eargs)
{
UpdateFile(eargs.Name, eargs.FullPath);
if (eargs is RenamedEventArgs)
{
audioClips.Remove(Path.GetFileNameWithoutExtension((eargs as RenamedEventArgs).OldName));
}
CheckMusicList();
SeasonEnvironment.ClearCachedObjects();
}
private static void UpdateFile(string fileName, string filePath)
{
if (Path.GetExtension(fileName).Equals(".json", StringComparison.OrdinalIgnoreCase))
UpdateSettings(Path.GetFileNameWithoutExtension(fileName), filePath);
else
UpdateClip(Path.GetFileNameWithoutExtension(fileName), filePath);
}
private static void UpdateClip(string clipName, string fileName)
{
bool removed = audioClips.Remove(clipName);
if (!TryGetAudioClip(fileName, out AudioClip audioClip))
return;
audioClips.Add(clipName, audioClip);
LogInfo($"Custom music {(removed ? "updated" : "added")}: {clipName}");
}
private static void UpdateSettings(string clipName, string fileName)
{
bool removed = clipSettings.Remove(clipName);
if (!TryGetMusicSettings(fileName, out MusicSettings musicSettings))
return;
clipSettings.Add(clipName, musicSettings);
LogInfo($"Custom music settings {(removed ? "updated" : "added")}: {clipName}");
}
internal static bool TryGetAudioClip(string path, out AudioClip audioClip)
{
audioClip = null;
string uri = "file:///" + path.Replace("\\", "/");
UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip(uri, AudioType.UNKNOWN);
if (request == null)
return false;
request.SendWebRequest();
while (!request.isDone) { }
if (request.error != null)
{
LogWarning($"Failed to load audio from {path}: {request.error}");
return false;
}
audioClip = (request.downloadHandler as DownloadHandlerAudioClip)?.audioClip;
if ((bool)audioClip)
{
audioClip.name = Path.GetFileNameWithoutExtension(path);
return true;
}
return false;
}
internal static bool TryGetMusicSettings(string path, out MusicSettings musicSettings)
{
musicSettings = null;
if (!File.Exists(path))
return false;
try
{
musicSettings = JsonUtility.FromJson<MusicSettings>(File.ReadAllText(path));
}
catch (Exception e)
{
LogWarning($"Error reading file ({path})! Error: {e.Message}");
return false;
}
return true;
}
}
}