-
Notifications
You must be signed in to change notification settings - Fork 1
/
RobTopStringContainer.cpp
176 lines (143 loc) · 6.12 KB
/
RobTopStringContainer.cpp
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
/**
* LevelAPI - Geometry Dash level cacher with search functionality and more.
Copyright (C) 2023 Sergei Baigerov
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <cstdint>
#include <stdexcept>
#include <iostream>
#include <variant>
#include "RobTopStringContainer.hpp"
#include "StringSplit.h"
RobTopStringContainer::RobTopStringContainer() {
}
RobTopStringContainer::RobTopStringContainer(std::string str) {
m_sOriginalString = str;
}
void RobTopStringContainer::setParserForVariable(int index, std::function<ParsableTypes (std::string, int)> func) {
m_mFunctionContainer[index] = func;
}
void RobTopStringContainer::setParserForVariable(std::vector<int> indexes, std::function<ParsableTypes (std::string, int)> func) {
int i = 0;
while(i < indexes.size()) {
setParserForVariable(indexes[i], func);
i++;
}
}
void RobTopStringContainer::setParserForVariable(int index, std::function<ParsableTypes(std::string inputString, int inputID, int customArgumentValue)> func, int carg) {
m_mFunctionContainer[index] = func;
m_mFuncCustomArgList[index] = carg;
}
void RobTopStringContainer::setParserForVariable(std::vector<int> indexes, std::function<ParsableTypes(std::string inputString, int inputID, int customArgumentValue)> func, int carg) {
int i = 0;
while(i < indexes.size()) {
setParserForVariable(indexes[i], func, carg);
i++;
}
}
// template implementations for basic robtop values
template<> int RobTopStringContainer::getVariable<int>(int id) {
return std::get<int>(m_mContainer[id]);
}
template<> std::string RobTopStringContainer::getVariable<std::string>(int id) {
return std::get<std::string>(m_mContainer[id]);
}
template<> float RobTopStringContainer::getVariable<float>(int id) {
return std::get<float>(m_mContainer[id]);
}
template<> bool RobTopStringContainer::getVariable<bool>(int id) {
return std::get<bool>(m_mContainer[id]);
}
template<> std::vector<int> RobTopStringContainer::getVariable<std::vector<int>>(int id) {
return std::get<std::vector<int>>(m_mContainer[id]);
}
// template implementations for all unsigned integers
template<> uint8_t RobTopStringContainer::getVariable<uint8_t>(int id) {
return static_cast<uint8_t>(getVariable<int>(id));
}
template<> uint16_t RobTopStringContainer::getVariable<uint16_t>(int id) {
return static_cast<uint16_t>(getVariable<int>(id));
}
template<> uint32_t RobTopStringContainer::getVariable<uint32_t>(int id) {
return static_cast<uint32_t>(getVariable<int>(id));
}
template <> uint64_t RobTopStringContainer::getVariable<uint64_t>(int id) {
return static_cast<uint64_t>(getVariable<int>(id));
}
// template implementations for all signed integers
template<> int8_t RobTopStringContainer::getVariable<int8_t>(int id) {
return static_cast<int8_t>(getVariable<uint8_t>(id));
}
template<> int16_t RobTopStringContainer::getVariable<int16_t>(int id) {
return static_cast<int16_t>(getVariable<uint16_t>(id));
}
template<> int64_t RobTopStringContainer::getVariable<int64_t>(int id) {
return static_cast<int64_t>(getVariable<uint64_t>(id));
}
// additonal template implementations
template<> double RobTopStringContainer::getVariable<double>(int id) {
return (double)(getVariable<float>(id));
}
void RobTopStringContainer::resetValues() {
m_mContainer = {};
}
std::string RobTopStringContainer::variantToString(ParsableTypes var) {
if (std::holds_alternative<std::string>(var)) {
return std::get<std::string>(var);
}
if (std::holds_alternative<int>(var)) {
return std::to_string(std::get<int>(var));
}
if (std::holds_alternative<float>(var)) {
return std::to_string(std::get<float>(var));
}
if (std::holds_alternative<bool>(var)) {
int v = static_cast<int>(std::get<bool>(var));
return std::to_string(v);
}
return "";
}
bool RobTopStringContainer::variableExists(int id) {
return m_mContainer.count(id);
}
void RobTopStringContainer::setString(std::string str) {
this->m_sOriginalString = str;
}
void RobTopStringContainer::parse() {
int currentKey = 0;
int i = 0;
auto splittedString = splitString(m_sOriginalString.c_str(), ':');
while(i < splittedString.size()) {
try {
currentKey = std::stoi(splittedString[i]);
} catch (std::invalid_argument &e) { return; }
i++;
if(m_mFunctionContainer.count(currentKey)) {
if(std::holds_alternative<std::function<ParsableTypes(std::string inputString, int inputID)>>(m_mFunctionContainer[currentKey])) {
auto func = std::get<std::function<ParsableTypes(std::string inputString, int inputID)>>(m_mFunctionContainer[currentKey]);
auto res = func(splittedString[i], currentKey);
m_mContainer[currentKey] = res;
}
if(std::holds_alternative<std::function<ParsableTypes(std::string inputString, int inputID, int carg)>>(m_mFunctionContainer[currentKey])) {
auto func = std::get<std::function<ParsableTypes(std::string inputString, int inputID, int carg)>>(m_mFunctionContainer[currentKey]);
if(m_mFuncCustomArgList.count(currentKey)) {
auto res = func(splittedString[i], currentKey, m_mFuncCustomArgList[currentKey]);
m_mContainer[currentKey] = res;
}
}
} else {
std::cout << "WARN : key " << currentKey << " is not implemented!" << std::endl;
m_mContainer[currentKey] = splittedString[i];
}
i++;
}
}