2022-09-16 10:29:54 +02:00

127 lines
4.5 KiB
C#

using BepInEx;
using BepInEx.Configuration;
using EFT;
using UnityEngine;
using Comfort.Common;
using System.Reflection;
using System;
namespace CameraResolutionScale
{
[BepInPlugin("com.notGreg.cameraScaleControl", "notGreg's Camera Resolution Settings", "1.0.0")]
public class Plugin : BaseUnityPlugin
{
ConfigEntry<int> cameraResolutionScale;
ConfigEntry<int> scopeCameraResolutionScale;
public event EventHandler SettingChanged;
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)));
cameraResolutionScale.SettingChanged += (s, args) =>
{
updateSSRatio();
};
scopeCameraResolutionScale.SettingChanged += (s, args) =>
{
updateSSRatio();
};
}
void updateSSRatio()
{
if (!isAiming && FPSCamera != null)
{
defaultSSRatio = (float)_nextSSRation.GetValue(ssaaInstance);
Logger.LogInfo($"Updating defaultSSRatio to {defaultSSRatio}");
}
}
float defaultSSRatio = 1.0f;
Camera FPSCamera = null;
Camera scopeCamera = null;
bool isAiming = false;
bool doneOnce = false;
SSAA ssaaInstance = null;
FieldInfo _currentSSRatio = null;
FieldInfo _nextSSRation = null;
Utils utils = new Utils();
void Update()
{
if (!utils.gameIsReady())
{
//Logger.LogInfo("Resetting...");
doneOnce = false;
FPSCamera = null;
ssaaInstance = null;
scopeCamera = null;
return;
}
if (Camera.main == null) return;
//Logger.LogInfo("Check FPS camera");
if (GameObject.Find("FPS Camera") != null && FPSCamera == null)
{
//Logger.LogInfo("Assigning FPS Camera");
FPSCamera = GameObject.Find("FPS Camera").GetComponent<Camera>();
}
//Logger.LogInfo("Check Scope camera");
if (GameObject.Find("BaseOpticCamera(Clone)") != null && scopeCamera == null)
{
//Logger.LogInfo("Assigning Scope Camera");
scopeCamera = GameObject.Find("BaseOpticCamera(Clone)").GetComponent<Camera>();
}
var handsController = ((Singleton<GameWorld>.Instance.RegisteredPlayers[0].HandsController));
isAiming = handsController.IsAiming;
//Logger.LogInfo("Checking ssaaInstance");
if(ssaaInstance == null)
{
ssaaInstance = FPSCamera.GetComponent<SSAA>();
//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);
}
//Logger.LogInfo("Checking if isAiming");
if (isAiming && !doneOnce)
{
if (FPSCamera == null) return;
if (scopeCamera == null || scopeCamera.isActiveAndEnabled == false) return;
_nextSSRation.SetValue(ssaaInstance, (float)(defaultSSRatio * cameraResolutionScale.Value / 100f));
Logger.LogInfo($"Updated to: {_nextSSRation.GetValue(ssaaInstance)}");
scopeCamera.GetComponent<SSAAOptic>().OpticCameraToMainCameraResolutionRatio = (float)(scopeCameraResolutionScale.Value / 100f);
doneOnce = true;
return;
}
//Logger.LogInfo("Checking if not isAiming");
if (!isAiming && doneOnce)
{
Logger.LogInfo($"Restoring default scale: {defaultSSRatio}");
_nextSSRation.SetValue(ssaaInstance, defaultSSRatio);
doneOnce = false;
return;
}
}
}
}