-
Notifications
You must be signed in to change notification settings - Fork 11
/
console.h
155 lines (116 loc) · 3.8 KB
/
console.h
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
//---------------------------------------------------------------------------
#pragma once
#include "hal/connection.h"
#include "static_list.h"
#include "writer.h"
#include <stddef.h>
#if RUN_TESTS
#include <vector>
#endif
//---------------------------------------------------------------------------
struct ConsoleCommand {
const char *command;
const char *description;
void (*handler)(void *context, const char *line);
void *context;
};
//---------------------------------------------------------------------------
class ConsoleWriter final : public IWriter {
public:
virtual void Write(const char *data, size_t length);
static void WriteToActive(const char *data, size_t length) {
classData.active->Write(data, length);
}
static void VprintfToActive(const char *p, va_list args) {
classData.active->Vprintf(p, args);
}
static IWriter *GetActiveWriter() { return classData.active; }
static void Push(IWriter *writer);
static void Pop();
static void SetConnection(ConnectionId connectionId,
uint16_t connectionHandle);
static ConsoleWriter instance;
private:
struct ClassData {
IWriter *data[4];
size_t count;
IWriter *active;
};
static ClassData classData;
};
//---------------------------------------------------------------------------
class Console {
public:
Console();
int AllocateChannelId();
void HandleInput(const char *data, size_t length);
static void SendOk();
// Returns true if successful.
static bool RunCommand(const char *command, IWriter &writer);
static void RegisterCommand(const ConsoleCommand &command);
static void RegisterCommand(const char *command, const char *description,
void (*handler)(void *context, const char *line),
void *context);
static void HelloCommand(void *context, const char *line);
static void HelpCommand(void *context, const char *line);
static void Write(const char *data, size_t length) {
ConsoleWriter::WriteToActive(data, length);
}
static void WriteAsJson(const char *data, char *buffer);
static void WriteAsJson(const char *data);
static void WriteScriptEvent(const char *text);
template <typename T, typename... T2>
static void Printf(const char *format, T arg, T2... args) {
PrintfInternal(format, arg, args...);
}
template <size_t N> static void Printf(const char (&text)[N]) {
Write(text, N - 1);
}
static void Dump(const void *data, size_t length) {
ConsoleWriter::instance.GetActiveWriter()->Dump(data, length);
}
static void Flush();
#if RUN_TESTS
static std::vector<char> history;
#endif
static Console instance;
private:
struct Channel {
bool isTooLong;
bool usedChannelId;
int32_t id;
size_t bufferCount;
char buffer[256];
void Reset(uint32_t channelId, bool hasChannelId) {
id = channelId;
usedChannelId = hasChannelId;
isTooLong = false;
bufferCount = 0;
}
void AddByte(uint8_t c) {
if (bufferCount >= sizeof(buffer)) {
isTooLong = true;
return;
}
buffer[bufferCount++] = c;
}
};
class ChannelHistory {
public:
int AllocateId();
void Touch(int channelId);
private:
void AddHistoryEntry(int channelId);
StaticList<uint8_t, 16> history;
};
static const size_t CHANNEL_COUNT = 4;
StaticList<Channel *, CHANNEL_COUNT> freeBuffers;
StaticList<Channel *, CHANNEL_COUNT> activeBuffers;
ChannelHistory channelHistory;
Channel channels[CHANNEL_COUNT];
Channel *GetChannel(int channelId, bool hasChannelId);
void ProcessChannelCommand(Channel &channel);
static void PrintfInternal(const char *format, ...);
static const ConsoleCommand *GetCommand(const char *buffer);
};
//---------------------------------------------------------------------------