-
Notifications
You must be signed in to change notification settings - Fork 7
/
smbx38a_private.h
230 lines (205 loc) · 6.36 KB
/
smbx38a_private.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
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
/*
* PGE File Library - a library to process file formats, part of Moondust project
*
* Copyright (c) 2014-2025 Vitaly Novichkov <[email protected]>
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#pragma once
#ifndef SMBX38A_PRIVATE_H
#define SMBX38A_PRIVATE_H
#include <functional>
#include "smbx64.h"
#include "smbx64_macro.h"
#include "CSVReaderPGE.h"
#include "CSVUtils.h"
#include "pge_ff_units.h"
using namespace CSVReader;
extern const int SMBX38A_NpcGeneratorTypes[29];
extern const int SMBX38A_NpcGeneratorDirections[29];
//for Header readers.
//Use it if you want read file partially
//(you must create QTextStream in(&fstream); !!!)
#define SMBX38A_FileBegin() int file_format=0; /*File format number*/\
PGESTRING line /*Current Line data*/
#define SMBX38A_FileBeginN() /*int file_format=0;*/ /*File format number*/\
PGESTRING line /*Current Line data*/
#if !defined(_MSC_VER) || _MSC_VER > 1800
#define ReadSMBX38Level ReadSMBX38ALvlFile
#else
#define ReadSMBX38Level ReadSMBX38ALvlFile_OLD
#endif
//Jump to next line
#ifdef nextLine
#undef nextLine
#endif
#define nextLine() line = in.readLine()
// Common functions
static auto PGEUrlDecodeFunc = [](PGESTRING &data)
{
data = PGE_URLDEC(data);
};
static auto PGEBase64DecodeFunc = [](PGESTRING &data)
{
data = PGE_BASE64DEC(data);
};
static auto PGEBase64DecodeFuncA = [](PGESTRING &data)
{
data = PGE_BASE64DEC_A(data);
};
static auto PGELayerOrDefault = [](PGESTRING &data)
{
data = (IsEmpty(data) ? "Default" : PGE_URLDEC(data));
};
static auto PGEFilpBool = [](bool &value)
{
value = !value;
};
template<class T>
constexpr std::function<void(T &)> MakeMinFunc(T min)
{
return [ = ](T & value)
{
if(value < min)
value = min;
};
}
/*!
* \brief Converts floating point number value from the expression field.
* \param [__inout] expression Expression field. Clears if valid floating point number has been detected
* \param [__out] target Target value
*/
inline void SMBX38A_Exp2Float(PGESTRING &expression, float &target)
{
if(!SMBX64::IsFloat(expression))
target = 0.0f;
else
{
target = toFloat(expression);
expression.clear();
}
}
/*!
* \brief Converts floating point number value from the expression field.
* \param [__inout] expression Expression field. Clears if valid floating point number has been detected
* \param [__out] target Target value
*/
inline void SMBX38A_Exp2Double(PGESTRING &expression, double &target)
{
if(!SMBX64::IsFloat(expression))
target = 0.0;
else
{
target = toDouble(expression);
expression.clear();
}
}
/*!
* \brief Converts integer value from the expression field (with rounding possible floating point number value).
* \param [__inout] expression Expression field. Clears if valid floating point number has been detected
* \param [__out] target Target value
*/
template<typename T>
inline void SMBX38A_Exp2Int(PGESTRING &expression, T &target)
{
if(!SMBX64::IsFloat(expression))
target = 0;
else
{
target = static_cast<T>(round(toDouble(expression)));
expression.clear();
}
}
template<typename T>
inline void SMBX38A_Num2Exp(T source, PGESTRING &expression)
{
if(IsEmpty(expression))
expression = fromNum(source);
}
template<typename T>
inline void SMBX38A_Num2Exp_URLEN(T source, PGESTRING &expression)
{
if(IsEmpty(expression))
expression = fromNum(source);
else
expression = PGE_URLENC(expression);
}
template<typename T>
inline void SMBX38A_mapBGID_From(T &bgID)
{
if(bgID == 2)
bgID = 13;
else if((bgID >= 3) && (bgID <= 13))
bgID -= 1;
}
template<typename T>
inline T SMBX38A_mapBGID_To(T bgID)
{
if(bgID == 13)
bgID = 2;
else if((bgID >= 2) && (bgID <= 12))
bgID += 1;
return bgID;
}
template<typename NumT>
inline void SMBX38A_RestoreOrigTime(NumT &orig, const NumT &cur, PGE_FileLibrary::TimeUnit unit)
{
NumT value = PGE_FileLibrary::TimeUnitsCVT(orig,
PGE_FileLibrary::TimeUnit::FrameOneOf65sec,
unit);
if(value != cur)
orig = PGE_FileLibrary::TimeUnitsCVT(cur,
unit,
PGE_FileLibrary::TimeUnit::FrameOneOf65sec);
}
static inline void SMBX38A_CC_decode(int32_t &destKey, int64_t &destValue, const PGESTRING &key)
{
if(IsEmpty(key) || (key.size() < 4))
{
destKey = 0;
destValue = 0;
}
#ifdef PGE_FILES_QT
std::string skey = key.toStdString();
#else
const std::string &skey = key;
#endif
std::string k = skey.substr(0, 4);
destKey = std::strtol(k.c_str(), nullptr, 16);
std::string v = skey.substr(4);
if(!v.empty())
destValue = std::strtol(v.c_str(), nullptr, 16);
}
static inline PGESTRING SMBX38A_CC_encode(int32_t srcKey, int64_t srcValue)
{
char keyBuf[6] = {0};
char valueBuf[52] = {0};
int kb_l = std::snprintf(keyBuf, 5, "%04X", static_cast<unsigned int>(srcKey));
keyBuf[kb_l] = '\0';
int kv_l = std::snprintf(valueBuf, 51, "%X", static_cast<unsigned int>(srcValue));
valueBuf[kv_l] = '\0';
#ifdef PGE_FILES_QT
return QString::fromUtf8(keyBuf, 4) + QString::fromUtf8(valueBuf, kv_l);
#else
return PGESTRING(keyBuf, 4) + PGESTRING(valueBuf, kv_l);
#endif
}
#endif // SMBX38A_PRIVATE_H