Skip to content

Commit

Permalink
Added BroadcastLua + Prepared everything for the next few changes
Browse files Browse the repository at this point in the history
  • Loading branch information
RaphaelIT7 committed May 14, 2024
1 parent bc177ae commit cb50108
Show file tree
Hide file tree
Showing 13 changed files with 4,210 additions and 62 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ Files:

## Some notes

### Entities
We should block entity creation while `PackEntities_Normal` is running because else we might cause engine errors.

### NW
For the NW System I'm going to need CLuaNetworkedVars.
CLuaNetworkedVars::FindEntityVar -- Returns a LuaNetworkedVar_t. GetNW* and GetGlobal functions are gonna require this.
Expand All @@ -347,4 +350,15 @@ Als going to need LuaNetworkedVarEnts_t and going to recreate BuildNetworkVarTab
This is going to be pain.
Going to need g_pServerWorldTable, SetDataTableVar, IGMODDataTable, CGMODVariant.
Already got: [IGMODDataTable](https://github.com/danielga/sourcesdk-minimal/blob/1cacb57cd36ee5b77c970e91fff374046aa8574d/public/GarrysMod/IGMODDataTable.h#L5)
ToDo: Find out what the CGMODVariant is.
ToDo: Find out what the CGMODVariant is.
NOTE: Found out.

## Other Stuff

Gmod umsg names:
- "LuaCmd"
- "SWEPCmd"
- "AmmoPickup"
- "WeaponPickup"
- "BreakModel"
- "CheapBreakModel"
4 changes: 3 additions & 1 deletion source/detours.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ typedef void* (*TGetAmmoDef)();
extern TGetAmmoDef func_GetAmmoDef;
const Symbol GetAmmoDefSym = Symbol::FromName("_Z10GetAmmoDefv");

const Symbol CGameRules_Sym = Symbol::FromName("g_pGameRules");

/*
CLuaGameCallback stuff
*/
Expand Down Expand Up @@ -243,7 +245,7 @@ static inline T* ResolveSymbols(
inline bool CheckValue(const char* msg, const char* name, bool ret)
{
if (!ret) {
Msg("[vprof] Failed to %s %s!\n", msg, name);
Msg("[LuaThreaded] Failed to %s %s!\n", msg, name);
return false;
}

Expand Down
22 changes: 20 additions & 2 deletions source/library_Global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "lua_threaded.h"
#include <sstream>
#include "convar.h"
#include <recipientfilter.h>

LUA_FUNCTION(include)
{
Expand Down Expand Up @@ -318,14 +319,14 @@ LUA_FUNCTION(Global_Msg)

LUA_FUNCTION(CurTime)
{
LUA->PushNumber(gpGlobal->curtime);
LUA->PushNumber(gpGlobals->curtime);

return 1;
}

LUA_FUNCTION(RealTime)
{
LUA->PushNumber(gpGlobal->realtime);
LUA->PushNumber(gpGlobals->realtime);

return 1;
}
Expand All @@ -338,6 +339,22 @@ LUA_FUNCTION(RunConsoleCommand) // ToDo: Finish this. This is not how it should
return 1;
}

LUA_FUNCTION(BroadcastLua)
{
const char* lua = LUA->CheckString(1);

// Everything below is done inside the BroadcastLua(const char*) function in Gmod.
CRecipientFilter filter;
filter.AddAllPlayers();
filter.MakeReliable();

UserMessageBegin(filter, "LuaCmd");
MessageWriteString(lua);
MessageEnd();

return 1;
}

void InitGlobal(GarrysMod::Lua::ILuaInterface* LUA)
{
LUA->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB);
Expand All @@ -350,6 +367,7 @@ void InitGlobal(GarrysMod::Lua::ILuaInterface* LUA)
Add_Func(LUA, RunConsoleCommand, "RunConsoleCommand");
Add_Func(LUA, GetConVar_Internal, "GetConVar_Internal");
//Add_Func(LUA, CreateConVar, "CreateConVar");
Add_Func(LUA, BroadcastLua, "BroadcastLua");

Add_Func(LUA, CurTime, "CurTime");
Add_Func(LUA, RealTime, "RealTime");
Expand Down
43 changes: 3 additions & 40 deletions source/library_engine.cpp
Original file line number Diff line number Diff line change
@@ -1,34 +1,7 @@
#include <GarrysMod/InterfacePointers.hpp>
#include "lua_threaded.h"
#include <icommandline.h>
#include <eiface.h>

class IPlayerInfo;
class edict_t;
static const char playerinfomanager_name[] = "PlayerInfoManager002";
class IPlayerInfoManager
{
public:
virtual IPlayerInfo *GetPlayerInfo( edict_t *pEdict ) = 0;
virtual CGlobalVars *GetGlobalVars( ) = 0;
};

CGlobalVars* GlobalVars()
{
static CGlobalVars *iface_pointer = nullptr;
if (iface_pointer == nullptr)
{
SourceSDK::FactoryLoader server_loader("server");
auto player_info_manager = server_loader.GetInterface<IPlayerInfoManager>(
playerinfomanager_name
);
if (player_info_manager != nullptr)
iface_pointer = player_info_manager->GetGlobalVars();
}

return iface_pointer;
}

LUA_FUNCTION(engine_GetAddons)
{
PushValue(LUA, GMOD->addons);
Expand Down Expand Up @@ -66,21 +39,21 @@ LUA_FUNCTION(engine_ActiveGamemode)

LUA_FUNCTION(engine_TickInterval)
{
LUA->PushNumber(gpGlobal->interval_per_tick);
LUA->PushNumber(gpGlobals->interval_per_tick);

return 1;
}

LUA_FUNCTION(engine_AbsoluteFrameTime)
{
LUA->PushNumber(gpGlobal->absoluteframetime);
LUA->PushNumber(gpGlobals->absoluteframetime);

return 1;
}

LUA_FUNCTION(engine_TickCount)
{
LUA->PushNumber(gpGlobal->tickcount);
LUA->PushNumber(gpGlobals->tickcount);

return 1;
}
Expand Down Expand Up @@ -190,16 +163,6 @@ void UpdateEngine(GarrysMod::Lua::ILuaInterface* LUA) // We need to get all of t

void InitEngine(GarrysMod::Lua::ILuaInterface* LUA)
{
if (engine == nullptr)
{
engine = InterfacePointers::VEngineServer();
}

if (gpGlobal == nullptr)
{
gpGlobal = GlobalVars();
}

LUA->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB);
LUA->CreateTable();
Add_Func(LUA, engine_GetAddons, "GetAddons");
Expand Down
10 changes: 5 additions & 5 deletions source/library_game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ LUA_FUNCTION(game_GetIPAddress)

LUA_FUNCTION(game_GetMap)
{
LUA->PushString(gpGlobal->mapname.ToCStr());
LUA->PushString(gpGlobals->mapname.ToCStr());

return 1;
}
Expand All @@ -251,7 +251,7 @@ LUA_FUNCTION(game_GetMapNext)

LUA_FUNCTION(game_GetMapVersion)
{
LUA->PushNumber(gpGlobal->mapversion);
LUA->PushNumber(gpGlobals->mapversion);

return 1;
}
Expand Down Expand Up @@ -300,7 +300,7 @@ LUA_FUNCTION(game_LoadNextMap)

LUA_FUNCTION(game_MapLoadType)
{
switch(gpGlobal->eLoadType)
switch(gpGlobals->eLoadType)
{
case MapLoad_NewGame:
LUA->PushString("newgame");
Expand All @@ -323,7 +323,7 @@ LUA_FUNCTION(game_MapLoadType)

LUA_FUNCTION(game_MaxPlayers)
{
LUA->PushNumber(gpGlobal->maxClients);
LUA->PushNumber(gpGlobals->maxClients);

return 1;
}
Expand Down Expand Up @@ -373,7 +373,7 @@ LUA_FUNCTION(game_SetTimeScale)

LUA_FUNCTION(game_SinglePlayer)
{
LUA->PushBool(gpGlobal->maxClients == 1);
LUA->PushBool(gpGlobals->maxClients == 1);

return 1;
}
Expand Down
11 changes: 1 addition & 10 deletions source/library_resource.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#include "networkstringtabledefs.h"
#include "lua_threaded.h"

SourceSDK::ModuleLoader server_loader("server");

typedef void (*AddResource)(const char*, bool);
AddResource func_AddResource;
const Symbol AddResourceSym = Symbol::FromName("_ZL11AddResourcePKcb");
static SourceSDK::ModuleLoader server_loader("server");

#define ressource_workaround

INetworkStringTableContainer* networkstringtables;
LUA_FUNCTION(resource_AddFile) // ToDo
{
const char* file = LUA->CheckString(1);
Expand Down Expand Up @@ -77,13 +75,6 @@ LUA_FUNCTION(resource_AddWorkshop)

void InitResource(GarrysMod::Lua::ILuaInterface* LUA)
{
if (networkstringtables == nullptr) {
SourceSDK::FactoryLoader engine_loader("engine");
networkstringtables = (INetworkStringTableContainer*)engine_loader.GetFactory()(INTERFACENAME_NETWORKSTRINGTABLESERVER, nullptr);
if (networkstringtables == nullptr)
LUA->ThrowError("unable to initialize INetworkStringTableContainer");
}

func_AddResource = (AddResource)GetFunction(server_loader, "AddResource", AddResourceSym);

LUA->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB);
Expand Down
3 changes: 2 additions & 1 deletion source/lua_threaded.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ GMOD_MODULE_OPEN()

GarrysMod::Lua::ILuaInterface* LLUA = (GarrysMod::Lua::ILuaInterface*)LUA;

InitInterfaces();

Symbols_Init();

InitLuaThreaded(LLUA);
Expand All @@ -61,7 +63,6 @@ GMOD_MODULE_OPEN()

InitEnums(LLUA);

filesystem = InterfacePointers::FileSystem();
UpdateEngine(LLUA);

LUA->PushSpecial(GarrysMod::Lua::SPECIAL_GLOB);
Expand Down
62 changes: 61 additions & 1 deletion source/lua_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <GarrysMod/Lua/Interface.h>
#include "CLuaGameCallback.h"
#include <usermessages.h>
#include "lua_threaded.h"

#ifdef SYSTEM_WINDOWS
#include <GarrysMod/Lua/LuaShared.h>
#include <GarrysMod/InterfacePointers.hpp>

static SourceSDK::FactoryLoader luashared_loader("lua_shared");
GarrysMod::Lua::ILuaInterface* Win_CreateInterface() {
Expand All @@ -17,9 +19,67 @@ GarrysMod::Lua::ILuaInterface* Win_CreateInterface() {
}
#endif

class IPlayerInfo;
class edict_t;
static const char playerinfomanager_name[] = "PlayerInfoManager002";
class IPlayerInfoManager
{
public:
virtual IPlayerInfo *GetPlayerInfo( edict_t *pEdict ) = 0;
virtual CGlobalVars *GetGlobalVars( ) = 0;
};

CGlobalVars* GlobalVars()
{
static CGlobalVars *iface_pointer = nullptr;
if (iface_pointer == nullptr)
{
SourceSDK::FactoryLoader server_loader("server");
auto player_info_manager = server_loader.GetInterface<IPlayerInfoManager>(
playerinfomanager_name
);
if (player_info_manager != nullptr)
iface_pointer = player_info_manager->GetGlobalVars();
}

return iface_pointer;
}

IFileSystem* filesystem;
CGlobalVars* gpGlobal;
CGlobalVars* gpGlobals;
IVEngineServer* engine;
INetworkStringTableContainer* networkstringtables;

static SourceSDK::FactoryLoader engine_loader("engine");
static SourceSDK::FactoryLoader server_loader("server");
void InitInterfaces()
{
if (filesystem == nullptr)
{
filesystem = InterfacePointers::FileSystem();
}

if (engine == nullptr)
{
engine = InterfacePointers::VEngineServer();
}

if (gpGlobals == nullptr)
{
gpGlobals = GlobalVars();
}

if (networkstringtables == nullptr) {
networkstringtables = (INetworkStringTableContainer*)engine_loader.GetFactory()(INTERFACENAME_NETWORKSTRINGTABLESERVER, nullptr);
}

if (g_pGameRules == nullptr)
{
g_pGameRules = ResolveSymbol<CGameRules>(
server_loader, CGameRules_Sym
);
}
}

int interfaces_count = 0;
std::unordered_map<double, ILuaThread*> interfaces;
Expand Down
7 changes: 6 additions & 1 deletion source/lua_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ struct lua_State
#endif
#endif

#include "networkstringtabledefs.h"
#include "GameEventListener.h"
#include <unordered_map>
#include "ILuaConVars.h"
#include <gamerules.h>
#include "detours.h"
#include <setjmp.h>
#include <eiface.h>
Expand Down Expand Up @@ -295,7 +297,10 @@ extern GarrysMod::Lua::ILuaInterface* CreateInterface();
extern void ShutdownInterface(ILuaThread*);

extern std::string ToPath(std::string);

extern void InitInterfaces();
extern IFileSystem* filesystem;
extern CGlobalVars* gpGlobal;
extern CGlobalVars* gpGlobals;
extern IVEngineServer* engine;
extern INetworkStringTableContainer* networkstringtables;
#endif
Loading

0 comments on commit cb50108

Please sign in to comment.