Skip to content

Commit

Permalink
Added initial support for the main branch of Garry's Mod
Browse files Browse the repository at this point in the history
Bumped minor version
  • Loading branch information
danielga committed Mar 26, 2021
1 parent 5d5d563 commit 3b1368f
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 7 deletions.
3 changes: 2 additions & 1 deletion source/basic_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

#include <picojson.h>

#include "console_adapter.hpp"
#include "console_adapter_logging.hpp"
#include "console_adapter_spew.hpp"

#define LRDB_SERVER_PROTOCOL_VERSION "gmod-1"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "console_adapter.hpp"
#include "console_adapter_logging.hpp"

#if __has_include(<tier0/logging.h>)

#include <GarrysMod/InterfacePointers.hpp>

Expand Down Expand Up @@ -101,7 +103,7 @@ void console_adapter::queue_dispatcher( )
void console_adapter::Log( const LoggingContext_t *pContext, const tchar *pMessage )
{
std::unique_lock<std::mutex> lock( m_mutex );
if( !m_callback || ( pContext->m_Flags & LCF_DO_NOT_ECHO ) )
if( pContext->m_Flags & LCF_DO_NOT_ECHO )
return;

Color color = pContext->m_Color;
Expand All @@ -125,3 +127,5 @@ void console_adapter::Log( const LoggingContext_t *pContext, const tchar *pMessa
lock.unlock( );
m_condition.notify_all( );
}

#endif
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#if __has_include(<tier0/logging.h>)

#include <mutex>
#include <thread>
#include <atomic>
Expand Down Expand Up @@ -36,10 +38,10 @@ class console_adapter : private ILoggingListener

void set_callback( const std::function<void( const json::value &message )> &callback );

void run_command( [[maybe_unused]] const std::string &command );
void run_command( const std::string &command );

private:
void start( [[maybe_unused]] std::unique_lock<std::mutex> &lock );
void start( std::unique_lock<std::mutex> &lock );

void stop( std::unique_lock<std::mutex> &lock );

Expand All @@ -65,3 +67,5 @@ class console_adapter : private ILoggingListener
#endif

};

#endif
136 changes: 136 additions & 0 deletions source/console_adapter_spew.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
#include "console_adapter_spew.hpp"

#if !__has_include(<tier0/logging.h>)

#include <GarrysMod/InterfacePointers.hpp>

#include <cdll_int.h>
#include <eiface.h>

SpewOutputFunc_t console_adapter::s_original_spew = nullptr;
std::mutex console_adapter::s_mutex;
std::condition_variable console_adapter::s_condition;
std::vector<json::value> console_adapter::s_messages;

console_adapter::console_adapter( ) :
m_thread_stop( true )
{

#if IS_SERVERSIDE

m_engine_server = InterfacePointers::VEngineServer( );

#else

m_engine_client = InterfacePointers::VEngineClient( );

#endif

}

console_adapter::~console_adapter( )
{
set_callback( nullptr );
}

void console_adapter::set_callback( const std::function<void( const json::value &message )> &callback )
{
std::unique_lock<std::mutex> lock( s_mutex );

m_callback = callback;

if( callback )
start( lock );
else
stop( lock );
}

void console_adapter::run_command( const std::string &command )
{

#if IS_SERVERSIDE

m_engine_server->ServerCommand( command.c_str( ) );

#else

m_engine_client->ClientCmd_Unrestricted( command.c_str( ) );

#endif

}

void console_adapter::start( [[maybe_unused]] std::unique_lock<std::mutex> &lock )
{
if( !m_thread_stop )
return;

s_original_spew = GetSpewOutputFunc( );
SpewOutputFunc( &console_adapter::Log );

m_thread_stop = false;
m_thread = std::thread( &console_adapter::queue_dispatcher, this );
}

void console_adapter::stop( std::unique_lock<std::mutex> &lock )
{
if( m_thread_stop )
return;

SpewOutputFunc( s_original_spew );
s_original_spew = nullptr;

s_messages.clear( );

m_thread_stop = true;
lock.unlock( );
s_condition.notify_all( );
m_thread.join( );
}

void console_adapter::queue_dispatcher( )
{
while( !m_thread_stop )
{
std::vector<json::value> messages;
{
std::unique_lock<std::mutex> lock( s_mutex );
s_condition.wait( lock, [this] { return !s_messages.empty( ) || m_thread_stop; } );
if( m_thread_stop )
break;

std::swap( messages, s_messages );
}

for( const json::value &message : messages )
m_callback( message );
}
}

SpewRetval_t console_adapter::Log( SpewType_t spewType, const tchar *pMsg )
{
std::unique_lock<std::mutex> lock( s_mutex );

const Color &color = *GetSpewOutputColor( );

json::object jcolor;
jcolor["r"] = json::value( static_cast<double>( color.r( ) ) );
jcolor["g"] = json::value( static_cast<double>( color.g( ) ) );
jcolor["b"] = json::value( static_cast<double>( color.b( ) ) );
jcolor["a"] = json::value( static_cast<double>( color.a( ) ) );

json::object param;
param["group"] = json::value( GetSpewOutputGroup( ) );
param["severity"] = json::value( static_cast<double>( GetSpewOutputLevel( ) ) );
param["color"] = json::value( jcolor );
param["message"] = json::value( pMsg );

s_messages.emplace_back( std::move( param ) );

lock.unlock( );
s_condition.notify_all( );

return s_original_spew( spewType, pMsg );
}

#endif
74 changes: 74 additions & 0 deletions source/console_adapter_spew.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#pragma once

#if !__has_include(<tier0/logging.h>)

#include <mutex>
#include <thread>
#include <atomic>
#include <functional>
#include <condition_variable>

#include <picojson.h>

#include <tier0/dbg.h>

#if IS_SERVERSIDE

class IVEngineServer;

#else

class IVEngineClient;

#endif

struct LoggingContext_t;

namespace json
{
using namespace ::picojson;
}

// Doesn't support multiple instances.
class console_adapter
{
public:
console_adapter( );

~console_adapter( );

void set_callback( const std::function<void( const json::value &message )> &callback );

void run_command( const std::string &command );

private:
void start( std::unique_lock<std::mutex> &lock );

void stop( std::unique_lock<std::mutex> &lock );

void queue_dispatcher( );

static SpewRetval_t Log( SpewType_t spewType, const tchar *pMsg );

static SpewOutputFunc_t s_original_spew;
static std::mutex s_mutex;
static std::condition_variable s_condition;
static std::vector<json::value> s_messages;

std::thread m_thread;
std::atomic_bool m_thread_stop;
std::function<void( const json::value &message )> m_callback;

#if IS_SERVERSIDE

IVEngineServer *m_engine_server;

#else

IVEngineClient *m_engine_client;

#endif

};

#endif
4 changes: 2 additions & 2 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ namespace rdb

LUA->CreateTable( );

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

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

LUA->Push( -2 ); // push userdata to stack stop
Expand Down

0 comments on commit 3b1368f

Please sign in to comment.