-
Notifications
You must be signed in to change notification settings - Fork 398
Expand file tree
/
Copy pathSoundReplacement.cs
More file actions
162 lines (140 loc) · 5.53 KB
/
SoundReplacement.cs
File metadata and controls
162 lines (140 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Project: Daggerfall Unity
// Copyright: Copyright (C) 2009-2023 Daggerfall Workshop
// Web Site: http://www.dfworkshop.net
// License: MIT License (http://www.opensource.org/licenses/mit-license.php)
// Source Code: https://github.com/Interkarma/daggerfall-unity
// Original Author: TheLacus
// Contributors:
//
// Notes:
//
using System.Collections;
using System.IO;
using UnityEngine;
using DaggerfallWorkshop.Game.Utility.ModSupport;
namespace DaggerfallWorkshop.Utility.AssetInjection
{
/// <summary>
/// Handles import and injection of custom sounds and songs with the purpose of providing modding support.
/// Sound files are imported from mod bundles with load order or loaded directly from disk.
/// </summary>
public static class SoundReplacement
{
#region Fields & Properties
static readonly string soundPath = Path.Combine(Application.streamingAssetsPath, "Sound");
/// <summary>
/// Path to custom sounds and songs on disk.
/// </summary>
public static string SoundPath
{
get { return soundPath; }
}
#endregion
#region Public Methods
/// <summary>
/// Seek sound from mods.
/// </summary>
/// <param name="sound">Sound clip to seek.</param>
/// <param name="audioClip">Audioclip with imported sound data.</param>
/// <returns>True if sound is found.</returns>
public static bool TryImportSound(SoundClips sound, out AudioClip audioClip)
{
// For some inane reason loading .wav files unstreamed is completely broken.
return TryImportAudioClip(sound.ToString(), ".wav", true, out audioClip);
}
/// <summary>
/// Seek song from mods.
/// </summary>
/// <param name="song">Song to seek.</param>
/// <param name="audioClip">Audioclip with imported sound data.</param>
/// <returns>True if song is found.</returns>
public static bool TryImportSong(SongFiles song, out AudioClip audioClip)
{
return TryImportAudioClip(song.ToString(), ".ogg", true, out audioClip);
}
/// <summary>
/// Seek midi song from mods.
/// </summary>
/// <param name="filename">Name of song to seek including .mid extension.</param>
/// <param name="songBytes">Midi data as a byte array.</param>
/// <returns>True if song is found.</returns>
public static bool TryImportMidiSong(string filename, out byte[] songBytes)
{
return TryGetAudioBytes("song_" + filename, out songBytes);
}
#endregion
#region Private Methods
/// <summary>
/// Import sound data from modding locations as an audio clip.
/// </summary>
private static bool TryImportAudioClip(string name, string extension, bool streaming, out AudioClip audioClip)
{
if (DaggerfallUnity.Settings.AssetInjection)
{
// Seek from loose files
string path = Path.Combine(soundPath, name + extension);
if (File.Exists(path))
{
WWW www = new WWW("file://" + path); // TODO: Replace with UnityWebRequest
if (streaming) {
audioClip = www.GetAudioClip(true, true);
}
else
{
audioClip = www.GetAudioClip();
DaggerfallUnity.Instance.StartCoroutine(LoadAudioData(www, audioClip));
}
return true;
}
// Seek from mods
if (ModManager.Instance != null && ModManager.Instance.TryGetAsset(name, false, out audioClip))
{
if (audioClip.preloadAudioData || audioClip.LoadAudioData())
return true;
Debug.LogErrorFormat("Failed to load audiodata for audioclip {0}", name);
}
}
audioClip = null;
return false;
}
/// <summary>
/// Import midi data from modding locations as a byte array.
/// </summary>
private static bool TryGetAudioBytes(string name, out byte[] songBytes)
{
if (DaggerfallUnity.Settings.AssetInjection)
{
// Seek from loose files
string path = Path.Combine(soundPath, name);
if (File.Exists(path))
{
songBytes = File.ReadAllBytes(path);
return true;
}
// Seek from mods
if (ModManager.Instance != null)
{
TextAsset textAsset;
if (ModManager.Instance.TryGetAsset(name, false, out textAsset))
{
songBytes = textAsset.bytes;
return true;
}
}
}
songBytes = null;
return false;
}
/// <summary>
/// Load audio data from WWW in background.
/// </summary>
private static IEnumerator LoadAudioData(WWW www, AudioClip clip) // TODO: Replace with UnityWebRequest
{
yield return www;
if (clip.loadState == AudioDataLoadState.Failed)
Debug.LogErrorFormat("Failed to load audioclip: {0}", www.error);
www.Dispose();
}
#endregion
}
}