Implemented debug switch.

FOV update on aim-in
replaced *InGameFOV() functions with a property
This commit is contained in:
notGreg 2022-09-28 20:14:35 +02:00
parent 7edad7d276
commit b87e4e014a

View File

@ -7,15 +7,18 @@ using System.Reflection;
using System.Collections; using System.Collections;
using EFT.Settings.Graphics; using EFT.Settings.Graphics;
using System.ComponentModel; using System.ComponentModel;
using EFT.Animations;
using UnityEngine.Rendering.PostProcessing;
namespace ScopeTweaks namespace ScopeTweaks
{ {
[BepInPlugin("com.notGreg.scopeTweaks", "notGreg's Scope Tweaks", "2.0.0")] [BepInPlugin("com.notGreg.scopeTweaks", "notGreg's Scope Tweaks", "2.1.0")]
public class Plugin : BaseUnityPlugin public class Plugin : BaseUnityPlugin
{ {
ConfigEntry<int> cameraResolutionScale; ConfigEntry<int> cameraResolutionScale;
ConfigEntry<int> scopeCameraResolutionScale; ConfigEntry<int> scopeCameraResolutionScale;
ConfigEntry<EFOVScalingMode> scopeFixType; ConfigEntry<EFOVScalingMode> scopeFixType;
ConfigEntry<bool> enableDebug;
enum EFOVScalingMode enum EFOVScalingMode
{ {
@ -38,16 +41,17 @@ namespace ScopeTweaks
"General", "General",
"Scope camera scale %", "Scope camera scale %",
80, 80,
new ConfigDescription("Additional override applied on top of currently enabled resolution scaling method.", new AcceptableValueRange<int>(25, (int)(100f / currentScalingFactor)))); new ConfigDescription("Additional override applied on top of currently enabled resolution scaling method.", new AcceptableValueRange<int>(25, 100)));
scopeFixType = Config.Bind("General", "High FOV sight tweak", EFOVScalingMode.ScopesOnly, new ConfigDescription("")); scopeFixType = Config.Bind(
"General",
"High FOV sight tweak",
EFOVScalingMode.ScopesOnly,
new ConfigDescription(""));
enableDebug = Config.Bind("Debug", "Enable debug logging", false);
} }
void Update() void Update()
{ {
if (Singleton<AbstractGame>.Instance == null) return; if (Singleton<AbstractGame>.Instance == null) return;
@ -59,24 +63,32 @@ namespace ScopeTweaks
{ {
case GameStatus.Started: case GameStatus.Started:
{ {
Logger.LogInfo("Getting local player"); if(enableDebug.Value)Logger.LogInfo("Getting local player");
mainPlayer = getLocalPlayer(); mainPlayer = getLocalPlayer();
subscribeHandsChangedEvent(); subscribeHandsChangedEvent();
Logger.LogInfo("Assigning cameras..."); if(enableDebug.Value)Logger.LogInfo("Assigning cameras...");
StartCoroutine(tryGetMainCamera()); StartCoroutine(tryGetMainCamera());
StartCoroutine(tryGetScopeCamera()); StartCoroutine(tryGetScopeCamera());
defaultInGameFOV = inGameFOV;
break; break;
} }
case GameStatus.SoftStopping: case GameStatus.SoftStopping:
case GameStatus.Stopping:
case GameStatus.Stopped: case GameStatus.Stopped:
{ {
Logger.LogInfo("Resetting...");
if(enableDebug.Value)Logger.LogInfo("Resetting...");
FPSCamera = null; FPSCamera = null;
scopeCamera = null; scopeCamera = null;
mainPlayer = null; mainPlayer = null;
if(enableDebug.Value)Logger.LogInfo($"Restoring FOV in settings: {defaultInGameFOV}");
inGameFOV = defaultInGameFOV;
break; break;
} }
default: break; default: break;
@ -104,20 +116,20 @@ namespace ScopeTweaks
/// <summary> /// <summary>
/// FIELD OF VIEW /// FIELD OF VIEW
/// </summary> /// </summary>
///
int inGameFOV; private int defaultInGameFOV;
int getInGameFOV() private int inGameFOV
{ {
get {
int fov = Singleton<GClass1642>.Instance.Game.Settings.FieldOfView.Value; int fov = Singleton<GClass1642>.Instance.Game.Settings.FieldOfView.Value;
Logger.LogInfo($"In-game FOV: {fov}"); if (enableDebug.Value) Logger.LogInfo($"get_defaultInGameFOV: Reading from settings: {fov}");
return fov; return fov;
} }
set {
void setInGameFOV(int value) if (enableDebug.Value) Logger.LogInfo($"set_defaultInGameFOV: Writing to settings: {value}");
{
Logger.LogInfo($"setInGameFOV(): Update to {value}");
Singleton<GClass1642>.Instance.Game.Settings.FieldOfView.Value = value; Singleton<GClass1642>.Instance.Game.Settings.FieldOfView.Value = value;
} }
}
/// <summary> /// <summary>
/// CAMERA SETUP /// CAMERA SETUP
@ -136,11 +148,11 @@ namespace ScopeTweaks
if (GameObject.Find(cameraName) != null) if (GameObject.Find(cameraName) != null)
{ {
FPSCamera = GameObject.Find(cameraName).GetComponent<Camera>(); FPSCamera = GameObject.Find(cameraName).GetComponent<Camera>();
Logger.LogInfo($"{FPSCamera.name} found!"); if(enableDebug.Value)Logger.LogInfo($"{FPSCamera.name} found!");
} }
else else
{ {
Logger.LogMessage($"Camera \"{cameraName}\" not found, rescheduling..."); if(enableDebug.Value)Logger.LogMessage($"Camera \"{cameraName}\" not found.");
yield return myDelaySec; yield return myDelaySec;
StartCoroutine(tryGetMainCamera()); StartCoroutine(tryGetMainCamera());
yield break; yield break;
@ -159,14 +171,14 @@ namespace ScopeTweaks
if (GameObject.Find(cameraName) != null) if (GameObject.Find(cameraName) != null)
{ {
scopeCamera = GameObject.Find(cameraName).GetComponent<Camera>(); scopeCamera = GameObject.Find(cameraName).GetComponent<Camera>();
Logger.LogInfo($"Camera \"{scopeCamera.name}\" found!"); if(enableDebug.Value)Logger.LogInfo($"Camera \"{scopeCamera.name}\" found!");
} }
yield break; yield break;
} }
void setMainCameraResolutionScale(int value = 100) void setMainCameraResolutionScale(int value = 100)
{ {
Logger.LogInfo($"Setting MainCam res scale to {value}%"); if(enableDebug.Value)Logger.LogInfo($"Setting MainCam res scale to {value}%");
ssaaInstance = FPSCamera.GetComponent<SSAA>(); ssaaInstance = FPSCamera.GetComponent<SSAA>();
_nextSSRation = getFieldInfo("_nextSSRation"); _nextSSRation = getFieldInfo("_nextSSRation");
_nextSSRation.SetValue(ssaaInstance, (float)(currentScalingFactor * value / 100f)); _nextSSRation.SetValue(ssaaInstance, (float)(currentScalingFactor * value / 100f));
@ -175,11 +187,11 @@ namespace ScopeTweaks
void setScopeCameraResolutionScale(int value) void setScopeCameraResolutionScale(int value)
{ {
if(scopeCamera == null || !scopeCamera.isActiveAndEnabled) if(scopeCamera == null || !scopeCamera.isActiveAndEnabled)
{ Logger.LogInfo("ScopeCam inactive or absent!"); { if(enableDebug.Value)Logger.LogInfo("ScopeCam inactive or absent!");
return; } return; }
Logger.LogInfo($"Setting Scope res scale to {value}%"); if(enableDebug.Value)Logger.LogInfo($"Setting Scope res scale to {value}%");
scopeCamera.GetComponent<SSAAOptic>().OpticCameraToMainCameraResolutionRatio = (float)(currentScalingFactor * value / 100); scopeCamera.GetComponent<SSAAOptic>().OpticCameraToMainCameraResolutionRatio = (float)(currentScalingFactor * (value / 100f));
} }
/// <summary> /// <summary>
@ -187,7 +199,7 @@ namespace ScopeTweaks
/// </summary> /// </summary>
void subscribeHandsChangedEvent() void subscribeHandsChangedEvent()
{ {
Logger.LogInfo("Subscribing to HandsChanged Event"); if(enableDebug.Value)Logger.LogInfo("Subscribing to HandsChanged Event");
mainPlayer.HandsChangedEvent += (handsArgs) => mainPlayer.HandsChangedEvent += (handsArgs) =>
{ {
subscribeOnAimingChangedEvent(); subscribeOnAimingChangedEvent();
@ -197,62 +209,67 @@ namespace ScopeTweaks
void subscribeOnAimingChangedEvent() void subscribeOnAimingChangedEvent()
{ {
Logger.LogInfo("Subscribing to OnAimingChanged Event"); if(enableDebug.Value)Logger.LogInfo("Subscribing to OnAimingChanged Event");
mainPlayer.HandsController.OnAimingChanged += (aimingArgs) => mainPlayer.HandsController.OnAimingChanged += (aimingArgs) =>
{ {
currentScalingFactor = getCurrentScalingFactor(); currentScalingFactor = getCurrentScalingFactor();
StartCoroutine(tryGetScopeCamera()); StartCoroutine(tryGetScopeCamera());
Logger.LogInfo("AimingChanged: notAiming");
if (!mainPlayer.ProceduralWeaponAnimation.IsAiming) if (!mainPlayer.ProceduralWeaponAnimation.IsAiming)
{ {
switch(scopeFixType.Value) switch(scopeFixType.Value)
{ {
case EFOVScalingMode.Disabled: case EFOVScalingMode.Disabled:
{ {
Logger.LogInfo("Nothing to do!"); if(enableDebug.Value)Logger.LogInfo($"Switch: Not Aiming: ScalingMode: {scopeFixType.Value}");
break; break;
} }
case EFOVScalingMode.ScopesOnly: case EFOVScalingMode.ScopesOnly:
{ {
if(enableDebug.Value)Logger.LogInfo($"Switch: Not Aiming: ScalingMode: {scopeFixType.Value}");
setMainCameraResolutionScale(); setMainCameraResolutionScale();
GClass1774.Instance.SetFov(inGameFOV, 0.33f, false);
setInGameFOV(inGameFOV); GClass1774.Instance.SetFov(defaultInGameFOV, 0.33f, false);
inGameFOV = defaultInGameFOV;
break; break;
} }
case EFOVScalingMode.All: case EFOVScalingMode.All:
{ {
if(enableDebug.Value)Logger.LogInfo($"Switch: Not Aiming: ScalingMode: {scopeFixType.Value}");
setMainCameraResolutionScale(); setMainCameraResolutionScale();
GClass1774.Instance.SetFov(inGameFOV, 0.33f, false);
setInGameFOV(inGameFOV); GClass1774.Instance.SetFov(defaultInGameFOV, 0.33f, false);
inGameFOV = defaultInGameFOV;
break; break;
} }
} }
} }
Logger.LogInfo("Entering switch()");
if (mainPlayer.ProceduralWeaponAnimation.IsAiming) if (mainPlayer.ProceduralWeaponAnimation.IsAiming)
{ {
if (inGameFOV != 50) { inGameFOV = getInGameFOV(); } defaultInGameFOV = inGameFOV;
switch (scopeFixType.Value) switch (scopeFixType.Value)
{ {
case EFOVScalingMode.Disabled: case EFOVScalingMode.Disabled:
{ {
Logger.LogInfo("Plugin is disabled!"); if(enableDebug.Value)Logger.LogInfo($"Switch: Aiming: ScalingMode: {scopeFixType.Value}");
break; break;
} }
case EFOVScalingMode.ScopesOnly: case EFOVScalingMode.ScopesOnly:
{ {
Logger.LogInfo("Only magnified optics!"); if(enableDebug.Value)Logger.LogInfo($"Switch: Aiming: ScalingMode: {scopeFixType.Value}");
if(scopeCamera.isActiveAndEnabled) if (scopeCamera.isActiveAndEnabled)
{ {
if(enableDebug.Value)Logger.LogInfo("ScopeCamera not found or disabled!");
applyFixesWhileAiming(); applyFixesWhileAiming();
} }
break; break;
} }
case EFOVScalingMode.All: case EFOVScalingMode.All:
{ {
Logger.LogInfo("All sights!"); if(enableDebug.Value)Logger.LogInfo($"Switch: Aiming: ScalingMode: {scopeFixType.Value}");
applyFixesWhileAiming(); applyFixesWhileAiming();
@ -265,9 +282,13 @@ namespace ScopeTweaks
void applyFixesWhileAiming() void applyFixesWhileAiming()
{ {
GClass1774.Instance.SetFov(35, 0.25f, false); if(enableDebug.Value)Logger.LogInfo($"AimingSpeed: {mainPlayer.ProceduralWeaponAnimation.AimingSpeed}");
setInGameFOV(50); if(enableDebug.Value)Logger.LogInfo("Changing FOV while aiming");
GClass1774.Instance.SetFov(35, 0.25f, false);
inGameFOV = 50;
if(enableDebug.Value)Logger.LogInfo("Updating scope resolution");
setMainCameraResolutionScale(cameraResolutionScale.Value); setMainCameraResolutionScale(cameraResolutionScale.Value);
setScopeCameraResolutionScale(scopeCameraResolutionScale.Value); setScopeCameraResolutionScale(scopeCameraResolutionScale.Value);
} }
@ -280,12 +301,12 @@ namespace ScopeTweaks
float getCurrentScalingFactor() float getCurrentScalingFactor()
{ {
Logger.LogInfo("Getting current scaling factor:"); if(enableDebug.Value)Logger.LogInfo("Getting current scaling factor:");
var graphics = Singleton<GClass1642>.Instance.Graphics.Settings; var graphics = Singleton<GClass1642>.Instance.Graphics.Settings;
if (!graphics.DLSSEnabled && !graphics.FSREnabled) if (!graphics.DLSSEnabled && !graphics.FSREnabled)
{ {
Logger.LogInfo($"Supersampling factor: {graphics.SuperSamplingFactor}"); if(enableDebug.Value)Logger.LogInfo($"Supersampling factor: {graphics.SuperSamplingFactor}");
return graphics.SuperSamplingFactor; return graphics.SuperSamplingFactor;
} }
@ -300,7 +321,7 @@ namespace ScopeTweaks
case EDLSSMode.Performance: { DLSSFactor = 0.5f; break; } case EDLSSMode.Performance: { DLSSFactor = 0.5f; break; }
case EDLSSMode.UltraPerformance: { DLSSFactor = 0.33f; break; } case EDLSSMode.UltraPerformance: { DLSSFactor = 0.33f; break; }
} }
Logger.LogInfo($"DLSS factor: {DLSSFactor}"); if(enableDebug.Value)Logger.LogInfo($"DLSS factor: {DLSSFactor}");
return DLSSFactor; return DLSSFactor;
} }
if (graphics.FSREnabled) if (graphics.FSREnabled)
@ -314,11 +335,11 @@ namespace ScopeTweaks
case EFSRMode.Balanced: { FSRFactor = 0.59f; break; } case EFSRMode.Balanced: { FSRFactor = 0.59f; break; }
case EFSRMode.Performance: { FSRFactor = 0.5f; break; } case EFSRMode.Performance: { FSRFactor = 0.5f; break; }
} }
Logger.LogInfo($"FSR factor: {FSRFactor}"); if(enableDebug.Value)Logger.LogInfo($"FSR factor: {FSRFactor}");
return FSRFactor; return FSRFactor;
} }
Logger.LogInfo($"GetCurrentScalingFactor(): Something went wrong. Returning 1.0f"); if(enableDebug.Value)Logger.LogInfo($"GetCurrentScalingFactor(): Something went wrong. Returning 1.0f");
return 1.0f; return 1.0f;
} }
} }