212 lines
6.8 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-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-17 20:08:36 +02:00
[BepInPlugin("com.notGreg.scopeTweaks", "notGreg's Scope Tweaks", "1.0.0")]
2022-09-08 21:29:16 +02:00
public class Plugin : BaseUnityPlugin
{
2022-09-17 19:49:55 +02:00
Utils utils = new Utils();
2022-09-17 20:08:36 +02:00
CameraUtils scopeRes = new CameraUtils();
2022-09-17 19:49:55 +02:00
2022-09-08 21:29:16 +02:00
ConfigEntry<int> cameraResolutionScale;
ConfigEntry<int> scopeCameraResolutionScale;
2022-09-17 20:08:36 +02:00
ConfigEntry<bool> fovCompDistScale;
2022-09-16 13:24:26 +02:00
enum fovScalingMode
2022-09-17 19:49:55 +02:00
{
2022-09-16 13:24:26 +02:00
Disabled,
Enabled
}
2022-09-17 19:49:55 +02:00
void Awake()
{
cameraResolutionScale = Config.Bind(
"General",
"Main camera scale %",
80,
new ConfigDescription("Main camera resolution scale", new AcceptableValueRange<int>(50, 100)));
scopeCameraResolutionScale = Config.Bind(
"General",
"Scope camera scale %",
40,
new ConfigDescription("Scope camera resolution scale", new AcceptableValueRange<int>(25, 100)));
fovCompDistScale = Config.Bind(
2022-09-17 20:08:36 +02:00
"Fixes",
"High FOV scope fix",
true,
new ConfigDescription("Makes scopes more useable at high FOV values"));
2022-09-17 19:49:55 +02:00
}
2022-09-08 21:29:16 +02:00
2022-09-16 13:24:26 +02:00
2022-09-17 19:49:55 +02:00
float defaultSSRatio = 1.0f;
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 _currentSSRatio = null;
FieldInfo _nextSSRation = null;
2022-09-16 10:29:54 +02:00
2022-09-17 19:49:55 +02:00
int defaultFOVValue = 50;
2022-09-16 13:24:26 +02:00
2022-09-17 19:49:55 +02:00
void setUpCameras()
{
2022-09-17 20:08:36 +02:00
//Logger.LogInfo("Game started!");
2022-09-16 13:24:26 +02:00
2022-09-17 20:08:36 +02:00
//Logger.LogInfo("Get FPS Camera");
2022-09-17 19:49:55 +02:00
StartCoroutine(tryGetMainCamera());
2022-09-16 13:24:26 +02:00
2022-09-17 20:08:36 +02:00
//Logger.LogInfo("Get Scope Camera");
2022-09-17 19:49:55 +02:00
StartCoroutine(tryGetScopeCamera());
2022-09-16 10:29:54 +02:00
}
2022-09-17 19:49:55 +02:00
WaitForSeconds myDelaySec = new WaitForSeconds(1);
IEnumerator tryGetMainCamera()
2022-09-16 10:29:54 +02:00
{
2022-09-17 19:49:55 +02:00
if (scopeRes.getMainCamera() == null)
2022-09-16 10:29:54 +02:00
{
2022-09-17 19:49:55 +02:00
yield return myDelaySec;
StartCoroutine(tryGetMainCamera());
yield break;
2022-09-16 10:29:54 +02:00
}
2022-09-17 19:49:55 +02:00
FPSCamera = scopeRes.getMainCamera();
2022-09-08 21:29:16 +02:00
2022-09-17 20:08:36 +02:00
//Logger.LogInfo("Get SSAAInstance (Camera)");
2022-09-17 19:49:55 +02:00
ssaaInstance = scopeRes.getSSAAInstance(FPSCamera);
2022-09-08 21:29:16 +02:00
2022-09-17 20:08:36 +02:00
//Logger.LogInfo("Set SSRatios");
2022-09-17 19:49:55 +02:00
_nextSSRation = scopeRes.getFieldInfo("_nextSSRation");
_currentSSRatio = scopeRes.getFieldInfo("_currentSSRatio");
2022-09-17 20:08:36 +02:00
//Logger.LogInfo($"Updating defaultSSRatio to {_nextSSRation.GetValue(ssaaInstance)}");
2022-09-17 19:49:55 +02:00
defaultSSRatio = (float)_nextSSRation.GetValue(ssaaInstance);
}
2022-09-17 19:49:55 +02:00
IEnumerator tryGetScopeCamera()
2022-09-08 21:29:16 +02:00
{
2022-09-17 19:49:55 +02:00
if (scopeRes.getScopeCamera() == null)
2022-09-08 21:29:16 +02:00
{
2022-09-17 20:08:36 +02:00
//Logger.LogInfo("ScopeCamera not found! Restarting...");
2022-09-17 19:49:55 +02:00
yield return myDelaySec;
StartCoroutine(tryGetScopeCamera());
yield break;
2022-09-08 21:29:16 +02:00
2022-09-16 10:29:54 +02:00
}
2022-09-17 20:08:36 +02:00
//Logger.LogInfo("Assigning scopeCamera");
2022-09-17 19:49:55 +02:00
scopeCamera = scopeRes.getScopeCamera();
}
2022-09-16 13:24:26 +02:00
2022-09-17 19:49:55 +02:00
void subscribeToOnAimingChanged()
{
2022-09-17 20:08:36 +02:00
//Logger.LogInfo($"subscribeToOnAimingChanged: player = {mainPlayer}");
2022-09-17 19:49:55 +02:00
mainPlayer.HandsController.OnAimingChanged += (args) =>
{
if (mainPlayer.HandsController.IsAiming)
{
2022-09-17 20:08:36 +02:00
//Logger.LogInfo("Updating camera resolutions");
2022-09-17 19:49:55 +02:00
_nextSSRation.SetValue(ssaaInstance, (float)(defaultSSRatio * cameraResolutionScale.Value / 100f));
if (scopeCamera != null && scopeCamera.isActiveAndEnabled)
{
scopeCamera.GetComponent<SSAAOptic>().OpticCameraToMainCameraResolutionRatio = (float)(scopeCameraResolutionScale.Value / 100f);
}
return;
}
if (!mainPlayer.HandsController.IsAiming)
{
2022-09-17 20:08:36 +02:00
//Logger.LogInfo("Restoring camera resolutions");
2022-09-17 19:49:55 +02:00
_nextSSRation.SetValue(ssaaInstance, (float)(defaultSSRatio));
return;
}
};
}
2022-09-16 13:24:26 +02:00
2022-09-17 19:49:55 +02:00
GameStatus lastGameState = GameStatus.Stopped;
bool gameStateChanged(GameStatus currentState)
{
if (lastGameState != currentState)
{
lastGameState = currentState;
2022-09-17 20:08:36 +02:00
//Logger.LogInfo($"GameState changed! {currentState}");
2022-09-17 19:49:55 +02:00
return true;
}
//Logger.LogInfo("GameState unchanged.");
return false;
}
2022-09-16 13:24:26 +02:00
2022-09-17 19:49:55 +02:00
Player mainPlayer = null;
2022-09-16 13:24:26 +02:00
2022-09-17 19:49:55 +02:00
void Update()
{
2022-09-16 13:24:26 +02:00
2022-09-17 19:49:55 +02:00
if (Singleton<AbstractGame>.Instance == null) return;
GameStatus currentGameState = Singleton<AbstractGame>.Instance.Status;
2022-09-16 13:24:26 +02:00
2022-09-17 19:49:55 +02:00
if (currentGameState == GameStatus.Started && mainPlayer != null)
{
2022-09-17 19:49:55 +02:00
if (mainPlayer.HandsController.IsAiming)
{
2022-09-17 20:08:36 +02:00
//Logger.LogInfo("Changing fovCompDist");
if (fovCompDistScale.Value) { mainPlayer.ProceduralWeaponAnimation._fovCompensatoryDistance = 0; }
2022-09-17 19:49:55 +02:00
}
}
2022-09-16 10:29:54 +02:00
2022-09-17 19:49:55 +02:00
if (!gameStateChanged(currentGameState)) return;
2022-09-17 19:49:55 +02:00
if ((currentGameState == GameStatus.SoftStopping || currentGameState == GameStatus.Stopped))
{
2022-09-17 20:08:36 +02:00
//Logger.LogInfo("Game is stopping, resetting stuffs for the next raid");
2022-09-17 19:49:55 +02:00
mainPlayer = null;
return;
}
2022-09-17 19:49:55 +02:00
if (currentGameState == GameStatus.Started)
2022-09-08 21:29:16 +02:00
{
2022-09-17 20:08:36 +02:00
//Logger.LogInfo("Getting local player");
2022-09-17 19:49:55 +02:00
mainPlayer = utils.getLocalPlayer();
2022-09-17 20:08:36 +02:00
//Logger.LogInfo($"Returned: {mainPlayer.GetType()}");
2022-09-17 20:08:36 +02:00
//Logger.LogInfo("Updating default FOV value");
2022-09-17 19:49:55 +02:00
if (defaultFOVValue != Camera.main.fieldOfView) { defaultFOVValue = (int)Camera.main.fieldOfView; }
2022-09-16 13:24:26 +02:00
2022-09-17 20:08:36 +02:00
//Logger.LogInfo("setUpCameras()");
2022-09-17 19:49:55 +02:00
setUpCameras();
2022-09-16 13:24:26 +02:00
2022-09-17 20:08:36 +02:00
//Logger.LogInfo("UpdateSSRatio()");
2022-09-17 19:49:55 +02:00
defaultSSRatio = (float)_nextSSRation.GetValue(ssaaInstance);
2022-09-17 20:08:36 +02:00
//Logger.LogInfo($"Updating defaultSSRatio to {defaultSSRatio}");
2022-09-17 19:49:55 +02:00
//updateSSRatio();
2022-09-16 13:24:26 +02:00
2022-09-17 20:08:36 +02:00
//Logger.LogInfo("subscibeToOnAimingChanged()");
2022-09-17 19:49:55 +02:00
subscribeToOnAimingChanged();
2022-09-08 21:29:16 +02:00
2022-09-11 18:48:53 +02:00
return;
2022-09-08 21:29:16 +02:00
}
}
2022-09-17 19:49:55 +02:00
//void updateSSRatio()
//{
// if (!isAiming && FPSCamera != null)
// {
// defaultSSRatio = (float)_nextSSRation.GetValue(ssaaInstance);
2022-09-17 20:08:36 +02:00
// //Logger.LogInfo($"Updating defaultSSRatio to {defaultSSRatio}");
2022-09-17 19:49:55 +02:00
// }
//}
2022-09-08 21:29:16 +02:00
}
}