377 lines
14 KiB
C#

using BepInEx;
using BepInEx.Bootstrap;
using BepInEx.Configuration;
using Comfort.Common;
using EFT;
using EFT.Settings.Graphics;
using System.Collections;
using System.ComponentModel;
using UnityEngine;
/* Dependencies:
* ../BepInEx/core/
* BepInEx.dll
* ../EscapeFromTarkov_Data/Managed/
* Aki.Reflection.dll
* Assembly-CSharp.dll
* Comfort.dll
* ItemComponent.Types.dll
* System.dll
* Unity.Postprocessing.Runtime.dll
* UnityEngine.dll
* UnityEngine.CoreModule.dll
*/
namespace ScopeTweaks
{
[BepInPlugin("com.notGreg.scopeTweaks", "notGreg's Scope Tweaks", "3.5.3")]
[BepInDependency("FOVFix", BepInDependency.DependencyFlags.SoftDependency)]
public class Plugin : BaseUnityPlugin
{
ConfigEntry<int> scopeCameraResolutionScale;
//ConfigEntry<EFOVScalingMode> scopeFixType;
ConfigEntry<bool> enableDebug;
enum EFOVScalingMode
{
Disabled,
[Description("Magnified optics")]
ScopesOnly,
[Description("All sights")]
All,
}
//The following assignments should allow for faster patching in the future.
//The correct GClass can be found by searching for "ClearSettings" in Assembly-CSharp.dll via dnSpy, netFiddle, ilSpy, etc.
SharedGameSettingsClass settingsLibrary = Singleton<SharedGameSettingsClass>.Instance;
//The correct GClass can be found by searching for "SetFov" in Assembly-CSharp.dll via dnSpy, netFiddle, ilSpy, etc.
CameraClass setFovLibrary = CameraClass.Instance;
void Awake()
{
if (Chainloader.PluginInfos.ContainsKey("FOVFix"))
{
Logger.LogWarning("Fontaine's FOV Fix detected! FOV Fix will NOT be available.");
}
scopeCameraResolutionScale = Config.Bind(
"General",
"Scope camera scale %",
80,
new ConfigDescription("Additional override applied on top of currently enabled resolution scaling method.", new AcceptableValueRange<int>(10, 100)));
//scopeFixType = Config.Bind(
//"General",
//"High FOV sight tweak",
//EFOVScalingMode.ScopesOnly,
//new ConfigDescription(""));
enableDebug = Config.Bind("Debug", "Enable debug logging", false);
}
void FixedUpdate()
{
//Check if the game is in a valid state and execute logic depening on the status of the game
if (Singleton<AbstractGame>.Instance == null) return;
GameStatus currentGameState = Singleton<AbstractGame>.Instance.Status;
if (mainPlayer != null)
{
if (Input.GetKeyDown("["))
{
StopAllCoroutines();
CameraClass.Instance.SetFov(35, 1.0f, true);
}
if (Input.GetKeyDown("]"))
{
StopAllCoroutines();
CameraClass.Instance.SetFov(defaultInGameFOV, 1.0f, true);
}
}
//if (mainPlayer != null && mainPlayer.ProceduralWeaponAnimation.IsAiming)
//{
// if (Input.GetMouseButtonDown(0))
// {
// inGameFOV = 50;
// }
// if (Input.GetMouseButtonUp(0))
// {
// inGameFOV = defaultInGameFOV;
// }
//}
//logic should only execute once per game instead of every frame
if (!gameStateChanged(currentGameState)) return;
switch (currentGameState)
{
case GameStatus.Started:
{
if (enableDebug.Value) Logger.LogInfo("Getting local player");
mainPlayer = getLocalPlayer();
subscribeHandsChangedEvent();
if (enableDebug.Value) Logger.LogInfo("Assigning cameras...");
StartCoroutine(tryGetScopeCamera());
defaultInGameFOV = inGameFOV;
break;
}
case GameStatus.SoftStopping:
case GameStatus.Stopping:
case GameStatus.Stopped:
{
if (enableDebug.Value) Logger.LogInfo("Resetting...");
scopeCamera = null;
mainPlayer = null;
if (enableDebug.Value) Logger.LogInfo($"Restoring FOV in settings: {defaultInGameFOV}");
inGameFOV = defaultInGameFOV;
break;
}
default: break;
}
}
/// <summary>
/// GAME STATUS
/// </summary>
GameStatus lastGameState;
//compare current game status to the last saved game status, return true if game status has changed
bool gameStateChanged(GameStatus currentState)
{
if (currentState == lastGameState) return false;
lastGameState = currentState;
return true;
}
Player mainPlayer = null;
//find and return the player character in the session
Player getLocalPlayer()
{
if (enableDebug.Value) Logger.LogInfo("Looking for local player...");
Player localPlayer = Singleton<GameWorld>.Instance.RegisteredPlayers.Find(p => p.IsYourPlayer);
if (enableDebug.Value && localPlayer != null) Logger.LogInfo($"Found local player: {localPlayer.GetType()}");
return localPlayer;
}
/// <summary>
/// FIELD OF VIEW
/// </summary>
///
private int defaultInGameFOV;
private int inGameFOV
{
get
{
int fov = Singleton<SharedGameSettingsClass>.Instance.Game.Settings.FieldOfView.Value;
return fov;
}
set
{
Singleton<SharedGameSettingsClass>.Instance.Game.Settings.FieldOfView.Value = value;
}
}
/// <summary>
/// CAMERA SETUP
/// </summary>
Camera scopeCamera = null;
IEnumerator tryGetScopeCamera()
{
string cameraName = "BaseOpticCamera(Clone)";
if (GameObject.Find(cameraName) != null)
{
scopeCamera = GameObject.Find(cameraName).GetComponent<Camera>();
if (enableDebug.Value) Logger.LogInfo($"Camera \"{scopeCamera.name}\" found!");
StopCoroutine(tryGetScopeCamera());
}
yield break;
}
SSAAOptic ssaaOpticInstance = null;
void setScopeCameraResolutionScale(int value)
{
if (scopeCamera == null || !scopeCamera.isActiveAndEnabled)
{
if (enableDebug.Value) Logger.LogInfo("ScopeCam inactive or absent!");
return;
}
if (enableDebug.Value) Logger.LogInfo($"Setting Scope res scale to {value}%");
if (ssaaOpticInstance == null)
{
ssaaOpticInstance = scopeCamera.GetComponent<SSAAOptic>();
}
ssaaOpticInstance.OpticCameraToMainCameraResolutionRatio = (float)(currentScalingFactor * (value / 100.0f));
}
/// <summary>
/// PLAYER WEAPON EVENTS
/// </summary>
void subscribeHandsChangedEvent()
{
if (enableDebug.Value) Logger.LogInfo("Subscribing to HandsChanged Event");
mainPlayer.HandsChangedEvent += (handsArgs) =>
{
subscribeOnAimingChangedEvent();
};
}
void subscribeOnAimingChangedEvent()
{
if (enableDebug.Value) Logger.LogInfo("Subscribing to OnAimingChanged Event");
mainPlayer.HandsController.OnAimingChanged += (aimingArgs) =>
{
currentScalingFactor = getCurrentScalingFactor();
StartCoroutine(tryGetScopeCamera());
//if (!mainPlayer.ProceduralWeaponAnimation.IsAiming)
//{
// float aimSpeed = 0.7f * mainPlayer.ProceduralWeaponAnimation.AimingSpeed;
// switch (scopeFixType.Value)
// {
// case EFOVScalingMode.Disabled:
// {
// break;
// }
// case EFOVScalingMode.ScopesOnly:
// {
// if (mainPlayer.ProceduralWeaponAnimation.CurrentScope.IsOptic)
// {
// inGameFOV = defaultInGameFOV;
// StopAllCoroutines();
// setFovLibrary.SetFov(defaultInGameFOV, aimSpeed, true);
// //mainPlayer.ProceduralWeaponAnimation.ApplyFovAdjustments(mainPlayer);
// }
// break;
// }
// case EFOVScalingMode.All:
// {
// inGameFOV = defaultInGameFOV;
// StopAllCoroutines();
// setFovLibrary.SetFov(defaultInGameFOV, aimSpeed, true);
// //mainPlayer.ProceduralWeaponAnimation.ApplyFovAdjustments(mainPlayer);
// break;
// }
// }
// return;
//}
if (mainPlayer.ProceduralWeaponAnimation.IsAiming)
{
if(enableDebug.Value) Logger.LogInfo($"Scope: {mainPlayer.ProceduralWeaponAnimation.CurrentAimingMod.Item.LocalizedName()} isOptic: {mainPlayer.ProceduralWeaponAnimation.CurrentScope.IsOptic}");
if (enableDebug.Value) Logger.LogInfo("Updating scope resolution");
setScopeCameraResolutionScale(scopeCameraResolutionScale.Value);
defaultInGameFOV = inGameFOV;
//switch (scopeFixType.Value)
//{
// case EFOVScalingMode.Disabled:
// {
// break;
// }
// case EFOVScalingMode.ScopesOnly:
// {
// if (mainPlayer.ProceduralWeaponAnimation.CurrentScope.IsOptic)
// {
// forceADSFOV();
// }
// break;
// }
// case EFOVScalingMode.All:
// {
// forceADSFOV();
// break;
// }
//}
}
};
}
void forceADSFOV()
{
if (enableDebug.Value) Logger.LogInfo("Applying aiming tweaks");
float aimSpeed = mainPlayer.ProceduralWeaponAnimation.AimingSpeed * 0.7f;
//Doesn't take effect if Fontaine's FOV Fix is loaded.
if (!Chainloader.PluginInfos.ContainsKey("FOVFix"))
{
setFovLibrary.SetFov(35, aimSpeed, false);
inGameFOV = 50;
}
}
float currentScalingFactor = 1.0f;
float getCurrentScalingFactor()
{
if (enableDebug.Value) Logger.LogInfo("Getting current scaling factor:");
var graphics = Singleton<SharedGameSettingsClass>.Instance.Graphics.Settings;
if (graphics.DLSSEnabled)
{
float DLSSFactor = 1.0f;
switch (graphics.DLSSMode.Value)
{
case EDLSSMode.Off: { DLSSFactor = 1.0f; break; }
case EDLSSMode.Quality: { DLSSFactor = 0.67f; break; }
case EDLSSMode.Balanced: { DLSSFactor = 0.58f; break; }
case EDLSSMode.Performance: { DLSSFactor = 0.5f; break; }
case EDLSSMode.UltraPerformance: { DLSSFactor = 0.33f; break; }
}
if (enableDebug.Value) Logger.LogInfo($"DLSS factor: {DLSSFactor}");
return DLSSFactor;
}
if (graphics.FSREnabled)
{
float FSRFactor = 1.0f;
switch (graphics.FSRMode.Value)
{
case EFSRMode.Off: { FSRFactor = 1.0f; break; }
case EFSRMode.UltraQuality: { FSRFactor = 0.77f; break; }
case EFSRMode.Quality: { FSRFactor = 0.66f; break; }
case EFSRMode.Balanced: { FSRFactor = 0.59f; break; }
case EFSRMode.Performance: { FSRFactor = 0.5f; break; }
}
if (enableDebug.Value) Logger.LogInfo($"FSR factor: {FSRFactor}");
return FSRFactor;
}
if (graphics.FSR2Enabled)
{
float FSR2Factor = 1.0f;
switch (graphics.FSR2Mode.Value)
{
case EFSR2Mode.Off: { FSR2Factor = 1.0f; break; }
case EFSR2Mode.Quality: { FSR2Factor = 0.67f; break; }
case EFSR2Mode.Balanced: { FSR2Factor = 0.59f; break; }
case EFSR2Mode.Performance: { FSR2Factor = 0.5f; break; }
case EFSR2Mode.UltraPerformance: { FSR2Factor = 0.33f; break; }
}
if (enableDebug.Value) Logger.LogInfo($"FSR factor: {FSR2Factor}");
return FSR2Factor;
}
if (enableDebug.Value) Logger.LogInfo($"Supersampling factor: {graphics.SuperSamplingFactor}");
return graphics.SuperSamplingFactor;
}
}
}