Skip to content

Commit 436106d

Browse files
committed
Some render stuff/more methods
1 parent 059f419 commit 436106d

12 files changed

Lines changed: 282 additions & 44 deletions

File tree

‎Game.Client/GarrysMod/LuaCam.cs‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,62 @@ public static partial class LuaCam
2020
static int Start3DCount;
2121
static int OrthoViewCount;
2222

23+
public static void CheckMissingEnds() {
24+
if (Start2DCount > 0) {
25+
do {
26+
using MatRenderContextPtr renderContext = new(materials);
27+
renderContext.OverrideAlphaWriteEnable(false, false);
28+
((ClientModeShared)clientMode).SetupGModSurface(false);
29+
ClientModeShared.SetupVGuiMatrices(false, renderContext.Context);
30+
Start2DCount--;
31+
} while (Start2DCount > 0);
32+
Warning("Warning! Detected missing cam.End2D call!\n");
33+
}
34+
35+
if (Start3DCount > 0) {
36+
do {
37+
render.PopView(view.GetFrustum());
38+
Start3DCount--;
39+
} while (Start3DCount > 0);
40+
Warning("Warning! Detected missing cam.End3D call!\n");
41+
}
42+
43+
if (Start3D2DCount > 0) {
44+
do {
45+
using MatRenderContextPtr renderContext = new(materials);
46+
renderContext.MatrixMode(MaterialMatrixMode.Model);
47+
renderContext.PopMatrix();
48+
renderContext.OverrideDepthEnable(false, true);
49+
renderContext.GMOD_ForceFilterMode(true, 0);
50+
renderContext.GMOD_ForceFilterMode(false, 0);
51+
Start3D2DCount--;
52+
} while (Start3D2DCount > 0);
53+
Warning("Warning! Detected missing cam.End3D2D call!\n");
54+
}
55+
56+
if (LuaRender.CustomClipPlaneCount > 0) {
57+
do {
58+
using MatRenderContextPtr renderContext = new(materials);
59+
renderContext.PopCustomClipPlane();
60+
LuaRender.CustomClipPlaneCount--;
61+
} while (LuaRender.CustomClipPlaneCount > 0);
62+
Warning("Warning! Detected missing render.PopCustomClipPlane call!\n");
63+
}
64+
65+
if (LuaRender.RenderTargetStack > 0) {
66+
do {
67+
using MatRenderContextPtr renderContext = new(materials);
68+
renderContext.Flush(false);
69+
renderContext.PopRenderTargetAndViewport();
70+
LuaRender.RenderTargetStack--;
71+
} while (LuaRender.RenderTargetStack > 0);
72+
Warning("Warning! Detected missing render.PopRenderTarget call!\n");
73+
}
74+
75+
using MatRenderContextPtr context = new(materials);
76+
context.BindLocalCubemap(null);
77+
}
78+
2379
static ViewSetup StartView;
2480

2581
public static bool InSurface3D;

‎Game.Client/GarrysMod/LuaRender.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ static int SetRenderTargetEx(ILuaInterface lua) {
272272
return 0;
273273
}
274274
#endif
275-
static int RenderTargetStack;
275+
internal static int RenderTargetStack;
276276

277277
#if !MENU_DLL
278278
[LuaFunction]
@@ -627,7 +627,7 @@ static int ClearDepth(ILuaInterface lua) {
627627
// todo: RenderView
628628
// todo: GetViewSetup
629629
// todo: RenderHUD
630-
static int CustomClipPlaneCount;
630+
internal static int CustomClipPlaneCount;
631631

632632
[LuaFunction]
633633
static int PushCustomClipPlane(ILuaInterface lua) {

‎Game.Client/HL2MP/C_HL2MP_Player.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ public override void PostThink() {
103103
AngEyeAngles = EyeAngles();
104104
}
105105

106+
public void DoAnimationEvent(PlayerAnimEvent evt, int data, bool predicted) => PlayerAnimState.DoAnimationEvent(evt, data);
107+
106108
#if GMOD_DLL
107109
public override void UpdateClientSideAnimation() {
108110
QAngle eyeAngles = EyeAngles();

‎Game.Client/HLClient.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public void View_Render(ViewRects rects) {
285285
return;
286286
#if GMOD_DLL
287287
using MatRenderContextPtr renderContext = new(materials);
288-
renderContext.Flush(false);
288+
renderContext.SetRenderTarget(null);
289289
if (gGM != null && gGM.CallWithArgs((int)LUA_POOLEDSTRING.PreRender) && gGM.CallFinish(0) && !enginevgui.IsGameUIVisible())
290290
return;
291291
#endif

‎Game.Client/View.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,10 @@ public void Render(ViewRects rect) {
356356
if (drawViewModel)
357357
flags |= RenderViewInfo.DrawViewmodel;
358358

359-
RenderView(in viewEye, clearFlags, flags);
359+
#if GMOD_DLL
360+
if (!garrysmod.RenderScene(in viewEye))
361+
#endif
362+
RenderView(in viewEye, clearFlags, flags);
360363
}
361364

362365
View2D.X = rect[0].X;

‎Game.Shared/GarrysMod/GarrysMod.cs‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
using Source.Common.Formats.Keyvalues;
1717
using Source.Common.GarrysMod;
1818
using Source.Common.MaterialSystem;
19+
using Source.Common.Mathematics;
1920
using Source.Common.Networking;
2021

22+
using System.Numerics;
23+
2124
using Steamworks;
2225

2326
using System;
@@ -618,7 +621,61 @@ public void EntityCreated(BaseEntity? ent) {
618621
gGM.CallNoReturns(1);
619622
}
620623

624+
#if CLIENT_DLL
625+
static readonly ConVarRef volume = new("volume");
626+
static readonly ConVarRef volume_sfx = new("volume_sfx");
627+
static readonly ConVarRef snd_mute_losefocus = new("snd_mute_losefocus");
628+
629+
public void UpdateAudioVolume() {
630+
if (snd_mute_losefocus.GetBool() && !engine.IsActiveApp()) {
631+
get.Audio()!.SetGlobalVolume(0);
632+
return;
633+
}
634+
get.Audio()!.SetGlobalVolume(volume_sfx.GetFloat() * volume.GetFloat());
635+
}
636+
637+
public bool RenderScene(in ViewSetup view) {
638+
if (get.Audio() != null) {
639+
Vector3 origin = view.Origin;
640+
Vector3 velocity = default;
641+
MathLib.AngleVectors(view.Angles, out Vector3 forward, out Vector3 right, out Vector3 up);
642+
get.Audio()!.SetEar(ref origin, ref velocity, ref forward, ref up);
643+
UpdateAudioVolume();
644+
}
645+
646+
LuaCam.CheckMissingEnds();
647+
// todo: reset ShouldDrawLocalPlayer cache
648+
649+
using (MatRenderContextPtr renderContext = new(materials)) {
650+
renderContext.GMOD_ForceFilterMode(true, 0);
651+
renderContext.GMOD_ForceFilterMode(false, 0);
652+
renderContext.OverrideBlend(false, true, -1, -1, -1);
653+
renderContext.OverrideBlendSeparateAlpha(false, true, -1, -1, -1);
654+
}
655+
656+
if (g_Lua == null || gGM == null)
657+
return false;
658+
659+
C_BasePlayer? player = C_BasePlayer.GetLocalPlayer();
660+
if (player != null && player.GetActiveWeapon() is C_WeaponSWEP swep)
661+
swep.RenderScreen();
662+
663+
if (gGM.CallWithArgs((int)LUA_POOLEDSTRING.RenderScene)) {
664+
LuaVector.Push_Vector(view.Origin);
665+
LuaAngle.Push_Angle(view.Angles);
666+
g_Lua.PushNumber(view.FOV);
667+
return gGM.CallFinish(3);
668+
}
669+
return false;
670+
}
671+
#endif
672+
621673
public void EntityRemoved(BaseEntity ent, bool fullUpdate) {
674+
#if CLIENT_DLL
675+
LuaEntity.RemoveFromRemoveList(ent);
676+
#else
677+
Game.Server.GarrysMod.LuaEntity.RemoveFromRemoveList(ent);
678+
#endif
622679
SoundEnvelopeController.GetController().ShutdownSoundsForEntity(ent.EntIndex());
623680
if (g_Lua == null || gGM == null)
624681
return;

‎Game.Shared/GarrysMod/LuaEntity.cs‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ public static partial class LuaEntity
6262
static readonly List<EHANDLE> RemoveList = [];
6363
static bool ProcessingRemoveList;
6464

65+
public static void RemoveFromRemoveList(BaseEntity ent) {
66+
if (ProcessingRemoveList)
67+
return;
68+
for (int i = 0; i < RemoveList.Count; i++) {
69+
if (RemoveList[i].Get() == ent) {
70+
RemoveList.RemoveAt(i);
71+
return;
72+
}
73+
}
74+
}
75+
6576
public static void RemoveQueuedEntities() {
6677
ProcessingRemoveList = true;
6778
for (int i = 0; i < RemoveList.Count; i++) {
@@ -1502,6 +1513,40 @@ static int Entity__HasSpawnFlags(ILuaInterface lua) {
15021513
return 1;
15031514
}
15041515

1516+
[LuaMethod]
1517+
static int Entity__SetKeyValue(ILuaInterface lua) {
1518+
BaseEntity ent = Get_Entity(1, false)!;
1519+
for (int i = 0; i < RemoveList.Count; i++) {
1520+
if (RemoveList[i].Get() == ent)
1521+
return 0;
1522+
}
1523+
if (ent.IsMarkedForDeletion())
1524+
return 0;
1525+
1526+
ReadOnlySpan<char> key = lua.CheckString(2);
1527+
if (stricmp(key, "classname") == 0)
1528+
return 0;
1529+
1530+
ReadOnlySpan<char> value = lua.CheckString(3);
1531+
if (stricmp(key, "m_iEFlags") != 0 || (atoi(value) & 1) == 0)
1532+
ent.KeyValue(key, value);
1533+
return 0;
1534+
}
1535+
1536+
[LuaMethod]
1537+
static int Entity__SetAnimation(ILuaInterface lua) {
1538+
BaseEntity ent = Get_Entity(1, false)!;
1539+
if (!ent.IsPlayer())
1540+
return 0;
1541+
int anim = (int)Math.Clamp((float)(int)lua.CheckNumber(2), 0, 9);
1542+
HL2MP_Player? player = ent as HL2MP_Player;
1543+
if (anim == (int)PlayerAnim.Attack1)
1544+
player!.DoAnimationEvent(PlayerAnimEvent.AttackPrimary, 0, true);
1545+
else if (anim == (int)PlayerAnim.Reload)
1546+
player!.DoAnimationEvent(PlayerAnimEvent.Reload, 0, true);
1547+
return 0;
1548+
}
1549+
15051550
[LuaMethod]
15061551
static int Entity__GetSkin(ILuaInterface lua) {
15071552
BaseEntity ent = Get_Entity(1, false)!;

‎Game.Shared/GarrysMod/LuaGlobalFunctions.cs‎

Lines changed: 39 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -727,49 +727,26 @@ static int DisableClipping(ILuaInterface lua) {
727727
}
728728
#endif
729729

730-
[LuaGlobal]
731-
static int RunConsoleCommand(ILuaInterface lua) {
732-
string command = g_Lua!.CheckString(1);
733-
if (!LuaConVar.IsValidConsoleName(command)) {
734-
g_Lua.ErrorFromLua($"RunConsoleCommand: Command has invalid characters! ({command})\n\tThe first parameter of this function should contain only the command, the second parameter should contain arguments.");
735-
return 0;
736-
}
737-
738-
string? blocked = LuaConCommands.ConCommand_IsBlocked(command);
739-
if (blocked != null) {
740-
#if CLIENT_DLL
741-
if (blocked == "connect") {
742-
// todo menu system
743-
return 0;
744-
}
745-
#endif
746-
g_Lua.ErrorFromLua($"RunConsoleCommand: Command is blocked! ({blocked})");
747-
return 0;
748-
}
749-
750-
if (command.Length <= 1) {
751-
g_Lua.ErrorFromLua($"RunConsoleCommand: Command is too short, bailing! ({command})");
752-
return 0;
753-
}
754-
730+
public static bool BuildConsoleCommand(ILuaInterface lua, int start, string command, out string result) {
731+
result = "";
755732
StringBuilder buffer = new(command);
756-
for (int i = 2; i < 64; i++) {
757-
LuaType type = g_Lua.GetType(i);
733+
for (int i = start; i < 64; i++) {
734+
LuaType type = lua.GetType(i);
758735
if (type == LuaType.Nil)
759736
break;
760737

761-
string? argument = g_Lua.GetString(i);
738+
string? argument = lua.GetString(i);
762739
if (argument == null)
763740
break;
764741

765742
string? blockedArg = LuaConCommands.ConCommand_IsBlockedArg(argument);
766743
if (blockedArg != null) {
767-
g_Lua.ErrorFromLua($"RunConsoleCommand: Command argument is blocked! ({command} {blockedArg})");
768-
return 0;
744+
lua.ErrorFromLua($"RunConsoleCommand: Command argument is blocked! ({command} {blockedArg})");
745+
return false;
769746
}
770747

771748
if (type == LuaType.Number)
772-
argument = g_Lua.GetNumber(i).ToString("F2", CultureInfo.InvariantCulture);
749+
argument = lua.GetNumber(i).ToString("F2", CultureInfo.InvariantCulture);
773750

774751
StringBuilder escaped = new();
775752
for (int c = 0; c < argument.Length && c < 511; c++)
@@ -783,9 +760,39 @@ static int RunConsoleCommand(ILuaInterface lua) {
783760
}
784761
buffer.Append(';');
785762

786-
string result = buffer.ToString();
763+
result = buffer.ToString();
787764
if (result.Length > 1023)
788765
result = result[..1023];
766+
return true;
767+
}
768+
769+
[LuaGlobal]
770+
static int RunConsoleCommand(ILuaInterface lua) {
771+
string command = g_Lua!.CheckString(1);
772+
if (!LuaConVar.IsValidConsoleName(command)) {
773+
g_Lua.ErrorFromLua($"RunConsoleCommand: Command has invalid characters! ({command})\n\tThe first parameter of this function should contain only the command, the second parameter should contain arguments.");
774+
return 0;
775+
}
776+
777+
string? blocked = LuaConCommands.ConCommand_IsBlocked(command);
778+
if (blocked != null) {
779+
#if CLIENT_DLL
780+
if (blocked == "connect") {
781+
// todo menu system
782+
return 0;
783+
}
784+
#endif
785+
g_Lua.ErrorFromLua($"RunConsoleCommand: Command is blocked! ({blocked})");
786+
return 0;
787+
}
788+
789+
if (command.Length <= 1) {
790+
g_Lua.ErrorFromLua($"RunConsoleCommand: Command is too short, bailing! ({command})");
791+
return 0;
792+
}
793+
794+
if (!BuildConsoleCommand(lua, 2, command, out string result))
795+
return 0;
789796
#if CLIENT_DLL || MENU_DLL
790797
engine.ClientCmd(result);
791798
#else

‎Game.Shared/GarrysMod/LuaPlayer.cs‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,31 @@ static int Player__SetTeam(ILuaInterface lua) {
774774
return 0;
775775
}
776776

777+
[LuaMethod]
778+
static int Player__RunConsoleCommand(ILuaInterface lua) {
779+
BasePlayer player = Get_Player(1, false)!;
780+
string command = lua.CheckString(2);
781+
if (!LuaConVar.IsValidConsoleName(command)) {
782+
lua.ErrorFromLua($"RunConsoleCommand: Command has invalid characters! ({command})\n\tThe first parameter of this function should contain only the command, the second parameter should contain arguments.");
783+
return 0;
784+
}
785+
786+
string? blocked = LuaConCommands.ConCommand_IsBlocked(command);
787+
if (blocked != null) {
788+
lua.ErrorFromLua($"RunConsoleCommand: Command is blocked! ({blocked})");
789+
return 0;
790+
}
791+
792+
if (command.Length < 2) {
793+
lua.ErrorFromLua($"RunConsoleCommand: Command is too short, bailing! ({command})");
794+
return 0;
795+
}
796+
797+
if (LuaGlobalFunctions.BuildConsoleCommand(lua, 3, command, out string result))
798+
engine.ClientCommand(player.Edict(), result);
799+
return 0;
800+
}
801+
777802
[LuaMethod]
778803
static int Player__SendLua(ILuaInterface lua) {
779804
BasePlayer player = Get_Player(1, false)!;

‎Game.Shared/GarrysMod/WeaponSWEP.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,8 @@ public override void Drop(in Vector3 velocity) {
374374
public override ReadOnlySpan<char> GetName() => ScriptedEntity.ScriptNamePooled;
375375
#else
376376
public override ReadOnlySpan<char> GetName() => ScriptedEntity.GetScriptName();
377+
378+
public void RenderScreen() => ScriptedEntity.CallFunction((int)LUA_POOLEDSTRING.RenderScreen, false);
377379
#endif
378380

379381
public override int GetSlot() {

0 commit comments

Comments
 (0)