Skip to content

Commit 4356d7c

Browse files
authored
Feature/add function argument parameters (#40)
* Add function argument params for `http` library * Add function argument params for `concommand.Add` * Add function argument params for `cvars.AddChangeCallback` * Add function argument params for `duplicator` library * Add function argument params for `net.Receive` * Add function argument params for `saverestore` library * Add function argument params for `search.AddProvider` * Add function argument params for `sound` library * Add function argument params for `spawnmenu` library * Add function argument params for `steamworks` library * Add function argument params for `undo.AddFunction` * Don't override `duplicator.RegisterConstraint` actually The callback receives `...` arguments, which is too complex for LuaLS to handle. * Make `len` in `net.Receive` callback an `integer` Unless you can have a fraction of a bit, it makes sense to put an integer here. * Add function arg param types for `spawnmenu.PopulateFromTextFiles`
1 parent 604791d commit 4356d7c

19 files changed

+293
-0
lines changed

custom/concommand.Add.lua

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---[SHARED AND MENU] Creates a console command that runs a function in lua with optional autocompletion function and help text.
2+
---
3+
--- This will fail if the concommand was previously removed with concommand.Remove in a different realm (creating a command on the client that was removed from the server and vice-versa).
4+
---
5+
---[(View on wiki)](https://wiki.facepunch.com/gmod/concommand.Add)
6+
---@param name string The command name to be used in console.
7+
---
8+
--- This cannot be a name of existing console command or console variable. It will silently fail if it is.
9+
---@param callback fun(ply: Player, cmd: string, args: table, argStr: string) The function to run when the concommand is executed. Arguments passed are:
10+
--- * Player ply - The player that ran the concommand. NULL entity if command was entered with the dedicated server console.
11+
--- * string cmd - The concommand string (if one callback is used for several concommands).
12+
--- * table args - A table of all string arguments.
13+
--- * string argStr - The arguments as a string.
14+
---@param autoComplete? fun(cmd: string, args: string): string[]? The function to call which should return a table of options for autocompletion. (Console_Command_Auto-completion)
15+
--- This only properly works on the client since it is **not** networked. Arguments passed are:
16+
--- * string cmd - The concommand this autocompletion is for.
17+
--- * string args - The arguments typed so far.
18+
---@param helpText? string The text to display should a user run 'help cmdName'.
19+
---@param flags? number Concommand modifier flags. See Enums/FCVAR.
20+
function concommand.Add(name, callback, autoComplete, helpText, flags) end

custom/cvars.AddChangeCallback.lua

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---[SHARED AND MENU] Adds a callback to be called when the named convar changes.
2+
---
3+
--- This does not callback convars in the menu state.
4+
--- This does not callback convars on the client with FCVAR_GAMEDLL and convars on the server without FCVAR_GAMEDLL.
5+
--- This does not callback convars on the client with FCVAR_REPLICATED.
6+
---
7+
---[(View on wiki)](https://wiki.facepunch.com/gmod/cvars.AddChangeCallback)
8+
---@param name string The name of the convar to add the change callback to.
9+
---@param callback fun(convar: string, oldValue: string, newValue: string) The function to be called when the convar changes. The arguments passed are:
10+
--- * string convar - The name of the convar.
11+
--- * string oldValue - The old value of the convar.
12+
--- * string newValue - The new value of the convar.
13+
---@param identifier? string If set, you will be able to remove the callback using cvars.RemoveChangeCallback. The identifier is not required to be globally unique, as it's paired with the actual name of the convar.
14+
function cvars.AddChangeCallback(name, callback, identifier) end
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
---[SHARED] Registers a function to be called on each of an entity's bones when duplicator.ApplyBoneModifiers is called.
2+
---
3+
--- This function is available to call on the client, but registered functions aren't used anywhere!
4+
---
5+
---[(View on wiki)](https://wiki.facepunch.com/gmod/duplicator.RegisterBoneModifier)
6+
---@param key any The type of the key doesn't appear to matter, but it is preferable to use a string.
7+
---@param boneModifier fun(ply: Player, ent: Entity, boneID: number, bone: PhysObj, data: table) Function called on each bone that an ent has. Called during duplicator.ApplyBoneModifiers.
8+
--- Function parameters are:
9+
--- * Player ply
10+
--- * Entity ent
11+
--- * number boneID
12+
--- * PhysObj bone
13+
--- * table data
14+
---
15+
---
16+
--- The data table that is passed to boneModifier is set with duplicator.StoreBoneModifier
17+
function duplicator.RegisterBoneModifier(key, boneModifier) end
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---[SHARED] This allows you to specify a specific function to be run when your SENT is pasted with the duplicator, instead of relying on the generic automatic functions.
2+
---
3+
--- Automatically calls duplicator.Allow for the entity class.
4+
---
5+
---[(View on wiki)](https://wiki.facepunch.com/gmod/duplicator.RegisterEntityClass)
6+
---@param name string The ClassName of the entity you wish to register a factory for
7+
---@param _function fun(ply: Player, ...) The factory function you want to have called. It should have the arguments (Player, ...) where ... is whatever arguments you request to be passed. It also should return the entity created, otherwise duplicator.Paste result will not include it!
8+
---@param ... any Strings of the names of arguments you want passed to function the from the Structures/EntityCopyData. As a special case, "Data" will pass the whole structure.
9+
function duplicator.RegisterEntityClass(name, _function, ...) end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---[SHARED] This allows you to register tweaks to entities. For instance, if you were making an "unbreakable" addon, you would use this to enable saving the "unbreakable" state of entities between duplications.
2+
---
3+
--- This function registers a piece of generic code that is run on all entities with this modifier. In order to have it actually run, use duplicator.StoreEntityModifier.
4+
---
5+
--- This function does nothing when run clientside.
6+
---
7+
---[(View on wiki)](https://wiki.facepunch.com/gmod/duplicator.RegisterEntityModifier)
8+
---@param name string An identifier for your modification. This is not limited, so be verbose. `Person's 'Unbreakable' mod` is far less likely to cause conflicts than `unbreakable`
9+
---@param func fun(ply: Player, ent: Entity, data: table) The function to be called for your modification. It should have the arguments (`Player`, `Entity`, `Data`), where data is what you pass to duplicator.StoreEntityModifier.
10+
function duplicator.RegisterEntityModifier(name, func) end

custom/http.Fetch.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---[SHARED AND MENU] Launches an asynchronous **GET** request to a HTTP server.
2+
---
3+
--- HTTP requests returning a status code >= `400` are still considered a success and will call the Structures/HTTPRequest callback.
4+
---
5+
--- The Structures/HTTPRequest callback is usually only called on DNS or TCP errors (e.g. the website is unavailable or the domain does not exist).
6+
---
7+
--- A rough overview of possible Structures/HTTPRequest messages:
8+
--- * `invalid url` - Invalid/empty url ( no request was attempted )
9+
--- * `invalid request` - Steam HTTP lib failed to create a HTTP request
10+
--- * `error` - OnComplete callback's second argument, `bError`, is `true`
11+
--- * `unsuccessful` - OnComplete's first argument, `pResult->m_bRequestSuccessful`, returned `false`
12+
---
13+
--- This cannot send or receive multiple headers with the same name.
14+
--- HTTP-requests that respond with a large body may return an `unsuccessful` error. Try using the [Range](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range) header to download the file in chunks.
15+
---
16+
--- HTTP-requests to destinations on private networks (such as `192.168.0.1`, or `127.0.0.1`) won't work.
17+
---
18+
--- To enable HTTP-requests to destinations on private networks use Command Line Parameters `-allowlocalhttp`. (Dedicated servers only)
19+
---
20+
---[(View on wiki)](https://wiki.facepunch.com/gmod/http.Fetch)
21+
---@param url string The URL of the website to fetch.
22+
---@param onSuccess? fun(body: string, size: integer, headers: table<string, string>, code: integer) Function to be called on success. Arguments are
23+
--- * string body
24+
--- * number size - equal to string.len(body).
25+
--- * table headers
26+
--- * number code - The HTTP success code.
27+
---@param onFailure? fun(error: string) Function to be called on failure. Arguments are
28+
--- * string error - The error message.
29+
---@param headers? table KeyValue table for headers.
30+
function http.Fetch(url, onSuccess, onFailure, headers) end

custom/http.Post.lua

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---[SHARED AND MENU] Sends an asynchronous **POST** request to a HTTP server.
2+
---
3+
--- HTTP requests returning a status code >= `400` are still considered a success and will call the Structures/HTTPRequest callback.
4+
---
5+
--- The Structures/HTTPRequest callback is usually only called on DNS or TCP errors (e.g. the website is unavailable or the domain does not exist).
6+
---
7+
--- This cannot send or receive multiple headers with the same name.
8+
--- HTTP-requests that respond with a large body may return an `unsuccessful` error. Try using the [Range](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range) header to download the file in chunks.
9+
---
10+
--- HTTP-requests to destinations on private networks (such as `192.168.0.1`, or `127.0.0.1`) won't work.
11+
---
12+
--- To enable HTTP-requests to destinations on private networks use Command Line Parameters `-allowlocalhttp`. (Dedicated servers only)
13+
---
14+
---[(View on wiki)](https://wiki.facepunch.com/gmod/http.Post)
15+
---@param url string The url to of the website to post.
16+
---@param parameters table The post parameters (x-www-form-urlencoded) to be send to the server. **Keys and values must be strings**.
17+
---@param onSuccess? fun(body: string, size: integer, headers: table<string, string>, code: integer) Function to be called on success. Arguments are
18+
--- * string body
19+
--- * string size - equal to string.len(body).
20+
--- * table headers
21+
--- * number code - The HTTP success code.
22+
---@param onFailure? fun(error: string) Function to be called on failure. Arguments are
23+
--- * string error - The error message.
24+
---@param headers? table<string, string> KeyValue table for headers.
25+
function http.Post(url, parameters, onSuccess, onFailure, headers) end

custom/net.Receive.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---[SHARED] Adds a net message handler. Only one receiver can be used to receive the net message.
2+
--- The message-name is converted to lower-case so the message-names "`BigBlue`" and "`bigblue`" would be equal.
3+
--- You **must** put this function **outside** of any other function or hook for it to work properly unless you know what you are doing!
4+
---
5+
--- You **must** read information in the same order as you write it.
6+
---
7+
--- Each net message has a length limit of **64KB**!
8+
---
9+
---[(View on wiki)](https://wiki.facepunch.com/gmod/net.Receive)
10+
---@param messageName string The message name to hook to.
11+
---@param callback fun(len: integer, ply: Player) The function to be called if the specified message was received. Arguments are:
12+
---
13+
--- * number len - Length of the message, in bits.
14+
--- * Player ply - The player that sent the message, works **only** server-side.
15+
function net.Receive(messageName, callback) end

custom/saverestore.AddRestoreHook.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---[SHARED] Adds a restore/load hook for the Half-Life 2 save system.
2+
---
3+
---[(View on wiki)](https://wiki.facepunch.com/gmod/saverestore.AddRestoreHook)
4+
---@param identifier string The unique identifier for this hook.
5+
---@param callback fun(save: IRestore) The function to be called when an engine save is being loaded. It has one argument:
6+
---
7+
---
8+
--- IRestore save - The restore object to be used to read data from save file that is being loaded
9+
---
10+
---
11+
---
12+
---
13+
---
14+
--- You can also use those functions to read data:
15+
---
16+
---
17+
--- saverestore.ReadVar
18+
---
19+
---
20+
--- saverestore.ReadTable
21+
---
22+
---
23+
--- saverestore.LoadEntity
24+
function saverestore.AddRestoreHook(identifier, callback) end

custom/saverestore.AddSaveHook.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---[SHARED] Adds a save hook for the Half-Life 2 save system. You can this to carry data through level transitions in Half-Life 2.
2+
---
3+
---[(View on wiki)](https://wiki.facepunch.com/gmod/saverestore.AddSaveHook)
4+
---@param identifier string The unique identifier for this hook.
5+
---@param callback fun(save: ISave) The function to be called when an engine save is being saved. It has one argument:
6+
---
7+
---
8+
--- ISave save - The save object to be used to write data to the save file that is being saved
9+
---
10+
---
11+
---
12+
---
13+
---
14+
--- You can also use those functions to save data:
15+
---
16+
---
17+
--- saverestore.WriteVar
18+
---
19+
---
20+
--- saverestore.WriteTable
21+
---
22+
---
23+
--- saverestore.SaveEntity
24+
function saverestore.AddSaveHook(identifier, callback) end

0 commit comments

Comments
 (0)