forked from DMagic1/KSP_DebugStifle
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugStifle.cs
More file actions
188 lines (143 loc) · 4.51 KB
/
Copy pathDebugStifle.cs
File metadata and controls
188 lines (143 loc) · 4.51 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
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
using KSP.UI.Screens.DebugToolbar;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
namespace DebugStifle
{
[KSPAddon(KSPAddon.Startup.Instantly, true)]
public class DebugStifle : MonoBehaviour
{
private static bool loaded;
private static bool processed;
private static DebugScreenConsole console;
private bool open;
private static bool enableInput = false;
readonly static string SettingsPath = Path.Combine(KSPUtil.ApplicationRootPath, "GameData/DebugStifle/PluginData/settings.cfg");
private void Log(string message) => Debug.Log("[Debug_Stifler] " + message);
internal static void TryReadValue<T>(ref T target, ConfigNode node, string name)
{
if (node.HasValue(name))
{
try
{
target = (T)TypeDescriptor.GetConverter(typeof(T)).ConvertFromString(node.GetValue(name));
}
catch
{
// just skip over it
}
}
// skip again
}
private static void SaveSettings()
{
ConfigNode settings = new ConfigNode("SETTINGS");
Dictionary<string, object> settingValues = new Dictionary<string, object>
{
{ "enableInput", enableInput },
};
foreach (KeyValuePair<string, object> kvp in settingValues) settings.AddValue(kvp.Key, kvp.Value);
ConfigNode root = new ConfigNode();
root.AddNode(settings);
root.Save(SettingsPath); // this makes a new file if settings.cfg didnt exist already
}
private static void LoadSettings()
{
ConfigNode root = ConfigNode.Load(SettingsPath);
if (root != null)
{
ConfigNode settings = root.GetNode("SETTINGS");
if (settings != null)
{
void Read<T>(ref T field, string name) => TryReadValue(ref field, settings, name);
Read(ref enableInput, "enableInput");
}
}
}
private void Awake()
{
if (loaded)
{
Destroy(gameObject);
return;
}
loaded = true;
DontDestroyOnLoad(gameObject);
LoadSettings();
}
private void Update()
{
if (processed)
return;
if (!open && Input.GetKeyDown(KeyCode.F12) && GameSettings.MODIFIER_KEY.GetKey(false))
{
open = true;
StartCoroutine(WaitForDebug());
}
}
private IEnumerator WaitForDebug()
{
int timer = 0;
while (GameObject.FindObjectOfType<DebugScreen>() == null && timer < 20)
{
timer++;
Log("Searching For Debug Panel...");
yield return new WaitForSeconds(1);
}
yield return new WaitForSeconds(0.5f);
AlterPrefab();
}
private void AlterPrefab()
{
Log("Altering Debug Panel...");
DebugScreen screen = GameObject.FindObjectOfType<DebugScreen>();
if (screen == null)
return;
console = screen.GetComponentInChildren<DebugScreenConsole>();
if (console == null)
return;
if (console.inputField != null)
{
console.inputField.interactable = enableInput;
}
Button toggle = GameObject.Instantiate<Button>(console.submitButton);
toggle.transform.SetParent(console.submitButton.transform.parent, false);
toggle.onClick.RemoveAllListeners();
toggle.onClick.AddListener(ToggleInput);
TextMeshProUGUI toggleText = toggle.GetComponentInChildren<TextMeshProUGUI>();
if (toggleText == null)
return;
toggleText.text = "Toggle Input";
console.submitButton.transform.SetAsLastSibling();
LayoutElement layout = toggle.GetComponent<LayoutElement>();
if (layout == null)
return;
layout.minWidth += 38;
layout.preferredWidth += 38;
processed = true;
Log("Debug Panel Altered");
Destroy(gameObject);
}
private static void ToggleInput()
{
if (console?.inputField == null) return;
if (console.inputField.interactable)
{
console.inputField.DeactivateInputField();
console.inputField.interactable = false;
enableInput = false;
}
else
{
console.inputField.interactable = true;
enableInput = true;
}
SaveSettings(); // we have to do this here because every thing else gets destroyed the first time
}
}
}