Skip to content

Commit 8f6d9f8

Browse files
committed
gmod updates
1 parent 32de795 commit 8f6d9f8

19 files changed

Lines changed: 239 additions & 70 deletions

File tree

‎Game.Client/GarrysMod/LuaMesh.cs‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,20 @@ static int IMesh__DrawSkinned(ILuaInterface lua) {
287287
Matrix4x4 model = Matrix4x4.Identity;
288288
renderContext.GetMatrix(MaterialMatrixMode.Model, out model);
289289

290-
int bone = 1;
291-
while (IsMatrixMember(table, bone)) {
292-
Matrix4x4 bind = table.GetMemberMatrix(bone, default);
290+
int bone = 0;
291+
while (true) {
292+
if (!IsMatrixMember(table, bone)) {
293+
if (bone != 0)
294+
break;
295+
bone = 1;
296+
if (!IsMatrixMember(table, bone))
297+
break;
298+
}
293299
if (bone > 52) {
294300
lua.ErrorNoHalt($"Too many model transform matrices! ({53} max)\n");
295301
break;
296302
}
303+
Matrix4x4 bind = table.GetMemberMatrix(bone, default);
297304
if (transform)
298305
MathLib.MatrixMultiply(model, bind, out bind);
299306
renderContext.LoadBoneMatrix(bone, new ReadOnlySpan<Matrix4x4>(in bind).Cast<Matrix4x4, Matrix3x4>()[0]);
@@ -456,11 +463,11 @@ static int BoneData(ILuaInterface lua) {
456463
uint index = (uint)lua.CheckNumber(1);
457464
uint bone = (uint)lua.CheckNumber(2);
458465
if (index >= 3) {
459-
lua.Error($"Invalid bone matrix index. Must be integer [0,{index - 1}]\n");
466+
lua.Error($"Invalid bone matrix index. Must be integer [0,{2}]\n");
460467
return 0;
461468
}
462469
if (bone >= 53) {
463-
lua.Error($"Invalid bone matrix bone. Must be integer [1,{52}]\n");
470+
lua.Error($"Invalid bone matrix bone. Must be integer [0,{52}]\n");
464471
return 0;
465472
}
466473

‎Game.Client/GarrysMod/SpawnIcon.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ public override void OnThink() {
548548
string path = SpawnIconPath;
549549
Bootil.String.File.ExtractFilename(ref path);
550550
Bootil.String.File.StripExtension(ref path);
551-
string cmd = "mat_reloadmaterial " + path + "\n";
551+
string cmd = "mat_reloadmaterial \"" + path + "\"\n";
552552
if (!cmd.Contains(';'))
553553
engine.ClientCmd(cmd);
554554
}

‎Game.Client/View.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,9 @@ public void RenderView(in ViewSetup viewRender, ClearFlags clearFlags, RenderVie
519519
renderContext.PopRenderTargetAndViewport();
520520
renderContext.Flush();
521521
}
522+
#if GMOD_DLL
523+
gGM?.Call((int)Source.Common.GarrysMod.Lua.LUA_POOLEDSTRING.PostRender);
524+
#endif
522525
}
523526

524527
RenderingView = false;

‎Game.Shared/GarrysMod/GarrysMod.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,15 +327,15 @@ public static bool IsSacredEntity(ReadOnlySpan<char> className) {
327327

328328
static readonly string[] OverridableEntities = [
329329
"point_servercommand", "point_teleport", "point_viewcontrol", "env_credits", "env_hudhint", "env_screenoverlay", "env_zoom",
330-
"filter_activator_team", "filter_activator_context", "game_player_equip", "info_npc_spawn_destination", "logic_auto",
330+
"env_message", "filter_activator_team", "filter_activator_context", "game_player_equip", "info_npc_spawn_destination", "logic_auto",
331331
"player_loadsaved", "player_speedmod", "npc_template_maker", "npc_maker", "prop_physics_respawnable", "trigger_auto",
332332
"trigger_changelevel", "trigger_hurt", "trigger_multiple", "trigger_once", "trigger_teleport", "trigger_transition",
333-
"trigger_push", "ambient_generic", "func_pushable", "func_healthcharger", "func_recharge", "func_tankmortar",
333+
"trigger_push", "trigger_portal_cleanser", "ambient_generic", "func_pushable", "func_healthcharger", "func_recharge", "func_tankmortar",
334334
"item_suitcharger", "item_healthcharger", "item_longjump", "item_suit", "item_healthkit", "item_battery", "weaponbox",
335335
"speaker", "monster_satchel", "monster_tripmine", "info_player_teamspawn", "info_vehicle_groundspawn", "ammo_crossbow",
336336
"ammo_egonclip", "ammo_gaussclip", "ammo_glockclip", "ammo_9mmclip", "ammo_mp5clip", "ammo_9mmar", "ammo_9mmbox",
337337
"ammo_mp5grenades", "ammo_argrenades", "ammo_357", "ammo_rpgclip", "ammo_buckshot", "bounce_bomb", "combine_bouncemine",
338-
"combine_mine", "prop_car_alarm", "prop_car_glass", "prop_health_cabinet", "asw_door", "dod_control_point",
338+
"combine_mine", "npc_portal_turret_floor", "npc_turret_floor", "npc_ichthyosaur", "prop_car_alarm", "prop_car_glass", "prop_health_cabinet", "asw_door", "dod_control_point",
339339
"team_control_point", "prop_portal_stats_display", "npc_personality_core", "prop_tractor_beam", "prop_under_floor_button",
340340
"prop_floor_ball_button", "prop_floor_cube_button", "prop_under_button", "prop_floor_button", "prop_button",
341341
"prop_indicator_panel", "prop_testchamber_door", "prop_wall_projector", "npc_wheatley_boss", "prop_laser_relay",

‎Game.Shared/GarrysMod/LuaClass.cs‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ public class LuaClass
2525
public string Name;
2626
public string? DerivedFrom;
2727
public LuaType Type;
28+
public int LuaID;
2829
public List<LuaClassFunction>? Functions;
2930
public LuaObject MetaTable;
3031
public Action? InitFn;
3132

3233
public LuaClass(string name, LuaType type, Action? initFn, string? derivedFrom) {
3334
MetaTable = new();
3435
Functions = null;
36+
LuaID = -1;
3537
Name = name;
3638
DerivedFrom = derivedFrom;
3739
Type = type;
@@ -80,6 +82,8 @@ public unsafe LuaClassFunction Add(string name, delegate* unmanaged[Cdecl]<nint,
8082
}
8183

8284
public void Push(object? data) {
85+
if (g_LuaID != LuaID || LuaID == -1)
86+
Error("CLuaClass::Push - Invalid Lua instance! Is Lua shutting down?");
8387
if (!MetaTable.isTable())
8488
Error("CLuaClass::Push - Not Table!");
8589
g_Lua!.PushObjectUserType(data, Type);
@@ -103,6 +107,9 @@ public virtual void MetaTableDerive() {
103107
}
104108

105109
public void InitClasses() {
110+
if (LuaID != -1)
111+
Error($"Double initialization of class {Name}?\n");
112+
106113
ILuaObject? meta = g_Lua!.GetMetaTableObject(Name, (int)Type);
107114
if (meta == null)
108115
Error($"Error initializing class {Name}\n");
@@ -124,9 +131,13 @@ public void InitClasses() {
124131
}
125132

126133
InitFn?.Invoke();
134+
LuaID = g_LuaID;
127135
}
128136

129-
public void Shutdown() => MetaTable.UnReference();
137+
public void Shutdown() {
138+
MetaTable.UnReference();
139+
LuaID = -1;
140+
}
130141

131142
public static void InitLuaClasses(ILuaInterface lua) {
132143
if (g_LuaClasses == null)
@@ -143,8 +154,10 @@ public static void ShutdownLuaClasses(ILuaInterface lua) {
143154
if (g_LuaClasses == null)
144155
return;
145156

146-
for (int i = 0; i < g_LuaClasses.Count; i++)
157+
for (int i = 0; i < g_LuaClasses.Count; i++) {
147158
g_LuaClasses[i].MetaTable.UnReference();
159+
g_LuaClasses[i].LuaID = -1;
160+
}
148161
}
149162
}
150163
#endif

‎Game.Shared/GarrysMod/LuaEntity.cs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1033,7 +1033,12 @@ static int Entity__GetDTString(ILuaInterface lua) {
10331033
[LuaMethod]
10341034
static int Entity__DrawModel(ILuaInterface lua) {
10351035
BaseEntity ent = Get_Entity(1, false)!;
1036-
ent.DrawModel((StudioFlags)(int)lua.CheckNumberOpt(2, (int)StudioFlags.Render));
1036+
double flags = lua.CheckNumberOpt(2, (int)StudioFlags.Render);
1037+
if (ent.EntIndex() == 0) {
1038+
lua.ErrorNoHalt("Trying to call DrawModel on the world entity, this will not work.");
1039+
return 0;
1040+
}
1041+
ent.DrawModel((StudioFlags)(int)flags);
10371042
return 0;
10381043
}
10391044
#endif

‎Game.Shared/GarrysMod/LuaFileLibrary.cs‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,12 +792,16 @@ static int AsyncRead(ILuaInterface lua) {
792792
Bootil.String.Lower(ref fileName);
793793

794794
if (!IsFileAccessAllowed(fileName, path, false, false)) {
795-
lua.PushNumber(-1);
795+
AsyncReadEarlyReturn(lua, fileName, path);
796796
return 1;
797797
}
798798

799799
long size = filesystem.Size(fileName, path);
800-
int bytes = size < 0 ? 0 : (int)size;
800+
if (size <= 0) {
801+
AsyncReadEarlyReturn(lua, fileName, path);
802+
return 1;
803+
}
804+
int bytes = (int)size;
801805

802806
FileAsyncRequest request = new() {
803807
FileName = fileName,
@@ -815,6 +819,19 @@ static int AsyncRead(ILuaInterface lua) {
815819
lua.PushNumber((double)filesystem.AsyncRead(request));
816820
return 1;
817821
}
822+
823+
static void AsyncReadEarlyReturn(ILuaInterface lua, string fileName, string path) {
824+
using LuaObject callback = new(3, LuaType.None);
825+
if (callback.isFunction()) {
826+
callback.Push();
827+
lua.PushString(fileName);
828+
lua.PushString(path);
829+
lua.PushNumber(-1);
830+
lua.PushString("");
831+
lua.CallInternalNoReturns(4);
832+
}
833+
lua.PushNumber(-1);
834+
}
818835
#endif
819836

820837
[LuaFunction]

‎Game.Shared/GarrysMod/LuaGame.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ static void DoCleanUpMap() {
274274
Soundscape_OnStopAllSounds();
275275
List<C_BaseEntity> hairballs = [];
276276
for (C_BaseEntity? ent = cl_entitylist.FirstBaseEntity(); ent != null; ent = cl_entitylist.NextBaseEntity(ent))
277-
if (!ent.IsEFlagSet(EFL.KillMe) && strcmp(ent.GetClassname(), "class C_Hairball") == 0)
277+
if (!ent.IsEFlagSet(EFL.KillMe) && stricmp(ent.GetClassname(), "class C_Hairball") == 0)
278278
hairballs.Add(ent);
279279
foreach (C_BaseEntity ent in hairballs)
280280
ent.Release();

‎Game.Shared/GarrysMod/LuaGameCallback.cs‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ public class LuaGameCallback : ILuaGameCallback
4545
static readonly ConVar lua_log = new("lua_log_sv", "0", 0, "Log serverside Lua errors to lua_errors_server.txt.");
4646
#endif
4747
static readonly ConVar lua_error_url = new("lua_error_url", "", FCvar.Replicated | FCvar.DontRecord);
48+
#if CLIENT_DLL
49+
static readonly ConVar lua_print_stack = new("lua_print_stack_cl", "0", 0, "Print the Lua stack when a message is printed to console from Lua. This is meant for debugging, to find out source of the messages, and is not supposed to be enabled at all times.");
50+
#else
51+
static readonly ConVar lua_print_stack = new("lua_print_stack_sv", "0", 0, "Print the Lua stack when a message is printed to console from Lua. This is meant for debugging, to find out source of the messages, and is not supposed to be enabled at all times.");
52+
#endif
4853

4954
static readonly Dictionary<string, bool> m_ReportedErrors = [];
5055
static readonly Queue<LuaErrorToSend> LuaErrorQueue = [];
@@ -133,20 +138,39 @@ public static void SendQueuedErrors() {
133138

134139
public void Msg(ReadOnlySpan<char> msg, bool useless) => MsgColour(msg, in cMsgColor);
135140

136-
public void MsgColour(ReadOnlySpan<char> msg, in Color color) => Dbg._ColorSpewMessage(SpewType.Message, in color, msg.ToString());
141+
public void MsgColour(ReadOnlySpan<char> msg, in Color color) {
142+
Dbg._ColorSpewMessage(SpewType.Message, in color, msg.ToString());
137143

138-
public void LuaError(in LuaError error) {
139-
bool isAddon = LuaHelper.GetAddonFromError(in error, out IAddonSystem.Information addon, out bool overriding);
144+
if (lua_print_stack.GetInt() == 0 || (msg.Length == 1 && (msg[0] == '\t' || msg[0] == '\n')))
145+
return;
140146

141-
string stack = "";
147+
LuaError error = new();
148+
LuaGlobalFunctions.ReadStackFrom(ref error, g_Lua!);
149+
string stack = FormatLuaStack(error.Stack);
150+
151+
if (msg.Length == 0 || msg[^1] != '\n')
152+
Dbg._ColorSpewMessage(SpewType.Message, in color, $"\n{stack}\n");
153+
else
154+
Dbg._ColorSpewMessage(SpewType.Message, in color, $"{stack}\n");
155+
}
156+
157+
static string FormatLuaStack(List<LuaError.StackEntry> stack) {
158+
string result = "";
142159
string indent = "";
143160
int i = 0;
144-
foreach (LuaError.StackEntry entry in error.Stack) {
161+
foreach (LuaError.StackEntry entry in stack) {
145162
indent += " ";
146163
string function = entry.Function.Length == 0 ? "unknown" : entry.Function;
147164
string source = entry.Source.Length == 0 ? "filename" : entry.Source;
148-
stack += $"{indent}{++i}. {function} - {source}:{entry.Line}\n";
165+
result += $"{indent} {++i}. {function} - {source}:{entry.Line}\n";
149166
}
167+
return result;
168+
}
169+
170+
public void LuaError(in LuaError error) {
171+
bool isAddon = LuaHelper.GetAddonFromError(in error, out IAddonSystem.Information addon, out bool overriding);
172+
173+
string stack = FormatLuaStack(error.Stack);
150174

151175
string message = error.Message;
152176
string tag = "ERROR";

‎Source.Common/GUI/IInputInternal.cs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ public unsafe struct SentenceModeItem
123123
IPanel? GetMouseCapture();
124124
IPanel? GetMouseFocus();
125125

126+
void PushAppModalSurface(IPanel? panel);
127+
void PopAppModalSurface(IPanel? panel);
128+
126129
// InputInternal
127130

128131
void RunFrame();

0 commit comments

Comments
 (0)