Skip to content

Commit

Permalink
Refactoring client
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico8340 committed Jun 1, 2024
1 parent 1b78938 commit 8cf015b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 35 deletions.
96 changes: 62 additions & 34 deletions Client/mods/deathmatch/logic/luadefs/CLuaResourceDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
*****************************************************************************/

#include "StdInc.h"
#include <lua/CLuaFunctionParser.h>

using std::list;

void CLuaResourceDefs::LoadFunctions()
Expand All @@ -18,7 +20,7 @@ void CLuaResourceDefs::LoadFunctions()
{"call", Call},
{"getThisResource", GetThisResource},
{"getResourceConfig", GetResourceConfig},
{"getResourceName", GetResourceName},
{"getResourceName", ArgumentParser<GetResourceName>},
{"getResourceFromName", GetResourceFromName},
{"getResourceRootElement", GetResourceRootElement},
{"getResourceGUIElement", GetResourceGUIElement},
Expand Down Expand Up @@ -220,46 +222,72 @@ int CLuaResourceDefs::GetResourceConfig(lua_State* luaVM)
return 1;
}

int CLuaResourceDefs::GetResourceName(lua_State* luaVM)
std::string CLuaResourceDefs::GetResourceName(lua_State* luaVM, std::optional<CResource*> resourceElement)
{
// Verify arguments
CResource* pResource;
CScriptArgReader argStream(luaVM);
argStream.ReadUserData(pResource, nullptr);

if (!argStream.HasErrors())
if (!resourceElement)
{
if (!pResource)
{
// Find our vm and get the root
CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
if (pLuaMain)
{
pResource = pLuaMain->GetResource();
}
}
CLuaMain* localVM = m_pLuaManager->GetVirtualMachine(luaVM);
CResource* localResource;

if (pResource)
{
// Grab its name and return it
const char* szName = pResource->GetName();
if (szName)
{
lua_pushstring(luaVM, szName);
return 1;
}
}
else
m_pScriptDebugging->LogBadPointer(luaVM, "resource", 1);
if (!localVM)
return false;

localResource = localVM->GetResource();

if (!localResource)
return false;

resourceElement = localResource;
}
else
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());

// Failed
lua_pushboolean(luaVM, false);
return 1;
std::string resourceName = (*resourceElement)->GetName();

if (resourceName.empty())
return false;

return resourceName;
}

//int CLuaResourceDefs::GetResourceName(lua_State* luaVM)
//{
// // Verify arguments
// CResource* pResource;
// CScriptArgReader argStream(luaVM);
// argStream.ReadUserData(pResource, nullptr);
//
// if (!argStream.HasErrors())
// {
// if (!pResource)
// {
// // Find our vm and get the root
// CLuaMain* pLuaMain = m_pLuaManager->GetVirtualMachine(luaVM);
// if (pLuaMain)
// {
// pResource = pLuaMain->GetResource();
// }
// }
//
// if (pResource)
// {
// // Grab its name and return it
// const char* szName = pResource->GetName();
// if (szName)
// {
// lua_pushstring(luaVM, szName);
// return 1;
// }
// }
// else
// m_pScriptDebugging->LogBadPointer(luaVM, "resource", 1);
// }
// else
// m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
//
// // Failed
// lua_pushboolean(luaVM, false);
// return 1;
//}

int CLuaResourceDefs::GetResourceFromName(lua_State* luaVM)
{
// Verify arguments
Expand Down
3 changes: 2 additions & 1 deletion Client/mods/deathmatch/logic/luadefs/CLuaResourceDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class CLuaResourceDefs : public CLuaDefs
LUA_DECLARE(Call);
LUA_DECLARE(GetThisResource);
LUA_DECLARE(GetResourceConfig);
LUA_DECLARE(GetResourceName);
LUA_DECLARE(GetResourceFromName);
LUA_DECLARE(GetResourceRootElement);
LUA_DECLARE(GetResourceGUIElement);
Expand All @@ -30,4 +29,6 @@ class CLuaResourceDefs : public CLuaDefs
LUA_DECLARE(GetResourceState);
LUA_DECLARE(LoadString);
LUA_DECLARE(Load);

static std::string GetResourceName(lua_State* luaVM, std::optional<CResource*> resourceElement);
};

0 comments on commit 8cf015b

Please sign in to comment.