-
Notifications
You must be signed in to change notification settings - Fork 28
/
Commands.h
193 lines (155 loc) · 3.65 KB
/
Commands.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
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
// Copyright 2019-2020 the donut authors. See AUTHORS.md
#pragma once
#include "Core/File.h"
#include "GameCommands.h"
#include <fmt/format.h>
#include <algorithm>
#include <cctype>
#include <charconv>
#include <iostream>
#include <locale>
#include <string>
#include <unordered_map>
#include <vector>
namespace Donut
{
enum class ParamType : uint32_t
{
String,
Int,
Int64,
Float,
};
struct Command
{
bool (*sig)(const std::string&);
std::string help;
std::vector<std::vector<ParamType>> types;
};
class Commands
{
public:
Commands() = delete;
~Commands() = delete;
static bool Run(const std::string& name, const std::string& params)
{
auto command = _namedCommands.find(name);
if (command == _namedCommands.end())
{
std::cout << "command " << name << "not found" << std::endl;
return false;
}
if (!command->second.sig(params))
{
std::cout << "error running command " << name << "(" << params << ")" << std::endl;
return false;
}
return true;
}
static bool StringToInt64(const std::string& s, int64_t& v)
{
return std::from_chars(s.data(), s.data() + s.size(), v).ec == std::errc();
}
static bool StringToInt(const std::string& s, int32_t& v)
{
return std::from_chars(s.data(), s.data() + s.size(), v).ec == std::errc();
}
static bool StringToFloat(const std::string& s, float& v)
{
char* end;
v = std::strtof(s.c_str(), &end);
return end != nullptr;
}
static bool SplitParams(const std::string& s, std::vector<std::string>& params, size_t maxParams)
{
auto length = s.size();
if (length == 0)
return true;
bool openQuote = false;
std::string param = "";
for (size_t i = 0; i < length; ++i)
{
auto c = s[i];
if (c == '"')
{
openQuote = !openQuote;
continue;
}
else if (c == ',' && !openQuote)
{
if (param.empty())
return false;
if (params.size() >= maxParams)
return false;
trim(param);
params.push_back(param);
param.clear();
continue;
}
param.push_back(c);
}
if (openQuote || param.empty())
return false;
if (params.size() >= maxParams)
return false;
trim(param);
params.push_back(param);
return true;
}
static bool RunLine(std::string line)
{
if (line.empty() || std::all_of(line.begin(), line.end(), isspace))
return false;
trim(line);
if (line[0] == '/')
return false;
std::size_t commentPos = line.find_first_of("//");
if (commentPos != std::string::npos)
{
line = line.substr(0, commentPos);
trim(line);
}
std::size_t end = line.rfind(");");
if (end == std::string::npos)
return false;
std::size_t start = line.find_first_of("(");
if (start == std::string::npos)
return false;
auto name = line.substr(0, start);
trim(name);
auto count = (end - 1) - start;
auto params = count > 0 ? line.substr(start + 1, count) : "";
if (count > 0)
trim(params);
return Run(name, params);
}
static bool RunScript(const std::string& filename)
{
if (!FileSystem::exists(filename))
{
std::cout << "Script not found: " << filename << "\n";
return false;
}
File file;
file.Open(filename, FileMode::Read);
while (file.Position() < file.Size()) { RunLine(file.ReadLine()); }
file.Close();
return true;
}
private:
static inline void ltrim(std::string& s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace(ch); }));
}
static inline void rtrim(std::string& s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return !std::isspace(ch); }).base(), s.end());
}
static inline void trim(std::string& s)
{
ltrim(s);
rtrim(s);
}
static std::unordered_map<std::string, Command> _namedCommands;
};
} // namespace Donut