353 lines
13 KiB
C#
Raw Normal View History

2022-09-08 21:29:16 +02:00
using BepInEx;
using BepInEx.Bootstrap;
2022-09-08 21:29:16 +02:00
using BepInEx.Configuration;
using Comfort.Common;
2023-01-10 11:49:09 +01:00
using EFT;
2022-09-18 23:54:23 +02:00
using EFT.Settings.Graphics;
2023-01-10 11:49:09 +01:00
using System.Collections;
using System.ComponentModel;
2023-01-10 11:49:09 +01:00
using System.Reflection;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
2022-09-08 21:29:16 +02:00
2023-01-10 11:49:09 +01:00
/* 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
*/
2022-09-17 20:08:36 +02:00
namespace ScopeTweaks
2022-09-08 21:29:16 +02:00
{
[BepInPlugin("com.notGreg.scopeTweaks", "notGreg's Scope Tweaks", "3.5.3")]
[BepInDependency("FOVFix", BepInDependency.DependencyFlags.SoftDependency)]
2022-09-08 21:29:16 +02:00
public class Plugin : BaseUnityPlugin
{
ConfigEntry<int> scopeCameraResolutionScale;
2022-09-18 22:12:16 +02:00
ConfigEntry<EFOVScalingMode> scopeFixType;
ConfigEntry<bool> enableDebug;
2022-09-16 13:24:26 +02:00
2022-09-18 22:12:16 +02:00
enum EFOVScalingMode
2022-09-17 19:49:55 +02:00
{
2022-09-16 13:24:26 +02:00
Disabled,
[Description("Magnified optics")]
ScopesOnly,
[Description("All sights")]
All,
2022-09-16 13:24:26 +02:00
}
2023-01-10 11:49:09 +01:00
//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;
2023-01-10 11:49:09 +01:00
//The correct GClass can be found by searching for "SetFov" in Assembly-CSharp.dll via dnSpy, netFiddle, ilSpy, etc.
CameraClass setFovLibrary = CameraClass.Instance;
2023-01-10 11:49:09 +01:00
2022-09-17 19:49:55 +02:00
void Awake()
{
if (Chainloader.PluginInfos.ContainsKey("FOVFix"))
{
Logger.LogWarning("Fontaine's FOV Fix detected! FOV Fix will NOT be available.");
}
2022-09-17 19:49:55 +02:00
scopeCameraResolutionScale = Config.Bind(
"General",
"Scope camera scale %",
2022-09-20 22:02:46 +02:00
80,
new ConfigDescription("Additional override applied on top of currently enabled resolution scaling method.", new AcceptableValueRange<int>(10, 100)));
2022-09-24 08:42:25 +02:00
scopeFixType = Config.Bind(
"General",
"High FOV sight tweak",
EFOVScalingMode.ScopesOnly,
new ConfigDescription(""));
2022-09-24 08:42:25 +02:00
enableDebug = Config.Bind("Debug", "Enable debug logging", false);
}
2022-09-16 10:29:54 +02:00
void FixedUpdate()
2022-09-17 19:49:55 +02:00
{
2023-01-10 11:49:09 +01:00
//Check if the game is in a valid state and execute logic depening on the status of the game
2022-09-20 22:02:46 +02:00
if (Singleton<AbstractGame>.Instance == null) return;
GameStatus currentGameState = Singleton<AbstractGame>.Instance.Status;
2022-09-16 13:24:26 +02:00
2023-01-10 11:49:09 +01:00
//logic should only execute once per game instead of every frame
2022-09-20 22:02:46 +02:00
if (!gameStateChanged(currentGameState)) return;
2022-09-16 10:29:54 +02:00
2023-01-10 11:49:09 +01:00
switch (currentGameState)
2022-09-16 10:29:54 +02:00
{
2022-09-20 22:02:46 +02:00
case GameStatus.Started:
{
2023-01-10 11:49:09 +01:00
if (enableDebug.Value) Logger.LogInfo("Getting local player");
2022-09-20 22:02:46 +02:00
mainPlayer = getLocalPlayer();
2022-09-08 21:29:16 +02:00
2022-09-20 22:02:46 +02:00
subscribeHandsChangedEvent();
2022-09-08 21:29:16 +02:00
2023-01-10 11:49:09 +01:00
if (enableDebug.Value) Logger.LogInfo("Assigning cameras...");
2022-09-20 22:02:46 +02:00
StartCoroutine(tryGetScopeCamera());
defaultInGameFOV = inGameFOV;
2022-09-20 22:02:46 +02:00
break;
}
case GameStatus.SoftStopping:
case GameStatus.Stopping:
2022-09-20 22:02:46 +02:00
case GameStatus.Stopped:
{
2023-01-10 11:49:09 +01:00
if (enableDebug.Value) Logger.LogInfo("Resetting...");
2022-09-20 22:02:46 +02:00
scopeCamera = null;
mainPlayer = null;
2023-01-10 11:49:09 +01:00
if (enableDebug.Value) Logger.LogInfo($"Restoring FOV in settings: {defaultInGameFOV}");
inGameFOV = defaultInGameFOV;
2022-09-20 22:02:46 +02:00
break;
}
default: break;
}
2022-09-20 22:02:46 +02:00
}
/// <summary>
/// GAME STATUS
/// </summary>
2022-09-20 22:02:46 +02:00
GameStatus lastGameState;
2023-01-10 11:49:09 +01:00
//compare current game status to the last saved game status, return true if game status has changed
2022-09-20 22:02:46 +02:00
bool gameStateChanged(GameStatus currentState)
{
if (currentState == lastGameState) return false;
lastGameState = currentState;
return true;
2022-09-17 19:49:55 +02:00
}
2022-09-16 13:24:26 +02:00
Player mainPlayer = null;
2023-01-10 11:49:09 +01:00
//find and return the player character in the session
2022-09-20 22:02:46 +02:00
Player getLocalPlayer()
2022-09-18 22:12:16 +02:00
{
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;
2022-09-18 22:12:16 +02:00
}
2022-09-20 22:02:46 +02:00
/// <summary>
/// FIELD OF VIEW
/// </summary>
///
private int defaultInGameFOV;
private int inGameFOV
2022-09-18 22:12:16 +02:00
{
2023-01-10 11:49:09 +01:00
get
{
int fov = Singleton<SharedGameSettingsClass>.Instance.Game.Settings.FieldOfView.Value;
if (enableDebug.Value) Logger.LogInfo($"get_defaultInGameFOV: Reading from settings: {fov}");
return fov;
}
2023-01-10 11:49:09 +01:00
set
{
if (enableDebug.Value) Logger.LogInfo($"set_defaultInGameFOV: Writing to settings: {value}");
Singleton<SharedGameSettingsClass>.Instance.Game.Settings.FieldOfView.Value = value;
}
2022-09-18 22:12:16 +02:00
}
2022-09-18 23:54:23 +02:00
2022-09-20 22:02:46 +02:00
/// <summary>
/// CAMERA SETUP
/// </summary>
2022-09-24 08:42:25 +02:00
Camera scopeCamera = null;
2022-09-20 22:02:46 +02:00
IEnumerator tryGetScopeCamera()
2022-09-17 19:49:55 +02:00
{
2022-09-20 22:02:46 +02:00
string cameraName = "BaseOpticCamera(Clone)";
if (GameObject.Find(cameraName) != null)
2022-09-17 19:49:55 +02:00
{
2022-09-20 22:02:46 +02:00
scopeCamera = GameObject.Find(cameraName).GetComponent<Camera>();
2023-01-10 11:49:09 +01:00
if (enableDebug.Value) Logger.LogInfo($"Camera \"{scopeCamera.name}\" found!");
StopCoroutine(tryGetScopeCamera());
2022-09-17 19:49:55 +02:00
}
2022-09-20 22:02:46 +02:00
yield break;
2022-09-17 19:49:55 +02:00
}
2022-09-18 22:12:16 +02:00
SSAAOptic ssaaOpticInstance = null;
2022-09-20 22:02:46 +02:00
void setScopeCameraResolutionScale(int value)
2022-09-17 19:49:55 +02:00
{
2023-01-10 11:49:09 +01:00
if (scopeCamera == null || !scopeCamera.isActiveAndEnabled)
{
if (enableDebug.Value) Logger.LogInfo("ScopeCam inactive or absent!");
return;
}
2022-09-16 13:24:26 +02:00
2023-01-10 11:49:09 +01:00
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));
2022-09-20 22:02:46 +02:00
}
2022-09-20 22:02:46 +02:00
/// <summary>
/// PLAYER WEAPON EVENTS
/// </summary>
void subscribeHandsChangedEvent()
{
2023-01-10 11:49:09 +01:00
if (enableDebug.Value) Logger.LogInfo("Subscribing to HandsChanged Event");
2022-09-20 22:02:46 +02:00
mainPlayer.HandsChangedEvent += (handsArgs) =>
2022-09-17 19:49:55 +02:00
{
2022-09-20 22:02:46 +02:00
subscribeOnAimingChangedEvent();
};
}
2022-09-20 22:02:46 +02:00
void subscribeOnAimingChangedEvent()
{
2023-01-10 11:49:09 +01:00
if (enableDebug.Value) Logger.LogInfo("Subscribing to OnAimingChanged Event");
2022-09-20 22:02:46 +02:00
mainPlayer.HandsController.OnAimingChanged += (aimingArgs) =>
2022-09-08 21:29:16 +02:00
{
currentScalingFactor = getCurrentScalingFactor();
StartCoroutine(tryGetScopeCamera());
if (!mainPlayer.ProceduralWeaponAnimation.IsAiming)
2022-09-20 22:02:46 +02:00
{
float aimSpeed = 0.7f * mainPlayer.ProceduralWeaponAnimation.AimingSpeed;
2023-01-10 11:49:09 +01:00
switch (scopeFixType.Value)
{
case EFOVScalingMode.Disabled:
{
break;
}
case EFOVScalingMode.ScopesOnly:
{
if (mainPlayer.ProceduralWeaponAnimation.CurrentScope.IsOptic)
{
//setFovLibrary.SetFov(defaultInGameFOV, aimSpeed, false);
inGameFOV = defaultInGameFOV;
mainPlayer.ProceduralWeaponAnimation.ResetFovAdjustments(mainPlayer);
}
break;
}
case EFOVScalingMode.All:
{
//setFovLibrary.SetFov(defaultInGameFOV, aimSpeed, false);
inGameFOV = defaultInGameFOV;
mainPlayer.ProceduralWeaponAnimation.ResetFovAdjustments(mainPlayer);
break;
}
2023-01-10 11:49:09 +01:00
}
2023-01-10 11:57:07 +01:00
return;
2022-09-20 22:02:46 +02:00
}
2022-09-16 13:24:26 +02:00
if (mainPlayer.ProceduralWeaponAnimation.IsAiming)
2022-09-18 23:54:23 +02:00
{
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;
}
}
2022-09-20 22:02:46 +02:00
}
};
2022-09-18 23:54:23 +02:00
}
2022-09-08 21:29:16 +02:00
void forceADSFOV()
{
if (enableDebug.Value) Logger.LogInfo("Applying aiming tweaks");
2023-01-10 11:49:09 +01:00
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;
}
}
2022-09-24 08:42:25 +02:00
float currentScalingFactor = 1.0f;
2022-09-20 22:02:46 +02:00
2022-09-18 23:54:23 +02:00
float getCurrentScalingFactor()
{
2023-01-10 11:49:09 +01:00
if (enableDebug.Value) Logger.LogInfo("Getting current scaling factor:");
var graphics = Singleton<SharedGameSettingsClass>.Instance.Graphics.Settings;
2022-09-18 23:54:23 +02:00
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; }
2022-09-18 23:59:26 +02:00
case EDLSSMode.UltraPerformance: { DLSSFactor = 0.33f; break; }
2022-09-18 23:54:23 +02:00
}
2023-01-10 11:49:09 +01:00
if (enableDebug.Value) Logger.LogInfo($"DLSS factor: {DLSSFactor}");
2022-09-18 23:54:23 +02:00
return DLSSFactor;
}
if (graphics.FSREnabled)
{
float FSRFactor = 1.0f;
switch (graphics.FSRMode.Value)
{
case EFSRMode.Off: { FSRFactor = 1.0f; break; }
2022-09-18 23:59:26 +02:00
case EFSRMode.UltraQuality: { FSRFactor = 0.77f; break; }
2022-09-18 23:54:23 +02:00
case EFSRMode.Quality: { FSRFactor = 0.66f; break; }
case EFSRMode.Balanced: { FSRFactor = 0.59f; break; }
case EFSRMode.Performance: { FSRFactor = 0.5f; break; }
}
2023-01-10 11:49:09 +01:00
if (enableDebug.Value) Logger.LogInfo($"FSR factor: {FSRFactor}");
2022-09-18 23:54:23 +02:00
return FSRFactor;
2022-09-08 21:29:16 +02:00
}
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;
}
2022-09-18 23:54:23 +02:00
if (enableDebug.Value) Logger.LogInfo($"Supersampling factor: {graphics.SuperSamplingFactor}");
return graphics.SuperSamplingFactor;
2022-09-08 21:29:16 +02:00
}
2023-01-10 11:49:09 +01:00
}
2022-09-20 22:02:46 +02:00
}