Skip to content

Commit 64245e1

Browse files
committed
Refactor coroutine management and improve cancellation handling in CVarConsole and CVarDisplay
1 parent 6aecb43 commit 64245e1

File tree

4 files changed

+70
-16
lines changed

4 files changed

+70
-16
lines changed

Assets/Kekser/UnityCVarConsole/CVarConsole.cs

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,11 @@ public IEnumerator ReadKeyCoroutine(Action<char> onKeyRead)
136136

137137
while (!_cts.IsCancellationRequested)
138138
{
139-
yield return null;
140-
141139
if (!AnyKeyDown)
140+
{
141+
yield return null;
142142
continue;
143+
}
143144

144145
foreach (KeyCode keyCode in Enum.GetValues(typeof(KeyCode)))
145146
{
@@ -156,10 +157,9 @@ public IEnumerator ReadKeyCoroutine(Action<char> onKeyRead)
156157
onKeyRead(InputString[0]);
157158
yield break;
158159
}
160+
161+
yield return null;
159162
}
160-
161-
Display.HideCursor();
162-
onKeyRead('\0');
163163
}
164164

165165
public IEnumerator ReadLineCoroutine(Action<string> onLineRead, bool useHistory = false)
@@ -174,10 +174,11 @@ public IEnumerator ReadLineCoroutine(Action<string> onLineRead, bool useHistory
174174

175175
while (!_cts.IsCancellationRequested)
176176
{
177-
yield return null; // Wait for next frame
178-
179177
if (!AnyKeyDown)
178+
{
179+
yield return null;
180180
continue;
181+
}
181182

182183
bool consumedInputString = false;
183184

@@ -223,7 +224,7 @@ public IEnumerator ReadLineCoroutine(Action<string> onLineRead, bool useHistory
223224
break;
224225
case KeyCode.UpArrow:
225226
if (!useHistory)
226-
continue;
227+
break;
227228
int oldIndex1 = historyIndex;
228229
historyIndex++;
229230
historyIndex = Mathf.Clamp(historyIndex, -1, _history.Count - 1);
@@ -238,7 +239,7 @@ public IEnumerator ReadLineCoroutine(Action<string> onLineRead, bool useHistory
238239
break;
239240
case KeyCode.DownArrow:
240241
if (!useHistory)
241-
continue;
242+
break;
242243
int oldIndex2 = historyIndex;
243244
historyIndex--;
244245
historyIndex = Mathf.Clamp(historyIndex, -1, _history.Count - 1);
@@ -297,10 +298,9 @@ public IEnumerator ReadLineCoroutine(Action<string> onLineRead, bool useHistory
297298
Display.Write(' ');
298299
Display.SetCursor(startPosition.x + cursorPosition, startPosition.y);
299300
oldInputLength = newInputLength;
301+
302+
yield return null; // Wait for next frame
300303
}
301-
302-
Display.HideCursor();
303-
onLineRead(null);
304304
}
305305

306306
private IEnumerator SplitMessage(string message)
@@ -486,7 +486,17 @@ private void Start()
486486
_cts = new CancellationTokenSource();
487487
//Application.logMessageReceived -= OnLogMessageReceived; // TODO: improve input handling and re-enable
488488
RenderIntro();
489-
StartCoroutine(UpdateRunner());
489+
CVarCoroutines.Run(UpdateRunner());
490+
}
491+
492+
private void OnDestroy()
493+
{
494+
_cts?.Cancel();
495+
}
496+
497+
private void OnApplicationQuit()
498+
{
499+
_cts?.Cancel();
490500
}
491501
}
492502
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Collections;
2+
using UnityEngine;
3+
4+
namespace Kekser.UnityCVarConsole
5+
{
6+
public class CVarCoroutines : MonoBehaviour
7+
{
8+
private static bool _initialized;
9+
private static CVarCoroutines _instance;
10+
11+
private static void Initialize()
12+
{
13+
if (_initialized)
14+
return;
15+
16+
_instance = new GameObject("CVarCoroutines").AddComponent<CVarCoroutines>();
17+
DontDestroyOnLoad(_instance.gameObject);
18+
19+
_initialized = true;
20+
}
21+
22+
public static Coroutine Run(IEnumerator coroutine)
23+
{
24+
Initialize();
25+
return _instance.StartCoroutine(coroutine);
26+
}
27+
28+
public static void Stop(Coroutine coroutine)
29+
{
30+
if (!_initialized)
31+
return;
32+
33+
_instance.StopCoroutine(coroutine);
34+
}
35+
}
36+
}

Assets/Kekser/UnityCVarConsole/CVarCoroutines.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Kekser/UnityCVarConsole/CVarDisplay.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,12 @@ private void Awake()
5353
private void Start()
5454
{
5555
_cts = new CancellationTokenSource();
56-
StartCoroutine(RefreshLoop());
56+
CVarCoroutines.Run(RefreshLoop());
57+
}
58+
59+
private void OnDestroy()
60+
{
61+
_cts?.Cancel();
5762
}
5863

5964
private void OnApplicationQuit()
@@ -65,8 +70,6 @@ private IEnumerator RefreshLoop()
6570
{
6671
while (!_cts.IsCancellationRequested)
6772
{
68-
yield return new WaitForSeconds(1f / _refreshRate);
69-
7073
RecalculateSize();
7174
if (_isDirty)
7275
RenderText();
@@ -82,6 +85,8 @@ private IEnumerator RefreshLoop()
8285
_fontRenderer.Render();
8386
_lastCursorVisible = _cursorVisible;
8487
_isDirty = false;
88+
89+
yield return new WaitForSeconds(1f / _refreshRate);
8590
}
8691
}
8792

0 commit comments

Comments
 (0)