diff --git a/mp/src/game/client/client_momentum.vpc b/mp/src/game/client/client_momentum.vpc index bbf0b69336..bd57cc15f4 100644 --- a/mp/src/game/client/client_momentum.vpc +++ b/mp/src/game/client/client_momentum.vpc @@ -180,6 +180,8 @@ $Project "Client (Momentum)" $File "$SRCDIR\game\shared\momentum\util\run_compare.h" $File "$SRCDIR\game\shared\momentum\util\run_stats.h" $File "$SRCDIR\game\shared\momentum\util\run_stats.cpp" + $File "$SRCDIR\game\shared\momentum\util\jsontokv.h" + $File "$SRCDIR\game\shared\momentum\util\jsontokv.cpp" } $Folder "Shader Editor" [$WIN32] diff --git a/mp/src/game/server/server_momentum.vpc b/mp/src/game/server/server_momentum.vpc index b13c2f17ee..63f8a5585e 100644 --- a/mp/src/game/server/server_momentum.vpc +++ b/mp/src/game/server/server_momentum.vpc @@ -52,7 +52,8 @@ $Project "Server (Momentum)" $File "$SRCDIR\game\shared\momentum\util\binary_reader.cpp" $File "$SRCDIR\game\shared\momentum\util\binary_reader.h" $File "$SRCDIR\game\shared\momentum\util\serialization.h" - + $File "$SRCDIR\game\shared\momentum\util\jsontokv.h" + $File "$SRCDIR\game\shared\momentum\util\jsontokv.cpp" } $Folder "Timer" diff --git a/mp/src/game/shared/momentum/util/jsontokv.cpp b/mp/src/game/shared/momentum/util/jsontokv.cpp new file mode 100644 index 0000000000..9a3827f4cf --- /dev/null +++ b/mp/src/game/shared/momentum/util/jsontokv.cpp @@ -0,0 +1,71 @@ +#include "cbase.h" + +#include "jsontokv.h" +KeyValues *CJsonToKeyValues::ConvertJsonToKeyValues(JsonNode *node) +{ + // This simply calls MapNode(node), which handles creating the new keyvalue and making sure node is not nullptr + return MapNode(node); +} + +void CJsonToKeyValues::MapNode(JsonNode *node, KeyValues *kv) +{ + if (!node || !kv) + return; + JsonValue value = node->value; + // What are we? + switch (value.getTag()) + { + case JSON_NUMBER: + kv->SetFloat(node->key, value.toNumber()); + break; + case JSON_STRING: + kv->SetString(node->key, value.toString()); + break; + case JSON_ARRAY: + case JSON_OBJECT: + for (auto i : value) + { + // If what we're going to parse is an object, then we need to add it as a subkey. + if (i->value.getTag() == JSON_OBJECT || i->value.getTag() == JSON_ARRAY) + { + KeyValues *pSub = MapNode(i); + if (pSub) + { + kv->AddSubKey(pSub); + } + } + else // Otherwise (string, numbers, booleans) we just add them as an entry of the current key + { + MapNode(i, kv); + } + } + break; + case JSON_TRUE: + kv->SetBool(node->key, true); + break; + case JSON_FALSE: + kv->SetBool(node->key, false); + break; + case JSON_NULL: + kv->SetString(node->key, nullptr); + break; + } +} + +KeyValues *CJsonToKeyValues::MapNode(JsonNode *node) +{ + // The parent KV holds the name of the first parent (Which, in case there is only 1 node/value, it will be the same + // as the only value it has) + if (!node) + return nullptr; + + // @Ruben: When node->key is null on the json, key is not nullptr, but 0xffeeffee. + // MOM_TODO: Is it always that adress? If not, when / how does it change? + + // Parent keyvalue. + KeyValues *pNodeValues = + new KeyValues((node->key == nullptr || POINTER_TO_INT(node->key) == 0xffeeffee) ? nullptr : node->key); + + MapNode(node, pNodeValues); + return pNodeValues; +} \ No newline at end of file diff --git a/mp/src/game/shared/momentum/util/jsontokv.h b/mp/src/game/shared/momentum/util/jsontokv.h new file mode 100644 index 0000000000..c6d0aaa912 --- /dev/null +++ b/mp/src/game/shared/momentum/util/jsontokv.h @@ -0,0 +1,25 @@ +#pragma once + +#include "cbase.h" + +#include "gason.h" +#include + +class CJsonToKeyValues +{ + public: + DECLARE_CLASS_NOBASE(CJsonToKeyValues) + + CJsonToKeyValues(){}; + ~CJsonToKeyValues(){}; + + // Given a node, converts it and all its childern to KeyValues. + // Caller has to make sure the returned value is not of nullptr + KeyValues *ConvertJsonToKeyValues(JsonNode *); + + private: + // Maps a node (and later its childern) to a keyvalue + KeyValues *MapNode(JsonNode *); + // Maps a node and its children to the given keyvalue (recursively) + void MapNode(JsonNode *, KeyValues *); +}; \ No newline at end of file