325 lines
11 KiB
C#
Raw Normal View History

2022-09-08 21:29:16 +02:00
using BepInEx;
using BepInEx.Configuration;
using EFT;
using UnityEngine;
using Comfort.Common;
using System.Reflection;
2022-09-17 19:49:55 +02:00
using System.Collections;
2022-09-18 23:54:23 +02:00
using EFT.Settings.Graphics;
using System.ComponentModel;
2022-09-08 21:29:16 +02:00
2022-09-17 20:08:36 +02:00
namespace ScopeTweaks
2022-09-08 21:29:16 +02:00
{
2022-09-19 07:59:29 +02:00
[BepInPlugin("com.notGreg.scopeTweaks", "notGreg's Scope Tweaks", "2.0.0")]
2022-09-08 21:29:16 +02:00
public class Plugin : BaseUnityPlugin
{
ConfigEntry<int> cameraResolutionScale;
ConfigEntry<int> scopeCameraResolutionScale;
2022-09-18 22:12:16 +02:00
ConfigEntry<EFOVScalingMode> scopeFixType;
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
}
2022-09-17 19:49:55 +02:00
void Awake()
{
cameraResolutionScale = Config.Bind(
"General",
"Main camera scale %",
80,
2022-09-24 08:42:25 +02:00
new ConfigDescription("Additional override applied on top of currently enabled resolution scaling method.", new AcceptableValueRange<int>(25, 100)));
2022-09-17 19:49:55 +02:00
scopeCameraResolutionScale = Config.Bind(
"General",
"Scope camera scale %",
2022-09-20 22:02:46 +02:00
80,
2022-09-24 08:42:25 +02:00
new ConfigDescription("Additional override applied on top of currently enabled resolution scaling method.", new AcceptableValueRange<int>(25, (int)(100f / currentScalingFactor))));
2022-09-17 19:49:55 +02:00
scopeFixType = Config.Bind("General", "High FOV sight tweak", EFOVScalingMode.ScopesOnly, new ConfigDescription(""));
2022-09-17 19:49:55 +02:00
}
2022-09-08 21:29:16 +02:00
2022-09-16 10:29:54 +02:00
2022-09-24 08:42:25 +02:00
2022-09-16 10:29:54 +02:00
2022-09-20 22:02:46 +02:00
void Update()
2022-09-17 19:49:55 +02:00
{
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
2022-09-20 22:02:46 +02:00
if (!gameStateChanged(currentGameState)) return;
2022-09-16 10:29:54 +02:00
2022-09-20 22:02:46 +02:00
switch(currentGameState)
2022-09-16 10:29:54 +02:00
{
2022-09-20 22:02:46 +02:00
case GameStatus.Started:
{
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
Logger.LogInfo("Assigning cameras...");
2022-09-20 22:02:46 +02:00
StartCoroutine(tryGetMainCamera());
StartCoroutine(tryGetScopeCamera());
2022-09-20 22:02:46 +02:00
break;
}
case GameStatus.SoftStopping:
case GameStatus.Stopped:
{
Logger.LogInfo("Resetting...");
2022-09-20 22:02:46 +02:00
FPSCamera = null;
scopeCamera = null;
mainPlayer = null;
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;
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
2022-09-20 22:02:46 +02:00
Player mainPlayer;
Player getLocalPlayer()
2022-09-18 22:12:16 +02:00
{
2022-09-20 22:02:46 +02:00
return Singleton<GameWorld>.Instance.RegisteredPlayers.Find(p => p.IsYourPlayer);
2022-09-18 22:12:16 +02:00
}
2022-09-20 22:02:46 +02:00
/// <summary>
/// FIELD OF VIEW
/// </summary>
int inGameFOV;
int getInGameFOV()
2022-09-18 22:12:16 +02:00
{
2022-09-20 22:02:46 +02:00
int fov = Singleton<GClass1642>.Instance.Game.Settings.FieldOfView.Value;
Logger.LogInfo($"In-game FOV: {fov}");
2022-09-20 22:02:46 +02:00
return fov;
2022-09-18 22:12:16 +02:00
}
2022-09-20 22:02:46 +02:00
void setInGameFOV(int value)
2022-09-18 22:12:16 +02:00
{
Logger.LogInfo($"setInGameFOV(): Update to {value}");
2022-09-19 07:59:29 +02:00
Singleton<GClass1642>.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 FPSCamera = null;
Camera scopeCamera = null;
SSAA ssaaInstance = null;
FieldInfo _nextSSRation = null;
WaitForSeconds myDelaySec = new WaitForSeconds(1);
2022-09-20 22:02:46 +02:00
IEnumerator tryGetMainCamera()
2022-09-17 19:49:55 +02:00
{
2022-09-20 22:02:46 +02:00
string cameraName = "FPS Camera";
if (GameObject.Find(cameraName) != null)
2022-09-17 19:49:55 +02:00
{
2022-09-20 22:02:46 +02:00
FPSCamera = GameObject.Find(cameraName).GetComponent<Camera>();
Logger.LogInfo($"{FPSCamera.name} found!");
2022-09-18 23:54:23 +02:00
}
2022-09-20 22:02:46 +02:00
else
2022-09-18 23:54:23 +02:00
{
Logger.LogMessage($"Camera \"{cameraName}\" not found, rescheduling...");
2022-09-20 22:02:46 +02:00
yield return myDelaySec;
StartCoroutine(tryGetMainCamera());
yield break;
2022-09-18 23:54:23 +02:00
}
2022-09-20 22:02:46 +02:00
yield return null;
2022-09-18 23:54:23 +02:00
}
2022-09-20 22:02:46 +02:00
FieldInfo getFieldInfo(string fieldName)
2022-09-18 23:54:23 +02:00
{
2022-09-20 22:02:46 +02:00
return typeof(SSAA).GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance);
2022-09-17 19:49:55 +02:00
}
2022-09-16 13:24:26 +02:00
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>();
Logger.LogInfo($"Camera \"{scopeCamera.name}\" found!");
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-16 13:24:26 +02:00
2022-09-20 22:02:46 +02:00
void setMainCameraResolutionScale(int value = 100)
2022-09-18 22:12:16 +02:00
{
Logger.LogInfo($"Setting MainCam res scale to {value}%");
2022-09-20 22:02:46 +02:00
ssaaInstance = FPSCamera.GetComponent<SSAA>();
_nextSSRation = getFieldInfo("_nextSSRation");
_nextSSRation.SetValue(ssaaInstance, (float)(currentScalingFactor * value / 100f));
2022-09-18 22:12:16 +02:00
}
2022-09-20 22:02:46 +02:00
void setScopeCameraResolutionScale(int value)
2022-09-17 19:49:55 +02:00
{
2022-09-20 22:02:46 +02:00
if(scopeCamera == null || !scopeCamera.isActiveAndEnabled)
{ Logger.LogInfo("ScopeCam inactive or absent!");
2022-09-20 22:02:46 +02:00
return; }
2022-09-16 13:24:26 +02:00
Logger.LogInfo($"Setting Scope res scale to {value}%");
2022-09-20 22:02:46 +02:00
scopeCamera.GetComponent<SSAAOptic>().OpticCameraToMainCameraResolutionRatio = (float)(currentScalingFactor * value / 100);
}
2022-09-20 22:02:46 +02:00
/// <summary>
/// PLAYER WEAPON EVENTS
/// </summary>
void subscribeHandsChangedEvent()
{
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()
{
2022-09-24 08:42:25 +02:00
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());
Logger.LogInfo("AimingChanged: notAiming");
if (!mainPlayer.ProceduralWeaponAnimation.IsAiming)
2022-09-20 22:02:46 +02:00
{
2022-09-24 08:42:25 +02:00
switch(scopeFixType.Value)
{
case EFOVScalingMode.Disabled:
{
Logger.LogInfo("Nothing to do!");
break;
}
case EFOVScalingMode.ScopesOnly:
{
setMainCameraResolutionScale();
2022-09-24 08:42:25 +02:00
GClass1774.Instance.SetFov(inGameFOV, 0.33f, false);
setInGameFOV(inGameFOV);
break;
}
case EFOVScalingMode.All:
{
setMainCameraResolutionScale();
2022-09-24 08:42:25 +02:00
GClass1774.Instance.SetFov(inGameFOV, 0.33f, false);
setInGameFOV(inGameFOV);
break;
}
}
2022-09-20 22:02:46 +02:00
}
2022-09-16 13:24:26 +02:00
Logger.LogInfo("Entering switch()");
if (mainPlayer.ProceduralWeaponAnimation.IsAiming)
2022-09-18 23:54:23 +02:00
{
if (inGameFOV != 50) { inGameFOV = getInGameFOV(); }
switch (scopeFixType.Value)
{
case EFOVScalingMode.Disabled:
{
Logger.LogInfo("Plugin is disabled!");
break;
}
case EFOVScalingMode.ScopesOnly:
{
Logger.LogInfo("Only magnified optics!");
if(scopeCamera.isActiveAndEnabled)
{
applyFixesWhileAiming();
}
break;
}
case EFOVScalingMode.All:
{
Logger.LogInfo("All sights!");
applyFixesWhileAiming();
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 applyFixesWhileAiming()
{
GClass1774.Instance.SetFov(35, 0.25f, false);
setInGameFOV(50);
setMainCameraResolutionScale(cameraResolutionScale.Value);
setScopeCameraResolutionScale(scopeCameraResolutionScale.Value);
}
2022-09-20 22:02:46 +02:00
/// <summary>
/// IN-GAME SETTINGS
/// </summary>
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()
{
Logger.LogInfo("Getting current scaling factor:");
2022-09-19 07:59:29 +02:00
var graphics = Singleton<GClass1642>.Instance.Graphics.Settings;
2022-09-18 23:54:23 +02:00
if (!graphics.DLSSEnabled && !graphics.FSREnabled)
{
Logger.LogInfo($"Supersampling factor: {graphics.SuperSamplingFactor}");
2022-09-18 23:54:23 +02:00
return graphics.SuperSamplingFactor;
}
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
}
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; }
}
Logger.LogInfo($"FSR factor: {FSRFactor}");
2022-09-18 23:54:23 +02:00
return FSRFactor;
2022-09-08 21:29:16 +02:00
}
2022-09-18 23:54:23 +02:00
Logger.LogInfo($"GetCurrentScalingFactor(): Something went wrong. Returning 1.0f");
2022-09-18 23:54:23 +02:00
return 1.0f;
2022-09-08 21:29:16 +02:00
}
2022-09-20 22:02:46 +02:00
}
}