Skip to content
Permalink
Browse files

Add async encode/decodeString (#1226)

  • Loading branch information
StrixG authored and qaisjp committed Jan 24, 2020
1 parent 24a233f commit 273a87aa6cb276e65c471ff2e68f1f1ea44f9b5f
Showing with 72 additions and 6 deletions.
  1. +72 −6 Shared/mods/deathmatch/logic/luadefs/CLuaCryptDefs.cpp
@@ -368,12 +368,16 @@ int CLuaCryptDefs::EncodeString(lua_State* luaVM)
StringEncryptFunction algorithm;
SString data;
CStringMap options;
CLuaFunctionRef luaFunctionRef;

CScriptArgReader argStream(luaVM);
argStream.ReadEnumString(algorithm);
argStream.ReadString(data);
argStream.ReadStringMap(options);

argStream.ReadFunction(luaFunctionRef, LUA_REFNIL);
argStream.ReadFunctionComplete();

if (!argStream.HasErrors())
{
switch (algorithm)
@@ -389,9 +393,38 @@ int CLuaCryptDefs::EncodeString(lua_State* luaVM)
return 1;
}

SString result;
SharedUtil::TeaEncode(data, key, &result);
lua_pushlstring(luaVM, result, result.length());
// Async
if (VERIFY_FUNCTION(luaFunctionRef))
{
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
if (pLuaMain)
{
CLuaShared::GetAsyncTaskScheduler()->PushTask<SString>(
[data, key] {
// Execute time-consuming task
SString result;
SharedUtil::TeaEncode(data, key, &result);
return result;
},
[luaFunctionRef](const SString& result) {
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaFunctionRef.GetLuaVM());
if (pLuaMain)
{
CLuaArguments arguments;
arguments.PushString(result);
arguments.Call(pLuaMain, luaFunctionRef);
}
});

lua_pushboolean(luaVM, true);
}
}
else // Sync
{
SString result;
SharedUtil::TeaEncode(data, key, &result);
lua_pushlstring(luaVM, result, result.length());
}
return 1;
}
default:
@@ -414,12 +447,16 @@ int CLuaCryptDefs::DecodeString(lua_State* luaVM)
StringEncryptFunction algorithm;
SString data;
CStringMap options;
CLuaFunctionRef luaFunctionRef;

CScriptArgReader argStream(luaVM);
argStream.ReadEnumString(algorithm);
argStream.ReadString(data);
argStream.ReadStringMap(options);

argStream.ReadFunction(luaFunctionRef, LUA_REFNIL);
argStream.ReadFunctionComplete();

if (!argStream.HasErrors())
{
switch (algorithm)
@@ -435,9 +472,38 @@ int CLuaCryptDefs::DecodeString(lua_State* luaVM)
return 1;
}

SString result;
SharedUtil::TeaDecode(data, key, &result);
lua_pushlstring(luaVM, result, result.length());
// Async
if (VERIFY_FUNCTION(luaFunctionRef))
{
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
if (pLuaMain)
{
CLuaShared::GetAsyncTaskScheduler()->PushTask<SString>(
[data, key] {
// Execute time-consuming task
SString result;
SharedUtil::TeaDecode(data, key, &result);
return result;
},
[luaFunctionRef](const SString& result) {
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaFunctionRef.GetLuaVM());
if (pLuaMain)
{
CLuaArguments arguments;
arguments.PushString(result);
arguments.Call(pLuaMain, luaFunctionRef);
}
});

lua_pushboolean(luaVM, true);
}
}
else // Sync
{
SString result;
SharedUtil::TeaDecode(data, key, &result);
lua_pushlstring(luaVM, result, result.length());
}
return 1;
}
default:

0 comments on commit 273a87a

Please sign in to comment.
You can’t perform that action at this time.