|
| 1 | +--- |
| 2 | +uid: core_concepts_overview |
| 3 | +title: "Core Concepts" |
| 4 | +--- |
| 5 | + |
| 6 | +# Core Concepts |
| 7 | + |
| 8 | +Gmod.NET modules are essentially .NET (currently version 6.0) class libraries which reference [GmodNET.API nuget package](https://www.nuget.org/packages/GmodNET.API/) as their dependency. |
| 9 | +Each module should contain one or more implementation of [GmodNET.API.IModule](xref:GmodNET.API.IModule) interface. |
| 10 | +Gmod.NET runtime will instantiate each class derived from `IModule` by calling its parameterless constructor. |
| 11 | + |
| 12 | +## Module lifecycle |
| 13 | + |
| 14 | +At some moment Gmod.NET runtime will call each module's [Load](xref:GmodNET.API.IModule#GmodNET_API_IModule_Load_GmodNET_API_ILua_System_Boolean_GmodNET_API_ModuleAssemblyLoadContext_) method. |
| 15 | +```csharp |
| 16 | +public void Load(ILua lua, bool is_serverside, ModuleAssemblyLoadContext assembly_context) |
| 17 | +``` |
| 18 | +This method can be thought as an entry point of the module where all initial setup is performed. |
| 19 | +The `lua` parameter will receive an instance of [GmodNET.API.ILua](xref:GmodNET.API.ILua) interface which should be used to communicate with Garry's Mod. |
| 20 | +The `is_serverside` parameter can be used to easily check whether module is running in server or client environment. |
| 21 | +The `assembly_context` parameter receives an instance of [GmodNET.API.ModuleAssemblyLoadContext](xref:GmodNET.API.ModuleAssemblyLoadContext) in which module was loaded, |
| 22 | +which can be used for some advanced scenarios or debugging. |
| 23 | + |
| 24 | +All modules also must implement [Unload](xref:GmodNET.API.IModule#GmodNET_API_IModule_Unload_GmodNET_API_ILua_) method. |
| 25 | +```csharp |
| 26 | +public void Unload(ILua lua); |
| 27 | +``` |
| 28 | +This method is called by Gmod.NET runtime whenever module is unloaded manually or game/server session is over. |
| 29 | +The `lua` parameter receives an instance of [GmodNET.API.ILua](xref:GmodNET.API.ILua) interface which can be used to safely perform clenup tasks, |
| 30 | +like unregistering callbacks, removing data from global tables, etc. |
| 31 | + |
| 32 | +In order to load Gmod.NET module one should load Gmod.NET runtime via Lua using `require` function: |
| 33 | +```lua |
| 34 | +require("dotnet") |
| 35 | +``` |
| 36 | +Then module can be loaded via Lua [dotnet.load](xref:lua_api_dotnet#loadstring) function. |
| 37 | +Module also can be unloaded with Lua [dotnet.unload](xref:lua_api_dotnet#unloadstring) function. |
| 38 | +Generally, modules can be loaded and unloaded at any moment, thus enabling hot-reloading and hot-switching for faster development. |
| 39 | + |
| 40 | +## Module file-structure and dependencies |
| 41 | + |
| 42 | +Modules can bring any .NET or native dependencies (usually acquired as NuGet packages). |
| 43 | + |
| 44 | +Modules' binaries and their placement must satisfy the following convention: module's main dll, its `%modules_name%.deps.json`, |
| 45 | +and all dependencies must be placed in `Garrys_mod_root_folder/garrysmod/lua/bin/Modules/%exact_name_of_your_module_without_dll%/` folder. |
| 46 | +For example, if your module is called `TestNETModule.dll` and it depends on `SomeLibrary.dll`, |
| 47 | +then `TestNETModules.dll`, `TestNETModule.deps.json`, `GmodNET.API.dll` and `SomeLibrary.dll` must be in `garrysmod/lua/bin/Modules/TestNETModule/` folder. |
| 48 | +Missing dependencies, `deps.json` files, or wrong folder naming may lead to inability of Gmod.NET runtime to load module. |
| 49 | + |
| 50 | +Gmod.NET runtime will locate and load module's managed and native dependencies into isolated `AssemblyLoadContxt` by reading `*.deps.json` file and using [standard .NET dependency resolution process](https://github.com/dotnet/runtime/blob/main/docs/design/features/host-probing.md). |
| 51 | + |
| 52 | +If Development environment is enabled, Gmod.NET runtime can load modules from any location, not only `garrysmod/lua/bin/Modules` folder. |
| 53 | +For more info see [Development environment feature](xref:runtime_features_development_environment). |
| 54 | + |
| 55 | +## Module permissions and safety |
| 56 | + |
| 57 | +Under the hood Gmod.NET uses standard Garry's Mod C++ API. |
| 58 | +It means that all Gmod.NET modules have the same permissions and privileges as host Garry's Mod process, including filesystem access, networking, etc. |
| 59 | +Thus, ***never run untrusted Gmod.NET modules or start Garry's Mod as an administrator or root***. |
0 commit comments