add prefixed vars and fix format version parsing

This commit is contained in:
jacekpoz 2024-04-19 10:14:45 +02:00
parent 9b5e643c42
commit a8ae6b931e
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
3 changed files with 131 additions and 46 deletions

View file

@ -18,21 +18,27 @@
} name;
#define VAR_UINT(section, name) uint name;
#define VAR_UINT_P(section, name, prefix) VAR_UINT(section, name)
#define VAR_UINT_D(section, name, default) uint name = default;
#define VAR_INT(section, name) int name;
#define VAR_INT_P(section, name, prefix) VAR_INT(section, name)
#define VAR_INT_D(section, name, default) int name = default;
#define VAR_STRING(section, name) std::string name;
#define VAR_STRING_P(section, name, prefix) VAR_STRING(section, name)
#define VAR_STRING_D(section, name, default) std::string name = default;
#define VAR_BOOL(section, name) bool name;
#define VAR_BOOL_P(section, name, prefix) VAR_BOOL(section, name)
#define VAR_BOOL_D(section, name, default) bool name = default;
#define VAR_FLOAT(section, name) double name;
#define VAR_FLOAT_P(section, name, prefix) VAR_FLOAT(section, name)
#define VAR_FLOAT_D(section, name, default) double name = default;
#define VAR_COLOUR(section, name) sf::Color name;
#define VAR_COLOUR_P(section, name, prefix) VAR_COLOUR(section, name)
#define VAR_COLOUR_D(section, name, default) sf::Color name = default;
#define VAR_LIST(section, name, type) std::vector<type> name;
@ -46,16 +52,9 @@
ENUM_STREAM_OPS(name, __VA_ARGS__)
#define VAR_ENUM(section, name, enum_type) enum_type name;
#define VAR_ENUM_P(section, name, prefix) VAR_ENUM(section, name)
#define VAR_ENUM_D(section, name, enum_type, default) enum_type name = enum_type::default;
#define STRUCT(name, ...) \
struct name {\
__VA_ARGS__\
};
#define VAR_STRUCT(section, name, struct_type) struct_type name;
#define VAR_STRUCT_D(section, name, struct_type, default) struct_type name = default;
#define ENUM_TO_STRING_CASE(name, e) case name::e: os << #e; break;
#define ENUM_FROM_STRING(name, e) if (input == #e) en = name::e;
@ -74,6 +73,15 @@
return is;\
}
#define STRUCT(name, ...) \
struct name {\
__VA_ARGS__\
};
#define VAR_STRUCT(section, name, struct_type) struct_type name;
#define VAR_STRUCT_P(section, name, prefix) VAR_STRUCT(section, name)
#define VAR_STRUCT_D(section, name, struct_type, default) struct_type name = default;
#define STRUCT_FIELD_TO_STRING(name, field) os << #field << ": " << str.field << "\n";
#define STRUCT_FROM_STRING(name, field) \
std::getline(is, input, ',');\
@ -112,45 +120,54 @@
#undef UINT
#undef VAR_UINT
#undef VAR_UINT_P
#undef VAR_UINT_D
#undef INT
#undef VAR_INT
#undef VAR_INT_P
#undef VAR_INT_D
#undef STRING
#undef VAR_STRING
#undef VAR_STRING_P
#undef VAR_STRING_D
#undef BOOL
#undef VAR_BOOL
#undef VAR_BOOL_P
#undef VAR_BOOL_D
#undef FLOAT
#undef VAR_FLOAT
#undef VAR_FLOAT_P
#undef VAR_FLOAT_D
#undef COLOUR
#undef VAR_COLOUR
#undef VAR_COLOUR_P
#undef VAR_COLOUR_D
#undef ENUM
#undef VAR_ENUM
#undef VAR_ENUM_P
#undef VAR_ENUM_D
#undef ENUM_TO_STRING_CASE
#undef ENUM_FROM_STRING
#undef ENUM_STREAM_OPS
#undef ENUM
#undef VAR_ENUM
#undef VAR_ENUM_D
#undef STRUCT
#undef VAR_STRUCT
#undef VAR_STRUCT_P
#undef VAR_STRUCT_D
#undef STRUCT_FIELD_TO_STRING
#undef STRUCT_FROM_STRING
#undef STRUCT_STREAM_OPS
#undef STRUCT
#undef VAR_STRUCT
#undef VAR_STRUCT_D
#undef VAR_LIST
#undef VAR_LIST_SEP
#undef VAR_LIST_NUMBERED

View file

@ -5,33 +5,41 @@
#define SECTION(name, ...)
#define VAR_UINT(section, name)
#define VAR_UINT_P(section, name, prefix)
#define VAR_UINT_D(section, name, default)
#define VAR_INT(section, name)
#define VAR_INT_P(section, name, prefix)
#define VAR_INT_D(section, name, default)
#define VAR_STRING(section, name)
#define VAR_STRING_P(section, name, prefix)
#define VAR_STRING_D(section, name, default)
#define VAR_BOOL(section, name)
#define VAR_BOOL_P(section, name, prefix)
#define VAR_BOOL_D(section, name, default)
#define VAR_FLOAT(section, name)
#define VAR_FLOAT_P(section, name, prefix)
#define VAR_FLOAT_D(section, name, default)
#define VAR_COLOUR(section, name)
#define VAR_COLOUR_P(section, name, prefix)
#define VAR_COLOUR_D(section, name, default)
#define ENUM(name, ...)
#define VAR_ENUM(section, name, enum_type)
#define VAR_ENUM_P(section, name, prefix)
#define VAR_ENUM_D(section, name, enum_type, default)
#define STRUCT_STREAM_OPS(name, ...)
#define STRUCT(name, ...)
#define VAR_STRUCT(section, name, struct_type)
#define VAR_STRUCT_P(section, name, prefix)
#define VAR_STRUCT_D(section, name, struct_type, default)
#define STRUCT_STREAM_OPS(name, ...)
#define VAR_LIST(section, name, type)
#define VAR_LIST_SEP(section, name, type, sep)
#define VAR_LIST_NUMBERED(section, name, type)
@ -105,7 +113,7 @@ STRUCT_STREAM_OPS(HitObject, x, y, time, type, hitSound)
// https://osu.ppy.sh/wiki/en/Client/File_formats/osu_%28file_format%29
CONFIG(Difficulty,
VAR_UINT(, formatVersion)
VAR_UINT_P(, formatVersion, "osu file format v")
SECTION(general,
VAR_STRING(.general, audioFilename)
VAR_UINT_D(.general, audioLeadIn, 0)
@ -167,33 +175,41 @@ CONFIG(Difficulty,
#undef SECTION
#undef VAR_UINT
#undef VAR_UINT_P
#undef VAR_UINT_D
#undef VAR_INT
#undef VAR_INT_P
#undef VAR_INT_D
#undef VAR_STRING
#undef VAR_STRING_P
#undef VAR_STRING_D
#undef VAR_BOOL
#undef VAR_BOOL_P
#undef VAR_BOOL_D
#undef VAR_FLOAT
#undef VAR_FLOAT_P
#undef VAR_FLOAT_D
#undef VAR_COLOUR
#undef VAR_COLOUR_P
#undef VAR_COLOUR_D
#undef ENUM
#undef VAR_ENUM
#undef VAR_ENUM_P
#undef VAR_ENUM_D
#undef STRUCT_STREAM_OPS
#undef STRUCT
#undef VAR_STRUCT
#undef VAR_STRUCT_P
#undef VAR_STRUCT_D
#undef STRUCT_STREAM_OPS
#undef VAR_LIST
#undef VAR_LIST_SEP
#undef VAR_LIST_NUMBERED

View file

@ -48,7 +48,7 @@ inline void toLower(std::string &str) {
\
name ret;\
std::string currentSection;\
std::string namePascalCased;\
std::string _prefix;\
\
while (std::getline(configFile, line)) {\
/* remove \n*/\
@ -71,69 +71,107 @@ inline void toLower(std::string &str) {
__VA_ARGS__\
}
#define VAR_GENERIC(section, name, ...) \
namePascalCased = #name;\
namePascalCased[0] = std::toupper(namePascalCased[0]);\
namePascalCased.append(":");\
if (line.starts_with(namePascalCased)) {\
#define CHECK_VAR(section, name, ...) \
if (line.starts_with(_prefix)) {\
__VA_ARGS__\
ret section.name = value;\
continue;\
}
#define VAR_GENERIC_P(section, name, prefix, ...) \
_prefix = prefix;\
CHECK_VAR(section, name, __VA_ARGS__)
#define VAR_GENERIC(section, name, ...) \
_prefix = #name;\
_prefix[0] = std::toupper(_prefix[0]);\
_prefix.append(":");\
CHECK_VAR(section, name, __VA_ARGS__)
#define VAR_GENERIC_D(section, name, default, ...) \
ret section.name = default;\
VAR_GENERIC(section, name, __VA_ARGS__)
#define VAR_UINT(section, name) \
VAR_GENERIC(section, name,\
uint value = std::stoull(checkAndRemove(line, namePascalCased));\
uint value = std::stoull(checkAndRemove(line, _prefix));\
)
#define VAR_UINT_P(section, name, prefix) \
VAR_GENERIC_P(section, name, prefix,\
uint value = std::stoull(checkAndRemove(line, _prefix));\
)
#define VAR_UINT_D(section, name, default) \
VAR_GENERIC_D(section, name, default,\
uint value = std::stoull(checkAndRemove(line, namePascalCased));\
uint value = std::stoull(checkAndRemove(line, _prefix));\
)
#define VAR_INT(section, name) \
VAR_GENERIC(section, name,\
int value = std::stoll(checkAndRemove(line, namePascalCased));\
int value = std::stoll(checkAndRemove(line, _prefix));\
)
#define VAR_INT_P(section, name, prefix) \
VAR_GENERIC_P(section, name, prefix,\
int value = std::stoll(checkAndRemove(line, _prefix));\
)
#define VAR_INT_D(section, name, default) \
VAR_GENERIC_D(section, name, default,\
int value = std::stoll(checkAndRemove(line, namePascalCased));\
int value = std::stoll(checkAndRemove(line, _prefix));\
)
#define VAR_STRING(section, name) \
VAR_GENERIC(section, name,\
std::string value = checkAndRemove(line, namePascalCased);\
std::string value = checkAndRemove(line, _prefix);\
)
#define VAR_STRING_P(section, name, prefix) \
VAR_GENERIC_P(section, name, prefix,\
std::string value = checkAndRemove(line, _prefix);\
)
#define VAR_STRING_D(section, name, default) \
VAR_GENERIC_D(section, name, default,\
std::string value = checkAndRemove(line, namePascalCased);\
std::string value = checkAndRemove(line, _prefix);\
)
#define VAR_BOOL(section, name) \
VAR_GENERIC(section, name,\
bool value = std::stoi(checkAndRemove(line, namePascalCased));\
bool value = std::stoi(checkAndRemove(line, _prefix));\
)
#define VAR_BOOL_P(section, name, prefix) \
VAR_GENERIC_P(section, name, prefix,\
bool value = std::stoi(checkAndRemove(line, _prefix));\
)
#define VAR_BOOL_D(section, name, default) \
VAR_GENERIC_D(section, name, default,\
bool value = std::stoi(checkAndRemove(line, namePascalCased));\
bool value = std::stoi(checkAndRemove(line, _prefix));\
)
#define VAR_FLOAT(section, name) \
VAR_GENERIC(section, name,\
double value = std::stof(checkAndRemove(line, namePascalCased));\
double value = std::stof(checkAndRemove(line, _prefix));\
)
#define VAR_FLOAT_P(section, name, prefix) \
VAR_GENERIC_P(section, name, prefix,\
double value = std::stof(checkAndRemove(line, _prefix));\
)
#define VAR_FLOAT_D(section, name, default) \
VAR_GENERIC_D(section, name, default,\
double value = std::stof(checkAndRemove(line, namePascalCased));\
double value = std::stof(checkAndRemove(line, _prefix));\
)
#define VAR_COLOUR(section, name) \
VAR_GENERIC(section, name,\
std::string val = checkAndRemove(line, namePascalCased);\
std::string val = checkAndRemove(line, _prefix);\
std::array<sf::Uint8, 3> colours;\
std::stringstream ss(val);\
std::string colour;\
for (size_t i = 0; i < 3; ++i) {\
std::getline(ss, colour, ',');\
colours[i] = std::stoul(colour);\
}\
sf::Color value(colours[0], colours[1], colours[2]);\
)
#define VAR_COLOUR_P(section, name, prefix) \
VAR_GENERIC_P(section, name, prefix,\
std::string val = checkAndRemove(line, _prefix);\
std::array<sf::Uint8, 3> colours;\
std::stringstream ss(val);\
std::string colour;\
@ -145,7 +183,7 @@ inline void toLower(std::string &str) {
)
#define VAR_COLOUR_D(section, name, default) \
VAR_GENERIC_D(section, name, default,\
std::string val = checkAndRemove(line, namePascalCased);\
std::string val = checkAndRemove(line, _prefix);\
std::array<sf::Uint8, 3> colours;\
std::stringstream ss(val);\
std::string colour;\
@ -159,14 +197,21 @@ inline void toLower(std::string &str) {
#define ENUM(name, ...)
#define VAR_ENUM(section, name, enum_type) \
VAR_GENERIC(section, name,\
std::string val = checkAndRemove(line, namePascalCased);\
std::string val = checkAndRemove(line, _prefix);\
std::stringstream ss(val);\
enum_type value;\
ss >> value;\
)
#define VAR_ENUM_P(section, name, enum_type, prefix) \
VAR_GENERIC_P(section, name, prefix,\
std::string val = checkAndRemove(line, _prefix);\
std::stringstream ss(val);\
enum_type value;\
ss >> value;\
)
#define VAR_ENUM_D(section, name, enum_type, default) \
VAR_GENERIC_D(section, name, enum_type::default,\
std::string val = checkAndRemove(line, namePascalCased);\
std::string val = checkAndRemove(line, _prefix);\
std::stringstream ss(val);\
enum_type value;\
ss >> value;\
@ -177,14 +222,21 @@ inline void toLower(std::string &str) {
#define STRUCT(name, ...)
#define VAR_STRUCT(section, name, struct_type) \
VAR_GENERIC(section, name,\
std::string val = checkAndRemove(line, namePascalCased);\
std::string val = checkAndRemove(line, _prefix);\
std::stringstream ss(val);\
struct_type value;\
ss >> value;\
)
#define VAR_STRUCT_P(section, name, struct_type, prefix) \
VAR_GENERIC_P(section, name, prefix,\
std::string val = checkAndRemove(line, _prefix);\
std::stringstream ss(val);\
struct_type value;\
ss >> value;\
)
#define VAR_STRUCT_D(section, name, struct_type, default) \
VAR_GENERIC_D(section, name, default,\
std::string val = checkAndRemove(line, namePascalCased);\
std::string val = checkAndRemove(line, _prefix);\
std::stringstream ss(val);\
struct_type value;\
ss >> value;\
@ -192,17 +244,17 @@ inline void toLower(std::string &str) {
#define VAR_LIST(section, name, type) \
VAR_GENERIC(section, name,\
std::string val = checkAndRemove(line, namePascalCased);\
std::string val = checkAndRemove(line, _prefix);\
std::vector<type> value;\
)
#define VAR_LIST_SEP(section, name, type, sep) \
VAR_GENERIC(section, name,\
std::string val = checkAndRemove(line, namePascalCased);\
std::string val = checkAndRemove(line, _prefix);\
std::vector<type> value;\
)
#define VAR_LIST_NUMBERED(section, name, type) \
VAR_GENERIC(section, name,\
std::string val = checkAndRemove(line, namePascalCased);\
std::string val = checkAndRemove(line, _prefix);\
std::vector<type> value;\
)