Skip to content

Commit 8da5f88

Browse files
authored
Merge pull request #302 from patrikjuvonen/github-issue-291
Move tocolor and gettok to shared function definitions
2 parents 187d27f + e3f9546 commit 8da5f88

File tree

8 files changed

+99
-175
lines changed

8 files changed

+99
-175
lines changed

Client/mods/deathmatch/logic/lua/CLuaFunctionDefs.Util.cpp

Lines changed: 0 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -10,97 +10,6 @@
1010

1111
#include "StdInc.h"
1212

13-
int CLuaFunctionDefs::GetTok(lua_State* luaVM)
14-
{
15-
SString strInput = "";
16-
unsigned int uiToken = 0;
17-
unsigned int uiDelimiter = 0;
18-
SString strDelimiter;
19-
CScriptArgReader argStream(luaVM);
20-
argStream.ReadString(strInput);
21-
argStream.ReadNumber(uiToken);
22-
23-
if (argStream.NextIsNumber())
24-
{
25-
argStream.ReadNumber(uiDelimiter);
26-
wchar_t wUNICODE[2] = {uiDelimiter, '\0'};
27-
strDelimiter = UTF16ToMbUTF8(wUNICODE);
28-
}
29-
else // It's already a string
30-
argStream.ReadString(strDelimiter);
31-
32-
if (!argStream.HasErrors())
33-
{
34-
unsigned int uiCount = 0;
35-
36-
if (uiToken > 0 && uiToken < 1024)
37-
{
38-
unsigned int uiCount = 1;
39-
char* szText = new char[strInput.length() + 1];
40-
strcpy(szText, strInput);
41-
char* szToken = strtok(szText, strDelimiter);
42-
43-
// We're looking for the first part?
44-
if (uiToken != 1)
45-
{
46-
// strtok count number of times
47-
do
48-
{
49-
uiCount++;
50-
szToken = strtok(NULL, strDelimiter);
51-
} while (uiCount != uiToken);
52-
}
53-
54-
// Found it?
55-
if (szToken)
56-
{
57-
// Return it
58-
lua_pushstring(luaVM, szToken);
59-
delete[] szText;
60-
return 1;
61-
}
62-
63-
// Delete the text
64-
delete[] szText;
65-
}
66-
else
67-
m_pScriptDebugging->LogWarning(luaVM, "Token parameter sent to split must be greater than 0 and smaller than 1024");
68-
}
69-
else
70-
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
71-
72-
lua_pushboolean(luaVM, false);
73-
return 1;
74-
}
75-
76-
int CLuaFunctionDefs::tocolor(lua_State* luaVM)
77-
{
78-
// int tocolor ( int red, int green, int blue [, int alpha = 255] )
79-
int iRed;
80-
int iGreen;
81-
int iBlue;
82-
int iAlpha;
83-
84-
CScriptArgReader argStream(luaVM);
85-
argStream.ReadNumber(iRed);
86-
argStream.ReadNumber(iGreen);
87-
argStream.ReadNumber(iBlue);
88-
argStream.ReadNumber(iAlpha, 255);
89-
90-
if (!argStream.HasErrors())
91-
{
92-
// Make it into an unsigned long
93-
unsigned long ulColor = COLOR_RGBA(iRed, iGreen, iBlue, iAlpha);
94-
lua_pushinteger(luaVM, static_cast<lua_Integer>(ulColor));
95-
return 1;
96-
}
97-
98-
// Make it black so funcs dont break
99-
unsigned long ulColor = COLOR_RGBA(0, 0, 0, 255);
100-
lua_pushnumber(luaVM, static_cast<lua_Number>(ulColor));
101-
return 1;
102-
}
103-
10413
int CLuaFunctionDefs::GetValidPedModels(lua_State* luaVM)
10514
{
10615
int iIndex = 0;

Client/mods/deathmatch/logic/lua/CLuaFunctionDefs.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ class CLuaFunctionDefs
7878

7979
// Util functions to make scripting easier for the end user
8080
// Some of these are based on standard mIRC script funcs as a lot of people will be used to them
81-
LUA_DECLARE(GetTok);
82-
LUA_DECLARE(tocolor);
8381
LUA_DECLARE(GetValidPedModels);
8482
LUA_DECLARE(SetDevelopmentMode);
8583
LUA_DECLARE(GetDevelopmentMode);

Client/mods/deathmatch/logic/lua/CLuaManager.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,6 @@ void CLuaManager::LoadCFunctions(void)
252252
CLuaCFunctions::AddFunction("setCursorAlpha", CLuaFunctionDefs::SetCursorAlpha);
253253

254254
// Util functions
255-
CLuaCFunctions::AddFunction("gettok", CLuaFunctionDefs::GetTok);
256-
CLuaCFunctions::AddFunction("tocolor", CLuaFunctionDefs::tocolor);
257255
CLuaCFunctions::AddFunction("getValidPedModels", CLuaFunctionDefs::GetValidPedModels);
258256
CLuaCFunctions::AddFunction("downloadFile", CLuaFunctionDefs::DownloadFile);
259257

Server/mods/deathmatch/logic/lua/CLuaFunctionDefs.Utility.cpp

Lines changed: 0 additions & 73 deletions
This file was deleted.

Server/mods/deathmatch/logic/lua/CLuaFunctionDefs.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,6 @@ class CLuaFunctionDefs
116116

117117
LUA_DECLARE(shutdown);
118118

119-
// Util functions to make scripting easier for the end user
120-
// Some of these are based on standard mIRC script funcs as a lot of people will be used to them
121-
LUA_DECLARE(GetTok);
122-
123119
// Loaded Map Functions
124120
LUA_DECLARE(GetRootElement);
125121
LUA_DECLARE(LoadMapData);

Server/mods/deathmatch/logic/lua/CLuaManager.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,9 +235,6 @@ void CLuaManager::LoadCFunctions(void)
235235

236236
CLuaCFunctions::AddFunction("shutdown", CLuaFunctionDefs::shutdown, true);
237237

238-
// Util funcs
239-
CLuaCFunctions::AddFunction("gettok", CLuaFunctionDefs::GetTok);
240-
241238
// Loaded map funcs
242239
CLuaCFunctions::AddFunction("getRootElement", CLuaFunctionDefs::GetRootElement);
243240
CLuaCFunctions::AddFunction("loadMapData", CLuaFunctionDefs::LoadMapData);

Shared/mods/deathmatch/logic/luadefs/CLuaUtilDefs.cpp

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ void CLuaUtilDefs::LoadFunctions(void)
4141

4242
// Debug functions
4343
CLuaCFunctions::AddFunction("debugSleep", DebugSleep);
44+
45+
// Utility functions
46+
CLuaCFunctions::AddFunction("gettok", GetTok);
47+
CLuaCFunctions::AddFunction("tocolor", tocolor);
4448
}
4549

4650
int CLuaUtilDefs::DisabledFunction(lua_State* luaVM)
@@ -613,3 +617,94 @@ int CLuaUtilDefs::DebugSleep(lua_State* luaVM)
613617
lua_pushboolean(luaVM, false);
614618
return 1;
615619
}
620+
621+
int CLuaUtilDefs::GetTok(lua_State* luaVM)
622+
{
623+
SString strInput = "";
624+
unsigned int uiToken = 0;
625+
unsigned int uiDelimiter = 0;
626+
SString strDelimiter;
627+
CScriptArgReader argStream(luaVM);
628+
argStream.ReadString(strInput);
629+
argStream.ReadNumber(uiToken);
630+
631+
if (argStream.NextIsNumber())
632+
{
633+
argStream.ReadNumber(uiDelimiter);
634+
wchar_t wUNICODE[2] = { uiDelimiter, '\0' };
635+
strDelimiter = UTF16ToMbUTF8(wUNICODE);
636+
}
637+
else // It's already a string
638+
argStream.ReadString(strDelimiter);
639+
640+
if (!argStream.HasErrors())
641+
{
642+
unsigned int uiCount = 0;
643+
644+
if (uiToken > 0 && uiToken < 1024)
645+
{
646+
unsigned int uiCount = 1;
647+
char* szText = new char[strInput.length() + 1];
648+
strcpy(szText, strInput);
649+
char* szToken = strtok(szText, strDelimiter);
650+
651+
// We're looking for the first part?
652+
if (uiToken != 1)
653+
{
654+
// strtok count number of times
655+
do
656+
{
657+
uiCount++;
658+
szToken = strtok(NULL, strDelimiter);
659+
} while (uiCount != uiToken);
660+
}
661+
662+
// Found it?
663+
if (szToken)
664+
{
665+
// Return it
666+
lua_pushstring(luaVM, szToken);
667+
delete[] szText;
668+
return 1;
669+
}
670+
671+
// Delete the text
672+
delete[] szText;
673+
}
674+
else
675+
m_pScriptDebugging->LogWarning(luaVM, "Token parameter sent to split must be greater than 0 and smaller than 1024");
676+
}
677+
else
678+
m_pScriptDebugging->LogCustom(luaVM, argStream.GetFullErrorMessage());
679+
680+
lua_pushboolean(luaVM, false);
681+
return 1;
682+
}
683+
684+
int CLuaUtilDefs::tocolor(lua_State* luaVM)
685+
{
686+
// int tocolor ( int red, int green, int blue [, int alpha = 255 ] )
687+
int iRed;
688+
int iGreen;
689+
int iBlue;
690+
int iAlpha;
691+
692+
CScriptArgReader argStream(luaVM);
693+
argStream.ReadNumber(iRed);
694+
argStream.ReadNumber(iGreen);
695+
argStream.ReadNumber(iBlue);
696+
argStream.ReadNumber(iAlpha, 255);
697+
698+
if (!argStream.HasErrors())
699+
{
700+
// Make it into an unsigned long
701+
unsigned long ulColor = COLOR_RGBA(iRed, iGreen, iBlue, iAlpha);
702+
lua_pushinteger(luaVM, static_cast<lua_Integer>(ulColor));
703+
return 1;
704+
}
705+
706+
// Make it black so funcs dont break
707+
unsigned long ulColor = COLOR_RGBA(0, 0, 0, 255);
708+
lua_pushnumber(luaVM, static_cast<lua_Number>(ulColor));
709+
return 1;
710+
}

Shared/mods/deathmatch/logic/luadefs/CLuaUtilDefs.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,8 @@ class CLuaUtilDefs : public CLuaDefs
4848

4949
// Debug functions
5050
LUA_DECLARE(DebugSleep);
51+
52+
// Utility functions
53+
LUA_DECLARE(GetTok);
54+
LUA_DECLARE(tocolor);
5155
};

0 commit comments

Comments
 (0)