Skip to content

Commit

Permalink
Fix backwards compatibility for old resources
Browse files Browse the repository at this point in the history
  • Loading branch information
znvjder committed Jan 13, 2024
1 parent 2517c59 commit 4508f72
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 20 deletions.
6 changes: 4 additions & 2 deletions Client/mods/deathmatch/logic/CRemoteCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,10 @@ void CRemoteCall::DownloadFinishedCallback(const SHttpDownloadResult& result)
arguments.PushNumber(0);
}
else
// @todo: test
arguments.ReadJSONString(result.pData);
{
if (arguments.ReadJSONString(result.pData))
arguments.PushArguments(arguments);
}
}
else
{
Expand Down
17 changes: 16 additions & 1 deletion Client/mods/deathmatch/logic/lua/CLuaArguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,23 @@ bool CLuaArguments::WriteToBitStream(NetBitStreamInterface& bitStream, CFastHash
return bSuccess;
}

bool CLuaArguments::ReadJSONString(const char* szJSON)
bool CLuaArguments::ReadJSONString(const char* szJSON, bool bBackwardsCompatibility)
{
if (bBackwardsCompatibility)
{
// Backwards compatibility with old resources using function get()
// Fast isJSON check: Check first non-white space character is '[' or '{'
for (const char* ptr = szJSON; true;)
{
char c = *ptr++;
if (c == '[' || c == '{')
break;
if (isspace((uchar)c))
continue;
return false;
}
}

rapidjson::Document doc;
rapidjson::ParseResult result = doc.Parse(szJSON);

Expand Down
6 changes: 3 additions & 3 deletions Client/mods/deathmatch/logic/lua/CLuaArguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ class CLuaArguments
std::vector<CLuaArgument*>::const_iterator IterEnd() const { return m_Arguments.end(); };

// json parse
bool ReadJSONString(const char* szJSON);
bool ReadJSONArray(const rapidjson::Value& obj, std::vector<CLuaArguments*>* pKnownTables = NULL);
bool ReadJSONObject(const rapidjson::Value& obj, std::vector<CLuaArguments*>* pKnownTables = NULL);
bool ReadJSONString(const char* szJSON, bool bBackwardsCompatibility = false);
bool ReadJSONArray(const rapidjson::Value& obj, std::vector<CLuaArguments*>* pKnownTables = NULL);
bool ReadJSONObject(const rapidjson::Value& obj, std::vector<CLuaArguments*>* pKnownTables = NULL);

// json writer
bool SerializeToJSONString(rapidjson::StringBuffer* buffer, bool bSerialize = false, int flags = 1, bool bBackwardsCompatibility = false);
Expand Down
4 changes: 3 additions & 1 deletion Server/mods/deathmatch/logic/CElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,10 @@ void CElement::ReadCustomData(CEvents* pEvents, CXMLNode& Node)

// Make a lua argument from it and set the content
CLuaArguments args;
if (!args.ReadJSONString(pAttribute->GetValue().c_str()))
if (!args.ReadJSONString(pAttribute->GetValue().c_str(), true))
args.PushString(pAttribute->GetValue().c_str());
else
args.PushArguments(args);

// Don't trigger onElementDataChanged event
ESyncType syncType = g_pGame->GetConfig()->GetSyncMapElementData() ? ESyncType::BROADCAST : ESyncType::LOCAL;
Expand Down
9 changes: 5 additions & 4 deletions Server/mods/deathmatch/logic/CRemoteCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,14 @@ void CRemoteCall::DownloadFinishedCallback(const SHttpDownloadResult& result)
if (result.bSuccess)
{
if (pCall->IsFetch())
{
arguments.PushString(std::string(result.pData, result.dataSize));
arguments.PushNumber(0);
}
else
{
// @todo: test
arguments.ReadJSONString(result.pData);

arguments.PushNumber(0);
if (arguments.ReadJSONString(result.pData))
arguments.PushArguments(arguments);
}
}
else
Expand Down
4 changes: 3 additions & 1 deletion Server/mods/deathmatch/logic/CResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2565,7 +2565,9 @@ ResponseCode CResource::HandleRequestCall(HttpRequest* ipoHttpRequest, HttpRespo
else if (ipoHttpRequest->nRequestMethod == REQUESTMETHOD_POST)
{
const char* szRequestBody = ipoHttpRequest->sBody.c_str();
Arguments.ReadJSONString(szRequestBody);
if (Arguments.ReadJSONString(szRequestBody))
Arguments.PushArguments(Arguments);

}

CLuaArguments FormData;
Expand Down
18 changes: 16 additions & 2 deletions Server/mods/deathmatch/logic/lua/CLuaArguments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,10 +569,24 @@ bool CLuaArguments::WriteToBitStream(NetBitStreamInterface& bitStream, CFastHash
return bSuccess;
}

bool CLuaArguments::ReadJSONString(const char* szJSON)
bool CLuaArguments::ReadJSONString(const char* szJSON, bool bBackwardsCompatibility)
{
// allow comments? <rapidjson::ParseComments>
if (bBackwardsCompatibility)
{
// Backwards compatibility with old resources using function get()
// Fast isJSON check: Check first non-white space character is '[' or '{'
for (const char* ptr = szJSON; true;)
{
char c = *ptr++;
if (c == '[' || c == '{')
break;
if (isspace((uchar)c))
continue;
return false;
}
}

// allow comments? <rapidjson::ParseComments>
rapidjson::Document doc;
rapidjson::ParseResult result = doc.Parse(szJSON);

Expand Down
2 changes: 1 addition & 1 deletion Server/mods/deathmatch/logic/lua/CLuaArguments.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class CLuaArguments
bool IsEqualTo(const CLuaArguments& compareTo, std::set<const CLuaArguments*>* knownTables = nullptr) const;

// json parse
bool ReadJSONString(const char* szJSON);
bool ReadJSONString(const char* szJSON, bool bBackwardsCompatibility = false);
bool ReadJSONArray(const rapidjson::Value& obj, std::vector<CLuaArguments*>* pKnownTables = NULL);
bool ReadJSONObject(const rapidjson::Value& obj, std::vector<CLuaArguments*>* pKnownTables = NULL);

Expand Down
11 changes: 6 additions & 5 deletions Server/mods/deathmatch/logic/luadefs/CLuaFunctionDefs.Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,14 @@ int CLuaFunctionDefs::Get(lua_State* luaVM)
}
// We only have a single entry for a specific setting, so output a string
const std::string& strDataValue = pAttribute->GetValue();
if (!Args.ReadJSONString(strDataValue.c_str()))
if (!Args.ReadJSONString(strDataValue.c_str(), true))
{
// No valid JSON? Parse as plain text
Args.PushString(strDataValue);
}
else
Args.PushArguments(luaVM);

Args.PushArguments(luaVM);
uiArgCount = Args.Count();

/* Don't output a table because although it is more consistent with the multiple values output below,
Expand All @@ -286,14 +287,14 @@ int CLuaFunctionDefs::Get(lua_State* luaVM)
CXMLAttributes& attributes = pSubNode->GetAttributes();
Args.PushString(attributes.Find("name")->GetValue());
const std::string& strDataValue = attributes.Find("value")->GetValue();
if (!Args.ReadJSONString(strDataValue.c_str()))
if (!Args.ReadJSONString(strDataValue.c_str(), true))
{
Args.PushString(strDataValue);
}
}

// Push a table and return
// @todo: test that
Args.PushArguments(luaVM); // Args.PushAsTable(luaVM);
Args.PushAsTable(luaVM);
uiArgCount = Args.Count();
}

Expand Down

0 comments on commit 4508f72

Please sign in to comment.