LethalCompany/Lethal Company/ExportedProject/Assets/Scripts/Assembly-CSharp/IngamePlayerSettings.cs

598 lines
16 KiB
C#
Raw Normal View History

2023-12-22 22:51:17 +00:00
using System;
using Dissonance;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Rendering;
using UnityEngine.Rendering.HighDefinition;
using UnityEngine.SceneManagement;
public class IngamePlayerSettings : MonoBehaviour
{
[Serializable]
public class Settings
{
public bool playerHasFinishedSetup;
public bool startInOnlineMode = true;
public float gammaSetting;
public int lookSensitivity = 10;
2023-12-23 00:30:32 +00:00
public bool invertYAxis;
2023-12-22 22:51:17 +00:00
public float masterVolume = 1f;
public int framerateCapIndex;
public FullScreenMode fullScreenType;
[Header("MIC SETTINGS")]
public bool micEnabled = true;
public bool pushToTalk;
public int micDeviceIndex;
public string micDevice = string.Empty;
[Header("BINDINGS")]
public string keyBindings = string.Empty;
public Settings(bool finishedSetup = true, bool onlineMode = true)
{
playerHasFinishedSetup = finishedSetup;
startInOnlineMode = onlineMode;
}
public void CopySettings(Settings copyFrom)
{
playerHasFinishedSetup = copyFrom.playerHasFinishedSetup;
startInOnlineMode = copyFrom.startInOnlineMode;
gammaSetting = copyFrom.gammaSetting;
lookSensitivity = copyFrom.lookSensitivity;
micEnabled = copyFrom.micEnabled;
pushToTalk = copyFrom.pushToTalk;
micDeviceIndex = copyFrom.micDeviceIndex;
micDevice = copyFrom.micDevice;
keyBindings = copyFrom.keyBindings;
masterVolume = copyFrom.masterVolume;
framerateCapIndex = copyFrom.framerateCapIndex;
fullScreenType = copyFrom.fullScreenType;
2023-12-23 00:30:32 +00:00
invertYAxis = copyFrom.invertYAxis;
2023-12-22 22:51:17 +00:00
}
}
public Settings settings;
public Settings unsavedSettings;
public AudioSource SettingsAudio;
public Volume universalVolume;
private DissonanceComms comms;
public bool redoLaunchSettings;
public bool changesNotApplied;
public InputActionRebindingExtensions.RebindingOperation rebindingOperation;
private SettingsOption currentRebindingKeyUI;
public PlayerInput playerInput;
2023-12-23 00:30:32 +00:00
public bool encounteredErrorDuringSave;
2023-12-22 22:51:17 +00:00
public static IngamePlayerSettings Instance { get; private set; }
private void Awake()
{
if (Instance == null)
{
Instance = this;
UnityEngine.Object.DontDestroyOnLoad(base.gameObject);
2023-12-23 00:05:55 +00:00
try
{
LoadSettingsFromPrefs();
UpdateGameToMatchSettings();
}
2023-12-23 00:30:32 +00:00
catch (Exception e)
2023-12-22 22:51:17 +00:00
{
2023-12-23 00:30:32 +00:00
DisplaySaveFileError(e);
2023-12-23 00:05:55 +00:00
return;
}
if (settings.playerHasFinishedSetup)
{
2023-12-23 00:30:32 +00:00
PreInitSceneScript preInitSceneScript = UnityEngine.Object.FindObjectOfType<PreInitSceneScript>();
if (preInitSceneScript != null)
2023-12-23 00:05:55 +00:00
{
2023-12-23 00:30:32 +00:00
preInitSceneScript.SkipToFinalSetting();
2023-12-22 22:51:17 +00:00
}
}
}
else
{
UnityEngine.Object.Destroy(base.gameObject);
}
}
2023-12-23 00:30:32 +00:00
private void DisplaySaveFileError(Exception e)
{
Debug.LogError($"Error while loading general save data file!: {e}, enabling error panel for player");
encounteredErrorDuringSave = true;
PreInitSceneScript preInitSceneScript = UnityEngine.Object.FindObjectOfType<PreInitSceneScript>();
if (preInitSceneScript != null)
{
preInitSceneScript.EnableFileCorruptedScreen();
}
}
2023-12-22 22:51:17 +00:00
public void LoadSettingsFromPrefs()
{
string filePath = "LCGeneralSaveData";
settings.playerHasFinishedSetup = ES3.Load("PlayerFinishedSetup", filePath, defaultValue: false);
settings.startInOnlineMode = ES3.Load("StartInOnlineMode", filePath, defaultValue: false);
settings.gammaSetting = ES3.Load("Gamma", filePath, 0f);
settings.masterVolume = ES3.Load("MasterVolume", filePath, 1f);
settings.lookSensitivity = ES3.Load("LookSens", filePath, 10);
settings.micEnabled = ES3.Load("MicEnabled", filePath, defaultValue: true);
settings.pushToTalk = ES3.Load("PushToTalk", filePath, defaultValue: false);
settings.micDevice = ES3.Load("CurrentMic", filePath, "LCNoMic");
settings.keyBindings = ES3.Load("Bindings", filePath, string.Empty);
settings.framerateCapIndex = ES3.Load("FPSCap", filePath, 0);
settings.fullScreenType = (FullScreenMode)ES3.Load("ScreenMode", filePath, 1);
2023-12-23 00:30:32 +00:00
settings.invertYAxis = ES3.Load("InvertYAxis", filePath, defaultValue: false);
2023-12-22 22:51:17 +00:00
if (!string.IsNullOrEmpty(settings.keyBindings))
{
playerInput.actions.LoadBindingOverridesFromJson(settings.keyBindings);
}
unsavedSettings.CopySettings(settings);
}
public void SaveSettingsToPrefs()
{
string filePath = "LCGeneralSaveData";
2023-12-23 00:30:32 +00:00
try
{
ES3.Save("PlayerFinishedSetup", settings.playerHasFinishedSetup, filePath);
ES3.Save("StartInOnlineMode", settings.startInOnlineMode, filePath);
ES3.Save("Gamma", settings.gammaSetting, filePath);
ES3.Save("MasterVolume", settings.masterVolume, filePath);
ES3.Save("LookSens", settings.lookSensitivity, filePath);
ES3.Save("MicEnabled", settings.micEnabled, filePath);
ES3.Save("PushToTalk", settings.pushToTalk, filePath);
ES3.Save("CurrentMic", settings.micDevice, filePath);
ES3.Save("Bindings", settings.keyBindings, filePath);
ES3.Save("FPSCap", settings.framerateCapIndex, filePath);
ES3.Save("ScreenMode", (int)settings.fullScreenType, filePath);
ES3.Save("InvertYAxis", settings.invertYAxis, filePath);
}
catch (Exception e)
{
DisplaySaveFileError(e);
}
2023-12-22 22:51:17 +00:00
}
public void UpdateGameToMatchSettings()
{
ChangeGamma(0, settings.gammaSetting);
SetFramerateCap(settings.framerateCapIndex);
SetFullscreenMode((int)settings.fullScreenType);
AudioListener.volume = settings.masterVolume;
UpdateMicPushToTalkButton();
RefreshAndDisplayCurrentMicrophone();
SettingsOption[] array = UnityEngine.Object.FindObjectsOfType<SettingsOption>(includeInactive: true);
for (int i = 0; i < array.Length; i++)
{
array[i].SetValueToMatchSettings();
}
if (comms != null && StartOfRound.Instance != null)
{
comms.IsMuted = !settings.micEnabled;
}
}
public void SetOption(SettingsOptionType optionType, int value)
{
if (GameNetworkManager.Instance != null)
{
SettingsAudio.PlayOneShot(GameNetworkManager.Instance.buttonTuneSFX);
}
2023-12-23 00:30:32 +00:00
Debug.Log($"Set settings not applied!; {optionType}");
2023-12-22 22:51:17 +00:00
SetChangesNotAppliedTextVisible();
switch (optionType)
{
case SettingsOptionType.Gamma:
ChangeGamma(value);
break;
case SettingsOptionType.MasterVolume:
ChangeMasterVolume(value);
break;
case SettingsOptionType.LookSens:
ChangeLookSens(value);
break;
case SettingsOptionType.MicDevice:
SwitchMicrophoneSetting();
break;
case SettingsOptionType.MicEnabled:
SetMicrophoneEnabled();
break;
case SettingsOptionType.MicPushToTalk:
SetMicPushToTalk();
break;
case SettingsOptionType.FramerateCap:
SetFramerateCap(value);
break;
case SettingsOptionType.FullscreenType:
SetFullscreenMode(value);
break;
2023-12-23 00:30:32 +00:00
case SettingsOptionType.InvertYAxis:
SetInvertYAxis();
break;
2023-12-22 22:51:17 +00:00
case SettingsOptionType.OnlineMode:
case SettingsOptionType.ChangeBinding:
case SettingsOptionType.CancelOrConfirm:
break;
}
}
2023-12-23 00:30:32 +00:00
private void SetInvertYAxis()
{
unsavedSettings.invertYAxis = !unsavedSettings.invertYAxis;
}
2023-12-22 22:51:17 +00:00
private void SetFullscreenMode(int value)
{
Screen.fullScreenMode = (FullScreenMode)value;
unsavedSettings.fullScreenType = (FullScreenMode)value;
}
private void SetFramerateCap(int value)
{
switch (value)
{
case 0:
QualitySettings.vSyncCount = 1;
Application.targetFrameRate = -1;
break;
case 1:
QualitySettings.vSyncCount = 0;
Application.targetFrameRate = 250;
break;
default:
QualitySettings.vSyncCount = 0;
switch (value)
{
case 2:
Application.targetFrameRate = 144;
break;
case 3:
Application.targetFrameRate = 120;
break;
case 4:
Application.targetFrameRate = 60;
break;
case 5:
Application.targetFrameRate = 30;
break;
}
break;
}
unsavedSettings.framerateCapIndex = value;
}
public void ChangeGamma(int setTo, float overrideWithFloat = -500f)
{
Debug.Log("Changing gamma");
float num = Mathf.Clamp((float)setTo * 0.05f, -0.85f, 2f);
Debug.Log($"override float value: {overrideWithFloat}");
if (overrideWithFloat != -500f)
{
Debug.Log("Overriding float");
num = overrideWithFloat;
}
if (universalVolume.sharedProfile.TryGet<LiftGammaGain>(out var component))
{
component.gamma.SetValue(new Vector4Parameter(new Vector4(0f, 0f, 0f, num), overrideState: true));
}
Debug.Log("Gamma changed");
unsavedSettings.gammaSetting = num;
Debug.Log($"Set gamma to new value: {num}; {component.gamma}");
}
public void ChangeMasterVolume(int setTo)
{
unsavedSettings.masterVolume = (float)setTo / 100f;
AudioListener.volume = (float)setTo / 100f;
}
public void ChangeLookSens(int setTo)
{
unsavedSettings.lookSensitivity = setTo;
Debug.Log($"Set mouse sensitivity to new value: {setTo}");
}
public void RefreshAndDisplayCurrentMicrophone(bool saveResult = true)
{
Settings settings = ((!saveResult) ? unsavedSettings : this.settings);
settings.micDeviceIndex = 0;
bool flag = false;
2023-12-23 00:30:32 +00:00
string text = ((!saveResult) ? unsavedSettings.micDevice : this.settings.micDevice);
2023-12-22 22:51:17 +00:00
for (int i = 0; i < Microphone.devices.Length; i++)
{
if (Microphone.devices[i] == text)
{
settings.micDeviceIndex = i;
settings.micDevice = Microphone.devices[i];
flag = true;
break;
}
}
if (!flag)
{
if (Microphone.devices.Length == 0)
{
SetSettingsOptionsText(SettingsOptionType.MicDevice, "No device found \n (click to refresh)");
2023-12-23 00:30:32 +00:00
settings.micDevice = "LCNoMic";
2023-12-22 22:51:17 +00:00
Debug.Log("No recording devices found");
return;
}
settings.micDevice = Microphone.devices[0];
}
SetSettingsOptionsText(SettingsOptionType.MicDevice, "Current input device: \n " + settings.micDevice);
if (saveResult && comms != null)
{
comms.MicrophoneName = settings.micDevice;
}
}
public void SetSettingsOptionsText(SettingsOptionType optionType, string setToText)
{
SettingsOption[] array = UnityEngine.Object.FindObjectsOfType<SettingsOption>(includeInactive: true);
for (int i = 0; i < array.Length; i++)
{
if (array[i].optionType == optionType && array[i].textElement != null)
{
array[i].textElement.text = setToText;
}
}
}
public void SwitchMicrophoneSetting()
{
if (Microphone.devices.Length == 0)
{
Debug.Log("No mics found when trying to switch");
return;
}
Debug.Log("Switching microphone");
unsavedSettings.micDeviceIndex = ++unsavedSettings.micDeviceIndex % Microphone.devices.Length;
unsavedSettings.micDevice = Microphone.devices[unsavedSettings.micDeviceIndex];
SetSettingsOptionsText(SettingsOptionType.MicDevice, "Current input device: \n " + unsavedSettings.micDevice);
DisplayPlayerMicVolume displayPlayerMicVolume = UnityEngine.Object.FindObjectOfType<DisplayPlayerMicVolume>();
if (displayPlayerMicVolume != null)
{
displayPlayerMicVolume.SwitchMicrophone();
}
if (comms != null)
{
comms.MicrophoneName = unsavedSettings.micDevice;
}
}
public void SetMicrophoneEnabled()
{
unsavedSettings.micEnabled = !unsavedSettings.micEnabled;
if (comms != null && StartOfRound.Instance != null)
{
comms.IsMuted = !settings.micEnabled;
}
}
public void SetMicPushToTalk()
{
unsavedSettings.pushToTalk = !unsavedSettings.pushToTalk;
if (unsavedSettings.pushToTalk)
{
SetSettingsOptionsText(SettingsOptionType.MicPushToTalk, "MODE: Push to talk");
}
else
{
SetSettingsOptionsText(SettingsOptionType.MicPushToTalk, "MODE: Voice activation");
}
}
public void UpdateMicPushToTalkButton()
{
if (settings.pushToTalk)
{
SetSettingsOptionsText(SettingsOptionType.MicPushToTalk, "MODE: Push to talk");
}
else
{
SetSettingsOptionsText(SettingsOptionType.MicPushToTalk, "MODE: Voice activation");
}
}
public void SetPlayerFinishedLaunchOptions()
{
settings.playerHasFinishedSetup = true;
unsavedSettings.playerHasFinishedSetup = true;
ES3.Save("PlayerFinishedSetup", value: true, "LCGeneralSaveData");
}
public void SetLaunchInOnlineMode(bool enable)
{
settings.startInOnlineMode = enable;
unsavedSettings.startInOnlineMode = enable;
ES3.Save("StartInOnlineMode", enable, "LCGeneralSaveData");
}
public void RebindKey(InputActionReference rebindableAction, SettingsOption optionUI)
{
if (rebindingOperation != null)
{
rebindingOperation.Dispose();
if (currentRebindingKeyUI != null)
{
currentRebindingKeyUI.currentlyUsedKeyText.enabled = true;
currentRebindingKeyUI.waitingForInput.SetActive(value: false);
}
}
2023-12-23 00:05:55 +00:00
optionUI.currentlyUsedKeyText.enabled = false;
optionUI.waitingForInput.SetActive(value: true);
2023-12-22 22:51:17 +00:00
Debug.Log("Begun rebinding key");
playerInput.DeactivateInput();
currentRebindingKeyUI = optionUI;
rebindingOperation = rebindableAction.action.PerformInteractiveRebinding().WithControlsExcluding("Mouse").OnMatchWaitForAnother(0.1f)
.OnComplete(delegate
{
CompleteRebind(optionUI);
})
.Start();
}
public void CompleteRebind(SettingsOption optionUI)
{
InputAction action = rebindingOperation.action;
if (rebindingOperation != null)
{
rebindingOperation.Dispose();
}
playerInput.ActivateInput();
int bindingIndexForControl = action.GetBindingIndexForControl(action.controls[0]);
optionUI.currentlyUsedKeyText.text = InputControlPath.ToHumanReadableString(action.bindings[bindingIndexForControl].effectivePath, InputControlPath.HumanReadableStringOptions.OmitDevice);
optionUI.currentlyUsedKeyText.enabled = true;
optionUI.waitingForInput.SetActive(value: false);
unsavedSettings.keyBindings = playerInput.actions.SaveBindingOverridesAsJson();
SetChangesNotAppliedTextVisible();
}
public void CancelRebind(SettingsOption optionUI = null)
{
if (rebindingOperation != null)
{
rebindingOperation.Dispose();
}
playerInput.ActivateInput();
if (!(optionUI == null))
{
optionUI.currentlyUsedKeyText.enabled = true;
optionUI.waitingForInput.SetActive(value: false);
}
}
public void ResetSettingsToDefault()
{
SetChangesNotAppliedTextVisible(visible: false);
CancelRebind();
playerInput.actions.RemoveAllBindingOverrides();
ES3.Save("Bindings", string.Empty, "LCGeneralSaveData");
Settings copyFrom = new Settings(settings.playerHasFinishedSetup, settings.startInOnlineMode);
settings.CopySettings(copyFrom);
unsavedSettings.CopySettings(copyFrom);
SaveSettingsToPrefs();
UpdateGameToMatchSettings();
}
public void SaveChangedSettings()
{
SetChangesNotAppliedTextVisible(visible: false);
Debug.Log("Saving changed settings");
settings.CopySettings(unsavedSettings);
SaveSettingsToPrefs();
UpdateGameToMatchSettings();
}
public void DisplayConfirmChangesScreen(bool visible)
{
MenuManager menuManager = UnityEngine.Object.FindObjectOfType<MenuManager>();
if (menuManager != null)
{
menuManager.PleaseConfirmChangesSettingsPanel.SetActive(visible);
menuManager.PleaseConfirmChangesSettingsPanelBackButton.Select();
return;
}
QuickMenuManager quickMenuManager = UnityEngine.Object.FindObjectOfType<QuickMenuManager>();
if (quickMenuManager != null)
{
quickMenuManager.PleaseConfirmChangesSettingsPanel.SetActive(visible);
quickMenuManager.PleaseConfirmChangesSettingsPanelBackButton.Select();
}
}
public void DiscardChangedSettings()
{
SetChangesNotAppliedTextVisible(visible: false);
Debug.Log("Discarding changed settings");
unsavedSettings.CopySettings(settings);
if (!string.IsNullOrEmpty(settings.keyBindings))
{
playerInput.actions.LoadBindingOverridesFromJson(settings.keyBindings);
}
else
{
playerInput.actions.RemoveAllBindingOverrides();
}
UpdateGameToMatchSettings();
}
private void OnDestroy()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
private void OnDisable()
{
SceneManager.sceneLoaded -= OnSceneLoaded;
}
private void OnEnable()
{
SceneManager.sceneLoaded += OnSceneLoaded;
}
private void OnSceneLoaded(Scene scene, LoadSceneMode loadType)
{
if (loadType == LoadSceneMode.Single)
{
UpdateGameToMatchSettings();
comms = UnityEngine.Object.FindObjectOfType<DissonanceComms>();
}
}
private void SetChangesNotAppliedTextVisible(bool visible = true)
{
changesNotApplied = visible;
MenuManager menuManager = UnityEngine.Object.FindObjectOfType<MenuManager>();
if (menuManager != null)
{
menuManager.changesNotAppliedText.enabled = visible;
if (visible)
{
menuManager.settingsBackButton.text = "DISCARD";
}
else
{
menuManager.settingsBackButton.text = "BACK";
}
return;
}
QuickMenuManager quickMenuManager = UnityEngine.Object.FindObjectOfType<QuickMenuManager>();
if (quickMenuManager != null)
{
quickMenuManager.changesNotAppliedText.enabled = visible;
if (visible)
{
quickMenuManager.settingsBackButton.text = "Discard changes";
}
else
{
quickMenuManager.settingsBackButton.text = "Back";
}
}
}
}