From 7d9f4e5a8424d568077bc1de4614b2cdceda8c6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Fri, 9 Oct 2026 18:33:21 +0300 Subject: [PATCH] Add -nolua that forces an empty dummy implementation --- Game.Shared/GarrysMod/GarrysMod.cs | 11 + Source.Common/GarrysMod/Lua/LuaNull.cs | 298 +++++++++++++++++++++++++ 2 files changed, 309 insertions(+) create mode 100644 Source.Common/GarrysMod/Lua/LuaNull.cs diff --git a/Game.Shared/GarrysMod/GarrysMod.cs b/Game.Shared/GarrysMod/GarrysMod.cs index ebdcf2c89..57267b3a3 100644 --- a/Game.Shared/GarrysMod/GarrysMod.cs +++ b/Game.Shared/GarrysMod/GarrysMod.cs @@ -279,6 +279,17 @@ public void Startup() { if (g_Lua != null) Error("CLuaManager::Startup Lua already exsits?\n"); + if (commandLine.FindParm("-nolua") != 0) { + g_Lua = new Source.Common.GarrysMod.Lua.LuaNullInterface(); + g_Lua.SetPathID(LuaPathID); +#if CLIENT_DLL + g_Lua.SetType(0); +#else + g_Lua.SetType(1); +#endif + return; + } + #if CLIENT_DLL g_Lua = get.LuaShared()!.CreateLuaInterface(Realm.Client, false); g_Lua.Init(LuaGameCallback.g_LuaCallback, Singleton().CheckParm("-withjit")); diff --git a/Source.Common/GarrysMod/Lua/LuaNull.cs b/Source.Common/GarrysMod/Lua/LuaNull.cs new file mode 100644 index 000000000..a2766b6be --- /dev/null +++ b/Source.Common/GarrysMod/Lua/LuaNull.cs @@ -0,0 +1,298 @@ +using Source.Common.Commands; +using Source.Common.Mathematics; +using Source.Common.Physics; + +using System.Diagnostics.CodeAnalysis; +using System.Numerics; +using System.Text; + +namespace Source.Common.GarrysMod.Lua; + +public sealed class LuaNullObject : ILuaObject +{ + public static readonly LuaNullObject Instance = new(); + + public void Set(ILuaObject? obj) { } + public void SetFromStack(int i) { } + public void UnReference() { } + + public new LuaType GetType() => LuaType.Nil; + public string? GetString() => null; + public float GetFloat() => 0; + public int GetInt() => 0; + public nint GetUserData() => 0; + + public void SetMember(ReadOnlySpan name) { } + public void SetMember(ReadOnlySpan name, ILuaObject? obj) { } + public void SetMember(ReadOnlySpan name, float val) { } + public void SetMember(ReadOnlySpan name, bool val) { } + public void SetMember(ReadOnlySpan name, ReadOnlySpan val) { } + public void SetMember(ReadOnlySpan name, CFunc f) { } + public unsafe void SetMember(ReadOnlySpan name, delegate* unmanaged[Cdecl] f) { } + + public bool GetMemberBool(ReadOnlySpan name, bool b = true) => b; + public int GetMemberInt(ReadOnlySpan name, int i = 0) => i; + public float GetMemberFloat(ReadOnlySpan name, float f = 0.0f) => f; + public string? GetMemberStr(ReadOnlySpan name, string? s = "") => s; + public nint GetMemberUserData_DontUseMe(ReadOnlySpan name, nint u = 0) => u; + public nint GetMemberUserData_DontUseMe(float name, nint u = 0) => u; + public void GetMember(ReadOnlySpan name, ILuaObject obj) { } + public void GetMember(ILuaObject key, ILuaObject obj) { } + + public void SetMetaTable(ILuaObject obj) { } + public void SetUserData(nint obj) { } + + public void Push() { } + + public bool isNil() => true; + public bool isTable() => false; + public bool isString() => false; + public bool isNumber() => false; + public bool isFunction() => false; + public bool isUserData() => false; + + public void GetMember(float key, ILuaObject obj) { } + + public void SetMember(float key) { } + public void SetMember(float key, ILuaObject? obj) { } + public void SetMember(float key, float val) { } + public void SetMember(float key, bool val) { } + public void SetMember(float key, ReadOnlySpan val) { } + public void SetMember(float key, CFunc f) { } + + public string? GetMemberStr(float name, string? s = "") => s; + + public void SetMember(ILuaObject key, ILuaObject? value) { } + public bool GetBool() => false; + + public bool PushMemberFast(int stackPos) => false; + public void SetMemberFast(int key, int value) { } + + public void SetFloat(float val) { } + public void SetString(ReadOnlySpan val) { } + + public double GetDouble() => 0; + + public void SetMember_FixKey(ReadOnlySpan key, float val) { } + public void SetMember_FixKey(ReadOnlySpan key, ReadOnlySpan val) { } + public void SetMember_FixKey(ReadOnlySpan key, ILuaObject? val) { } + public void SetMember_FixKey(ReadOnlySpan key, double val) { } + public void SetMember_FixKey(ReadOnlySpan key, int val) { } + + public bool isBool() => false; + + public void SetMemberDouble(scoped ReadOnlySpan name, double val) { } + + public void SetMemberNil(ReadOnlySpan name) { } + public void SetMemberNil(float key) { } + + public void Init() { } + + public void SetFromGlobal(ReadOnlySpan name) { } + + public string? GetStringLen(out uint len) { + len = 0; + return null; + } + + public uint GetMemberUInt(ReadOnlySpan name, uint def) => def; + + public void SetMember(ReadOnlySpan name, ulong val) { } + public void SetMember(ReadOnlySpan name, int val) { } + public void SetReference(int i) { } + + public void RemoveMember(ReadOnlySpan name) { } + public void RemoveMember(float key) { } + + public bool MemberIsNil(ReadOnlySpan name) => true; + + public void SetMemberDouble(float key, double val) { } + public double GetMemberDouble(ReadOnlySpan name, double def) => def; + + public IHandleEntity? GetMemberEntity(ReadOnlySpan name, IHandleEntity? def) => def; + public void SetMemberEntity(float key, IHandleEntity? ent) { } + public void SetMemberEntity(ReadOnlySpan name, IHandleEntity? ent) { } + public bool isEntity() => false; + public IHandleEntity? GetEntity() => null; + public void SetEntity(IHandleEntity? ent) { } + + public void SetMemberVector(ReadOnlySpan name, in Vector3 vec) { } + public void SetMemberVector(float key, in Vector3 vec) { } + public Vector3 GetMemberVector(ReadOnlySpan name, in Vector3 def) => def; + public Vector3 GetMemberVector(int key) => default; + public Vector3 GetVector() => default; + public bool isVector() => false; + + public void SetMemberAngle(ReadOnlySpan name, in QAngle ang) { } + public QAngle GetMemberAngle(ReadOnlySpan name, in QAngle def) => def; + public QAngle GetAngle() => default; + public bool isAngle() => false; + + public void SetMemberMatrix(ReadOnlySpan name, in Matrix4x4 mat) { } + public void SetMemberMatrix(float key, in Matrix4x4 mat) { } + public void SetMemberMatrix(int key, in Matrix4x4 mat) { } + + public void SetMemberPhysObject(ReadOnlySpan name, IPhysicsObject? obj) { } + public double GetMemberDouble(float key, double def) => def; + public IHandleEntity? GetMemberEntity(int key, IHandleEntity? def) => def; + public Matrix4x4 GetMemberMatrix(int key, in Matrix4x4 def) => def; +} + +public sealed class LuaNullInterface : ILuaInterface +{ + static class Dummy where T : unmanaged + { + public static T Value; + } + + byte Type; + string PathID = ""; + + public int Top() => 0; + public void Push(int stackPos) { } + public void Pop(int amt = 1) { } + public void GetTable(int stackPos) { } + public void GetField(int stackPos, ReadOnlySpan name) { } + public void SetField(int stackPos, ReadOnlySpan name) { } + public void CreateTable() { } + public void SetTable(int stackPos) { } + public void SetMetaTable(int stackPos) { } + public bool GetMetaTable(int stackPos) => false; + public void Call(int args, int results) { } + public int PCall(int args, int results, int errorFunc) => 0; + public bool Equal(int a, int b) => false; + public bool RawEqual(int a, int b) => false; + public void Insert(int stackPos) { } + public void Remove(int stackPos) { } + public bool Next(int stackPos) => false; + public nint NewUserdata(uint size) => 0; + [DoesNotReturn] public void ThrowError(ReadOnlySpan error) => throw new NotSupportedException(error.ToString()); + public void CheckType(int stackPos, LuaType type) { } + [DoesNotReturn] public void ArgError(int argNum, ReadOnlySpan message) => throw new NotSupportedException(message.ToString()); + public void RawGet(int stackPos) { } + public void RawSet(int stackPos) { } + public string? GetString(int stackPos = -1) => null; + public double GetNumber(int stackPos = -1) => 0; + public bool GetBool(int stackPos = -1) => false; + public CFunc? GetCFunction(int stackPos = -1) => null; + public nint GetUserdata(int stackPos = -1) => 0; + public void PushNil() { } + public void PushString(ReadOnlySpan val) { } + public void PushNumber(double val) { } + public void PushBool(bool val) { } + public void PushCFunction(CFunc val) { } + public unsafe void PushCFunction(delegate* unmanaged[Cdecl] val) { } + public void PushCClosure(CFunc val, int vars) { } + public void PushUserdata(nint userdata) { } + public int ReferenceCreate() => -1; + public void ReferenceFree(int i) { } + public void ReferencePush(int i) { } + public void PushSpecial(Special type) { } + public bool IsType(int stackPos, LuaType type) => type == LuaType.Nil; + public LuaType GetType(int stackPos) => LuaType.Nil; + public string GetTypeName(LuaType type) => "nil"; + public void CreateMetaTableType(ReadOnlySpan name, int type) { } + public string CheckString(int stackPos = -1) => ""; + public double CheckNumber(int stackPos = -1) => 0; + public int ObjLen(int stackPos = -1) => 0; + public QAngle GetAngle(int stackPos = -1) => default; + public Vector3 GetVector(int stackPos = -1) => default; + public void PushAngle(in QAngle val) { } + public void PushVector(in Vector3 val) { } + public void SetState(lua_State state) { } + public int CreateMetaTable(ReadOnlySpan name) => 0; + public bool PushMetaTable(LuaType type) => false; + public void PushUserType(nint data, LuaType type) { } + public void SetUserType(int stackPos, nint data) { } + + public ReadOnlySpan GetStringBytes(int stackPos = -1) => []; + public void PushString(ReadOnlySpan val) { } + + public void PushValueUserType(in T val, LuaType type) where T : unmanaged { } + public ref T GetValueUserType(int stackPos, LuaType type) where T : unmanaged => ref Dummy.Value; + + public void PushObjectUserType(T? obj, LuaType type) where T : class { } + public T? GetObjectUserType(int stackPos, LuaType type) where T : class => null; + public void ReleaseUserTypeObject(object obj) { } + + public bool Init(ILuaGameCallback callbacks, bool isServer) => true; + public void Shutdown() { } + public void Cycle() { } + public ILuaObject Global() => LuaNullObject.Instance; + public ILuaObject GetObject(int index) => LuaNullObject.Instance; + public void PushLuaObject(ILuaObject? obj) { } + public void PushLuaFunction(CFunc func) { } + public int HandleException(Exception e) => 0; + [DoesNotReturn] public void LuaError(ReadOnlySpan err, int index) => throw new NotSupportedException(err.ToString()); + [DoesNotReturn] public void TypeError(ReadOnlySpan name, int index) => throw new NotSupportedException(name.ToString()); + public bool CallInternal(int args, int rets) => false; + public void CallInternalNoReturns(int args) { } + public bool CallInternalGetBool(int args) => false; + public string? CallInternalGetString(int args) => null; + public bool CallInternalGet(int args, ILuaObject obj) => false; + public void NewGlobalTable(ReadOnlySpan name) { } + public ILuaObject NewTemporaryObject() => LuaNullObject.Instance; + public bool isUserData(int index) => false; + public ILuaObject? GetMetaTableObject(ReadOnlySpan name, int type) => LuaNullObject.Instance; + public ILuaObject? GetMetaTableObject(int index) => LuaNullObject.Instance; + public ILuaObject GetReturn(int index) => LuaNullObject.Instance; + public bool IsServer() => Type == 1; + public bool IsClient() => Type == 0; + public bool IsMenu() => Type == 2; + public void DestroyObject(ILuaObject? obj) { } + public ILuaObject CreateObject() => LuaNullObject.Instance; + public void SetMember(ILuaObject table, ILuaObject key, ILuaObject? value) { } + public ILuaObject GetNewTable() => LuaNullObject.Instance; + public void SetMember(ILuaObject table, float key) { } + public void SetMember(ILuaObject table, float key, ILuaObject? value) { } + public void SetMember(ILuaObject table, ReadOnlySpan key) { } + public void SetMember(ILuaObject table, ReadOnlySpan key, ILuaObject? value) { } + public void SetType(byte type) => Type = type; + public void PushLong(long num) { } + public int GetFlags(int index) => 0; + public bool FindOnObjectsMetaTable(int objIndex, int keyIndex) => false; + public bool FindObjectOnTable(int tableIndex, int keyIndex) => false; + public void SetMemberFast(ILuaObject table, int keyIndex, int valueIndex) { } + public bool RunString(ReadOnlySpan filename, ReadOnlySpan path, ReadOnlySpan stringToRun, bool run, bool showErrors) => false; + public bool IsEqual(ILuaObject? objA, ILuaObject? objB) => false; + [DoesNotReturn] public void Error(ReadOnlySpan err) => throw new NotSupportedException(err.ToString()); + public string GetStringOrError(int index) => ""; + public bool RunLuaModule(ReadOnlySpan name) => false; + public bool FindAndRunScript(ReadOnlySpan filename, bool run, bool showErrors, ReadOnlySpan source, bool noReturns) => false; + public void SetPathID(ReadOnlySpan pathID) => PathID = pathID.ToString(); + public string GetPathID() => PathID; + public void ErrorNoHalt(ReadOnlySpan msg) { } + public void Msg(ReadOnlySpan msg) { } + public void PushPath(ReadOnlySpan path) { } + public void PopPath() { } + public string? GetPath() => null; + public Color GetColor(int index) => default; + public void PushColor(Color color) { } + public int GetStack(int level, ref lua_Debug dbg) => 0; + public int GetInfo(ReadOnlySpan what, ref lua_Debug dbg) => 0; + public string? GetLocal(ref lua_Debug dbg, int n) => null; + public string? GetUpvalue(int funcIndex, int n) => null; + public bool RunStringEx(ReadOnlySpan filename, ReadOnlySpan path, ReadOnlySpan stringToRun, bool run, bool printErrors, bool dontPushErrors, bool noReturns) => false; + public ReadOnlySpan GetDataString(int index) => []; + public void ErrorFromLua(ReadOnlySpan msg) { } + public string GetCurrentLocation() => ""; + public void MsgColour(in Color col, ReadOnlySpan msg) { } + public void GetCurrentFile(out string outStr) => outStr = ""; + public bool CompileString(out byte[] dump, ReadOnlySpan stringToCompile) { + dump = []; + return false; + } + public bool CallFunctionProtected(int args, int rets, bool showError) => false; + public bool Require(ReadOnlySpan name) => false; + public string GetActualTypeName(int stackPos) => "nil"; + public void PreCreateTable(int arrelems, int nonarrelems) { } + public void PushPooledString(int index) { } + public string GetPooledString(int index) => ""; + public int AddThreadedCall(ILuaThreadedCall call) => 0; + public void AppendStackTrace(StringBuilder output) { } + public ConVar CreateConVar(ReadOnlySpan name, ReadOnlySpan defaultValue, ReadOnlySpan helpString, int flags) => throw new NotSupportedException(); + public ConCommand CreateConCommand(ReadOnlySpan name, ReadOnlySpan helpString, int flags, FnCommandCallback? callback, FnCommandCompletionCallback? completionCallback) => throw new NotSupportedException(); + public string CheckStringOpt(int stackPos, ReadOnlySpan def) => def.ToString(); + public double CheckNumberOpt(int stackPos, double def) => def; + public int RegisterMetaTable(ReadOnlySpan name, ILuaObject tbl) => 0; +}