forked from KaylinOwO/projectnacl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcvwrapper.cpp
More file actions
168 lines (140 loc) · 4.83 KB
/
cvwrapper.cpp
File metadata and controls
168 lines (140 loc) · 4.83 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
/*
* cvwrapper.cpp
*
* Created on: Dec 18, 2016
* Author: nullifiedcat
*/
#include "cvwrapper.h"
#include "sdk.h"
int CatVar::last_id{ 0 };
int rebased_count{ 0 };
int GetRebasedCatVarCount() {
return rebased_count;
}
// Prevent initialization errors.
std::vector<CatVar*>& registrationArray() {
static std::vector<CatVar*> vector;
return vector;
}
std::vector<CatCommand*>& commandRegistrationArray() {
static std::vector<CatCommand*> vector;
return vector;
}
std::vector<ConVar*>& RegisteredVarsList()
{
static std::vector<ConVar*> list{};
return list;
}
std::vector<ConCommand*>& RegisteredCommandsList()
{
static std::vector<ConCommand*> list{};
return list;
}
CatCommand::CatCommand(std::string _name, std::string _help, FnCommandCallback_t _callback)
: name(_name), help(_help), callback(_callback) {
commandRegistrationArray().push_back(this);
}
CatCommand::CatCommand(std::string _name, std::string _help, FnCommandCallbackVoid_t _callback)
: name(_name), help(_help), callback_void(_callback) {
commandRegistrationArray().push_back(this);
}
void CatCommand::Register() {
char* name_c = new char[256];
char* help_c = new char[256];
if (name.at(0) == '+' || name.at(0) == '-') {
strncpy(name_c, (name).c_str(), 255);
}
else {
strncpy(name_c, ("NaCl_" + name).c_str(), 255);
}
strncpy(help_c, help.c_str(), 255);
// if (callback) cmd = new ConCommand(name_c, callback, help_c);
// else if (callback_void) cmd = new ConCommand(name_c, callback_void, help_c);
// else throw std::logic_error("no callback in CatCommand");
gInts.cvar->RegisterConCommand((ConCommandBase*)cmd);
RegisteredCommandsList().push_back(cmd);
// name_c and help_c are not freed because ConCommandBase doesn't copy them
}
void RegisterCatCommands() {
while (!commandRegistrationArray().empty()) {
CatCommand* cmd = commandRegistrationArray().back();
cmd->Register();
commandRegistrationArray().pop_back();
}
}
CatVar::CatVar(CatVar_t _type, std::string _name, std::string _defaults, std::string _desc_short, std::string _desc_long)
: type(_type), name(_name), defaults(_defaults), desc_short(_desc_short), desc_long(_desc_long) {
registrationArray().push_back(this);
}
CatVar::CatVar(CatVar_t _type, std::string _name, std::string _defaults, std::string _desc_short, std::string _desc_long, float max_val)
: type(_type), name(_name), defaults(_defaults), desc_short(_desc_short), desc_long(_desc_long), restricted(true) {
max = max_val;
registrationArray().push_back(this);
}
CatVar::CatVar(CatVar_t _type, std::string _name, std::string _defaults, std::string _desc_short, std::string _desc_long, float min_val, float max_val)
: type(_type), name(_name), defaults(_defaults), desc_short(_desc_short), desc_long(_desc_long), restricted(true) {
min = min_val;
max = max_val;
registrationArray().push_back(this);
}
CatVar::CatVar(CatEnum& cat_enum, std::string _name, std::string _defaults, std::string _desc_short, std::string _desc_long)
: type(CV_ENUM), name(_name), defaults(_defaults), desc_short(_desc_short), desc_long(_desc_long), enum_type(&cat_enum), restricted(true) {
min = cat_enum.min_value;
max = cat_enum.max_value;
registrationArray().push_back(this);
}
void CatVar::OnRegister(CatVar::RegisterCallbackFn fn) {
if (registered) fn(this);
else callbacks.push_back(fn);
}
ConVar* CreateConVar(std::string name, std::string value, std::string help)
{
char* namec = new char[256];
char* valuec = new char[256];
char* helpc = new char[256];
strncpy(namec, name.c_str(), 255);
strncpy(valuec, value.c_str(), 255);
strncpy(helpc, help.c_str(), 255);
//gLog.Console("Creating ConVar: %s %s %s", namec, valuec, helpc);
ConVar* ret = new ConVar(const_cast<const char*>(namec), const_cast<const char*>(valuec), 0, const_cast<const char*>(helpc));
gInts.cvar->RegisterConCommand(ret);
RegisteredVarsList().push_back(ret);
return ret;
}
void CatVar::Register() {
CatVarList().push_back(this);
id = last_id++;
convar = CreateConVar("NaCl_" + name, defaults, desc_short);
convar_parent = convar->m_pParent;
current_base = defaults;
while (!callbacks.empty()) {
callbacks.back()(this);
callbacks.pop_back();
}
registered = true;
}
// Used during int to setup catvars
void RegisterCatVars() {
while (registrationArray().size()) {
CatVar* var = registrationArray().back();
var->Register();
registrationArray().pop_back();
}
}
// Use when creating a CatEnum for use in a CatVar
CatEnum::CatEnum(std::vector<std::string> values, int min) : value_names(values) {
min_value = min;
max_value = min + int(values.size()) - 1;
size = int(values.size());
}
// use to get a name from a CatEnum
std::string CatEnum::Name(int value) {
if (value >= min_value && value < max_value) {
return value_names.at(unsigned(value) - unsigned(min_value));
}
return "unknown";
}
std::vector<CatVar*>& CatVarList() {
static std::vector<CatVar*> object{};
return object;
}