-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainScript.cs
More file actions
160 lines (146 loc) · 6.28 KB
/
Copy pathMainScript.cs
File metadata and controls
160 lines (146 loc) · 6.28 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using static UnityLoader.Console;
namespace UnityLoader
{
public class MainScript : MonoBehaviour
{
Texture2D consoleBackground,
inputBackground;
int loaderIndex = 0;
Rect consoleRect,
menuRect;
Vector2 consoleScroll = Vector2.zero;
bool consoleOpen = false;
KeyCode toggleKey = KeyCode.F2,
enterKey = KeyCode.Return;
void Start()
{
consoleRect = new Rect(0, 0, Screen.width, Screen.height * .35f);
/*menuRect = new Rect(Screen.width - 300, Screen.height - 400, 300, 400);*/
consoleBackground = Utils.MakeTexture(1, 1, new NormalizedColor(25, 25, 25, 250));
inputBackground = Utils.MakeTexture(1, 1, new NormalizedColor(25, 25, 25, 200));
Debug.Log("Welcome to TweakLoader! Use <i>help</i> for a list of available commands.");
RegisterListener(new CoreListener());
RegisterListener(new DebugListener(), true);
}
void Update()
{
if (Input.GetKeyDown(toggleKey))
{
consoleOpen = !consoleOpen;
}
else
{
if (Event.current.isKey)
{
if (Event.current.keyCode == enterKey)
{
if (!string.IsNullOrEmpty(ConsoleInput) && consoleOpen) { ProcessCommands(); }
}
if (Event.current.type == EventType.KeyDown)
{
if (Event.current.keyCode == toggleKey && consoleOpen)
{
GUIUtility.keyboardControl = 0;
consoleOpen = false;
}
}
}
}
}
void OnGUI()
{
if (consoleOpen)
{
if (Event.current.keyCode == toggleKey) { GUIUtility.keyboardControl = 0; }
ApplySkins();
consoleRect = GUIUtility.AlignRectToDevice(consoleRect);
consoleRect = GUI.Window(0, consoleRect, OnWindow, "");
}
}
void OnWindow(int id)
{
consoleScroll = GUILayout.BeginScrollView(consoleScroll);
foreach (string message in Debug.GetLog())
{
GUILayout.Label(message);
GUILayout.Space(1f);
}
GUILayout.EndScrollView();
ConsoleInput = GUILayout.TextField(ConsoleInput);
}
void ProcessCommands()
{
try
{
Debug.Log(">" + ConsoleInput);
bool commandExists = false;
string[] cmd = ConsoleInput.Split(' '), args;
int argCount;
string truePrefix = string.Empty;
bool symbolPrefix;
foreach (ICommandListener listener in Listeners)
{
truePrefix = listener.GetPrefix().Trim();
if (truePrefix.StartsWith("@symbol:"))
{
if (truePrefix.Length > 8)
truePrefix = truePrefix.Substring(8, truePrefix.Length - 8);
else truePrefix = "";
symbolPrefix = true;
}
else { truePrefix = listener.GetPrefix(); symbolPrefix = false; }
argCount = (cmd.Length - 2) + Convert.ToInt32(symbolPrefix);
args = argCount > 0 ? new string[argCount] : new string[0];
if (argCount > 0)
for (int i = 2 - Convert.ToInt32(symbolPrefix); i < cmd.Length; i++) { args[i - (2 - Convert.ToInt32(symbolPrefix))] = cmd[i]; }
if (cmd[0].StartsWith(truePrefix))
{
if (listener.ProcessCommand(symbolPrefix ? cmd[0].Substring(truePrefix.Length, cmd[0].Length - truePrefix.Length) :
cmd[1], CommandArg.ConvertArray(args))) { commandExists = true; break; }
}
}
if (!commandExists) { Debug.Log("Command not recognized.", LogType.Warning); }
}
catch (Exception e)
{
Debug.Log("Exception caught: " + e.ToString() + " - " + e.Message, LogType.Severe);
}
ConsoleInput = "";
consoleScroll = new Vector2(0, float.MaxValue);
}
void ApplySkins()
{
GUI.skin.label.richText = true;
GUI.skin.window.onNormal.background = consoleBackground;
GUI.skin.window.onNormal.textColor = Color.white;
GUI.skin.window.onFocused.background = consoleBackground;
GUI.skin.window.onFocused.textColor = Color.white;
GUI.skin.window.onActive.background = consoleBackground;
GUI.skin.window.onActive.textColor = Color.white;
GUI.skin.window.onHover.background = consoleBackground;
GUI.skin.window.onHover.textColor = Color.white;
GUI.skin.window.focused.background = consoleBackground;
GUI.skin.window.focused.textColor = Color.white;
GUI.skin.window.hover.background = consoleBackground;
GUI.skin.window.hover.textColor = Color.white;
GUI.skin.window.active.background = consoleBackground;
GUI.skin.window.active.textColor = Color.white;
GUI.skin.window.normal.background = consoleBackground;
GUI.skin.window.normal.textColor = Color.white;
GUI.skin.textField.normal.background = inputBackground;
GUI.skin.textField.onNormal.background = inputBackground;
GUI.skin.textField.focused.background = inputBackground;
GUI.skin.textField.onFocused.background = inputBackground;
GUI.skin.textField.hover.background = inputBackground;
GUI.skin.textField.onHover.background = inputBackground;
GUI.skin.textField.active.background = inputBackground;
GUI.skin.textField.onActive.background = inputBackground;
}
}
}