Skip to content

Commit 586b869

Browse files
authored
New style Lua argument parsing (#973)
The current argstream argument parsing is rather verbose and repetitive. However all of the information used can be inferred at compile time by the compiler. See #973 for details
1 parent a4f447a commit 586b869

File tree

12 files changed

+1188
-152
lines changed

12 files changed

+1188
-152
lines changed

Client/mods/deathmatch/logic/luadefs/CLuaDefs.h

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,51 @@ class CLuaDefs
6464
static CClientDFFManager* m_pDFFManager;
6565
static CClientColModelManager* m_pColModelManager;
6666
static CRegisteredCommands* m_pRegisteredCommands;
67-
};
67+
68+
protected:
69+
// Old style: Only warn on failure. This should
70+
// not be used for new functions. ReturnOnError
71+
// must be a value to use as result on invalid argument
72+
template <auto ReturnOnError, auto T>
73+
static inline int ArgumentParserWarn(lua_State* L)
74+
{
75+
return CLuaFunctionParser<false, ReturnOnError, T>()(L, m_pScriptDebugging);
76+
}
77+
78+
// Special case for overloads
79+
// This combines multiple functions into one (via CLuaOverloadParser)
80+
template <auto ReturnOnError, auto FunctionA, auto FunctionB, auto... Functions>
81+
static inline int ArgumentParserWarn(lua_State* L)
82+
{
83+
// Pad functions to have the same number of parameters by
84+
// filling both up to the larger number of parameters with dummy_type arguments
85+
using PaddedFunctionA = pad_func_with_func<FunctionA, FunctionB>;
86+
using PaddedFunctionB = pad_func_with_func<FunctionB, FunctionA>;
87+
// Combine functions
88+
using Overload = CLuaOverloadParser<PaddedFunctionA::Call, PaddedFunctionB::Call>;
89+
90+
return ArgumentParserWarn<ReturnOnError, Overload::Call, Functions...>(L);
91+
}
92+
93+
// New style: hard error on usage mistakes
94+
template <auto T>
95+
static inline int ArgumentParser(lua_State* L)
96+
{
97+
return CLuaFunctionParser<true, nullptr, T>()(L, m_pScriptDebugging);
98+
}
99+
100+
// Special case for overloads
101+
// This combines multiple functions into one (via CLuaOverloadParser)
102+
template <auto FunctionA, auto FunctionB, auto... Functions>
103+
static inline int ArgumentParser(lua_State* L)
104+
{
105+
// Pad functions to have the same number of parameters by
106+
// filling both up to the larger number of parameters with dummy_type arguments
107+
using PaddedFunctionA = pad_func_with_func<FunctionA, FunctionB>;
108+
using PaddedFunctionB = pad_func_with_func<FunctionB, FunctionA>;
109+
// Combine functions
110+
using Overload = CLuaOverloadParser<PaddedFunctionA::Call, PaddedFunctionB::Call>;
111+
112+
return ArgumentParser<Overload::Call, Functions...>(L);
113+
}
114+
};

Server/mods/deathmatch/StdInc.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ struct SAclRequest;
113113
#include "packets/CServerInfoSyncPacket.h"
114114
#include "packets/CDiscordJoinPacket.h"
115115

116+
// has to be included early to prevent "unknown type name 'CRemoteCall'" in CLuaFunctionParser.h
117+
#include "CRemoteCalls.h"
118+
116119
// Lua function definitions
117120
#include "luadefs/CLuaElementDefs.h"
118121
#include "luadefs/CLuaAccountDefs.h"
@@ -142,9 +145,6 @@ struct SAclRequest;
142145
#include "luadefs/CLuaWaterDefs.h"
143146
#include "luadefs/CLuaWorldDefs.h"
144147

145-
// has to be included before CLuaFunctionParseHelpers to prevent "invalid use of incomplete type ‘class CRemoteCalls´
146-
#include "CRemoteCalls.h"
147-
148148
// Lua includes
149149
#include "lua/LuaCommon.h"
150150
#include "lua/CLuaMain.h"

Server/mods/deathmatch/logic/CGame.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,12 @@ class CGame;
5757
#include "lua/CLuaManager.h"
5858

5959
#include "CLightsyncManager.h"
60+
#include "CBanManager.h"
6061

6162
// Forward declarations
6263
class ASE;
6364
class CAccessControlListManager;
6465
class CAccountManager;
65-
class CBanManager;
6666
class CBlipManager;
6767
class CClock;
6868
class CColManager;

Server/mods/deathmatch/logic/luadefs/CLuaDefs.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "../CVehicleManager.h"
3232
#include "../CRegistry.h"
3333
#include "../CDatabaseManager.h"
34+
#include <lua/CLuaFunctionParser.h>
3435

3536
// Used by script handlers to verify elements
3637
#define SCRIPT_VERIFY_BLIP(blip) (m_pBlipManager->Exists(blip)&&!blip->IsBeingDeleted())
@@ -84,4 +85,51 @@ class CLuaDefs
8485
static CResourceManager* m_pResourceManager;
8586
static CAccessControlListManager* m_pACLManager;
8687
static CMainConfig* m_pMainConfig;
88+
89+
protected:
90+
// Old style: Only warn on failure. This should
91+
// not be used for new functions. ReturnOnError
92+
// must be a value to use as result on invalid argument
93+
template <auto ReturnOnError, auto T>
94+
static inline int ArgumentParserWarn(lua_State* L)
95+
{
96+
return CLuaFunctionParser<false, ReturnOnError, T>()(L, m_pScriptDebugging);
97+
}
98+
99+
// Special case for overloads
100+
// This combines multiple functions into one (via CLuaOverloadParser)
101+
template <auto ReturnOnError, auto FunctionA, auto FunctionB, auto... Functions>
102+
static inline int ArgumentParserWarn(lua_State* L)
103+
{
104+
// Pad functions to have the same number of parameters by
105+
// filling both up to the larger number of parameters with dummy_type arguments
106+
using PaddedFunctionA = pad_func_with_func<FunctionA, FunctionB>;
107+
using PaddedFunctionB = pad_func_with_func<FunctionB, FunctionA>;
108+
// Combine functions
109+
using Overload = CLuaOverloadParser<PaddedFunctionA::Call, PaddedFunctionB::Call>;
110+
111+
return ArgumentParserWarn<ReturnOnError, Overload::Call, Functions...>(L);
112+
}
113+
114+
// New style: hard error on usage mistakes
115+
template <auto T>
116+
static inline int ArgumentParser(lua_State* L)
117+
{
118+
return CLuaFunctionParser<true, nullptr, T>()(L, m_pScriptDebugging);
119+
}
120+
121+
// Special case for overloads
122+
// This combines multiple functions into one (via CLuaOverloadParser)
123+
template <auto FunctionA, auto FunctionB, auto... Functions>
124+
static inline int ArgumentParser(lua_State* L)
125+
{
126+
// Pad functions to have the same number of parameters by
127+
// filling both up to the larger number of parameters with dummy_type arguments
128+
using PaddedFunctionA = pad_func_with_func<FunctionA, FunctionB>;
129+
using PaddedFunctionB = pad_func_with_func<FunctionB, FunctionA>;
130+
// Combine functions
131+
using Overload = CLuaOverloadParser<PaddedFunctionA::Call, PaddedFunctionB::Call>;
132+
133+
return ArgumentParser<Overload::Call, Functions...>(L);
134+
}
87135
};

0 commit comments

Comments
 (0)