Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #2937: Improve performance of tocolor #3044

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
2 changes: 2 additions & 0 deletions Client/mods/deathmatch/logic/lua/CLuaMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ SString CLuaMain::ms_strExpectedUndumpHash;
#include "luascripts/coroutine_debug.lua.h"
#include "luascripts/exports.lua.h"
#include "luascripts/inspect.lua.h"
#include "luascripts/color.lua.h"

CLuaMain::CLuaMain(CLuaManager* pLuaManager, CResource* pResourceOwner, bool bEnableOOP)
{
Expand Down Expand Up @@ -183,6 +184,7 @@ void CLuaMain::LoadEmbeddedScripts()
LoadScript(EmbeddedLuaCode::exports);
LoadScript(EmbeddedLuaCode::coroutine_debug);
LoadScript(EmbeddedLuaCode::inspect);
LoadScript(EmbeddedLuaCode::tocolor);
DECLARE_PROFILER_SECTION(OnPostLoadScript)
}

Expand Down
2 changes: 2 additions & 0 deletions Server/mods/deathmatch/logic/lua/CLuaMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ extern CNetServer* g_pRealNetServer;
#include "luascripts/coroutine_debug.lua.h"
#include "luascripts/exports.lua.h"
#include "luascripts/inspect.lua.h"
#include "luascripts/color.lua.h"

CLuaMain::CLuaMain(CLuaManager* pLuaManager, CObjectManager* pObjectManager, CPlayerManager* pPlayerManager, CVehicleManager* pVehicleManager,
CBlipManager* pBlipManager, CRadarAreaManager* pRadarAreaManager, CMapManager* pMapManager, CResource* pResourceOwner, bool bEnableOOP)
Expand Down Expand Up @@ -241,6 +242,7 @@ void CLuaMain::LoadEmbeddedScripts()
LoadScript(EmbeddedLuaCode::exports);
LoadScript(EmbeddedLuaCode::coroutine_debug);
LoadScript(EmbeddedLuaCode::inspect);
LoadScript(EmbeddedLuaCode::tocolor);
}

void CLuaMain::RegisterModuleFunctions()
Expand Down
29 changes: 0 additions & 29 deletions Shared/mods/deathmatch/logic/luadefs/CLuaUtilDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ void CLuaUtilDefs::LoadFunctions()

// Utility functions
{"gettok", GetTok},
{"tocolor", tocolor},
{"getProcessMemoryStats", ArgumentParser<GetProcessMemoryStats>},
};

Expand Down Expand Up @@ -710,31 +709,3 @@ int CLuaUtilDefs::GetTok(lua_State* luaVM)
lua_pushboolean(luaVM, false);
return 1;
}

int CLuaUtilDefs::tocolor(lua_State* luaVM)
{
// int tocolor ( int red, int green, int blue [, int alpha = 255 ] )
int iRed;
int iGreen;
int iBlue;
int iAlpha;

CScriptArgReader argStream(luaVM);
argStream.ReadNumber(iRed);
argStream.ReadNumber(iGreen);
argStream.ReadNumber(iBlue);
argStream.ReadNumber(iAlpha, 255);

if (!argStream.HasErrors())
{
// Make it into an unsigned long
unsigned long ulColor = COLOR_RGBA(iRed, iGreen, iBlue, iAlpha);
lua_pushinteger(luaVM, static_cast<lua_Integer>(ulColor));
return 1;
}

// Make it black so funcs dont break
unsigned long ulColor = COLOR_RGBA(0, 0, 0, 255);
lua_pushnumber(luaVM, static_cast<lua_Number>(ulColor));
return 1;
}
1 change: 0 additions & 1 deletion Shared/mods/deathmatch/logic/luadefs/CLuaUtilDefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,4 @@ class CLuaUtilDefs : public CLuaDefs

// Utility functions
LUA_DECLARE(GetTok);
LUA_DECLARE(tocolor);
};
28 changes: 28 additions & 0 deletions Shared/mods/deathmatch/logic/luascripts/color.lua.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace EmbeddedLuaCode
{
const char* const tocolor = R"~LUA~(

--[[
SERVER AND CLIENT.

Provide a lua implementation of old tocolor function
--]]

local _errMsg = "Bad argument @ 'tocolor' [Expected number at argument %d, got %s]"

tocolor = function(r,g,b,a)
if type(r) ~= 'number' then error(_errMsg:format(1, type(r))) end
if type(g) ~= 'number' then error(_errMsg:format(2, type(g))) end
if type(b) ~= 'number' then error(_errMsg:format(3, type(b))) end

if type(a) ~= 'number' then a = 255 end
r = r > 255 and 255 or r < 0 and 0 or r
g = g > 255 and 255 or g < 0 and 0 or g
b = b > 255 and 255 or b < 0 and 0 or b
a = a > 255 and 255 or a < 0 and 0 or a

return b + g * 256 + r * 256 * 256 + a * 256 * 256 * 256
end

)~LUA~";
}
Loading