174 lines
5.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-16 10:29:54 +02:00
using System;
2022-09-16 13:24:26 +02:00
using System.ComponentModel;
using EFT.UI;
2022-09-08 21:29:16 +02:00
namespace CameraResolutionScale
2022-09-08 21:29:16 +02:00
{
[BepInPlugin("com.notGreg.cameraScaleControl", "notGreg's Camera Resolution Settings", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
ConfigEntry<int> cameraResolutionScale;
ConfigEntry<int> scopeCameraResolutionScale;
2022-09-16 13:24:26 +02:00
ConfigEntry<fovScalingMode> disableFOVScaling;
GameObject rootObject;
string rootObjectPath = "Common UI/Common UI/SettingScreen";
GameObject fovObject;
string fovObjectPath = "Game Settings/Image/Other Settings/FOV/FOV";
int defaultFOVValue = 50;
SelectSlider fovSliderValue;
enum fovScalingMode
{
Disabled,
[Description("Only magnified optics")]
ScopeOnly,
Enabled
}
2022-09-08 21:29:16 +02:00
2022-09-16 10:29:54 +02:00
public event EventHandler SettingChanged;
2022-09-16 13:24:26 +02:00
2022-09-08 21:29:16 +02:00
void Awake()
{
cameraResolutionScale = Config.Bind("General", "Main camera scale %", 80, new ConfigDescription("Main camera resulution sclae", new AcceptableValueRange<int>(50, 100)));
scopeCameraResolutionScale = Config.Bind("General", "Scope camera scale %", 40, new ConfigDescription("Scope camera resulution scale", new AcceptableValueRange<int>(25, 100)));
2022-09-16 13:24:26 +02:00
disableFOVScaling = Config.Bind("Experimental", "Disable ADS FOV scaling", fovScalingMode.Disabled);
2022-09-16 10:29:54 +02:00
cameraResolutionScale.SettingChanged += (s, args) =>
{
updateSSRatio();
};
scopeCameraResolutionScale.SettingChanged += (s, args) =>
{
updateSSRatio();
2022-09-16 13:24:26 +02:00
};
Logger.LogInfo("Setting up game settings references");
rootObject = GameObject.Find(rootObjectPath);
fovObject = rootObject.transform.Find(fovObjectPath).gameObject;
fovSliderValue = fovObject.GetComponent<SelectSlider>();
defaultFOVValue = fovSliderValue.CurrentValue();
Logger.LogInfo($"Default camera FOV: {defaultFOVValue}");
2022-09-16 10:29:54 +02:00
}
void updateSSRatio()
{
if (!isAiming && FPSCamera != null)
{
defaultSSRatio = (float)_nextSSRation.GetValue(ssaaInstance);
Logger.LogInfo($"Updating defaultSSRatio to {defaultSSRatio}");
}
2022-09-08 21:29:16 +02:00
}
float defaultSSRatio = 1.0f;
Camera FPSCamera = null;
2022-09-08 21:29:16 +02:00
Camera scopeCamera = null;
bool isAiming = false;
2022-09-11 18:48:53 +02:00
bool doneOnce = false;
2022-09-08 21:29:16 +02:00
SSAA ssaaInstance = null;
FieldInfo _currentSSRatio = null;
FieldInfo _nextSSRation = null;
2022-09-08 21:29:16 +02:00
Utils utils = new Utils();
2022-09-08 21:29:16 +02:00
void Update()
{
if (!utils.gameIsReady())
2022-09-08 21:29:16 +02:00
{
2022-09-16 10:29:54 +02:00
//Logger.LogInfo("Resetting...");
doneOnce = false;
2022-09-16 10:29:54 +02:00
FPSCamera = null;
ssaaInstance = null;
2022-09-16 10:29:54 +02:00
scopeCamera = null;
2022-09-08 21:29:16 +02:00
return;
}
2022-09-16 10:29:54 +02:00
if (Camera.main == null) return;
2022-09-08 21:29:16 +02:00
2022-09-16 10:29:54 +02:00
//Logger.LogInfo("Check FPS camera");
if (GameObject.Find("FPS Camera") != null && FPSCamera == null)
{
2022-09-16 10:29:54 +02:00
//Logger.LogInfo("Assigning FPS Camera");
FPSCamera = GameObject.Find("FPS Camera").GetComponent<Camera>();
}
2022-09-16 10:29:54 +02:00
//Logger.LogInfo("Check Scope camera");
if (GameObject.Find("BaseOpticCamera(Clone)") != null && scopeCamera == null)
{
2022-09-16 10:29:54 +02:00
//Logger.LogInfo("Assigning Scope Camera");
scopeCamera = GameObject.Find("BaseOpticCamera(Clone)").GetComponent<Camera>();
}
2022-09-16 13:24:26 +02:00
2022-09-08 21:29:16 +02:00
var handsController = ((Singleton<GameWorld>.Instance.RegisteredPlayers[0].HandsController));
isAiming = handsController.IsAiming;
2022-09-16 10:29:54 +02:00
//Logger.LogInfo("Checking ssaaInstance");
if(ssaaInstance == null)
{
ssaaInstance = FPSCamera.GetComponent<SSAA>();
2022-09-16 10:29:54 +02:00
//figure out a way to update it if resolution or reso scale changes
_nextSSRation = typeof(SSAA).GetField("_nextSSRation", BindingFlags.NonPublic | BindingFlags.Instance);
_currentSSRatio = typeof(SSAA).GetField("_currentSSRatio", BindingFlags.NonPublic | BindingFlags.Instance);
Logger.LogInfo($"Updating defaultSSRatio to {_nextSSRation.GetValue(ssaaInstance)}");
defaultSSRatio = (float)_nextSSRation.GetValue(ssaaInstance);
}
2022-09-16 10:29:54 +02:00
//Logger.LogInfo("Checking if isAiming");
2022-09-11 18:48:53 +02:00
if (isAiming && !doneOnce)
2022-09-08 21:29:16 +02:00
{
if (FPSCamera == null) return;
if (scopeCamera == null || scopeCamera.isActiveAndEnabled == false) return;
2022-09-16 13:24:26 +02:00
Logger.LogInfo("Updating settings FOV to 50");
fovSliderValue.UpdateValue(50);
_nextSSRation.SetValue(ssaaInstance, (float)(defaultSSRatio * cameraResolutionScale.Value / 100f));
Logger.LogInfo($"Updated to: {_nextSSRation.GetValue(ssaaInstance)}");
scopeCamera.GetComponent<SSAAOptic>().OpticCameraToMainCameraResolutionRatio = (float)(scopeCameraResolutionScale.Value / 100f);
2022-09-11 18:48:53 +02:00
doneOnce = true;
2022-09-08 21:29:16 +02:00
return;
}
2022-09-16 10:29:54 +02:00
//Logger.LogInfo("Checking if not isAiming");
if (!isAiming && doneOnce)
2022-09-08 21:29:16 +02:00
{
2022-09-16 13:24:26 +02:00
Logger.LogInfo($"Restoring default hipfire FOV: {defaultFOVValue}");
fovSliderValue.UpdateValue(defaultFOVValue);
Logger.LogInfo($"Restoring default scale: {defaultSSRatio}");
_nextSSRation.SetValue(ssaaInstance, defaultSSRatio);
2022-09-11 18:48:53 +02:00
doneOnce = false;
return;
2022-09-08 21:29:16 +02:00
}
}
}
}