Skip to content

Commit

Permalink
feat: add initial clang format and tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
roflmuffin committed Oct 5, 2023
1 parent fbff445 commit 0a74133
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 86 deletions.
4 changes: 4 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
Language: Cpp
BasedOnStyle: Microsoft
ColumnLimit: 120
111 changes: 111 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
---
# Configure clang-tidy for this project.

# Here is an explanation for why some of the checks are disabled:
#
# -google-readability-namespace-comments: the *_CLIENT_NS is a macro, and
# clang-tidy fails to match it against the initial value.
#
# -modernize-use-trailing-return-type: clang-tidy recommends using
# `auto Foo() -> std::string { return ...; }`, we think the code is less
# readable in this form.
#
# --modernize-concat-nested-namespaces: clang-tidy recommends
# `namespace google::cloud {}` over `namespace google { namespace cloud { } }`
# We need to support C++14, which does not supported nested namespaces.
#
# --modernize-use-nodiscard: clang-tidy recommends adding a nodiscard annotation
# to functions where the return value should not be ignored.
# We need to support C++14, which does not supported the annotation.
#
# -modernize-return-braced-init-list: We think removing typenames and using
# only braced-init can hurt readability.
#
# -modernize-avoid-c-arrays: We only use C arrays when they seem to be the
# right tool for the job, such as `char foo[] = "hello"`. In these cases,
# avoiding C arrays often makes the code less readable, and std::array is
# not a drop-in replacement because it doesn't deduce the size.
#
# -performance-move-const-arg: This warning requires the developer to
# know/care more about the implementation details of types/functions than
# should be necessary. For example, `A a; F(std::move(a));` will trigger a
# warning IFF `A` is a trivial type (and therefore the move is
# meaningless). It would also warn if `F` accepts by `const&`, which is
# another detail that the caller need not care about.
#
# -readability-redundant-declaration: A friend declaration inside a class
# counts as a declaration, so if we also declare that friend outside the
# class in order to document it as part of the public API, that will
# trigger a redundant declaration warning from this check.
#
# -readability-function-cognitive-complexity: too many false positives with
# clang-tidy-12. We need to disable this check in macros, and that setting
# only appears in clang-tidy-13.
#
# -bugprone-narrowing-conversions: too many false positives around
# `std::size_t` vs. `*::difference_type`.
#
# -bugprone-easily-swappable-parameters: too many false positives.
#
# -bugprone-implicit-widening-of-multiplication-result: too many false positives.
# Almost any expression of the form `2 * variable` or `long x = a_int * b_int;`
# generates an error.
#
# -bugprone-unchecked-optional-access: too many false positives in tests.
# Despite what the documentation says, this warning appears after
# `ASSERT_TRUE(variable)` or `ASSERT_TRUE(variable.has_value())`.
#
Checks: >
-*,
abseil-*,
bugprone-*,GetGame
google-*,
misc-*,
modernize-*,
performance-*,
portability-*,
readability-*,
-google-readability-braces-around-statements,
-google-readability-namespace-comments,
-google-runtime-references,
-misc-non-private-member-variables-in-classes,
-misc-const-correctness,
-modernize-return-braced-init-list,
-modernize-use-trailing-return-type,
-modernize-concat-nested-namespaces,
-modernize-use-nodiscard,
-modernize-avoid-c-arrays,
-performance-move-const-arg,
-readability-braces-around-statements,
-readability-identifier-length,
-readability-magic-numbers,
-readability-named-parameter,
-readability-redundant-declaration,
-readability-function-cognitive-complexity,
-readability-convert-member-functions-to-static,
-bugprone-narrowing-conversions,
-bugprone-easily-swappable-parameters,
-bugprone-implicit-widening-of-multiplication-result,
-bugprone-unchecked-optional-access
# Turn all the warnings from the checks above into errors.
WarningsAsErrors: "*"

HeaderFilterRegex: "(google/cloud/|generator/).*\\.h$"

CheckOptions:
- key: readability-identifier-naming.ClassCase
value: CamelCase
- key: readability-identifier-naming.EnumCase
value: CamelCase
- key: readability-identifier-naming.FunctionCase
value: camelBack
- key: readability-identifier-naming.MemberCase
value: CamelCase
- key: readability-identifier-naming.ParameterCase
value: camelBack
- key: readability-identifier-naming.UnionCase
value: CamelCase
- key: readability-identifier-naming.VariableCase
value: CamelCase
...
2 changes: 2 additions & 0 deletions libraries/.clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Disable all checks in this folder.
Checks: '-*'
128 changes: 73 additions & 55 deletions src/sample_mm.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#pragma ide diagnostic ignored "readability-identifier-naming"
/**
* vim: set ts=4 sw=4 tw=99 noet :
* ======================================================
Expand All @@ -12,35 +13,40 @@
* This sample plugin is public domain.
*/

#include <stdio.h>
#include "sample_mm.h"

#include <cstdio>

#include "iserver.h"

SH_DECL_HOOK3_void(IServerGameDLL, GameFrame, SH_NOATTRIB, 0, bool, bool, bool);
SH_DECL_HOOK4_void(IServerGameClients, ClientActive, SH_NOATTRIB, 0, CPlayerSlot, bool, const char *, uint64);
SH_DECL_HOOK5_void(IServerGameClients, ClientDisconnect, SH_NOATTRIB, 0, CPlayerSlot, int, const char *, uint64, const char *);
SH_DECL_HOOK5_void(IServerGameClients, ClientDisconnect, SH_NOATTRIB, 0, CPlayerSlot, int, const char *, uint64,
const char *);
SH_DECL_HOOK4_void(IServerGameClients, ClientPutInServer, SH_NOATTRIB, 0, CPlayerSlot, char const *, int, uint64);
SH_DECL_HOOK1_void(IServerGameClients, ClientSettingsChanged, SH_NOATTRIB, 0, CPlayerSlot );
SH_DECL_HOOK6_void(IServerGameClients, OnClientConnected, SH_NOATTRIB, 0, CPlayerSlot, const char*, uint64, const char *, const char *, bool);
SH_DECL_HOOK6(IServerGameClients, ClientConnect, SH_NOATTRIB, 0, bool, CPlayerSlot, const char*, uint64, const char *, bool, CBufferString *);
SH_DECL_HOOK1_void(IServerGameClients, ClientSettingsChanged, SH_NOATTRIB, 0, CPlayerSlot);
SH_DECL_HOOK6_void(IServerGameClients, OnClientConnected, SH_NOATTRIB, 0, CPlayerSlot, const char *, uint64,
const char *, const char *, bool);
SH_DECL_HOOK6(IServerGameClients, ClientConnect, SH_NOATTRIB, 0, bool, CPlayerSlot, const char *, uint64, const char *,
bool, CBufferString *);
SH_DECL_HOOK2(IGameEventManager2, FireEvent, SH_NOATTRIB, 0, bool, IGameEvent *, bool);

SH_DECL_HOOK2_void( IServerGameClients, ClientCommand, SH_NOATTRIB, 0, CPlayerSlot, const CCommand & );
SH_DECL_HOOK2_void(IServerGameClients, ClientCommand, SH_NOATTRIB, 0, CPlayerSlot, const CCommand &);

SamplePlugin g_SamplePlugin;
IServerGameDLL *server = NULL;
IServerGameClients *gameclients = NULL;
IVEngineServer *engine = NULL;
IGameEventManager2 *gameevents = NULL;
ICvar *icvar = NULL;
IServerGameDLL *server = nullptr;
IServerGameClients *gameclients = nullptr;
IVEngineServer *engine = nullptr;
IGameEventManager2 *gameevents = nullptr;
ICvar *icvar = nullptr;

// Should only be called within the active game loop (i e map should be loaded and active)
// otherwise that'll be nullptr!
CGlobalVars *GetGameGlobals()
// Should only be called within the active game loop (i e map should be loaded
// and active) otherwise that'll be nullptr!
CGlobalVars *getGameGlobals()
{
INetworkGameServer *server = g_pNetworkServerService->GetIGameServer();

if(!server)
if (!server)
return nullptr;

return g_pNetworkServerService->GetIGameServer()->GetGlobals();
Expand All @@ -53,7 +59,7 @@ ConVar sample_cvar("sample_cvar", "42", 0);

CON_COMMAND_F(sample_command, "Sample command", FCVAR_NONE)
{
META_CONPRINTF( "Sample command called by %d. Command: %s\n", context.GetPlayerSlot(), args.GetCommandString() );
META_CONPRINTF("Sample command called by %d. Command: %s\n", context.GetPlayerSlot(), args.GetCommandString());
}

PLUGIN_EXPOSE(SamplePlugin, g_SamplePlugin);
Expand All @@ -65,26 +71,31 @@ bool SamplePlugin::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen,
GET_V_IFACE_CURRENT(GetEngineFactory, icvar, ICvar, CVAR_INTERFACE_VERSION);
GET_V_IFACE_ANY(GetServerFactory, server, IServerGameDLL, INTERFACEVERSION_SERVERGAMEDLL);
GET_V_IFACE_ANY(GetServerFactory, gameclients, IServerGameClients, INTERFACEVERSION_SERVERGAMECLIENTS);
GET_V_IFACE_ANY(GetEngineFactory, g_pNetworkServerService, INetworkServerService, NETWORKSERVERSERVICE_INTERFACE_VERSION);
GET_V_IFACE_ANY(GetEngineFactory, g_pNetworkServerService, INetworkServerService,
NETWORKSERVERSERVICE_INTERFACE_VERSION);

// Currently doesn't work from within mm side, use GetGameGlobals() in the mean time instead
// gpGlobals = ismm->GetCGlobals();
// Currently doesn't work from within mm side, use GetGameGlobals() in the
// mean time instead gpGlobals = ismm->GetCGlobals();

META_CONPRINTF( "Starting plugin.\n" );
META_CONPRINTF("Starting plugin.\n");

SH_ADD_HOOK_MEMFUNC(IServerGameDLL, GameFrame, server, this, &SamplePlugin::Hook_GameFrame, true);
SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientActive, gameclients, this, &SamplePlugin::Hook_ClientActive, true);
SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, gameclients, this, &SamplePlugin::Hook_ClientDisconnect, true);
SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, gameclients, this, &SamplePlugin::Hook_ClientPutInServer, true);
SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, gameclients, this, &SamplePlugin::Hook_ClientSettingsChanged, false);
SH_ADD_HOOK_MEMFUNC(IServerGameClients, OnClientConnected, gameclients, this, &SamplePlugin::Hook_OnClientConnected, false);
SH_ADD_HOOK_MEMFUNC( IServerGameClients, ClientConnect, gameclients, this, &SamplePlugin::Hook_ClientConnect, false );
SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, gameclients, this, &SamplePlugin::Hook_ClientDisconnect,
true);
SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, gameclients, this, &SamplePlugin::Hook_ClientPutInServer,
true);
SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, gameclients, this,
&SamplePlugin::Hook_ClientSettingsChanged, false);
SH_ADD_HOOK_MEMFUNC(IServerGameClients, OnClientConnected, gameclients, this, &SamplePlugin::Hook_OnClientConnected,
false);
SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientConnect, gameclients, this, &SamplePlugin::Hook_ClientConnect, false);
SH_ADD_HOOK_MEMFUNC(IServerGameClients, ClientCommand, gameclients, this, &SamplePlugin::Hook_ClientCommand, false);

META_CONPRINTF( "All hooks started!\n" );
META_CONPRINTF("All hooks started!\n");

g_pCVar = icvar;
ConVar_Register( FCVAR_RELEASE | FCVAR_CLIENT_CAN_EXECUTE | FCVAR_GAMEDLL );
ConVar_Register(FCVAR_RELEASE | FCVAR_CLIENT_CAN_EXECUTE | FCVAR_GAMEDLL);

return true;
}
Expand All @@ -93,12 +104,18 @@ bool SamplePlugin::Unload(char *error, size_t maxlen)
{
SH_REMOVE_HOOK_MEMFUNC(IServerGameDLL, GameFrame, server, this, &SamplePlugin::Hook_GameFrame, true);
SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientActive, gameclients, this, &SamplePlugin::Hook_ClientActive, true);
SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, gameclients, this, &SamplePlugin::Hook_ClientDisconnect, true);
SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, gameclients, this, &SamplePlugin::Hook_ClientPutInServer, true);
SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, gameclients, this, &SamplePlugin::Hook_ClientSettingsChanged, false);
SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, OnClientConnected, gameclients, this, &SamplePlugin::Hook_OnClientConnected, false);
SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientConnect, gameclients, this, &SamplePlugin::Hook_ClientConnect, false);
SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientCommand, gameclients, this, &SamplePlugin::Hook_ClientCommand, false);
SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientDisconnect, gameclients, this,
&SamplePlugin::Hook_ClientDisconnect, true);
SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientPutInServer, gameclients, this,
&SamplePlugin::Hook_ClientPutInServer, true);
SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientSettingsChanged, gameclients, this,
&SamplePlugin::Hook_ClientSettingsChanged, false);
SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, OnClientConnected, gameclients, this,
&SamplePlugin::Hook_OnClientConnected, false);
SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientConnect, gameclients, this, &SamplePlugin::Hook_ClientConnect,
false);
SH_REMOVE_HOOK_MEMFUNC(IServerGameClients, ClientCommand, gameclients, this, &SamplePlugin::Hook_ClientCommand,
false);

return true;
}
Expand All @@ -110,44 +127,49 @@ void SamplePlugin::AllPluginsLoaded()
*/
}

void SamplePlugin::Hook_ClientActive( CPlayerSlot slot, bool bLoadGame, const char *pszName, uint64 xuid )
void SamplePlugin::Hook_ClientActive(CPlayerSlot slot, bool bLoadGame, const char *pszName, uint64 xuid)
{
META_CONPRINTF( "Hook_ClientActive(%d, %d, \"%s\", %d)\n", slot, bLoadGame, pszName, xuid );
META_CONPRINTF("Hook_ClientActive(%d, %d, \"%s\", %d)\n", slot, bLoadGame, pszName, xuid);
}

void SamplePlugin::Hook_ClientCommand( CPlayerSlot slot, const CCommand &args )
void SamplePlugin::Hook_ClientCommand(CPlayerSlot slot, const CCommand &args)
{
META_CONPRINTF( "Hook_ClientCommand(%d, \"%s\")\n", slot, args.GetCommandString() );
META_CONPRINTF("Hook_ClientCommand(%d, \"%s\")\n", slot, args.GetCommandString());
}

void SamplePlugin::Hook_ClientSettingsChanged( CPlayerSlot slot )
void SamplePlugin::Hook_ClientSettingsChanged(CPlayerSlot slot)
{
META_CONPRINTF( "Hook_ClientSettingsChanged(%d)\n", slot );
META_CONPRINTF("Hook_ClientSettingsChanged(%d)\n", slot);
}

void SamplePlugin::Hook_OnClientConnected( CPlayerSlot slot, const char *pszName, uint64 xuid, const char *pszNetworkID, const char *pszAddress, bool bFakePlayer )
void SamplePlugin::Hook_OnClientConnected(CPlayerSlot slot, const char *pszName, uint64 xuid, const char *pszNetworkID,
const char *pszAddress, bool bFakePlayer)
{
META_CONPRINTF( "Hook_OnClientConnected(%d, \"%s\", %d, \"%s\", \"%s\", %d)\n", slot, pszName, xuid, pszNetworkID, pszAddress, bFakePlayer );
META_CONPRINTF("Hook_OnClientConnected(%d, \"%s\", %d, \"%s\", \"%s\", %d)\n", slot, pszName, xuid, pszNetworkID,
pszAddress, bFakePlayer);
}

bool SamplePlugin::Hook_ClientConnect( CPlayerSlot slot, const char *pszName, uint64 xuid, const char *pszNetworkID, bool unk1, CBufferString *pRejectReason )
bool SamplePlugin::Hook_ClientConnect(CPlayerSlot slot, const char *pszName, uint64 xuid, const char *pszNetworkID,
bool unk1, CBufferString *pRejectReason)
{
META_CONPRINTF( "Hook_ClientConnect(%d, \"%s\", %d, \"%s\", %d, \"%s\")\n", slot, pszName, xuid, pszNetworkID, unk1, pRejectReason->ToGrowable()->Get() );
META_CONPRINTF("Hook_ClientConnect(%d, \"%s\", %d, \"%s\", %d, \"%s\")\n", slot, pszName, xuid, pszNetworkID, unk1,
pRejectReason->ToGrowable()->Get());

RETURN_META_VALUE(MRES_IGNORED, true);
}

void SamplePlugin::Hook_ClientPutInServer( CPlayerSlot slot, char const *pszName, int type, uint64 xuid )
void SamplePlugin::Hook_ClientPutInServer(CPlayerSlot slot, char const *pszName, int type, uint64 xuid)
{
META_CONPRINTF( "Hook_ClientPutInServer(%d, \"%s\", %d, %d, %d)\n", slot, pszName, type, xuid );
META_CONPRINTF("Hook_ClientPutInServer(%d, \"%s\", %d, %d, %d)\n", slot, pszName, type, xuid);
}

void SamplePlugin::Hook_ClientDisconnect( CPlayerSlot slot, int reason, const char *pszName, uint64 xuid, const char *pszNetworkID )
void SamplePlugin::Hook_ClientDisconnect(CPlayerSlot slot, int reason, const char *pszName, uint64 xuid,
const char *pszNetworkID)
{
META_CONPRINTF( "Hook_ClientDisconnect(%d, %d, \"%s\", %d, \"%s\")\n", slot, reason, pszName, xuid, pszNetworkID );
META_CONPRINTF("Hook_ClientDisconnect(%d, %d, \"%s\", %d, \"%s\")\n", slot, reason, pszName, xuid, pszNetworkID);
}

void SamplePlugin::Hook_GameFrame( bool simulating, bool bFirstTick, bool bLastTick )
void SamplePlugin::Hook_GameFrame(bool simulating, bool bFirstTick, bool bLastTick)
{
/**
* simulating:
Expand All @@ -158,12 +180,8 @@ void SamplePlugin::Hook_GameFrame( bool simulating, bool bFirstTick, bool bLastT
}

// Potentially might not work
void SamplePlugin::OnLevelInit( char const *pMapName,
char const *pMapEntities,
char const *pOldLevel,
char const *pLandmarkName,
bool loadGame,
bool background )
void SamplePlugin::OnLevelInit(char const *pMapName, char const *pMapEntities, char const *pOldLevel,
char const *pLandmarkName, bool loadGame, bool background)
{
META_CONPRINTF("OnLevelInit(%s)\n", pMapName);
}
Expand Down Expand Up @@ -222,4 +240,4 @@ const char *SamplePlugin::GetName()
const char *SamplePlugin::GetURL()
{
return "http://www.sourcemm.net/";
}
}
Loading

0 comments on commit 0a74133

Please sign in to comment.