Skip to content

Commit 71b07c8

Browse files
oSoMoNmisyltoad
authored andcommitted
convar: fix usertype registration for ConVars
This address a regression introduced by 50583f6. Fixes #2161.
1 parent f114d75 commit 71b07c8

3 files changed

Lines changed: 38 additions & 26 deletions

File tree

src/convar.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "convar.h"
22
#include "Utils/Version.h"
3+
34
#include <algorithm>
5+
#include <cassert>
46

57
LogScope console_log("console");
68

@@ -16,8 +18,10 @@ namespace gamescope
1618
assert( !GetCommands().contains( pszName ) );
1719
GetCommands()[ std::string( pszName ) ] = this;
1820

21+
#if HAVE_SCRIPTING
1922
if ( bRegisterScript )
2023
RegisterScript( pszName, this );
24+
#endif
2125
}
2226

2327
ConCommand::~ConCommand()
@@ -79,10 +83,4 @@ namespace gamescope
7983
{
8084
PrintVersion();
8185
});
82-
83-
#if !HAVE_SCRIPTING
84-
void ConCommand::RegisterScript( std::string_view, ConCommand * )
85-
{
86-
}
87-
#endif
88-
}
86+
}

src/convar.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
#include <span>
44
#include <string>
55
#include <string_view>
6-
#include <unordered_map>
7-
#include <utility>
86
#include <optional>
97
#include <charconv>
108
#include <type_traits>
119
#include <cstdint>
1210
#include <functional>
13-
#include <cassert>
1411

1512
#include "Utils/Dict.h"
1613

@@ -116,7 +113,9 @@ namespace gamescope
116113
std::string_view GetDescription() const { return m_pszDescription; }
117114

118115
static Dict<ConCommand *>& GetCommands();
116+
#if HAVE_SCRIPTING
119117
static void RegisterScript( std::string_view name, ConCommand *cmd );
118+
#endif
120119
protected:
121120
std::string_view m_pszName;
122121
std::string_view m_pszDescription;
@@ -140,12 +139,16 @@ namespace gamescope
140139
RunCallback();
141140
}
142141

142+
#if HAVE_SCRIPTING
143143
if ( bRegisterScript )
144-
{
145144
RegisterScript( pszName, this );
146-
}
145+
#endif
147146
}
148147

148+
#if HAVE_SCRIPTING
149+
static void RegisterScript( std::string_view name, ConVar<T> *cv );
150+
#endif
151+
149152
const T& Get() const
150153
{
151154
return m_Value;

src/convar_script.cpp

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
#if HAVE_SCRIPTING
2+
13
#include "convar.h"
24
#include "Script/Script.h"
35
#include "backend.h"
46

57
namespace gamescope
68
{
7-
#if HAVE_SCRIPTING
89
namespace detail
910
{
1011
struct ConVarScriptRegistrar
@@ -30,23 +31,33 @@ namespace gamescope
3031
};
3132
}
3233

33-
static auto s_ConCommandType = detail::ConVarScriptRegistrar::RegisterConCommand();
34-
static auto s_CVBool = detail::ConVarScriptRegistrar::RegisterConVarType<bool>();
35-
static auto s_CVInt = detail::ConVarScriptRegistrar::RegisterConVarType<int>();
36-
static auto s_CVFloat = detail::ConVarScriptRegistrar::RegisterConVarType<float>();
37-
static auto s_CVU32 = detail::ConVarScriptRegistrar::RegisterConVarType<uint32_t>();
38-
static auto s_CVU64 = detail::ConVarScriptRegistrar::RegisterConVarType<uint64_t>();
39-
static auto s_CVString = detail::ConVarScriptRegistrar::RegisterConVarType<std::string>();
40-
static auto s_CVVCS = detail::ConVarScriptRegistrar::RegisterConVarType<VirtualConnectorStrategy>();
41-
static auto s_CVTCM = detail::ConVarScriptRegistrar::RegisterConVarType<TouchClickMode>();
42-
4334
void ConCommand::RegisterScript( std::string_view name, ConCommand *cmd )
4435
{
4536
CScriptScopedLock().Manager().Gamescope().Convars.Base[name] = cmd;
4637
}
47-
#else
48-
void ConCommand::RegisterScript( std::string_view, ConCommand * )
38+
39+
template <typename T>
40+
void ConVar<T>::RegisterScript( std::string_view name, ConVar<T> *cv )
4941
{
42+
CScriptScopedLock().Manager().Gamescope().Convars.Base[name] = cv;
5043
}
51-
#endif
44+
45+
#define REGISTER_CONVAR_TYPE_IMPL( T, N ) \
46+
static auto s_ConVarType_##N = detail::ConVarScriptRegistrar::RegisterConVarType<T>(); \
47+
template void ConVar<T>::RegisterScript( std::string_view, ConVar<T> * );
48+
49+
#define REGISTER_CONVAR_TYPE_EXPAND( T, N ) REGISTER_CONVAR_TYPE_IMPL( T, N )
50+
#define REGISTER_CONVAR_TYPE( T ) REGISTER_CONVAR_TYPE_EXPAND( T, __COUNTER__ )
51+
52+
static auto s_ConCommandType = detail::ConVarScriptRegistrar::RegisterConCommand();
53+
REGISTER_CONVAR_TYPE( bool )
54+
REGISTER_CONVAR_TYPE( int )
55+
REGISTER_CONVAR_TYPE( float )
56+
REGISTER_CONVAR_TYPE( uint32_t )
57+
REGISTER_CONVAR_TYPE( uint64_t )
58+
REGISTER_CONVAR_TYPE( std::string )
59+
REGISTER_CONVAR_TYPE( VirtualConnectorStrategy )
60+
REGISTER_CONVAR_TYPE( TouchClickMode )
5261
}
62+
63+
#endif

0 commit comments

Comments
 (0)