Skip to content

Commit

Permalink
Fixed some type conversion and logic issues
Browse files Browse the repository at this point in the history
Improved mutex logic
Updated readme
Updated license year range
  • Loading branch information
danielga committed Sep 17, 2018
1 parent 1407d17 commit c2f899d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 34 deletions.
2 changes: 1 addition & 1 deletion license.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ gm_spew
A Garry's Mod module that intercepts the engine's messages and
passes them to Lua. Also able to prevent them from printing.
-----------------------------------------------------------------------
Copyright (c) 2015-2017, Daniel Almeida
Copyright (c) 2015-2018, Daniel Almeida
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
7 changes: 3 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

A Garry's Mod module that intercepts the engine's messages and passes them to Lua. Also able to prevent them from printing.

# Info
## Info

Mac was not tested at all (sorry but I'm poor).

If stuff starts erroring or fails to work, be sure to check the correct line endings (\n and such) are present in the files for each OS.

This project requires [garrysmod_common][1], a framework to facilitate the creation of compilations files (Visual Studio, make, XCode, etc). Simply set the environment variable 'GARRYSMOD_COMMON' or the premake option 'gmcommon' to the path of your local copy of [garrysmod_common][1]. We also use [SourceSDK2013][2], so set the environment variable 'SOURCE_SDK' or the premake option 'sourcesdk' to the path of your local copy of [SourceSDK2013][2].

This project requires [garrysmod_common][1], a framework to facilitate the creation of compilations files (Visual Studio, make, XCode, etc). Simply set the environment variable 'GARRYSMOD_COMMON' or the premake option 'gmcommon' to the path of your local copy of [garrysmod_common][1]. We also use [SourceSDK2013][2], so set the environment variable 'SOURCE_SDK' or the premake option 'sourcesdk' to the path of your local copy of [SourceSDK2013][2]. The previous links to [SourceSDK2013][2] point to my own fork of VALVe's repo and for good reason: Garry's Mod has lots of backwards incompatible changes to interfaces and it's much smaller, being perfect for automated build systems like Travis-CI.

[1]: https://github.com/danielga/garrysmod_common
[2]: https://github.com/ValveSoftware/source-sdk-2013
[2]: https://github.com/danielga/sourcesdk-minimal
61 changes: 32 additions & 29 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ namespace spew
struct Spew
{
Spew( ) :
time( 0.0 ),
type( SPEW_MESSAGE ),
level( -1 )
{ }

Spew( SpewType_t type, int level, const char *group, const Color &color, const char *msg ) :
Spew( SpewType_t type, int32_t level, const char *group, const Color &color, const char *msg ) :
time( Plat_FloatTime( ) ),
type( type ),
level( level ),
Expand All @@ -32,11 +33,11 @@ struct Spew
std::string msg;
};

static const size_t MAX_MESSAGES = 100000;
static const size_t MaxMessages = 1000;
static SpewOutputFunc_t original_spew = nullptr;
static std::queue<Spew> spew_queue;
static std::mutex spew_locker;
static bool blocked_spews[SPEW_TYPE_COUNT] = { false };
static bool blocked_spews[SPEW_TYPE_COUNT] = { false, false, false, false, false };

LUA_FUNCTION_STATIC( Get )
{
Expand All @@ -53,13 +54,14 @@ LUA_FUNCTION_STATIC( Get )

LUA->CreateTable( );

Spew spew;
for( size_t k = 0; k < count; ++k )
{
spew_locker.lock( );
spew = spew_queue.front( );
spew_queue.pop( );
spew_locker.unlock( );
Spew spew;
{
std::lock_guard<std::mutex> lock( spew_locker );
spew = spew_queue.front( );
spew_queue.pop( );
}

LUA->PushNumber( k + 1 );
LUA->CreateTable( );
Expand Down Expand Up @@ -105,14 +107,15 @@ LUA_FUNCTION_STATIC( Block )
{
int32_t type = -1;
if( LUA->Top( ) != 0 )
type = static_cast<uint32_t>( LUA->CheckNumber( 1 ) );
type = static_cast<int32_t>( LUA->CheckNumber( 1 ) );

switch( type )
{
case -1:
for( size_t k = 0; k < SPEW_TYPE_COUNT; ++k )
blocked_spews[k] = true;

LUA->PushBool( true );
break;

case SPEW_MESSAGE:
Expand All @@ -136,14 +139,15 @@ LUA_FUNCTION_STATIC( Unblock )
{
int32_t type = -1;
if( LUA->Top( ) != 0 )
type = static_cast<uint32_t>( LUA->CheckNumber( 1 ) );
type = static_cast<int32_t>( LUA->CheckNumber( 1 ) );

switch( type )
{
case -1:
for( size_t k = 0; k < SPEW_TYPE_COUNT; ++k )
blocked_spews[k] = false;

LUA->PushBool( true );
break;

case SPEW_MESSAGE:
Expand All @@ -165,31 +169,36 @@ LUA_FUNCTION_STATIC( Unblock )

static SpewRetval_t EngineSpewReceiver( SpewType_t type, const char *msg )
{
spew_locker.lock( );
if( spew_queue.size( ) >= MAX_MESSAGES )
std::lock_guard<std::mutex> lock( spew_locker );
if( spew_queue.size( ) >= MaxMessages )
spew_queue.pop( );

spew_queue.push( Spew(
spew_queue.emplace(
type,
GetSpewOutputLevel( ),
GetSpewOutputGroup( ),
*GetSpewOutputColor( ),
msg
) );
spew_locker.unlock( );
);

return ( type < SPEW_TYPE_COUNT && blocked_spews[type] ) ? SPEW_CONTINUE : original_spew( type, msg );
return ( type < SPEW_TYPE_COUNT && blocked_spews[type] ) ? SPEW_CONTINUE :
original_spew( type, msg );
}

static void Initialize( GarrysMod::Lua::ILuaBase *LUA )
{
original_spew = GetSpewOutputFunc( );
SpewOutputFunc( EngineSpewReceiver );

LUA->PushSpecial( GarrysMod::Lua::SPECIAL_GLOB );

LUA->CreateTable( );

LUA->PushString( "spew 1.0.0" );
LUA->SetField( -2, "Version" );

// version num follows LuaJIT style, xxyyzz
LUA->PushNumber( 10000 );
LUA->SetField( -2, "VersionNum" );

LUA->PushNumber( SPEW_MESSAGE );
LUA->SetField( -2, "MESSAGE" );

Expand All @@ -205,30 +214,24 @@ static void Initialize( GarrysMod::Lua::ILuaBase *LUA )
LUA->PushNumber( SPEW_LOG );
LUA->SetField( -2, "LOG" );

LUA->PushCFunction( spew::Get );
LUA->PushCFunction( Get );
LUA->SetField( -2, "Get" );

LUA->PushCFunction( spew::Block );
LUA->PushCFunction( Block );
LUA->SetField( -2, "Block" );

LUA->PushCFunction( spew::Unblock );
LUA->PushCFunction( Unblock );
LUA->SetField( -2, "Unblock" );

LUA->SetField( -2, "spew" );

LUA->Pop( 1 );
LUA->SetField( GarrysMod::Lua::INDEX_GLOBAL, "spew" );
}

static void Deinitialize( GarrysMod::Lua::ILuaBase *LUA )
{
SpewOutputFunc( original_spew );

LUA->PushSpecial( GarrysMod::Lua::SPECIAL_GLOB );

LUA->PushNil( );
LUA->SetField( -2, "spew" );

LUA->Pop( 1 );
LUA->SetField( GarrysMod::Lua::INDEX_GLOBAL, "spew" );
}

}
Expand Down

0 comments on commit c2f899d

Please sign in to comment.