From a999eb5303bc08e821448e59c08f55e7c7c737d3 Mon Sep 17 00:00:00 2001 From: PureDark Date: Fri, 20 Jul 2018 16:38:00 +0800 Subject: [PATCH] initial --- TransparentWall.sln | 25 ++++ TransparentWall/Plugin.cs | 173 +++++++++++++++++++++++++ TransparentWall/ReflectionUtil.cs | 41 ++++++ TransparentWall/TransparentWall.cs | 112 ++++++++++++++++ TransparentWall/TransparentWall.csproj | 69 ++++++++++ 5 files changed, 420 insertions(+) create mode 100644 TransparentWall.sln create mode 100644 TransparentWall/Plugin.cs create mode 100644 TransparentWall/ReflectionUtil.cs create mode 100644 TransparentWall/TransparentWall.cs create mode 100644 TransparentWall/TransparentWall.csproj diff --git a/TransparentWall.sln b/TransparentWall.sln new file mode 100644 index 0000000..3f1f2dc --- /dev/null +++ b/TransparentWall.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.27703.2026 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TransparentWall", "TransparentWall\TransparentWall.csproj", "{63CE724B-0772-42F6-863D-F3AE19B6E67C}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {63CE724B-0772-42F6-863D-F3AE19B6E67C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {63CE724B-0772-42F6-863D-F3AE19B6E67C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {63CE724B-0772-42F6-863D-F3AE19B6E67C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {63CE724B-0772-42F6-863D-F3AE19B6E67C}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F7E82560-8FF3-462F-B05F-5AEFA27006C8} + EndGlobalSection +EndGlobal diff --git a/TransparentWall/Plugin.cs b/TransparentWall/Plugin.cs new file mode 100644 index 0000000..44bf29b --- /dev/null +++ b/TransparentWall/Plugin.cs @@ -0,0 +1,173 @@ +using IllusionPlugin; +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using UnityEngine; +using UnityEngine.SceneManagement; + +namespace TransparentWall +{ + class Plugin : IEnhancedPlugin, IPlugin + { + public static string PluginName = "TransparentWall"; + public const string VersionNum = "0.1.0"; + + public string Name => PluginName; + public string Version => VersionNum; + public string[] Filter { get; } + + public const string KeyTranparentWall = "TransparentWall"; + public const string KeyHMD = "HMD"; + public const string KeyCameraPlus = "CameraPlus"; + public const string KeyMutiView = "MutiViewFirstPerson"; + public const string KeyDynamicCamera = "DynamicCamera"; + public const string KeyLIV = "LIVCamera"; + + public static bool IsTranparentWall + { + get + { + return ModPrefs.GetBool(Plugin.PluginName, KeyTranparentWall, false); + } + set + { + ModPrefs.SetBool(Plugin.PluginName, KeyTranparentWall, value); + } + } + + public static bool IsHMDOn + { + get + { + return ModPrefs.GetBool(Plugin.PluginName, KeyHMD, true); + } + set + { + ModPrefs.SetBool(Plugin.PluginName, KeyHMD, value); + } + } + + public static bool IsCameraPlusOn + { + get + { + return ModPrefs.GetBool(Plugin.PluginName, KeyCameraPlus, true); + } + set + { + ModPrefs.SetBool(Plugin.PluginName, KeyCameraPlus, value); + } + } + + public static bool IsMutiViewFirstPersonOn + { + get + { + return ModPrefs.GetBool(Plugin.PluginName, KeyMutiView, true); + } + set + { + ModPrefs.SetBool(Plugin.PluginName, KeyMutiView, value); + } + } + + public static bool IsDynamicCameraOn + { + get + { + return ModPrefs.GetBool(Plugin.PluginName, KeyDynamicCamera, true); + } + set + { + ModPrefs.SetBool(Plugin.PluginName, KeyDynamicCamera, value); + } + } + + public static bool IsLIVCameraOn + { + get + { + return ModPrefs.GetBool(Plugin.PluginName, KeyLIV, true); + } + set + { + ModPrefs.SetBool(Plugin.PluginName, KeyLIV, value); + } + } + + + public void OnApplicationStart() + { + CheckForUserDataFolder(); + SceneManager.activeSceneChanged += SceneManagerOnActiveSceneChanged; + } + + public void OnApplicationQuit() + { + SceneManager.activeSceneChanged -= SceneManagerOnActiveSceneChanged; + } + + private void SceneManagerOnActiveSceneChanged(Scene arg0, Scene scene) + { + if (scene.buildIndex == 5) + { + new GameObject("TransparentWall").AddComponent(); + } + } + + public void OnLateUpdate() + { + } + + public void OnLevelWasLoaded(int level) + { + } + + public void OnLevelWasInitialized(int level) + { + } + + public void OnUpdate() + { + } + + public void OnFixedUpdate() + { + } + + private void CheckForUserDataFolder() + { + string userDataPath = Environment.CurrentDirectory + "/UserData"; + if (!Directory.Exists(userDataPath)) + { + Directory.CreateDirectory(userDataPath); + } + if ("".Equals(ModPrefs.GetString(Plugin.PluginName, Plugin.KeyTranparentWall, ""))) + { + ModPrefs.SetBool(Plugin.PluginName, Plugin.KeyTranparentWall, true); + } + if ("".Equals(ModPrefs.GetString(Plugin.PluginName, Plugin.KeyHMD, ""))) + { + ModPrefs.SetBool(Plugin.PluginName, Plugin.KeyHMD, true); + } + if ("".Equals(ModPrefs.GetString(Plugin.PluginName, Plugin.KeyCameraPlus, ""))) + { + ModPrefs.SetBool(Plugin.PluginName, Plugin.KeyCameraPlus, true); + } + if ("".Equals(ModPrefs.GetString(Plugin.PluginName, Plugin.KeyMutiView, ""))) + { + ModPrefs.SetBool(Plugin.PluginName, Plugin.KeyMutiView, true); + } + if ("".Equals(ModPrefs.GetString(Plugin.PluginName, Plugin.KeyDynamicCamera, ""))) + { + ModPrefs.SetBool(Plugin.PluginName, Plugin.KeyDynamicCamera, true); + } + if ("".Equals(ModPrefs.GetString(Plugin.PluginName, Plugin.KeyLIV, ""))) + { + ModPrefs.SetBool(Plugin.PluginName, Plugin.KeyLIV, true); + } + } + } +} diff --git a/TransparentWall/ReflectionUtil.cs b/TransparentWall/ReflectionUtil.cs new file mode 100644 index 0000000..dccae0b --- /dev/null +++ b/TransparentWall/ReflectionUtil.cs @@ -0,0 +1,41 @@ +using System; +using System.Reflection; +using UnityEngine; + +namespace TransparentWall +{ + public static class ReflectionUtil + { + public static void SetPrivateField(object obj, string fieldName, object value) + { + obj.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).SetValue(obj, value); + } + + public static T GetPrivateField(object obj, string fieldName) + { + return (T)((object)obj.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic).GetValue(obj)); + } + + public static void SetPrivateProperty(object obj, string propertyName, object value) + { + obj.GetType().GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic).SetValue(obj, value, null); + } + + public static void InvokePrivateMethod(object obj, string methodName, object[] methodParams) + { + obj.GetType().GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic).Invoke(obj, methodParams); + } + + public static Component CopyComponent(Component original, Type originalType, Type overridingType, GameObject destination) + { + Component component = destination.AddComponent(overridingType); + FieldInfo[] fields = originalType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.GetField); + for (int i = 0; i < fields.Length; i++) + { + FieldInfo fieldInfo = fields[i]; + fieldInfo.SetValue(component, fieldInfo.GetValue(original)); + } + return component; + } + } +} diff --git a/TransparentWall/TransparentWall.cs b/TransparentWall/TransparentWall.cs new file mode 100644 index 0000000..54033a7 --- /dev/null +++ b/TransparentWall/TransparentWall.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using UnityEngine; + +namespace TransparentWall +{ + public class TransparentWall : MonoBehaviour + { + public static int WallLayer = 25; + + private BeatmapObjectSpawnController _beatmapObjectSpawnController; + + private void Start() + { + try + { + this._beatmapObjectSpawnController = Resources.FindObjectsOfTypeAll().First(); + + if (_beatmapObjectSpawnController != null) + { + _beatmapObjectSpawnController.obstacleDiStartMovementEvent += this.HandleObstacleDiStartMovementEvent; + } + + setupCams(); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message + "\n" + ex.StackTrace); + } + } + + private void OnDestroy() + { + if (_beatmapObjectSpawnController != null) + { + _beatmapObjectSpawnController.obstacleDiStartMovementEvent -= this.HandleObstacleDiStartMovementEvent; + } + } + private void setupCams() + { + if (Plugin.IsLIVCameraOn) + { + Camera[] cameras = FindObjectsOfType(); + foreach (Camera camera in cameras) + { + camera.cullingMask &= ~(1 << WallLayer); + } + } + StartCoroutine(setupCamerasCoroutine()); + } + + private IEnumerator setupCamerasCoroutine() + { + yield return new WaitForEndOfFrame(); + Camera mainCamera = FindObjectsOfType().FirstOrDefault(x => x.CompareTag("MainCamera")); + if (Plugin.IsHMDOn) + mainCamera.cullingMask &= ~(1 << WallLayer); + else + mainCamera.cullingMask |= (1 << WallLayer); + + foreach (var plugin in IllusionInjector.PluginManager.Plugins) + { + if (plugin.Name == "CameraPlus" || plugin.Name == "CameraPlusOrbitEdition" || plugin.Name == "DynamicCamera") + { + var _cameraPlus = ReflectionUtil.GetPrivateField(plugin, "_cameraPlus"); + if (_cameraPlus != null) + { + Camera cam = ReflectionUtil.GetPrivateField(_cameraPlus, "_cam"); + if (cam !=null) + { + if (((plugin.Name == "CameraPlus" || plugin.Name == "CameraPlusOrbitEdition") && Plugin.IsCameraPlusOn) || (plugin.Name == "DynamicCamera" && Plugin.IsDynamicCameraOn)) + cam.cullingMask &= ~(1 << WallLayer); + else + cam.cullingMask |= (1 << WallLayer); + } + Camera multi = ReflectionUtil.GetPrivateField(_cameraPlus, "multi"); + if (multi != null) + { + if (Plugin.IsMutiViewFirstPersonOn) + multi.cullingMask &= ~(1 << WallLayer); + else + multi.cullingMask |= (1 << WallLayer); + } + } + break; + } + } + } + + public virtual void HandleObstacleDiStartMovementEvent(BeatmapObjectSpawnController obstacleSpawnController, ObstacleController obstacleController) + { + try + { + if (Plugin.IsTranparentWall) + { + StretchableObstacle _stretchableObstacle = ReflectionUtil.GetPrivateField(obstacleController, "_stretchableObstacle"); + StretchableCube _stretchableCoreOutside = ReflectionUtil.GetPrivateField(_stretchableObstacle, "_stretchableCoreOutside"); + StretchableCube _stretchableCoreInside = ReflectionUtil.GetPrivateField(_stretchableObstacle, "_stretchableCoreInside"); + MeshRenderer _meshRenderer = ReflectionUtil.GetPrivateField(_stretchableCoreOutside, "_meshRenderer"); + MeshRenderer _meshRenderer2 = ReflectionUtil.GetPrivateField(_stretchableCoreInside, "_meshRenderer"); + _stretchableCoreOutside.gameObject.layer = WallLayer; + _stretchableCoreInside.gameObject.layer = WallLayer; + } + } + catch (Exception ex) + { + Console.WriteLine(ex.Message + "\n" + ex.StackTrace); + } + } + } +} diff --git a/TransparentWall/TransparentWall.csproj b/TransparentWall/TransparentWall.csproj new file mode 100644 index 0000000..a165b6d --- /dev/null +++ b/TransparentWall/TransparentWall.csproj @@ -0,0 +1,69 @@ + + + + + Debug + AnyCPU + {63CE724B-0772-42F6-863D-F3AE19B6E67C} + Library + Properties + TransparentWall + TransparentWall + v4.6 + 512 + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + false + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + false + + + + ..\..\..\SteamLibrary\SteamApps\common\Beat Saber\Beat Saber_Data\Managed\Assembly-CSharp.dll + + + ..\..\..\SteamLibrary\SteamApps\common\Beat Saber\Beat Saber_Data\Managed\IllusionInjector.dll + + + ..\..\..\SteamLibrary\SteamApps\common\Beat Saber\Beat Saber_Data\Managed\IllusionPlugin.dll + + + + + + + + + ..\..\..\SteamLibrary\SteamApps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.dll + + + ..\..\..\SteamLibrary\SteamApps\common\Beat Saber\Beat Saber_Data\Managed\UnityEngine.CoreModule.dll + + + + + + + + + + + copy ".\TransparentWall.dll" "F:\SteamLibrary\SteamApps\common\Beat Saber\Plugins" +Start steam://rungameid/620980 + + \ No newline at end of file