Skip to content
Merged

Lua #219

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
14 changes: 7 additions & 7 deletions Game.Client/GarrysMod/LuaPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ static int Panel____index(ILuaInterface lua) {
return 1;

if (panel != null) {
string? key = lua.GetString(2);
if (key != null && key.Length == 1) {
ReadOnlySpan<byte> key = lua.GetStringBytes(2);
if (key.Length == 1) {
if (key[0] == 'x' || key[0] == 'X') {
panel.GetPos(out int x, out _);
lua.PushNumber(x);
Expand All @@ -88,7 +88,7 @@ static int Panel__PaintManual(ILuaInterface lua) {
lua.Error("Tried to use a NULL Panel!");
return 0;
}
panel.PaintManual(lua.GetType(2) != LuaType.Nil &&lua.GetBool(2));
panel.PaintManual(lua.GetType(2) != LuaType.Nil && lua.GetBool(2));
return 0;
}

Expand All @@ -112,13 +112,13 @@ static int Panel____newindex(ILuaInterface lua) {
ILuaObject key = lua.GetObject(2)!;
if (key.isString()) {
ILuaObject value = lua.GetObject(3)!;
string? name = key.GetString();
if (name == "x" || name == "X") {
ReadOnlySpan<byte> name = lua.GetStringBytes(2);
if (name.Length == 1 && (name[0] == 'x' || name[0] == 'X')) {
panel.GetPos(out _, out int y);
panel.SetPos((int)value.GetFloat(), y);
return 0;
}
if (name == "y" || name == "Y") {
if (name.Length == 1 && (name[0] == 'y' || name[0] == 'Y')) {
panel.GetPos(out int x, out _);
panel.SetPos(x, (int)value.GetFloat());
return 0;
Expand Down Expand Up @@ -904,7 +904,6 @@ static int Panel__IsLoading(ILuaInterface lua) {
return 1;
}

#if !MENU_DLL
[LuaMethod]
static int Panel__UpdateHTMLTexture(ILuaInterface lua) {
Panel? panel = (Panel?)PanelClass.Get(1);
Expand All @@ -918,6 +917,7 @@ static int Panel__UpdateHTMLTexture(ILuaInterface lua) {
return 0;
}

#if !MENU_DLL
[LuaMethod]
static int Panel__GetHTMLMaterial(ILuaInterface lua) {
Panel? panel = (Panel?)PanelClass.Get(1);
Expand Down
10 changes: 10 additions & 0 deletions Game.Lua/LuaInterfaceImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ public bool Init(ILuaGameCallback callbacks, bool isServer) {
luaL_openlibs(state);
DoStackCheck();

GetField(LuaIndex.Global, "string");
GetField(-1, "gmatch");
SetField(-2, "gfind");
lua_settop(state, -2);

GetField(LuaIndex.Global, "math");
GetField(-1, "fmod");
SetField(-2, "mod");
lua_settop(state, -2);

GetField(LuaIndex.Global, "debug");
PushNil();
SetField(-2, "setlocal");
Expand Down
2 changes: 1 addition & 1 deletion Game.MenuSystem/Game.MenuSystem.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</ItemGroup>

<ItemGroup Condition="!$(DefineConstants.Contains('SWDS'))">
<Compile Include="..\Game.Shared\GarrysMod\LuaLibrary.cs;..\Game.Shared\GarrysMod\LuaClass.cs;..\Game.Shared\GarrysMod\LuaUser.cs;..\Game.Shared\GarrysMod\LuaObject.cs;..\Game.Shared\GarrysMod\LuaHelper.cs;..\Game.Shared\GarrysMod\LuaGlobalFunctions.cs;..\Game.Shared\GarrysMod\LuaConVar.cs;..\Game.Shared\GarrysMod\LuaConCommand.cs;..\Game.Shared\GarrysMod\LuaVector.cs;..\Game.Shared\GarrysMod\LuaAngle.cs;..\Game.Shared\GarrysMod\LuaMaterial.cs;..\Game.Shared\GarrysMod\LuaTexture.cs;..\Game.Shared\GarrysMod\LuaTimer.cs;..\Game.Shared\GarrysMod\LuaUtil.cs;..\Game.Shared\GarrysMod\LuaFileLibrary.cs;..\Game.Shared\GarrysMod\LuaSql.cs;..\Game.Shared\GarrysMod\LuaSystem.cs;..\Game.Shared\GarrysMod\LuaEngine.cs;..\Game.Shared\GarrysMod\LuaEnums.cs;..\Game.Shared\GarrysMod\LuaHTTPRequest.cs;..\Game.Shared\GarrysMod\LuaMotionSensor.cs;..\Game.Shared\GarrysMod\LuaPermissions.cs;..\Game.Shared\GarrysMod\LuaSteamworks.cs;..\Game.Shared\GarrysMod\LuaVideoWriter.cs" LinkBase="Shared\GarrysMod" />
<Compile Include="..\Game.Shared\GarrysMod\LuaLibrary.cs;..\Game.Shared\GarrysMod\LuaClass.cs;..\Game.Shared\GarrysMod\LuaUser.cs;..\Game.Shared\GarrysMod\LuaObject.cs;..\Game.Shared\GarrysMod\LuaHelper.cs;..\Game.Shared\GarrysMod\LuaGlobalFunctions.cs;..\Game.Shared\GarrysMod\LuaConVar.cs;..\Game.Shared\GarrysMod\LuaConCommand.cs;..\Game.Shared\GarrysMod\LuaVector.cs;..\Game.Shared\GarrysMod\LuaAngle.cs;..\Game.Shared\GarrysMod\LuaMaterial.cs;..\Game.Shared\GarrysMod\LuaTexture.cs;..\Game.Shared\GarrysMod\LuaTimer.cs;..\Game.Shared\GarrysMod\LuaUtil.cs;..\Game.Shared\GarrysMod\LuaFileLibrary.cs;..\Game.Shared\GarrysMod\LuaSql.cs;..\Game.Shared\GarrysMod\LuaSystem.cs;..\Game.Shared\GarrysMod\LuaEngine.cs;..\Game.Shared\GarrysMod\LuaEnums.cs;..\Game.Shared\GarrysMod\LuaHTTPRequest.cs;..\Game.Shared\GarrysMod\LuaMotionSensor.cs;..\Game.Shared\GarrysMod\LuaPermissions.cs;..\Game.Shared\GarrysMod\LuaSteamworks.cs;..\Game.Shared\GarrysMod\LuaVideoWriter.cs;..\Game.Shared\GarrysMod\LuaDump.cs" LinkBase="Shared\GarrysMod" />
<Compile Include="..\Game.Client\GarrysMod\LuaVGUI.cs;..\Game.Client\GarrysMod\LuaPanel.cs;..\Game.Client\GarrysMod\LuaSurface.cs;..\Game.Client\GarrysMod\LuaGui.cs;..\Game.Client\GarrysMod\LuaInput.cs;..\Game.Client\GarrysMod\LuaRender.cs;..\Game.Client\GarrysMod\LuaLanguage.cs;..\Game.Client\GarrysMod\LuaAchievements.cs;..\Game.Client\GarrysMod\HtmlPanel.cs;..\Game.Client\GarrysMod\LuaFonts.cs" LinkBase="Client\GarrysMod" />
</ItemGroup>

Expand Down
28 changes: 28 additions & 0 deletions Game.MenuSystem/MenuPanels.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#if MENU_DLL
using Source;
using Source.Common.Formats.Keyvalues;
using Source.Common.MaterialSystem;
using Source.Common.GUI;
using Source.GUI.Controls;

Expand Down Expand Up @@ -44,6 +46,32 @@ public override void OnThink() {
Surface.MovePopupToFront(this);
MenuSystem.g_MenuSystem.Think();
}

static TimeUnit_t LastPaintTime;
public static TimeUnit_t FrameTime;

public override void Paint() {
if (IsMarkedForDeletion())
return;

using MatRenderContextPtr renderContext = new(materials);
renderContext.ClearBuffers(false, true);

double now = Platform.Time;
LastPaintTime = now - LastPaintTime;
FrameTime = 0.0;
if (LastPaintTime >= 0.0) {
FrameTime = LastPaintTime;
if (LastPaintTime >= 0.1)
FrameTime = 0.1;
}
LastPaintTime = now;

PaintDrawOnTopPanels();

MenuSystem.RunHook(LUA_POOLEDSTRING.DrawOverlay);
MenuSystem.RunHook(LUA_POOLEDSTRING.CaptureVideo);
}
}

public static class MenuPanels
Expand Down
25 changes: 25 additions & 0 deletions Game.MenuSystem/MenuSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,25 @@ internal static void CallHook(ReadOnlySpan<char> name) {
hook.UnReference();
}

internal static void RunHook(LUA_POOLEDSTRING name) {
if (g_Lua == null || g_Lua.Global() == null)
return;

LuaObject hook = new();
g_Lua.Global().GetMember("hook", hook);
if (hook.isTable()) {
LuaObject run = new();
hook.GetMember("Run", run);
if (run.isFunction()) {
run.Push();
g_Lua.PushPooledString((int)name);
g_Lua.CallInternalNoReturns(1);
}
run.UnReference();
}
hook.UnReference();
}

public void Think() {
if (g_Lua != null && g_Lua.Global() != null) {
g_Lua.Cycle();
Expand Down Expand Up @@ -257,6 +276,12 @@ public void SendProblemToMenu(ReadOnlySpan<char> id, int severity, ReadOnlySpan<
#endif
}

public void OnPauseMenuBlockedTooManyTimes() {
#if MENU_DLL
CallHook("OnPauseMenuBlockedTooManyTimes");
#endif
}

public void ServerDetails(ReadOnlySpan<char> serverName, ReadOnlySpan<char> loadingURL, ReadOnlySpan<char> mapName, byte maxPlayers, short playerSlots, ReadOnlySpan<char> gamemode) {
#if MENU_DLL
filesystem.Gamemodes().OnJoinServer(gamemode);
Expand Down
10 changes: 7 additions & 3 deletions Game.Shared/GarrysMod/LuaAngle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ public static partial class LuaAngle

public static void Push_Angle(in QAngle ang) => g_Lua!.PushAngle(in ang);

static char FirstChar(ReadOnlySpan<char> str) => str.IsEmpty ? '\0' : str[0];
static char FirstChar(int stackPos) {
g_Lua!.CheckType(stackPos, LuaType.String);
ReadOnlySpan<byte> str = g_Lua.GetStringBytes(stackPos);
return str.IsEmpty ? '\0' : (char)str[0];
}

[LuaMethod]
static int Angle____newindex(ILuaInterface lua) {
Expand All @@ -37,7 +41,7 @@ static int Angle____newindex(ILuaInterface lua) {
return 0;
}
else {
switch (FirstChar(g_Lua.CheckString(2))) {
switch (FirstChar(2)) {
case 'P': case 'X': case 'p': case 'x': index = 0; break;
case 'Y': case 'y': index = 1; break;
case 'R': case 'Z': case 'r': case 'z': index = 2; break;
Expand Down Expand Up @@ -65,7 +69,7 @@ static int Angle____index(ILuaInterface lua) {
return 0;
}
else {
switch (FirstChar(g_Lua.CheckString(2))) {
switch (FirstChar(2)) {
case 'P': case 'X': case 'p': case 'x': index = 0; break;
case 'Y': case 'y': index = 1; break;
case 'R': case 'Z': case 'r': case 'z': index = 2; break;
Expand Down
5 changes: 3 additions & 2 deletions Game.Shared/GarrysMod/LuaClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ static int __index_Derived(ILuaInterface lua) {
}

public object? Get(int stackPos) {
if (!g_Lua!.IsType(stackPos, Type)) {
object? obj = g_Lua!.GetObjectUserType<object>(stackPos, Type);
if (obj == null && !g_Lua.IsType(stackPos, Type)) {
g_Lua.TypeError(Name, stackPos);
return null;
}
return g_Lua.GetObjectUserType<object>(stackPos, Type);
return obj;
}

public ref T GetValue<T>(int stackPos) where T : unmanaged {
Expand Down
Loading
Loading