Skip to content

Commit 82abf1b

Browse files
committed
Use helpers_extended to get function pointers and interfaces
Update license Clean up headers and includes Bump revision number
1 parent f9b07ce commit 82abf1b

16 files changed

+95
-455
lines changed

license.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ gm_sourcenet
22
A module for Garry's Mod that provides interfaces to many systems
33
of VALVe's engine.
44
-----------------------------------------------------------------------
5-
Copyright (c) 2015-2019, Daniel Almeida
5+
Copyright (c) 2015-2020, Daniel Almeida
66
All rights reserved.
77

88
Redistribution and use in source and binary forms, with or without

premake5.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ newoption({
66

77
local gmcommon = assert(_OPTIONS.gmcommon or os.getenv("GARRYSMOD_COMMON"),
88
"you didn't provide a path to your garrysmod_common (https://github.com/danielga/garrysmod_common) directory")
9-
include(path.join(gmcommon, "generator.v2.lua"))
9+
include(gmcommon .. "/generator.v2.lua")
1010

1111
CreateWorkspace({name = "sourcenet", abi_compatible = true})
1212
CreateProject({serverside = true})
1313
IncludeLuaShared()
14+
IncludeHelpersExtended()
1415
IncludeSDKCommon()
1516
IncludeSDKTier0()
1617
IncludeSDKTier1()
@@ -21,9 +22,9 @@ CreateWorkspace({name = "sourcenet", abi_compatible = true})
2122

2223
CreateProject({serverside = false})
2324
IncludeLuaShared()
25+
IncludeHelpersExtended()
2426
IncludeSDKCommon()
2527
IncludeSDKTier0()
2628
IncludeSDKTier1()
2729
IncludeScanning()
2830
IncludeDetouring()
29-

source/gameeventmanager.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "sn_bf_write.hpp"
66

77
#include <GarrysMod/Lua/AutoReference.h>
8+
89
#include <igameevents.h>
910

1011
namespace GameEventManager

source/hooks.cpp

Lines changed: 32 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
#include "sn_bf_write.hpp"
77
#include "net.hpp"
88

9-
#include <inetmsghandler.h>
10-
#include <cdll_int.h>
11-
#include <scanning/symbolfinder.hpp>
129
#include <GarrysMod/Lua/Helpers.hpp>
13-
#include <detouring/classproxy.hpp>
10+
#include <GarrysMod/FunctionPointers.hpp>
1411
#include <Platform.hpp>
1512

13+
#include <detouring/classproxy.hpp>
14+
15+
#include <inetmsghandler.h>
16+
#include <cdll_int.h>
17+
1618
namespace Hooks
1719
{
1820

@@ -64,17 +66,6 @@ namespace Hooks
6466
class CNetChanProxy : public Detouring::ClassProxy<CNetChan, CNetChanProxy>
6567
{
6668
private:
67-
68-
#if defined SYSTEM_WINDOWS
69-
70-
typedef bool( __thiscall *ProcessMessages_t )( CNetChan *netchan, bf_read &buf );
71-
72-
#else
73-
74-
typedef bool( *ProcessMessages_t )( CNetChan *netchan, bf_read &buf );
75-
76-
#endif
77-
7869
typedef CNetChan TargetClass;
7970
typedef CNetChanProxy SubstituteClass;
8071

@@ -83,25 +74,12 @@ namespace Hooks
8374
return NetChannel::Get( LUA, index );
8475
}
8576

86-
static const char ProcessMessages_sig[];
87-
static const size_t ProcessMessages_siglen;
88-
89-
static ProcessMessages_t ProcessMessages_original;
77+
static FunctionPointers::CNetChan_ProcessMessages_t ProcessMessages_original;
9078

9179
public:
9280
static void Initialize( GarrysMod::Lua::ILuaBase *LUA )
9381
{
94-
{
95-
SymbolFinder symfinder;
96-
97-
ProcessMessages_original =
98-
reinterpret_cast<ProcessMessages_t>( symfinder.Resolve(
99-
global::engine_loader.GetModuleLoader( ).GetModule( ),
100-
ProcessMessages_sig,
101-
ProcessMessages_siglen
102-
) );
103-
}
104-
82+
ProcessMessages_original = FunctionPointers::CNetChan_ProcessMessages( );
10583
if( ProcessMessages_original == nullptr )
10684
LUA->ThrowError( "failed to locate CNetChan::ProcessMessages" );
10785
}
@@ -210,15 +188,19 @@ namespace Hooks
210188
{
211189
CNetChan *netchan = This( );
212190

213-
static uint8_t data[100000] = { 0 };
214191
if( !buf.IsOverflowed( ) )
215192
{
216-
memcpy( data, buf.GetBasePointer( ), buf.GetNumBytesRead( ) );
193+
static uint8_t data[100000] = { 0 };
217194

218-
int32_t bitsread = buf.GetNumBitsRead( );
195+
const int32_t bytesread = buf.GetNumBytesRead( );
196+
if( bytesread > 0 )
197+
std::memcpy( data, buf.GetBasePointer( ), bytesread );
198+
199+
const int32_t bitsread = buf.GetNumBitsRead( );
219200
bf_write write( data, sizeof( data ) );
220201
write.SeekToBit( bitsread );
221202

203+
bool handled = false;
222204
HOOK_INIT( "PreProcessMessages" );
223205
HOOK_PUSH( NetChannel::Push( LUA, netchan ) );
224206
HOOK_PUSH( bf_read **reader = sn_bf_read::Push( LUA, &buf ) );
@@ -232,18 +214,27 @@ namespace Hooks
232214
) );
233215
}
234216

235-
HOOK_CALL( 0 );
217+
if( HOOK_CALL( 1 ) )
218+
{
219+
if( LUA->IsType( -1, GarrysMod::Lua::Type::BOOL ) )
220+
handled = LUA->GetBool( -1 );
221+
222+
LUA->Pop( 1 );
223+
}
236224

237225
*reader = nullptr;
238226
*writer = nullptr;
239227
HOOK_END( );
240228

241-
buf.StartReading(
242-
write.GetBasePointer( ),
243-
write.GetNumBytesWritten( ),
244-
bitsread,
245-
write.GetNumBitsWritten( )
246-
);
229+
if( handled )
230+
buf.StartReading(
231+
write.GetBasePointer( ),
232+
write.GetNumBytesWritten( ),
233+
bitsread,
234+
write.GetNumBitsWritten( )
235+
);
236+
else
237+
buf.Seek( bitsread );
247238
}
248239

249240
return Call<bool, bf_read &>( ProcessMessages_original, buf );
@@ -262,28 +253,7 @@ namespace Hooks
262253

263254
CNetChanProxy CNetChanProxy::Singleton;
264255

265-
#if defined SYSTEM_WINDOWS
266-
267-
const char CNetChanProxy::ProcessMessages_sig[] = "\x55\x8B\xEC\x83\xEC\x2C\xF7\x05";
268-
const size_t CNetChanProxy::ProcessMessages_siglen =
269-
sizeof( CNetChanProxy::ProcessMessages_sig ) - 1;
270-
271-
#elif ( defined SYSTEM_LINUX && defined SOURCENET_SERVER ) || defined SYSTEM_MACOSX
272-
273-
const char CNetChanProxy::ProcessMessages_sig[] =
274-
"@_ZN8CNetChan15ProcessMessagesER7bf_read";
275-
const size_t CNetChanProxy::ProcessMessages_siglen = 0;
276-
277-
#elif defined SYSTEM_LINUX && defined SOURCENET_CLIENT
278-
279-
const char CNetChanProxy::ProcessMessages_sig[] =
280-
"\x55\x89\xE5\x57\x56\x53\x83\xEC\x6C\x8B\x3D\x2A\x2A\x2A\x2A\x8B";
281-
const size_t CNetChanProxy::ProcessMessages_siglen =
282-
sizeof( CNetChanProxy::ProcessMessages_sig ) - 1;
283-
284-
#endif
285-
286-
CNetChanProxy::ProcessMessages_t CNetChanProxy::ProcessMessages_original = nullptr;
256+
FunctionPointers::CNetChan_ProcessMessages_t CNetChanProxy::ProcessMessages_original = nullptr;
287257

288258
class INetChannelHandlerProxy : public Detouring::ClassProxy<INetChannelHandler, INetChannelHandlerProxy>
289259
{

source/main.cpp

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -23,42 +23,15 @@
2323

2424
#endif
2525

26-
#include <scanning/symbolfinder.hpp>
26+
#include <GarrysMod/InterfacePointers.hpp>
27+
2728
#include <interface.h>
2829
#include <eiface.h>
2930
#include <cdll_int.h>
3031
#include <iserver.h>
31-
#include <Platform.hpp>
3232

3333
namespace global
3434
{
35-
36-
#if defined SYSTEM_WINDOWS
37-
38-
static const char IServer_sig[] = "\x2A\x2A\x2A\x2A\xE8\x2A\x2A\x2A\x2A\xD8\x6D\x24\x83\x4D\xEC\x10";
39-
static const size_t IServer_siglen = sizeof( IServer_sig ) - 1;
40-
41-
#elif defined SYSTEM_LINUX
42-
43-
#if defined SOURCENET_SERVER
44-
45-
static const char IServer_sig[] = "@sv";
46-
static const size_t IServer_siglen = 0;
47-
48-
#elif defined SOURCENET_CLIENT
49-
50-
static const char IServer_sig[] = "\x2A\x2A\x2A\x2A\xE8\x2A\x2A\x2A\x2A\xC7\x85\x68\xFF\xFF\xFF\x68";
51-
static const size_t IServer_siglen = sizeof( IServer_sig ) - 1;
52-
53-
#endif
54-
55-
#elif defined SYSTEM_MACOSX
56-
57-
static const char IServer_sig[] = "\x2A\x2A\x2A\x2A\x8B\x08\x89\x04\x24\xFF\x51\x28\xF3\x0F\x10\x45";
58-
static const size_t IServer_siglen = sizeof( IServer_sig ) - 1;
59-
60-
#endif
61-
6235
static bool loaded = false;
6336

6437
const char *tostring_format = "%s: %p";
@@ -116,11 +89,11 @@ namespace global
11689
{
11790
LUA->CreateTable( );
11891

119-
LUA->PushString( "sourcenet 1.1.8" );
92+
LUA->PushString( "sourcenet 1.1.9" );
12093
LUA->SetField( -2, "Version" );
12194

12295
// version num follows LuaJIT style, xxyyzz
123-
LUA->PushNumber( 10108 );
96+
LUA->PushNumber( 10109 );
12497
LUA->SetField( -2, "VersionNum" );
12598

12699
LUA->SetField( GarrysMod::Lua::INDEX_GLOBAL, "sourcenet" );
@@ -142,33 +115,19 @@ GMOD_MODULE_OPEN( )
142115
{
143116
global::lua = LUA;
144117

145-
global::engine_server = global::engine_loader.GetInterface<IVEngineServer>( INTERFACEVERSION_VENGINESERVER );
118+
global::engine_server = InterfacePointers::VEngineServer( );
146119
if( global::engine_server == nullptr )
147120
LUA->ThrowError( "failed to retrieve server engine interface" );
148121

149122
global::is_dedicated = global::engine_server->IsDedicatedServer( );
150123
if( !global::is_dedicated )
151124
{
152-
global::engine_client = global::engine_loader.GetInterface<IVEngineClient>( VENGINE_CLIENT_INTERFACE_VERSION );
125+
global::engine_client = InterfacePointers::VEngineClient( );
153126
if( global::engine_client == nullptr )
154127
LUA->ThrowError( "failed to retrieve client engine interface" );
155128
}
156129

157-
IServer **pserver = nullptr;
158-
{
159-
SymbolFinder symfinder;
160-
161-
pserver = reinterpret_cast<IServer **>( symfinder.Resolve(
162-
global::engine_loader.GetModuleLoader( ).GetModule( ),
163-
global::IServer_sig,
164-
global::IServer_siglen
165-
) );
166-
}
167-
168-
if( pserver == nullptr )
169-
LUA->ThrowError( "failed to locate IServer pointer" );
170-
171-
global::server = *pserver;
130+
global::server = InterfacePointers::Server( );
172131
if( global::server == nullptr )
173132
LUA->ThrowError( "failed to locate IServer" );
174133

source/main.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#pragma once
22

33
#include <GarrysMod/Lua/Interface.h>
4-
#include <lua.hpp>
5-
#include <stdint.h>
6-
#include <string>
74
#include <GarrysMod/FactoryLoader.hpp>
5+
#include <lua.hpp>
86
#include <Platform.hpp>
7+
8+
#include <cstdint>
9+
#include <string>
910
#include <limits>
1011

1112
class IVEngineServer;

0 commit comments

Comments
 (0)