From 7254effb9ba214936ba5e450a8229781466ba7d2 Mon Sep 17 00:00:00 2001 From: Falki-git <72734856+Falki-git@users.noreply.github.com> Date: Sun, 3 Sep 2023 20:09:21 +0200 Subject: [PATCH] Lock game input when "Lock" is clicked --- src/UI/Widgets/TimeScaleWidget.cs | 80 +++++++++++++++++++++++++++++++ src/UnityExplorer.sln | 6 +-- 2 files changed, 83 insertions(+), 3 deletions(-) diff --git a/src/UI/Widgets/TimeScaleWidget.cs b/src/UI/Widgets/TimeScaleWidget.cs index 4854ed17..88db344a 100644 --- a/src/UI/Widgets/TimeScaleWidget.cs +++ b/src/UI/Widgets/TimeScaleWidget.cs @@ -68,6 +68,85 @@ void OnPauseButtonClicked() lockBtn.ButtonText.text = locked ? "Unlock" : "Lock"; } + // Enable or disable game input + public void LockInput() + { + // to enable game input => KSP.Game.GameManager.Instance.Game.Input.Enable(); + // to disable game input => KSP.Game.GameManager.Instance.Game.Input.Disable(); + // is game input enabled => KSP.Game.GameManager.Instance.Game.Input.m_Global.enabled + + // Get the currently executing assembly (your code). + Assembly currentAssembly = Assembly.GetExecutingAssembly(); + + // Find the "Assembly-CSharp" assembly among the loaded assemblies. + Assembly targetAssembly = AppDomain.CurrentDomain.GetAssemblies() + .FirstOrDefault(assembly => assembly.GetName().Name == "Assembly-CSharp"); + + Type gameManagerType = targetAssembly.GetType("KSP.Game.GameManager"); + + + if (gameManagerType != null) + { + // Get the "Instance" property from KSP.Game.GameManager + PropertyInfo instanceProperty = gameManagerType.GetProperty("Instance", BindingFlags.Public | BindingFlags.Static); + + if (instanceProperty != null) + { + // Get the value of the "Instance" property (an instance of KSP.Game.GameManager). + object gameManagerInstance = instanceProperty.GetValue(null, null); + + // Get the "Game" property from the GameManager instance. + PropertyInfo gameProperty = gameManagerType.GetProperty("Game"); + + if (gameProperty != null) + { + // Get the value of the "Game" property (an instance of whatever type "Game" is). + object gameInstance = gameProperty.GetValue(gameManagerInstance, null); + + // Get the "Input" property. + PropertyInfo inputProperty = gameInstance.GetType().GetProperty("Input"); + + if (inputProperty != null) + { + // Get the value of the "Input" property (an instance of whatever type "Input" is). + object inputInstance = inputProperty.GetValue(gameInstance, null); + + //PropertyInfo globalProperty = inputInstance.GetType().GetProperty("m_Global", BindingFlags.NonPublic | BindingFlags.Instance); + FieldInfo globalField = inputInstance.GetType().GetField("m_Global", BindingFlags.NonPublic | BindingFlags.Instance); + + if (globalField != null) + { + object globalInstance = globalField.GetValue(inputInstance); + + PropertyInfo enabledProperty = globalInstance.GetType().GetProperty("enabled"); + + if (enabledProperty != null) + { + bool enabled = (bool)enabledProperty.GetValue(globalInstance, null); + + // Get the Enable and Disable methods + MethodInfo enableMethod = inputInstance.GetType().GetMethod("Enable"); + MethodInfo disableMethod = inputInstance.GetType().GetMethod("Disable"); + + /// If it's needed to lock gameinput depending on current value => + //if (enabled) + // disableMethod?.Invoke(inputInstance, null); + //else + // enableMethod?.Invoke(inputInstance, null); + + if (locked) + disableMethod?.Invoke(inputInstance, null); + else + enableMethod?.Invoke(inputInstance, null); + + } + } + } + } + } + } + } + // UI Construction void ConstructUI(GameObject parent) @@ -85,6 +164,7 @@ void ConstructUI(GameObject parent) lockBtn = UIFactory.CreateButton(parent, "PauseButton", "Lock", new Color(0.2f, 0.2f, 0.2f)); UIFactory.SetLayoutElement(lockBtn.Component.gameObject, minHeight: 25, minWidth: 50); lockBtn.OnClick += OnPauseButtonClicked; + lockBtn.OnClick += LockInput; } // Only allow Time.timeScale to be set if the user hasn't "locked" it or if we are setting the value internally. diff --git a/src/UnityExplorer.sln b/src/UnityExplorer.sln index ef2e7e66..ab0e0442 100644 --- a/src/UnityExplorer.sln +++ b/src/UnityExplorer.sln @@ -7,8 +7,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnityExplorer", "UnityExplo EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution - Release_BIE_Cpp|Any CPU = Release_BIE_Cpp|Any CPU Release_BIE_CoreCLR|Any CPU = Release_BIE_CoreCLR|Any CPU + Release_BIE_Cpp|Any CPU = Release_BIE_Cpp|Any CPU Release_BIE5_Mono|Any CPU = Release_BIE5_Mono|Any CPU Release_BIE6_Mono|Any CPU = Release_BIE6_Mono|Any CPU Release_ML_Cpp_net472|Any CPU = Release_ML_Cpp_net472|Any CPU @@ -18,10 +18,10 @@ Global Release_STANDALONE_Mono|Any CPU = Release_STANDALONE_Mono|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution + {B21DBDE3-5D6F-4726-93AB-CC3CC68BAE7D}.Release_BIE_CoreCLR|Any CPU.ActiveCfg = BIE5_Mono|Any CPU + {B21DBDE3-5D6F-4726-93AB-CC3CC68BAE7D}.Release_BIE_CoreCLR|Any CPU.Build.0 = BIE5_Mono|Any CPU {B21DBDE3-5D6F-4726-93AB-CC3CC68BAE7D}.Release_BIE_Cpp|Any CPU.ActiveCfg = BIE_Cpp|Any CPU {B21DBDE3-5D6F-4726-93AB-CC3CC68BAE7D}.Release_BIE_Cpp|Any CPU.Build.0 = BIE_Cpp|Any CPU - {B21DBDE3-5D6F-4726-93AB-CC3CC68BAE7D}.Release_BIE_CoreCLR|Any CPU.ActiveCfg = BIE_Cpp_CoreCLR|Any CPU - {B21DBDE3-5D6F-4726-93AB-CC3CC68BAE7D}.Release_BIE_CoreCLR|Any CPU.Build.0 = BIE_Cpp_CoreCLR|Any CPU {B21DBDE3-5D6F-4726-93AB-CC3CC68BAE7D}.Release_BIE5_Mono|Any CPU.ActiveCfg = BIE5_Mono|Any CPU {B21DBDE3-5D6F-4726-93AB-CC3CC68BAE7D}.Release_BIE5_Mono|Any CPU.Build.0 = BIE5_Mono|Any CPU {B21DBDE3-5D6F-4726-93AB-CC3CC68BAE7D}.Release_BIE6_Mono|Any CPU.ActiveCfg = BIE6_Mono|Any CPU