Skip to content
Merged

Lua #232

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Game.Server/BaseEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,7 @@ public void SetParent(BaseEntity? parentEntity, int attachment = -1) {
if (attachment == -1)
attachment = ParentAttachment;

if (!IsPlayer() && Game.Client.GarrysMod.GarrysMod.IsSacredEntity(Classname ?? "")) {
if (!IsPlayer() && Game.Server.GarrysMod.GarrysMod.IsSacredEntity(Classname ?? "")) {
Warning($"Refusing to set parent on {Classname}!\n");
return;
}
Expand Down
12 changes: 6 additions & 6 deletions Game.Server/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ public class CEntityFactoryDictionary : IEntityFactoryDictionary
className = className.SliceNullTerminatedString();
IEntityFactory? factory = FindFactory(className);
#if GMOD_DLL
if (Game.Client.GarrysMod.GarrysMod.g_LuaManager != null) {
if (Game.Server.GarrysMod.GarrysMod.g_LuaManager != null) {
Span<char> lowered = stackalloc char[512];
strcpy(lowered, className);
strlower(lowered);
IServerNetworkable? luaEnt = Game.Client.GarrysMod.GarrysMod.g_LuaManager.CreateEntity(((ReadOnlySpan<char>)lowered).SliceNullTerminatedString(), factory != null);
IServerNetworkable? luaEnt = Game.Server.GarrysMod.GarrysMod.g_LuaManager.CreateEntity(((ReadOnlySpan<char>)lowered).SliceNullTerminatedString(), factory != null);
if (luaEnt != null)
return luaEnt;
}
Expand Down Expand Up @@ -523,8 +523,8 @@ public static void TransmitShakeEvent(BasePlayer player, float localAmplitude, f
public static void PrecacheOther(ReadOnlySpan<char> className, ReadOnlySpan<char> modelName = default) {
bool precaching = s_RemoveImmediateSemaphore == 0;
if (precaching)
Game.Client.GarrysMod.GarrysMod.g_GModPrecaching++;
if (Game.Client.GarrysMod.GarrysMod.g_GModPrecaching > 0x20)
Game.Server.GarrysMod.GarrysMod.g_GModPrecaching++;
if (Game.Server.GarrysMod.GarrysMod.g_GModPrecaching > 0x20)
Error("Something seems to have gone wroong");

BaseEntity? entity = CreateEntityByName(className);
Expand All @@ -540,7 +540,7 @@ public static void PrecacheOther(ReadOnlySpan<char> className, ReadOnlySpan<char
}

if (precaching)
Game.Client.GarrysMod.GarrysMod.g_GModPrecaching--;
Game.Server.GarrysMod.GarrysMod.g_GModPrecaching--;
}

public static void ClientPrintFilter<Filter>(scoped in Filter filter, HudPrint dest, ReadOnlySpan<char> msgName, ReadOnlySpan<char> param1 = default, ReadOnlySpan<char> param2 = default, ReadOnlySpan<char> param3 = default, ReadOnlySpan<char> param4 = default) where Filter : IRecipientFilter {
Expand Down Expand Up @@ -711,7 +711,7 @@ public static int DispatchSpawn(BaseEntity? entity) {
public static void RemoveSafe(BaseEntity? entity) {
if (entity == null)
return;
if (Game.Client.GarrysMod.GarrysMod.IsSacredEntity(entity.Classname ?? "")) {
if (Game.Server.GarrysMod.GarrysMod.IsSacredEntity(entity.Classname ?? "")) {
Warning($"Refusing to delete entity '{entity.Classname ?? ""}'!\n");
return;
}
Expand Down
68 changes: 16 additions & 52 deletions Game.Shared/GarrysMod/GarrysMod.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#if CLIENT_DLL || GAME_DLL
#if CLIENT_DLL
global using static Game.Client.GarrysMod.GarrysModSingletons;
#else
global using static Game.Server.GarrysMod.GarrysModSingletons;
#endif

using Game.Shared;
#if GAME_DLL
using Game.Server.GarrysMod;
#endif

using Microsoft.Extensions.DependencyInjection;

Expand All @@ -27,7 +28,11 @@
using System.Collections.Generic;
using System.Text;

#if CLIENT_DLL
namespace Game.Client.GarrysMod;
#else
namespace Game.Server.GarrysMod;
#endif

public static class GarrysModSingletons
{
Expand Down Expand Up @@ -230,11 +235,7 @@ public static bool Kill() {
g_LuaManager = null;
}
gGM?.Dispose();
#if CLIENT_DLL
LuaTimer.Shutdown();
#else
Game.Server.GarrysMod.LuaTimer.Shutdown();
#endif
return true;
}

Expand Down Expand Up @@ -263,7 +264,7 @@ public static bool Create() {
g_LuaManager.Startup();
#if GAME_DLL
gGM.LoadCurrentlyActiveGamemode();
Game.Server.GarrysMod.GModDataPack.DataPack().BuildSearchPaths();
GModDataPack.DataPack().BuildSearchPaths();
#endif
return true;
}
Expand Down Expand Up @@ -370,15 +371,9 @@ public void Startup() {
Msg("Clientside Lua startup!\n");
enginevgui.UpdateCustomProgressBar(0.95f, "Starting Lua...");
#endif
#if CLIENT_DLL
if (LuaNetworkedVars.g_LuaNetworkedVars != null)
Error("g_LuaNetworkedVars");
LuaNetworkedVars.g_LuaNetworkedVars = new LuaNetworkedVars();
#else
if (Game.Server.GarrysMod.LuaNetworkedVars.g_LuaNetworkedVars != null)
Error("g_LuaNetworkedVars");
Game.Server.GarrysMod.LuaNetworkedVars.g_LuaNetworkedVars = new Game.Server.GarrysMod.LuaNetworkedVars();
#endif
if (g_Lua != null)
Error("CLuaManager::Startup Lua already exsits?\n");

Expand All @@ -400,35 +395,26 @@ public void Startup() {
g_Lua.SetType(0);
#else
g_Lua = get.LuaShared()!.CreateLuaInterface(Realm.Server, false);
g_Lua.Init(Game.Server.GarrysMod.LuaGameCallback.g_LuaCallback, Singleton<ICommandLine>().CheckParm("-withjit"));
g_Lua.Init(LuaGameCallback.g_LuaCallback, Singleton<ICommandLine>().CheckParm("-withjit"));
g_Lua.SetPathID(LuaPathID);
g_Lua.SetType(1);
#endif
g_Lua.Global().SetMember("VERSION", (float)get.Version());
g_Lua.Global().SetMember("VERSIONSTR", get.VersionStr());
g_Lua.Global().SetMember("BRANCH", get.Branch());
#if GAME_DLL
Game.Server.GarrysMod.FileServ.Add("lua/send.txt");
FileServ.Add("lua/send.txt");
#endif
#if CLIENT_DLL
LuaUser.InitLuaLibraries(g_Lua);
LuaClass.InitLuaClasses(g_Lua);
#else
Game.Server.GarrysMod.LuaUser.InitLuaLibraries(g_Lua);
Game.Server.GarrysMod.LuaClass.InitLuaClasses(g_Lua);
#endif
#if CLIENT_DLL
g_Lua.Global().SetMember("SERVER", false);
g_Lua.Global().SetMember("CLIENT", true);
#else
g_Lua.Global().SetMember("SERVER", true);
g_Lua.Global().SetMember("CLIENT", false);
#endif
#if CLIENT_DLL
LuaEntity.MakeLuaNULLEntity();
#else
Game.Server.GarrysMod.LuaEntity.MakeLuaNULLEntity();
#endif
g_Lua.FindAndRunScript("includes/init.lua", true, true, "!UNKNOWN", true);
#if CLIENT_DLL
if (gGM == null)
Expand Down Expand Up @@ -469,7 +455,7 @@ public void SendScriptsInFolder(ReadOnlySpan<char> folder, ReadOnlySpan<char> so
List<LuaFindResult> files = [];
get.LuaShared()!.FindScripts($"{folder}/*.lua", "lsv", files);
foreach (LuaFindResult file in files)
Game.Server.GarrysMod.FileServ.AddCSLuaFile($"{folder}/{file.FileName}", new string(source));
FileServ.AddCSLuaFile($"{folder}/{file.FileName}", new string(source));
}
#endif

Expand All @@ -492,22 +478,12 @@ public void Shutdown() {
GModBase.Shutdown();
#endif
g_LuaID++;
#if CLIENT_DLL
LuaClass.ShutdownLuaClasses(g_Lua!);
#else
Game.Server.GarrysMod.LuaClass.ShutdownLuaClasses(g_Lua!);
#endif
get.LuaShared()!.CloseLuaInterface(g_Lua!);
g_Lua = null;
#if CLIENT_DLL
if (LuaNetworkedVars.g_LuaNetworkedVars == null)
Error("!g_LuaNetworkedVars");
LuaNetworkedVars.g_LuaNetworkedVars = null;
#else
if (Game.Server.GarrysMod.LuaNetworkedVars.g_LuaNetworkedVars == null)
Error("!g_LuaNetworkedVars");
Game.Server.GarrysMod.LuaNetworkedVars.g_LuaNetworkedVars = null;
#endif
}
}

Expand Down Expand Up @@ -613,11 +589,7 @@ public void EntityCreated(BaseEntity? ent) {
if (gGM == null || !gGM.CallWithArgs((int)LUA_POOLEDSTRING.OnEntityCreated))
return;

#if CLIENT_DLL
LuaEntity.Push_Entity(ent);
#else
Game.Server.GarrysMod.LuaEntity.Push_Entity(ent);
#endif
gGM.CallNoReturns(1);
}

Expand Down Expand Up @@ -671,21 +643,13 @@ public bool RenderScene(in ViewSetup view) {
#endif

public void EntityRemoved(BaseEntity ent, bool fullUpdate) {
#if CLIENT_DLL
LuaEntity.RemoveFromRemoveList(ent);
#else
Game.Server.GarrysMod.LuaEntity.RemoveFromRemoveList(ent);
#endif
SoundEnvelopeController.GetController().ShutdownSoundsForEntity(ent.EntIndex());
if (g_Lua == null || gGM == null)
return;

if (gGM.CallWithArgs((int)LUA_POOLEDSTRING.EntityRemoved)) {
#if CLIENT_DLL
LuaEntity.Push_Entity(ent);
#else
Game.Server.GarrysMod.LuaEntity.Push_Entity(ent);
#endif
g_Lua.PushBool(fullUpdate);
gGM.CallNoReturns(2);
}
Expand All @@ -695,7 +659,7 @@ public void EntityRemoved(BaseEntity ent, bool fullUpdate) {
ent.GetLuaTable()?.UnReference();
ent.GetLuaEntity()?.UnReference();
}
Game.Server.GarrysMod.LuaNetworkedVars.g_LuaNetworkedVars?.ClearEntity(ent.GetRefEHandle().Index);
LuaNetworkedVars.g_LuaNetworkedVars?.ClearEntity(ent.GetRefEHandle().Index);
#endif
}

Expand All @@ -708,9 +672,9 @@ public void Think() {
LuaFileLibrary.AsyncCycle();
UGCDownloadManager.Think();
#else
Game.Server.GarrysMod.LuaNetworkedVars.g_LuaNetworkedVars?.Cycle();
Game.Server.GarrysMod.LuaTimer.Cycle();
Game.Server.GarrysMod.LuaFileLibrary.AsyncCycle();
LuaNetworkedVars.g_LuaNetworkedVars?.Cycle();
LuaTimer.Cycle();
LuaFileLibrary.AsyncCycle();
#endif
// HTTP.Cycle();
// ErrorReporter.RunQueue();
Expand Down
6 changes: 6 additions & 0 deletions Game.Shared/GarrysMod/LuaDump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@
using System.Text;
using System.Text.RegularExpressions;

#if CLIENT_DLL
namespace Game.Client.GarrysMod;
#elif MENU_DLL
namespace Game.MenuSystem.GarrysMod;
#else
namespace Game.Server.GarrysMod;
#endif

public static class LuaDump
{
Expand Down
4 changes: 2 additions & 2 deletions Game.Shared/GarrysMod/LuaFileLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ static int File__Read(ILuaInterface lua) {
toRead = length;
else {
#if !MENU_DLL
if (Game.Client.GarrysMod.GarrysMod.lua_strict.GetInt() != 0) {
if (GarrysMod.lua_strict.GetInt() != 0) {
lua.ErrorFromLua($"File:Read() - Invalid requested size {(int)length}\n");
return 0;
}
Expand Down Expand Up @@ -310,7 +310,7 @@ static int File__Write(ILuaInterface lua) {
}

#if !MENU_DLL
if (Game.Client.GarrysMod.GarrysMod.lua_strict.GetInt() != 0)
if (GarrysMod.lua_strict.GetInt() != 0)
#endif
lua.ErrorFromLua($"File:Write() - Invalid write size {data.Length}\n");
return 0;
Expand Down
10 changes: 5 additions & 5 deletions Game.Shared/GarrysMod/LuaGamemode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void Dispose() {
}

public void LoadGamemode(string name, bool reload) {
if (Game.Client.GarrysMod.GarrysMod.g_LuaManager == null)
if (GarrysMod.g_LuaManager == null)
Error("Tried to LoadGamemode with NULL g_LuaManager");
if (g_Lua == null)
Error("Tried to LoadGamemode with NULL g_Lua");
Expand All @@ -59,16 +59,16 @@ public void LoadGamemode(string name, bool reload) {
foreach (ILegacyAddons.Information addon in filesystem.LegacyAddons().GetList()) {
string path = addon.GamemodesPath + "/" + folder + "/entities";
if (!string.IsNullOrEmpty(addon.GamemodesPath) && filesystem.IsDirectory(path, "MOD"))
get.LuaShared()!.MountLuaAdd(path, Game.Client.GarrysMod.GarrysMod.LuaPathID);
get.LuaShared()!.MountLuaAdd(path, GarrysMod.LuaPathID);
}
get.LuaShared()!.MountLuaAdd("workshop/" + folder + "/entities", Game.Client.GarrysMod.GarrysMod.LuaPathID);
get.LuaShared()!.MountLuaAdd("workshop/" + folder + "/entities", GarrysMod.LuaPathID);
get.LuaShared()!.MountLuaAdd(folder + "/entities", g_Lua.GetPathID());
}

LuaTable GM = new("GM", 0);
GM.SetMember("Folder", folder);
GM.SetMember("FolderName", name);
if (!Game.Client.GarrysMod.GarrysMod.g_LuaManager.RunScript(init, Game.Client.GarrysMod.GarrysMod.LuaPathID, true, "!GM"))
if (!GarrysMod.g_LuaManager.RunScript(init, GarrysMod.LuaPathID, true, "!GM"))
g_Lua.ErrorFromLua($"Couldn't Load Init Script: '{init}'\n");

string derived = GM.GetMemberStr("DerivedFrom", "base") ?? "";
Expand Down Expand Up @@ -104,7 +104,7 @@ public void LoadGamemode(string name, bool reload) {

public bool IsValidGamemode(string name) {
string init = name + "/gamemode/" + "init.lua";
return Game.Client.GarrysMod.GarrysMod.g_LuaManager!.ScriptExists(init, Game.Client.GarrysMod.GarrysMod.LuaPathID);
return GarrysMod.g_LuaManager!.ScriptExists(init, GarrysMod.LuaPathID);
}

public void DeriveGamemode(string name) {
Expand Down
2 changes: 1 addition & 1 deletion Game.Shared/GarrysMod/LuaNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ static void ValidateDataSize(ILuaInterface lua, int bits) {
if (g_NetIncoming == null)
return;

if (Game.Client.GarrysMod.GarrysMod.lua_strict.GetInt() != 0 && bits > g_NetIncoming.BitsLeft)
if (GarrysMod.lua_strict.GetInt() != 0 && bits > g_NetIncoming.BitsLeft)
lua.ErrorFromLua($"Trying to read more data ({bits} bit) than the net message has left ({g_NetIncoming.BitsLeft} bits)!\n");
}

Expand Down
2 changes: 1 addition & 1 deletion Game.Shared/GarrysMod/LuaPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ static int Player__SendLua(ILuaInterface lua) {
if (stristr(code, "while true do end").IsEmpty) {
Game.Server.RecipientFilter filter = new Game.Server.SingleUserRecipientFilter(player);
filter.MakeReliable();
Game.Client.GarrysMod.GarrysMod.BroadcastLua(filter, code);
GarrysMod.BroadcastLua(filter, code);
}
return 0;
}
Expand Down
4 changes: 4 additions & 0 deletions Game.Shared/GarrysMod/LuaSENTManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
using Source.Common.GarrysMod;
using Source.Common.GarrysMod.Lua;

#if CLIENT_DLL
using static Game.Client.GarrysMod.GarrysMod;
#else
using static Game.Server.GarrysMod.GarrysMod;
#endif

#if CLIENT_DLL
namespace Game.Client.GarrysMod;
Expand Down
4 changes: 4 additions & 0 deletions Game.Shared/GarrysMod/LuaSWEPManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
using Source.Common.GarrysMod;
using Source.Common.GarrysMod.Lua;

#if CLIENT_DLL
using static Game.Client.GarrysMod.GarrysMod;
#else
using static Game.Server.GarrysMod.GarrysMod;
#endif

#if CLIENT_DLL
namespace Game.Client.GarrysMod;
Expand Down
2 changes: 1 addition & 1 deletion Game.Shared/GarrysMod/LuaTimer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static partial class LuaTimer
[LuaFunction]
static int Check(ILuaInterface lua) {
#if !MENU_DLL
if (Game.Client.GarrysMod.GarrysMod.lua_strict.GetInt() != 0)
if (GarrysMod.lua_strict.GetInt() != 0)
lua.ErrorFromLua("timer.Check is deprecated and does nothing. Do not use it.");
#endif
return 0;
Expand Down
4 changes: 4 additions & 0 deletions Game.Shared/GarrysMod/ScriptedEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
using Source.Common;
using Source.Common.GarrysMod.Lua;

#if CLIENT_DLL
using static Game.Client.GarrysMod.GarrysMod;
#else
using static Game.Server.GarrysMod.GarrysMod;
#endif

#if CLIENT_DLL
namespace Game.Client.GarrysMod;
Expand Down
Loading
Loading