| | 1 | | using System; |
| | 2 | | using DCL.Helpers; |
| | 3 | | using DCL.Interface; |
| | 4 | | using UnityEngine; |
| | 5 | | using UnityEngine.Audio; |
| | 6 | |
|
| | 7 | | namespace DCL.SettingsCommon |
| | 8 | | { |
| | 9 | | public class Settings : Singleton<Settings> |
| | 10 | | { |
| | 11 | | const string QUALITY_SETTINGS_KEY = "Settings.Quality"; |
| | 12 | | const string GENERAL_SETTINGS_KEY = "Settings.General"; |
| | 13 | | const string AUDIO_SETTINGS_KEY = "Settings.Audio"; |
| | 14 | | public event Action OnResetAllSettings; |
| 0 | 15 | | public QualitySettingsData qualitySettingsPresets => qualitySettingsPreset; |
| | 16 | |
|
| | 17 | | public ISettingsRepository<QualitySettings> qualitySettings; |
| | 18 | | public ISettingsRepository<GeneralSettings> generalSettings; |
| | 19 | | public ISettingsRepository<AudioSettings> audioSettings; |
| | 20 | |
|
| | 21 | | private static QualitySettingsData qualitySettingsPreset = null; |
| | 22 | |
|
| | 23 | | public QualitySettingsData autoqualitySettings = null; |
| | 24 | | public QualitySettings lastValidAutoqualitySet; |
| | 25 | |
|
| | 26 | | private readonly BooleanVariable autosettingsEnabled = null; |
| | 27 | | private readonly AudioMixer audioMixer; |
| | 28 | |
|
| 1 | 29 | | public Settings() |
| | 30 | | { |
| 1 | 31 | | if (qualitySettingsPreset == null) |
| | 32 | | { |
| 1 | 33 | | qualitySettingsPreset = Resources.Load<QualitySettingsData>("ScriptableObjects/QualitySettingsData"); |
| | 34 | | } |
| | 35 | |
|
| 1 | 36 | | if (autoqualitySettings == null) |
| | 37 | | { |
| 1 | 38 | | autoqualitySettings = Resources.Load<QualitySettingsData>("ScriptableObjects/AutoQualitySettingsData"); |
| 1 | 39 | | lastValidAutoqualitySet = autoqualitySettings[autoqualitySettings.Length / 2]; |
| | 40 | | } |
| | 41 | |
|
| 1 | 42 | | if (autosettingsEnabled == null) |
| 1 | 43 | | autosettingsEnabled = Resources.Load<BooleanVariable>("ScriptableObjects/AutoQualityEnabled"); |
| | 44 | |
|
| 1 | 45 | | qualitySettings = new ProxySettingsRepository<QualitySettings>( |
| | 46 | | new PlayerPrefsQualitySettingsRepository( |
| | 47 | | new PlayerPrefsSettingsByKey(QUALITY_SETTINGS_KEY), |
| | 48 | | qualitySettingsPreset.defaultPreset), |
| | 49 | | new SettingsModule<QualitySettings>( |
| | 50 | | QUALITY_SETTINGS_KEY, |
| | 51 | | qualitySettingsPreset.defaultPreset)); |
| 1 | 52 | | generalSettings = new ProxySettingsRepository<GeneralSettings>( |
| | 53 | | new PlayerPrefsGeneralSettingsRepository( |
| | 54 | | new PlayerPrefsSettingsByKey(GENERAL_SETTINGS_KEY), |
| | 55 | | GetDefaultGeneralSettings()), |
| | 56 | | new SettingsModule<GeneralSettings>( |
| | 57 | | GENERAL_SETTINGS_KEY, |
| | 58 | | GetDefaultGeneralSettings())); |
| 1 | 59 | | audioSettings = new ProxySettingsRepository<AudioSettings>( |
| | 60 | | new PlayerPrefsAudioSettingsRepository( |
| | 61 | | new PlayerPrefsSettingsByKey(AUDIO_SETTINGS_KEY), |
| | 62 | | GetDefaultAudioSettings()), |
| | 63 | | new SettingsModule<AudioSettings>( |
| | 64 | | AUDIO_SETTINGS_KEY, |
| | 65 | | GetDefaultAudioSettings())); |
| | 66 | |
|
| 1 | 67 | | SubscribeToVirtualAudioMixerEvents(); |
| 1 | 68 | | audioMixer = Resources.Load<AudioMixer>("AudioMixer"); |
| 1 | 69 | | } |
| | 70 | |
|
| 4 | 71 | | public void Dispose() { UnsubscribeFromVirtualAudioMixerEvents(); } |
| | 72 | |
|
| | 73 | | public void LoadDefaultSettings() |
| | 74 | | { |
| 22 | 75 | | autosettingsEnabled.Set(false); |
| | 76 | |
|
| 22 | 77 | | qualitySettings.Reset(); |
| 22 | 78 | | generalSettings.Reset(); |
| 22 | 79 | | audioSettings.Reset(); |
| 22 | 80 | | } |
| | 81 | |
|
| | 82 | | public void ResetAllSettings() |
| | 83 | | { |
| 0 | 84 | | LoadDefaultSettings(); |
| 0 | 85 | | SaveSettings(); |
| 0 | 86 | | OnResetAllSettings?.Invoke(); |
| 0 | 87 | | } |
| | 88 | |
|
| | 89 | | private GeneralSettings GetDefaultGeneralSettings() |
| | 90 | | { |
| 2 | 91 | | return new GeneralSettings |
| | 92 | | { |
| | 93 | | mouseSensitivity = 0.6f, |
| | 94 | | scenesLoadRadius = 4, |
| | 95 | | avatarsLODDistance = 16, |
| | 96 | | maxNonLODAvatars = DataStore.DataStore_AvatarsLOD.DEFAULT_MAX_AVATAR, |
| | 97 | | voiceChatVolume = 1, |
| | 98 | | voiceChatAllow = GeneralSettings.VoiceChatAllow.ALL_USERS, |
| | 99 | | autoqualityOn = false, |
| | 100 | | namesOpacity = 0.5f, |
| | 101 | | profanityChatFiltering = true |
| | 102 | | }; |
| | 103 | | } |
| | 104 | |
|
| | 105 | | private AudioSettings GetDefaultAudioSettings() |
| | 106 | | { |
| 2 | 107 | | return new AudioSettings() |
| | 108 | | { |
| | 109 | | masterVolume = 1f, |
| | 110 | | voiceChatVolume = 1f, |
| | 111 | | avatarSFXVolume = 1f, |
| | 112 | | uiSFXVolume = 1f, |
| | 113 | | sceneSFXVolume = 1f, |
| | 114 | | musicVolume = 1f, |
| | 115 | | chatSFXEnabled = true |
| | 116 | | }; |
| | 117 | | } |
| | 118 | |
|
| | 119 | | /// <summary> |
| | 120 | | /// Apply the auto quality setting by its index on the array |
| | 121 | | /// </summary> |
| | 122 | | /// <param name="index">Index within the autoQualitySettings array</param> |
| | 123 | | public void ApplyAutoQualitySettings(int index) |
| | 124 | | { |
| 0 | 125 | | if (index < 0 || index >= autoqualitySettings.Length) |
| 0 | 126 | | return; |
| | 127 | |
|
| 0 | 128 | | lastValidAutoqualitySet = autoqualitySettings[index]; |
| | 129 | |
|
| 0 | 130 | | var qualiltyData = qualitySettings.Data; |
| | 131 | |
|
| 0 | 132 | | lastValidAutoqualitySet.baseResolution = qualiltyData.baseResolution; |
| 0 | 133 | | lastValidAutoqualitySet.fpsCap = qualiltyData.fpsCap; |
| | 134 | |
|
| 0 | 135 | | qualitySettings.Apply(lastValidAutoqualitySet); |
| 0 | 136 | | } |
| | 137 | |
|
| | 138 | | private void SubscribeToVirtualAudioMixerEvents() |
| | 139 | | { |
| 1 | 140 | | DataStore.i.virtualAudioMixer.voiceChatVolume.OnChange += ApplyVoiceChatSettings; |
| 1 | 141 | | DataStore.i.virtualAudioMixer.musicVolume.OnChange += ApplyMusicVolume; |
| 1 | 142 | | DataStore.i.virtualAudioMixer.avatarSFXVolume.OnChange += ApplyAvatarSFXVolume; |
| 1 | 143 | | DataStore.i.virtualAudioMixer.uiSFXVolume.OnChange += ApplyUISFXVolume; |
| 1 | 144 | | } |
| | 145 | |
|
| | 146 | | private void UnsubscribeFromVirtualAudioMixerEvents() |
| | 147 | | { |
| 2 | 148 | | DataStore.i.virtualAudioMixer.voiceChatVolume.OnChange -= ApplyVoiceChatSettings; |
| 2 | 149 | | DataStore.i.virtualAudioMixer.musicVolume.OnChange -= ApplyMusicVolume; |
| 2 | 150 | | DataStore.i.virtualAudioMixer.avatarSFXVolume.OnChange -= ApplyAvatarSFXVolume; |
| 2 | 151 | | DataStore.i.virtualAudioMixer.uiSFXVolume.OnChange -= ApplyUISFXVolume; |
| 2 | 152 | | } |
| | 153 | |
|
| | 154 | | public void ApplyMasterVolume() |
| | 155 | | { |
| | 156 | | // Update the "All" mixer group |
| 2 | 157 | | audioMixer.SetFloat("AllBusVolume", Utils.ToAudioMixerGroupVolume(audioSettings.Data.masterVolume)); |
| | 158 | |
|
| | 159 | | // Update voice chat volume, as it does not pass through the AudioMixer |
| 2 | 160 | | ApplyVoiceChatSettings(); |
| 2 | 161 | | } |
| | 162 | |
|
| | 163 | | public void ApplyVoiceChatSettings(float currentDataStoreVolume = 0f, float previousDataStoreVolume = 0f) |
| | 164 | | { |
| 6 | 165 | | AudioSettings audioSettingsData = audioSettings.Data; |
| 6 | 166 | | float calculatedVolume = Utils.ToVolumeCurve(DataStore.i.virtualAudioMixer.voiceChatVolume.Get() * audioSett |
| 6 | 167 | | WebInterface.ApplySettings(calculatedVolume, (int)generalSettings.Data.voiceChatAllow); |
| 6 | 168 | | } |
| | 169 | |
|
| 4 | 170 | | public void ApplyAvatarSFXVolume(float currentDataStoreVolume = 0f, float previousDataStoreVolume = 0f) { audioM |
| | 171 | |
|
| 4 | 172 | | public void ApplyUISFXVolume(float currentDataStoreVolume = 0f, float previousDataStoreVolume = 0f) { audioMixer |
| | 173 | |
|
| 4 | 174 | | public void ApplyMusicVolume(float currentDataStoreVolume = 0f, float previousDataStoreVolume = 0f) { audioMixer |
| | 175 | |
|
| | 176 | | public void SaveSettings() |
| | 177 | | { |
| 0 | 178 | | generalSettings.Save(); |
| 0 | 179 | | qualitySettings.Save(); |
| 0 | 180 | | audioSettings.Save(); |
| 0 | 181 | | PlayerPrefsUtils.Save(); |
| 0 | 182 | | } |
| | 183 | | } |
| | 184 | | } |