From 804e4643f4c5a6e7b0cf4146411d94a2c2caf0d3 Mon Sep 17 00:00:00 2001 From: notGreg Date: Tue, 20 Sep 2022 22:02:46 +0200 Subject: [PATCH] 2.0.0-RC1 --- CameraResolutionScale/Plugin.cs | 305 ++++++++---------- .../UpdateScopeResolution.cs | 40 --- CameraResolutionScale/Utils.cs | 26 -- .../notGreg.ScopeTweaks.csproj | 2 - 4 files changed, 142 insertions(+), 231 deletions(-) delete mode 100644 CameraResolutionScale/UpdateScopeResolution.cs delete mode 100644 CameraResolutionScale/Utils.cs diff --git a/CameraResolutionScale/Plugin.cs b/CameraResolutionScale/Plugin.cs index 9191a83..a4ec868 100644 --- a/CameraResolutionScale/Plugin.cs +++ b/CameraResolutionScale/Plugin.cs @@ -12,9 +12,6 @@ namespace ScopeTweaks [BepInPlugin("com.notGreg.scopeTweaks", "notGreg's Scope Tweaks", "2.0.0")] public class Plugin : BaseUnityPlugin { - Utils utils = new Utils(); - CameraUtils scopeRes = new CameraUtils(); - ConfigEntry cameraResolutionScale; ConfigEntry scopeCameraResolutionScale; ConfigEntry scopeFixType; @@ -31,12 +28,12 @@ namespace ScopeTweaks "General", "Main camera scale %", 80, - new ConfigDescription("Main camera resolution scale", new AcceptableValueRange(50, 100))); + new ConfigDescription("Main camera resolution scale", new AcceptableValueRange(25, 100))); scopeCameraResolutionScale = Config.Bind( "General", "Scope camera scale %", - 40, + 80, new ConfigDescription("Scope camera resolution scale", new AcceptableValueRange(25, 100))); scopeFixType = Config.Bind("General", "High FOV scope tweak", EFOVScalingMode.Enabled, new ConfigDescription("")); @@ -47,203 +44,185 @@ namespace ScopeTweaks SSAA ssaaInstance = null; FieldInfo _nextSSRation = null; + WaitForSeconds myDelaySec = new WaitForSeconds(1); - int defaultFOVValue; - - void setUpCameras() + void Update() { - //Logger.LogInfo("Game started!"); + if (Singleton.Instance == null) return; + GameStatus currentGameState = Singleton.Instance.Status; - //Logger.LogInfo("Get FPS Camera"); - StartCoroutine(tryGetMainCamera()); + if (!gameStateChanged(currentGameState)) return; - //Logger.LogInfo("Get Scope Camera"); - StartCoroutine(tryGetScopeCamera()); + switch(currentGameState) + { + case GameStatus.Started: + { + //Logger.LogInfo("Getting local player"); + mainPlayer = getLocalPlayer(); + + subscribeHandsChangedEvent(); + + //Logger.LogInfo("Assigning cameras..."); + StartCoroutine(tryGetMainCamera()); + StartCoroutine(tryGetScopeCamera()); + + break; + } + case GameStatus.SoftStopping: + case GameStatus.Stopped: + { + //Logger.LogInfo("Resetting..."); + FPSCamera = null; + scopeCamera = null; + mainPlayer = null; + break; + } + default: break; + } } - WaitForSeconds myDelaySec = new WaitForSeconds(1); + /// + /// GAME STATUS + /// + + GameStatus lastGameState; + bool gameStateChanged(GameStatus currentState) + { + if (currentState == lastGameState) return false; + lastGameState = currentState; + return true; + } + + Player mainPlayer; + Player getLocalPlayer() + { + return Singleton.Instance.RegisteredPlayers.Find(p => p.IsYourPlayer); + } + + /// + /// FIELD OF VIEW + /// + + int inGameFOV; + int getInGameFOV() + { + int fov = Singleton.Instance.Game.Settings.FieldOfView.Value; + //Logger.LogInfo($"In-game FOV: {fov}"); + return fov; + } + + void setInGameFOV(int value) + { + //Logger.LogInfo($"Setting FOV to {value}"); + Singleton.Instance.Game.Settings.FieldOfView.Value = value; + } + + /// + /// CAMERA SETUP + /// + IEnumerator tryGetMainCamera() { - if (scopeRes.getMainCamera() == null) + string cameraName = "FPS Camera"; + if (GameObject.Find(cameraName) != null) { + FPSCamera = GameObject.Find(cameraName).GetComponent(); + //Logger.LogInfo($"{FPSCamera.name} found!"); + } + else + { + //Logger.LogMessage($"Camera \"{cameraName}\" not found, rescheduling..."); yield return myDelaySec; StartCoroutine(tryGetMainCamera()); yield break; } - FPSCamera = scopeRes.getMainCamera(); - defaultFOVValue = (int)FPSCamera.fieldOfView; + yield return null; + } - //Logger.LogInfo("Get SSAAInstance (Camera)"); - ssaaInstance = scopeRes.getSSAAInstance(FPSCamera); - - //Logger.LogInfo("Set SSRatios"); - _nextSSRation = scopeRes.getFieldInfo("_nextSSRation"); + FieldInfo getFieldInfo(string fieldName) + { + return typeof(SSAA).GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance); } IEnumerator tryGetScopeCamera() { - if (scopeRes.getScopeCamera() == null) + string cameraName = "BaseOpticCamera(Clone)"; + if (GameObject.Find(cameraName) != null) { - //Logger.LogInfo("ScopeCamera not found! Restarting..."); - yield return myDelaySec; - StartCoroutine(tryGetScopeCamera()); - yield break; + scopeCamera = GameObject.Find(cameraName).GetComponent(); + //Logger.LogInfo($"Camera \"{scopeCamera.name}\" found!"); } - - //Logger.LogInfo("Assigning scopeCamera"); - scopeCamera = scopeRes.getScopeCamera(); + yield break; } - void updateMainCameraResolution(float value) + void setMainCameraResolutionScale(int value = 100) { - //Logger.LogInfo($"Updating {Camera.main.name} SSRatio to {value / 100f}"); - _nextSSRation.SetValue(ssaaInstance, (float)(getCurrentScalingFactor() * value / 100f)); + //Logger.LogInfo($"Setting MainCam res scale to {value}%"); + ssaaInstance = FPSCamera.GetComponent(); + _nextSSRation = getFieldInfo("_nextSSRation"); + _nextSSRation.SetValue(ssaaInstance, (float)(currentScalingFactor * value / 100f)); } - void updateScopeCameraResolution(float value) + void setScopeCameraResolutionScale(int value) { - //Logger.LogInfo($"Updating {scopeCamera.name} to {value / 100f}"); - scopeCamera.GetComponent().OpticCameraToMainCameraResolutionRatio = (float)(value / 100f); + if(scopeCamera == null || !scopeCamera.isActiveAndEnabled) + { //Logger.LogInfo("ScopeCam inactive or absent!"); + return; } + + //Logger.LogInfo($"Setting Scope res scale to {value}%"); + scopeCamera.GetComponent().OpticCameraToMainCameraResolutionRatio = (float)(currentScalingFactor * value / 100); } - void updateInGameFOVValue(int value) + /// + /// PLAYER WEAPON EVENTS + /// + void subscribeHandsChangedEvent() { - //Logger.LogInfo($"Updating in-game FOV value to: {value}"); - Singleton.Instance.Game.Settings.FieldOfView.Value = value; - } - - void onAimingChanged() - { - //Logger.LogInfo($"isAiming: {mainPlayer.HandsController.IsAiming}"); - if (mainPlayer.HandsController.IsAiming) + //Logger.LogInfo("Subscribing to HandsChanged Event"); + mainPlayer.HandsChangedEvent += (handsArgs) => { - //Logger.LogInfo($"Updating MainCamRes to {cameraResolutionScale.Value}"); - updateMainCameraResolution(cameraResolutionScale.Value); - - switch (scopeFixType.Value) - { - case EFOVScalingMode.Disabled: - { break; } - case EFOVScalingMode.Enabled: - { - //Logger.LogInfo($"Scopes only! ScopeCamRes: {scopeCameraResolutionScale.Value}"); - if (scopeCamera == null || !scopeCamera.isActiveAndEnabled) break; - updateScopeCameraResolution(scopeCameraResolutionScale.Value); - updateInGameFOVValue(50); - break; - } - } - - return; - } - - if (!mainPlayer.HandsController.IsAiming) - { - //Logger.LogWarning($"Restoring default MainCamRes {defaultSSRatio * 100f}"); - updateMainCameraResolution(getCurrentScalingFactor() * 100f); - - switch (scopeFixType.Value) - { - case EFOVScalingMode.Disabled: { break; } - case EFOVScalingMode.Enabled: - { - //Logger.LogInfo($"Restoring FOV to {defaultFOVValue}"); - updateInGameFOVValue(defaultFOVValue); - GClass1774.Instance.SetFov(defaultFOVValue, 1f, true); - break; - } - } - return; - } - } - void subscribeToOnAimingChanged() - { - mainPlayer.HandsController.OnAimingChanged += (args) => - { - onAimingChanged(); + subscribeOnAimingChangedEvent(); }; } - GameStatus lastGameState = GameStatus.Stopped; - bool gameStateChanged(GameStatus currentState) + void subscribeOnAimingChangedEvent() { - if (lastGameState != currentState) + //Logger.LogInfo("Subscribing to OnAimingChanged Event"); + mainPlayer.HandsController.OnAimingChanged += (aimingArgs) => { - lastGameState = currentState; - //Logger.LogInfo($"GameState changed! {currentState}"); - return true; - } - //Logger.LogInfo("GameState unchanged."); - return false; - } - - IEnumerator tryApplyHideoutWorkaround() - { - if (Singleton.Instance != null) - { - Singleton.Instance.HandsChangedEvent += (argsHand) => + if (scopeFixType.Value != EFOVScalingMode.Enabled) return; + if(!mainPlayer.ProceduralWeaponAnimation.IsAiming) { - Singleton.Instance.HandsController.OnAimingChanged += (argsAim) => - { - onAimingChanged(); - }; - }; - } - else - { - yield return myDelaySec; - StartCoroutine(tryApplyHideoutWorkaround()); - } - } + GClass1774.Instance.SetFov(inGameFOV, 0.33f, false); + setInGameFOV(inGameFOV); + } + setMainCameraResolutionScale(); - Player mainPlayer = null; - bool doneOnce = false; - void Update() - { - if (Singleton.Instance == null) - { - doneOnce = false; - return; - } - GameStatus currentGameState = Singleton.Instance.Status; - - if (currentGameState == GameStatus.Started && mainPlayer != null && mainPlayer.GetType() == typeof(HideoutPlayer) && !doneOnce) - { - //Logger.LogInfo("Hideout workaround"); - doneOnce = true; - StartCoroutine(tryApplyHideoutWorkaround()); - } - - if (!gameStateChanged(currentGameState)) return; - - if ((currentGameState == GameStatus.SoftStopping || currentGameState == GameStatus.Stopped)) - { - //Logger.LogInfo("Game is stopping, resetting stuffs for the next raid"); - mainPlayer = null; - return; - } - - if (currentGameState == GameStatus.Started) - { - //Logger.LogInfo("Getting local player"); - mainPlayer = utils.getLocalPlayer(); - - //Logger.LogInfo("setUpCameras()"); - setUpCameras(); - - //Logger.LogInfo("Grab default FOV from settings singleton"); - defaultFOVValue = Singleton.Instance.Game.Settings.FieldOfView; - - mainPlayer.HandsChangedEvent += (args) => + currentScalingFactor = getCurrentScalingFactor(); + StartCoroutine(tryGetScopeCamera()); + if(mainPlayer.HandsController.IsAiming) { - //Logger.LogInfo($"Hands changed! {args.Item.Name} Subscribing again..."); - subscribeToOnAimingChanged(); - }; - } + if(inGameFOV != 50) { inGameFOV = getInGameFOV(); } + + GClass1774.Instance.SetFov(35, 0.25f, false); + setInGameFOV(50); + + setMainCameraResolutionScale(cameraResolutionScale.Value); + setScopeCameraResolutionScale(scopeCameraResolutionScale.Value); + return; + } + }; } + /// + /// IN-GAME SETTINGS + /// + + float currentScalingFactor; + float getCurrentScalingFactor() { + //Logger.LogInfo("Getting current scaling factor:"); var graphics = Singleton.Instance.Graphics.Settings; if (!graphics.DLSSEnabled && !graphics.FSREnabled) @@ -281,8 +260,8 @@ namespace ScopeTweaks return FSRFactor; } - Logger.LogInfo($"GetCurrentScalingFactor(): Something went wrong. Returning 1.0f"); + //Logger.LogInfo($"GetCurrentScalingFactor(): Something went wrong. Returning 1.0f"); return 1.0f; } - } -} + } +} \ No newline at end of file diff --git a/CameraResolutionScale/UpdateScopeResolution.cs b/CameraResolutionScale/UpdateScopeResolution.cs deleted file mode 100644 index cb6821e..0000000 --- a/CameraResolutionScale/UpdateScopeResolution.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System.Reflection; -using UnityEngine; - -namespace ScopeTweaks -{ - public class CameraUtils - { - public Camera getMainCamera() - { - Camera fpsCamera = GameObject.Find("FPS Camera").GetComponent(); - if (fpsCamera != null && fpsCamera.isActiveAndEnabled) return fpsCamera; - - return null; - } - - - public Camera getScopeCamera() - { - if (Camera.allCamerasCount <= 1) return null; - string scopeCamName = "BaseOpticCamera(Clone)"; - if (GameObject.Find(scopeCamName) == null) return null; - Camera scopeCamera = GameObject.Find("BaseOpticCamera(Clone)").GetComponent(); - if (scopeCamera != null && scopeCamera.isActiveAndEnabled) return scopeCamera; - - return null; - } - - - public SSAA getSSAAInstance(Camera camera) - { - return camera.GetComponent(); - } - - - public FieldInfo getFieldInfo(string fieldName) - { - return typeof(SSAA).GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance); - } - } -} diff --git a/CameraResolutionScale/Utils.cs b/CameraResolutionScale/Utils.cs deleted file mode 100644 index e97f6f7..0000000 --- a/CameraResolutionScale/Utils.cs +++ /dev/null @@ -1,26 +0,0 @@ -using Comfort.Common; -using EFT; - -namespace ScopeTweaks -{ - public class Utils - { - public bool gameIsReady() - { - var gameWorld = Singleton.Instance; - var sessionResultPanel = Singleton.Instance; - - if (gameWorld == null - || gameWorld.AllPlayers == null - || gameWorld.AllPlayers.Count <= 0 - || sessionResultPanel != null) - { return false; } - return true; - } - - public Player getLocalPlayer() - { - return Singleton.Instance.RegisteredPlayers.Find(p => p.IsYourPlayer); - } - } -} diff --git a/CameraResolutionScale/notGreg.ScopeTweaks.csproj b/CameraResolutionScale/notGreg.ScopeTweaks.csproj index d1ebaa5..0d0bbdf 100644 --- a/CameraResolutionScale/notGreg.ScopeTweaks.csproj +++ b/CameraResolutionScale/notGreg.ScopeTweaks.csproj @@ -70,8 +70,6 @@ - - \ No newline at end of file