This repository has been archived by the owner on May 5, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
RuntimeInspector.cs
199 lines (178 loc) · 5.52 KB
/
RuntimeInspector.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
using RuntimeUnityEditor.Core;
using System.Collections.Generic;
using UnityEngine;
/* RuntimeUnityEditor plugin is used for the runtime inspector: https://github.com/ManlyMarco/RuntimeUnityEditor/blob/master/LICENSE
* GNU GENERAL PUBLIC LICENSE Version 3, 29 June 2007
* Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
*
* Modified by yasirkula on 21 February 2021:
* - Removed Gizmos and REPL-related classes
* - Removed AssetBundleManagerHelper.cs
* - Removed wireframe rendering button from Hierarchy
* - Removed custom skin from InterfaceMaker
* - Removed cursor functions from RuntimeUnityEditorCore
* - Changed UnityFeatureHelper.OpenLog's path
*/
namespace GreenHell_RuntimeInspectorExt
{
public class MainMenuExtended : MainMenu
{
protected override void Start()
{
base.Start();
RuntimeInspector.Initialize();
}
}
public class RuntimeInspector : MonoBehaviour
{
private sealed class Logger : ILoggerWrapper
{
public void Log( LogLevel logLevel, object log )
{
if( log != null )
ModAPI.Log.Write( string.Concat( logLevel, ": ", log.ToString() ) );
}
}
private static RuntimeInspector instance;
private RuntimeUnityEditorCore inspector;
private KeyCode[] toggleKey = new KeyCode[0];
private float toggleKeyHeldTime = 0f;
private bool toggleKeyTriggered = false;
private bool rmbHeld = false;
public static void Initialize()
{
if( !instance )
{
instance = new GameObject( "__RuntimeInspector__" ).AddComponent<RuntimeInspector>();
DontDestroyOnLoad( instance.gameObject );
}
}
private void Start()
{
inspector = new RuntimeUnityEditorCore( this, new Logger() )
{
EnableMouseInspect = false,
HierarchyUpdateInterval = 0.5f
};
toggleKey = GetConfigurableKey( "RuntimeInspector", "ToggleKey" );
}
private void OnGUI()
{
inspector.OnGUI();
}
private void Update()
{
// Don't toggle the runtime inspector while typing something to chat
if( toggleKey.Length > 0 && ( !InputsManager.Get() || !InputsManager.Get().m_TextInputActive ) )
{
// Check if configurable key is held
// First, make sure that all modifier keys are held
bool modifierKeysHeld = true;
for( int i = 0; i < toggleKey.Length - 1; i++ )
{
if( !Input.GetKey( toggleKey[i] ) )
{
modifierKeysHeld = false;
toggleKeyTriggered = false;
break;
}
}
if( modifierKeysHeld )
{
if( !toggleKeyTriggered && Input.GetKeyDown( toggleKey[toggleKey.Length - 1] ) )
{
toggleKeyTriggered = true;
toggleKeyHeldTime = 0f;
}
else if( toggleKeyTriggered && Input.GetKey( toggleKey[toggleKey.Length - 1] ) )
{
toggleKeyHeldTime += Time.unscaledDeltaTime;
if( toggleKeyHeldTime >= 0.5f )
{
toggleKeyTriggered = false;
// Toggle inspector's visibility
inspector.Show = !inspector.Show;
SetCursorVisibility( inspector.Show );
}
}
else
toggleKeyTriggered = false;
}
}
// Allow rotating the camera while RMB is held
if( inspector.Show && Player.Get() )
{
if( !rmbHeld && Input.GetMouseButtonDown( 1 ) )
{
rmbHeld = true;
Player.Get().UnblockRotation();
}
else if( rmbHeld && Input.GetMouseButtonUp( 1 ) )
{
rmbHeld = false;
Player.Get().BlockRotation();
}
}
inspector.Update();
}
private void SetCursorVisibility( bool isVisible )
{
CursorManager.Get().ShowCursor( isVisible, false );
Player player = Player.Get();
if( player )
{
if( isVisible )
{
player.BlockRotation();
player.BlockInspection();
}
else
{
if( rmbHeld )
rmbHeld = false;
else
player.UnblockRotation();
player.UnblockInspection();
}
}
}
// Returns configurable key's corresponding KeyCode(s) by parsing RuntimeConfiguration.xml
private KeyCode[] GetConfigurableKey( string modID, string keyID )
{
List<KeyCode> keys = new List<KeyCode>( 2 );
string configurationFile = Application.dataPath + "/../Mods/RuntimeConfiguration.xml";
if( System.IO.File.Exists( configurationFile ) )
{
string configuration = System.IO.File.ReadAllText( configurationFile );
string modTag = "<Mod ID=\"" + modID + "\"";
int modTagStart = configuration.IndexOf( modTag );
if( modTagStart >= 0 )
{
int nextModTagStart = configuration.IndexOf( "<Mod ID=", modTagStart + modTag.Length );
int modTagEnd = ( nextModTagStart > modTagStart ) ? nextModTagStart : configuration.Length;
string keyTag = "<Button ID=\"" + keyID + "\">";
int keyTagStart = configuration.IndexOf( keyTag, modTagStart + modTag.Length );
if( keyTagStart > modTagStart && keyTagStart < modTagEnd )
{
int keyTagEnd = configuration.IndexOf( "</Button>", keyTagStart + keyTag.Length );
if( keyTagEnd > keyTagStart && keyTagEnd < modTagEnd )
{
string[] keyRawSplit = configuration.Substring( keyTagStart + keyTag.Length, keyTagEnd - keyTagStart - keyTag.Length ).Split( '+' );
for( int i = 0; i < keyRawSplit.Length; i++ )
{
// Fix typos in common modifier keys
if( keyRawSplit[i] == "LeftCtrl" )
keyRawSplit[i] = "LeftControl";
else if( keyRawSplit[i] == "RightCtrl" )
keyRawSplit[i] = "RightControl";
if( keyRawSplit[i].Length > 0 && System.Enum.IsDefined( typeof( KeyCode ), keyRawSplit[i] ) )
keys.Add( (KeyCode) System.Enum.Parse( typeof( KeyCode ), keyRawSplit[i] ) );
}
}
}
}
}
return keys.ToArray();
}
}
}