-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilities.cpp
More file actions
337 lines (293 loc) · 9.99 KB
/
Utilities.cpp
File metadata and controls
337 lines (293 loc) · 9.99 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#include "Utilities.h"
extern string g_pluginName ("NifSE");
extern IDebugLog g_Log ((g_pluginName+".log").c_str());
extern UInt32 g_pluginVersion (CURRENT_VERSION);
extern PluginHandle g_pluginHandle (kPluginHandle_Invalid);
extern OBSEScriptInterface* scrInterface (NULL);
extern OBSEArrayVarInterface* arrInterface (NULL);
extern OBSEMessagingInterface* msgInterface (NULL);
extern OBSESerializationInterface* serInterface (NULL);
extern OBSEStringVarInterface* strInterface (NULL);
// some utility functions because I got tired of writing the same stuff for all my debug code
void PrintAndLog(string func) {
Console_Print("");
Console_Print(("NifSE::"+func+" called!").c_str());
_MESSAGE(("\n\n"+func+" called!").c_str());
}
void PrintAndLog(string func, string msg) {
Console_Print("");
Console_Print(("NifSE::"+func+" - "+msg).c_str());
_MESSAGE((func+" - "+msg).c_str());
}
void dPrintAndLog(string msg) {
#ifdef _DEBUG
Console_Print(msg.c_str());
#endif
_MESSAGE(msg.c_str());
}
void dPrintAndLog(string func, string msg) {
#ifdef _DEBUG
Console_Print(("NifSE::"+func+" - "+msg).c_str());
#endif
_MESSAGE((func+" - "+msg).c_str());
}
// helper function for creating an OBSE StringMap from a std::map<std::string, OBSEElement>
OBSEArray* StringMapFromStdMap(const map<string, OBSEElement>& data, Script* callingScript) {
// create empty string map
OBSEArray* arr = arrInterface->CreateStringMap(NULL, NULL, 0, callingScript);
// add each key-value pair
for (std::map<std::string, OBSEElement>::const_iterator iter = data.begin(); iter != data.end(); ++iter) {
arrInterface->SetElement(arr, iter->first.c_str(), iter->second);
}
return arr;
}
// helper function for creating an OBSE Map from a std::map<double, OBSEElement>
OBSEArray* MapFromStdMap(const map<double, OBSEElement>& data, Script* callingScript) {
OBSEArray* arr = arrInterface->CreateMap(NULL, NULL, 0, callingScript);
for (std::map<double, OBSEElement>::const_iterator iter = data.begin(); iter != data.end(); ++iter) {
arrInterface->SetElement(arr, iter->first, iter->second);
}
return arr;
}
// helper function for creating OBSE Array from std::vector<OBSEElement>
OBSEArray* ArrayFromStdVector(const vector<OBSEElement>& data, Script* callingScript) {
OBSEArray* arr = arrInterface->CreateArray(&(data[0]), data.size(), callingScript);
return arr;
}
string BoolToString(bool b) {
return b?"TRUE":"FALSE";
}
bool StringToBool(string str) {
return str.compare("TRUE") == 0;
}
string UIntToString(UInt32 uint) {
std::stringstream buf;
buf << uint;
return buf.str();
}
UInt32 StringToUInt(string str) {
UInt32 uint;
std::stringstream buf (str);
buf >> uint;
return uint;
}
string SIntToString(SInt32 sint) {
std::stringstream buf;
buf << sint;
return buf.str();
}
SInt32 StringToSInt(string str) {
SInt32 sint;
std::stringstream buf (str);
buf >> sint;
return sint;
}
string FloatToString(float flt) {
std::stringstream buf;
buf << flt;
return buf.str();
}
float StringToFloat(string str) {
float flt = 0;
std::stringstream buf (str);
buf >> flt;
return flt;
}
string VectorToString(vector<float> vec) {
string str = "[";
for ( unsigned int i = 0; i < vec.size(); ++i )
str += FloatToString(vec[i])+"|";
return str;
}
string VectorToString(vector<Niflib::byte> vec) {
string str = "[";
for ( unsigned int i = 0; i < vec.size(); ++i )
str += UIntToString(vec[i])+"|";
return str;
}
string VectorToString(vector<unsigned int> vec) {
string str = "[";
for ( unsigned int i = 0; i < vec.size(); ++i )
str += UIntToString(vec[i])+"|";
return str;
}
string VectorToString(vector<string> vec) {
string str = "[";
for ( unsigned int i = 0; i < vec.size(); ++i )
str += vec[i]+"|";
return str;
}
string VectorToString(Niflib::Vector3 vec) {
return "["+FloatToString(vec.x)+"|"+FloatToString(vec.y)+"|"+FloatToString(vec.z)+"|";
}
string VectorToString(Niflib::TexCoord mat) {
return "["+FloatToString(mat.u)+"|"+FloatToString(mat.v)+"|";
}
string VectorToString(Niflib::Color3 color) {
return "["+FloatToString(color.r)+"|"+FloatToString(color.g)+"|"+FloatToString(color.b)+"|";
}
string VectorToString(Niflib::Color4 color) {
return "["+FloatToString(color.r)+"|"+FloatToString(color.g)+"|"+FloatToString(color.b)+"|"+FloatToString(color.a)+"|";
}
vector<string> split(const string& str, char div) {
vector<string> vec = vector<string>();
string::size_type posS = 0;
string::size_type posF;
while ( posS < str.size() ) {
posF = str.find(div, posS);
vec.push_back(str.substr(posS,posF-posS));
posS = posF + 1;
}
return vec;
}
vector<float> StringToVector(string str) {
vector<float> vec = vector<float>();
string::size_type posS = 1;
string::size_type posF = str.find("|");
while ( posS < str.size() ) {
posF = str.find("|", posS);
vec.push_back(StringToFloat(str.substr(posS,posF-posS)));
posS = posF + 1;
}
return vec;
}
vector<Niflib::byte> StringToVectorB(string str) {
vector<Niflib::byte> vec = vector<Niflib::byte>();
string::size_type posS = 1;
string::size_type posF = str.find("|");
while ( posS < str.size() ) {
posF = str.find("|", posS);
vec.push_back(StringToUInt(str.substr(posS,posF-posS)));
posS = posF + 1;
}
return vec;
}
vector<unsigned int> StringToVectorU(string str) {
vector<unsigned int> vec = vector<unsigned int>();
string::size_type posS = 1;
string::size_type posF = str.find("|");
while ( posS < str.size() ) {
posF = str.find("|", posS);
vec.push_back(StringToUInt(str.substr(posS,posF-posS)));
posS = posF + 1;
}
return vec;
}
vector<string> StringToVectorS(string str) {
return split(str.substr(1), '|');
}
Niflib::Vector3 StringToVector3(string str) {
Niflib::Vector3 vec;
string::size_type posS = 1;
string::size_type posF = str.find("|",1);
vec.x = StringToFloat(str.substr(posS,posF-posS));
posS = posF+1;
posF = str.find("|",posS);
vec.y = StringToFloat(str.substr(posS,posF-posS));
posS = posF+1;
posF = str.find("|",posS);
vec.z = StringToFloat(str.substr(posS,posF-posS));
return vec;
}
Niflib::TexCoord StringToVectorT(string str) {
Niflib::TexCoord vec;
string::size_type posS = 1;
string::size_type posF = str.find("|",1);
vec.u = StringToFloat(str.substr(posS,posF-posS));
posS = posF+1;
posF = str.find("|",posS);
vec.v = StringToFloat(str.substr(posS,posF-posS));
return vec;
}
Niflib::Color3 StringToVectorC3(string str) {
Niflib::Color3 color;
string::size_type posS = 1;
string::size_type posF = str.find("|",1);
color.r = StringToFloat(str.substr(posS,posF-posS));
posS = posF+1;
posF = str.find("|", posS);
color.g = StringToFloat(str.substr(posS,posF-posS));
posS = posF+1;
posF = str.find("|", posS);
color.b = StringToFloat(str.substr(posS,posF-posS));
return color;
}
Niflib::Color4 StringToVectorC4(string str) {
Niflib::Color4 color;
string::size_type posS = 1;
string::size_type posF = str.find("|",1);
color.r = StringToFloat(str.substr(posS,posF-posS));
posS = posF+1;
posF = str.find("|", posS);
color.g = StringToFloat(str.substr(posS,posF-posS));
posS = posF+1;
posF = str.find("|", posS);
color.b = StringToFloat(str.substr(posS,posF-posS));
posS = posF+1;
posF = str.find("|", posS);
color.a = StringToFloat(str.substr(posS,posF-posS));
return color;
}
string MatrixToString(vector< vector <float> > mat) {
string str = "";
for ( unsigned int i = 0; i < mat.size(); ++i ) {
str += "[";
for ( unsigned int j = 0; j < mat[i].size(); ++j )
str += FloatToString(mat[i][j])+"|";
}
return str;
}
string MatrixToString(Niflib::Matrix33 mat) {
float matArr[3][3];
mat.AsFloatArr(matArr);
return "["+FloatToString(matArr[0][0])+"|"+FloatToString(matArr[0][1])+"|"+FloatToString(matArr[0][2])+
"|["+FloatToString(matArr[1][0])+"|"+FloatToString(matArr[1][1])+"|"+FloatToString(matArr[1][2])+
"|["+FloatToString(matArr[2][0])+"|"+FloatToString(matArr[2][1])+"|"+FloatToString(matArr[2][2])+"|";
}
string MatrixToString(Niflib::Matrix44 mat) {
float matArr[4][4];
mat.AsFloatArr(matArr);
return "["+FloatToString(matArr[0][0])+"|"+FloatToString(matArr[0][1])+"|"+FloatToString(matArr[0][2])+"|"+FloatToString(matArr[0][3])+
"|["+FloatToString(matArr[1][0])+"|"+FloatToString(matArr[1][1])+"|"+FloatToString(matArr[1][2])+"|"+FloatToString(matArr[1][3])+
"|["+FloatToString(matArr[2][0])+"|"+FloatToString(matArr[2][1])+"|"+FloatToString(matArr[2][2])+"|"+FloatToString(matArr[2][3])+
"|["+FloatToString(matArr[3][0])+"|"+FloatToString(matArr[3][1])+"|"+FloatToString(matArr[3][2])+"|"+FloatToString(matArr[3][3])+"|";
}
vector< vector<float> > StringToMatrix(string str) {
vector< vector<float> > mat = vector< vector<float> >();
string::size_type posS = 1;
string::size_type posF = str.find("|");
mat.push_back(vector<float>());
unsigned int i = 0;
while ( posS < str.size() ) {
if ( str[posS] == '[' ) {
mat.push_back(vector<float>());
++posS;
++i;
}
posF = str.find("|", posS);
mat[i].push_back(StringToFloat(str.substr(posS,posF-posS)));
posS = posF + 1;
}
return mat;
}
string Color4ToString(Niflib::Color4 color) {
return "[" + FloatToString(color.r) + "|" + FloatToString(color.g) + "|" + FloatToString(color.b) + "|" + FloatToString(color.a) + "|";
}
Niflib::Color4 StringToColor4(string str) {
Niflib::Color4 color;
string::size_type posS = 1;
string::size_type posF = str.find("|",1);
color.r = StringToFloat(str.substr(posS,posF-posS));
posS = posF+1;
posF = str.find("|",posS);
color.g = StringToFloat(str.substr(posS,posF-posS));
posS = posF+1;
posF = str.find("|",posS);
color.b = StringToFloat(str.substr(posS,posF-posS));
posS = posF+1;
posF = str.find("|",posS);
color.a = StringToFloat(str.substr(posS,posF-posS));
return color;
}
extern list<string> BSAlist = list<string>();
extern list<string>::iterator BSAit = list<string>::iterator();