Skip to content

Commit 3a1eff3

Browse files
committed
'Debugging Gmod.NET modules' section added
1 parent 151f384 commit 3a1eff3

File tree

7 files changed

+80
-1
lines changed

7 files changed

+80
-1
lines changed
40.2 KB
Loading
70.3 KB
Loading
40.2 KB
Loading
40.8 KB
Loading
260 KB
Loading
83.6 KB
Loading

docfx_project/articles/tutorials/debugging/index.md

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,83 @@ Here is how you can enable and use Development environment on Windows:
2929
5. To unload module, which which was loaded by absolute path, one should also use module's full path, like `lua_run dotnet.unload([[C:\Users\glebc\source\repos\TestModule\bin\Debug\net5.0\TestModule.dll]])`.
3030
[![A screenshot of Garry's Mod game console with unloaded Gmod.NET module](images/gmod3.png)](images/gmod3.png)
3131

32-
## Debugging Gmod.NET modules
32+
## Debugging Gmod.NET modules
33+
34+
Gmod.NET ships with full-featured .NET runtime by Microsoft. In particular, it means that one can use all kinds of .NET debuggers, including debuggers shipped with Visual Studio, Visual Studio for Mac, Visual Studio Code, and JetBrains Rider, or diagnostics tools while development. Here is an instruction on how you can use Visual Studio 2019 on Windows to debug your Gmod.NET module.
35+
36+
1. Start Garry's Mod in [Development environment](#development-environment).
37+
38+
2. Open your module's project in Visual Studio. For the sake of this tutorial we will be working with module named `ExampleModule` with the single source file `ExampleModule.cs`:
39+
```csharp
40+
using System;
41+
using GmodNET.API;
42+
43+
namespace ExampleModule
44+
{
45+
public class ExampleModule : IModule
46+
{
47+
public string ModuleName => nameof(ExampleModule);
48+
49+
public string ModuleVersion => "1.0.0";
50+
51+
int luaFuncInvokationCount;
52+
53+
public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context)
54+
{
55+
luaFuncInvokationCount = 0;
56+
57+
lua.PushGlobalTable();
58+
lua.PushManagedFunction(LuaFunc);
59+
lua.SetField(-2, "LuaFunc");
60+
lua.Pop(1);
61+
}
62+
63+
int LuaFunc(ILua lua)
64+
{
65+
luaFuncInvokationCount++;
66+
67+
lua.PushGlobalTable();
68+
lua.GetField(-1, "print");
69+
lua.PushString($"Hello, this is LuaFunc. Number of invokations of the function: {luaFuncInvokationCount}");
70+
lua.MCall(1, 0);
71+
lua.Pop(1);
72+
73+
return 0;
74+
}
75+
76+
public void Unload(ILua lua)
77+
{
78+
lua.PushGlobalTable();
79+
lua.PushNil();
80+
lua.SetField(-2, "LuaFunc");
81+
lua.Pop(1);
82+
}
83+
}
84+
}
85+
```
86+
This module adds global Lua function `LuaFunc` which greets user and says how many times it was called.
87+
88+
3. Add a breaking point somewhere in your code. We will add one at line 26, where `luaFuncInvokationCount`field is incremented.
89+
[![A screenshot of Visual Studio](images/vs1.png)](images/vs1.png)
90+
91+
4. Build your module in `Debug` configuration. In such configuration, additional information for debugger will be generated and no optimizations applied.
92+
93+
5. Load Gmod.NET Runtime in game, do not load your module yet.
94+
95+
6. In Visual Studio in the top context menu navigate to `Debug > Attach to Process...`
96+
[![A screenshot of Visual Studio](images/vs2.png)](images/vs2.png)
97+
98+
7. In the opened window, set debug type to `Managed (.NET Core, .NET 5+) code`.
99+
[![A screenshot of Visual Studio](images/vs3.png)](images/vs3.png)
100+
101+
8. Select `gmod.exe` process which has type `Managed (.NET Core, .NET 5+) code` in the list and press `Attach`.
102+
[![A screenshot of Visual Studio](images/vs4.png)](images/vs4.png)
103+
104+
9. Immediately after attaching debugger the breaking point may be marked as inactive due to Visual Studio's inability to locate debug symbols. It is OK, since our module (and thus its symbols) is not loaded yet.
105+
106+
10. Load module and invoke `LuaFunc`. The breakpoint will be hit and game process paused. From this point you can debug your module as any other .NET application. For example, you can explore and edit current values of variables.
107+
[![A screenshot of Visual Studio](images/vs5.png)](images/vs5.png)
108+
109+
11. If you want to update your code, unload your module, stop debugging in Visual Studio, make changes, build your updated module, load it with Gmod.NET Runtime, and reattach Visual Studio debugger by using `Debug > Reattach to Process` option in the Visual Studio top context menu.
110+
[![A screenshot of Visual Studio](images/vs6.png)](images/vs6.png)
111+
Debugger have to be reattached in order to make changes visible, otherwise Visual Studio will keep outdated symbols and breakpoints in the updated code won't be hit.

0 commit comments

Comments
 (0)