Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/develop' into repla…
Browse files Browse the repository at this point in the history
…yfromleaderboards_wip
  • Loading branch information
Gocnak committed Jul 13, 2016
2 parents 11e9860 + c38900c commit 013fd7b
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 1 deletion.
2 changes: 2 additions & 0 deletions mp/src/game/client/client_momentum.vpc
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion mp/src/game/server/server_momentum.vpc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
71 changes: 71 additions & 0 deletions mp/src/game/shared/momentum/util/jsontokv.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
25 changes: 25 additions & 0 deletions mp/src/game/shared/momentum/util/jsontokv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include "cbase.h"

#include "gason.h"
#include <KeyValues.h>

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 *);
};

0 comments on commit 013fd7b

Please sign in to comment.