267 lines
9.0 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;
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,
2022-09-18 23:54:23 +02:00
Enabled,
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-20 22:02:46 +02:00
new ConfigDescription("Main camera resolution scale", 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-17 19:49:55 +02:00
new ConfigDescription("Scope camera resolution scale", new AcceptableValueRange<int>(25, 100)));
2022-09-19 00:03:42 +02:00
scopeFixType = Config.Bind("General", "High FOV scope tweak", EFOVScalingMode.Enabled, new ConfigDescription(""));
2022-09-17 19:49:55 +02:00
}
2022-09-08 21:29:16 +02:00
2022-09-17 19:49:55 +02:00
Camera FPSCamera = null;
Camera scopeCamera = null;
2022-09-16 10:29:54 +02:00
2022-09-17 19:49:55 +02:00
SSAA ssaaInstance = null;
FieldInfo _nextSSRation = null;
2022-09-20 22:02:46 +02:00
WaitForSeconds myDelaySec = new WaitForSeconds(1);
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");
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
2022-09-20 22:02:46 +02:00
//Logger.LogInfo("Assigning cameras...");
StartCoroutine(tryGetMainCamera());
StartCoroutine(tryGetScopeCamera());
2022-09-20 22:02:46 +02:00
break;
}
case GameStatus.SoftStopping:
case GameStatus.Stopped:
{
//Logger.LogInfo("Resetting...");
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}");
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
{
2022-09-20 22:02:46 +02:00
//Logger.LogInfo($"Setting FOV 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>
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
{
2022-09-20 22:02:46 +02:00
//Logger.LogMessage($"Camera \"{cameraName}\" not found, rescheduling...");
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
{
2022-09-20 22:02:46 +02:00
//Logger.LogInfo($"Setting MainCam res scale to {value}%");
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!");
return; }
2022-09-16 13:24:26 +02:00
2022-09-20 22:02:46 +02:00
//Logger.LogInfo($"Setting Scope res scale to {value}%");
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");
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()
{
//Logger.LogInfo("Subscribing to OnAimingChanged Event");
mainPlayer.HandsController.OnAimingChanged += (aimingArgs) =>
2022-09-08 21:29:16 +02:00
{
2022-09-20 22:02:46 +02:00
if (scopeFixType.Value != EFOVScalingMode.Enabled) return;
if(!mainPlayer.ProceduralWeaponAnimation.IsAiming)
{
GClass1774.Instance.SetFov(inGameFOV, 0.33f, false);
setInGameFOV(inGameFOV);
}
setMainCameraResolutionScale();
2022-09-16 13:24:26 +02:00
2022-09-20 22:02:46 +02:00
currentScalingFactor = getCurrentScalingFactor();
StartCoroutine(tryGetScopeCamera());
if(mainPlayer.HandsController.IsAiming)
2022-09-18 23:54:23 +02:00
{
2022-09-20 22:02:46 +02:00
if(inGameFOV != 50) { inGameFOV = getInGameFOV(); }
GClass1774.Instance.SetFov(35, 0.25f, false);
setInGameFOV(50);
setMainCameraResolutionScale(cameraResolutionScale.Value);
setScopeCameraResolutionScale(scopeCameraResolutionScale.Value);
return;
}
};
2022-09-18 23:54:23 +02:00
}
2022-09-08 21:29:16 +02:00
2022-09-20 22:02:46 +02:00
/// <summary>
/// IN-GAME SETTINGS
/// </summary>
float currentScalingFactor;
2022-09-18 23:54:23 +02:00
float getCurrentScalingFactor()
{
2022-09-20 22:02:46 +02:00
//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}");
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}");
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}");
return FSRFactor;
2022-09-08 21:29:16 +02:00
}
2022-09-18 23:54:23 +02:00
2022-09-20 22:02:46 +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
}
}