2022-09-08 21:29:16 +02:00
using BepInEx ;
using BepInEx.Configuration ;
using EFT ;
using UnityEngine ;
using Comfort.Common ;
2022-09-15 21:14:26 +02:00
using System.Reflection ;
2022-09-16 10:29:54 +02:00
using System ;
2022-09-08 21:29:16 +02:00
2022-09-15 21:14:26 +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 10:29:54 +02:00
public event EventHandler SettingChanged ;
2022-09-08 21:29:16 +02:00
void Awake ( )
{
2022-09-15 21:14:26 +02:00
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 10:29:54 +02:00
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}" ) ;
}
2022-09-08 21:29:16 +02:00
}
2022-09-15 21:14:26 +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
2022-09-15 21:14:26 +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-15 21:14:26 +02:00
2022-09-08 21:29:16 +02:00
void Update ( )
{
2022-09-15 21:14:26 +02:00
if ( ! utils . gameIsReady ( ) )
2022-09-08 21:29:16 +02:00
{
2022-09-16 10:29:54 +02:00
//Logger.LogInfo("Resetting...");
2022-09-15 21:14:26 +02:00
doneOnce = false ;
2022-09-16 10:29:54 +02:00
FPSCamera = null ;
2022-09-15 21:14:26 +02:00
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");
2022-09-15 21:14:26 +02:00
if ( GameObject . Find ( "FPS Camera" ) ! = null & & FPSCamera = = null )
{
2022-09-16 10:29:54 +02:00
//Logger.LogInfo("Assigning FPS Camera");
2022-09-15 21:14:26 +02:00
FPSCamera = GameObject . Find ( "FPS Camera" ) . GetComponent < Camera > ( ) ;
}
2022-09-16 10:29:54 +02:00
//Logger.LogInfo("Check Scope camera");
2022-09-15 21:14:26 +02:00
if ( GameObject . Find ( "BaseOpticCamera(Clone)" ) ! = null & & scopeCamera = = null )
{
2022-09-16 10:29:54 +02:00
//Logger.LogInfo("Assigning Scope Camera");
2022-09-15 21:14:26 +02:00
scopeCamera = GameObject . Find ( "BaseOpticCamera(Clone)" ) . GetComponent < Camera > ( ) ;
}
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");
2022-09-15 21:14:26 +02:00
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
2022-09-15 21:14:26 +02:00
_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
{
2022-09-15 21:14:26 +02:00
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 ) ;
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");
2022-09-15 21:14:26 +02:00
if ( ! isAiming & & doneOnce )
2022-09-08 21:29:16 +02:00
{
2022-09-15 21:14:26 +02:00
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
}
}
}
}