Utilities

Some utility functions.  Unlike the functions in misc.lua, this file only holds HL2 specific functions.

Summary
UtilitiesSome utility functions.
Functions
fileExistsChecks for the existence of a file by path.
fileReadReads a file and returns the contents.
fileWriteWrites file content.
fileAppendAppend to file content.
fileCreateDirCreate a directory.
fileDeleteDelete file contents.
fileIsDirIs file a directory?
execFileExecutes a file on the console.
execStringJust like execFile, except acts on newline-delimited strings.
execFileULibJust like execFile, except only for ULib-defined commands.
execStringULibJust like execString, except only for ULib-defined commands.
serializeSerializes a variable.
isSandboxReturns true if the current gamemode is sandbox or is derived from sandbox.
filesInDirReturns files in directory.
queueFunctionCallAdds a function call to the queue to be called.
namedQueueFunctionCallExactly like queueFunctionCall(), but allows for separately running queues to exist.
backupFileCopies a file to a backup file.
nameCheckCalls all ULibPlayerNameChanged hooks if a player changes their name.
getPlyByUID
pcallErrorAn adaptation of a function that used to exist before GM13, allows you to call functions safely and print errors (if it errors).

Functions

fileExists

function ULib.fileExists(f,
noMount)

Checks for the existence of a file by path.

Parameters

fThe path to check, rooted at the garry’s mod root directory.
noMount(Optional) If true, will not look in mounted directories.

Returns

True if the file exists, false otherwise.

Revisions

v2.51Initial revision (tired of Garry changing his API all the time).
v2.70Added noMount parameter to only look in mod directory.

fileRead

function ULib.fileRead(f,
noMount)

Reads a file and returns the contents.  This function is not very forgiving on providing oddly formatted filepaths.

Parameters

fThe file to read, rooted at the garrysmod directory.
noMount(Optional) If true, will not look in mounted directories.

Returns

The file contents or nil if the file does not exist.

Revisions

v2.51Initial revision (tired of Garry changing his API all the time).
v2.70Added noMount parameter to only look in mod directory.

fileWrite

function ULib.fileWrite(f,
content)

Writes file content.

Parameters

fThe file path to write to, rooted at the garrysmod directory.
contentThe content to write.

Revisions

v2.51Initial revision (tired of Garry changing his API all the time).

fileAppend

function ULib.fileAppend(f,
content)

Append to file content.

Parameters

fThe file path to append to, rooted at the garrysmod directory.
contentThe content to append.

Revisions

v2.51Initial revision (tired of Garry changing his API all the time).

fileCreateDir

function ULib.fileCreateDir(f)

Create a directory.

Parameters

fThe directory path to create, rooted at the garrysmod directory.

Revisions

v2.51Initial revision (tired of Garry changing his API all the time).

fileDelete

function ULib.fileDelete(f)

Delete file contents.

Parameters

fThe file path to delete, rooted at the garrysmod directory.

Revisions

v2.51Initial revision (tired of Garry changing his API all the time).

fileIsDir

function ULib.fileIsDir(f,
noMount)

Is file a directory?

Parameters

fThe file path to check, rooted at the garrysmod directory.
noMount(Optional) If true, will not look in mounted directories.

Returns

True if dir, false otherwise.

Revisions

v2.51Initial revision (tired of Garry changing his API all the time).
v2.70Added noMount parameter to only look in mod directory.

execFile

function ULib.execFile(f,
queueName,
noMount)

Executes a file on the console.  Use this instead of the “exec” command when the config lies outside the cfg folder.

Parameters

fThe file, relative to the garrysmod folder.
queueNameThe queue name to ULib.namedQueueFunctionCall to use.
noMount(Optional) If true, will not look in mounted directories.

Revisions

v2.40No longer strips comments, removed ability to execute on players.
v2.50Added option to conform to Garry’s API changes and queueName to specify queue name to use.
v2.51Removed option parameter.
v2.70Added noMount parameter to only look in mod directory.

execString

function ULib.execString(f,
queueName)

Just like execFile, except acts on newline-delimited strings.

Parameters

fThe string.
queueNameThe queue name to ULib.namedQueueFunctionCall to use.

Revisions

v2.40Initial.
v2.50Added queueName to specify queue name to use.  Removed ability to execute on players.

execFileULib

function ULib.execFileULib(f,
safeMode,
noMount)

Just like execFile, except only for ULib-defined commands.  It avoids the source engine command queue, and has an additional option to only execute commands marked as “safe” (up to the command author to properly define these).

Parameters

fThe file, relative to the garrysmod folder.
safeModeIf set to true, does not run “unsafe” commands.
noMount(Optional) If true, will not look in mounted directories.

Revisions

v2.62Initial.

execStringULib

function ULib.execStringULib(f,
safeMode)

Just like execString, except only for ULib-defined commands.  It avoids the source engine command queue, and has an additional option to only execute commands marked as “safe” (up to the command author to properly define these).

Parameters

fThe string.
safeModeIf set to true, does not run “unsafe” commands.

Revisions

v2.62Initial.

serialize

function ULib.serialize(v)

Serializes a variable.  It basically converts a variable into a runnable code string.  It works correctly with inline tables.

Parameters

vThe variable you wish to serialize

Returns

The string of the serialized variable

Revisions

v2.40Can now serialize entities and players

isSandbox

function ULib.isSandbox()

Returns true if the current gamemode is sandbox or is derived from sandbox.

filesInDir

function ULib.filesInDir(dir,
recurse,
noMount,
root)

Returns files in directory.

Parameters

dirThe dir to look for files in.
recurse(Optional, defaults to false) If true, searches directories recursively.
noMount(Optional) If true, will not look in mounted directories.
rootINTERNAL USE ONLY This helps with recursive functions.

Revisions

v2.10Initial (But dragged over from GM9 archive).
v2.40Fixed (was completely broken).
v2.50Now assumes paths relative to base folder.
v2.60Fix for Garry API-changes
v2.70Added noMount parameter to only look in mod directory.

queueFunctionCall

function ULib.queueFunctionCall(fn,
...)

Adds a function call to the queue to be called.  Guaranteed to be called sometime after the current frame.  Very handy when you need to delay a call for some reason.  Uses a think hook, but it’s only hooked when there’s stuff in the queue.

Parameters

fnThe function to call
...(Optional) The parameters to pass to the function

Revisions

v2.40Initial (But dragged over from UPS).

namedQueueFunctionCall

function ULib.namedQueueFunctionCall(queueName,
fn,
...)

Exactly like queueFunctionCall(), but allows for separately running queues to exist.

Parameters

queueNameThe unique name of the queue (the queue group)
fnThe function to call
...(Optional) The parameters to pass to the function

Revisions

v2.50Initial.

backupFile

function ULib.backupFile(f)

Copies a file to a backup file.  If a backup file already exists, makes incrementing numbered backup files.

Parameters

fThe file to backup, rooted in the garrysmod directory.

Returns

The pathname of the file it was backed up to.

Revisions

v2.40Initial.

nameCheck

function ULib.nameCheck(data)

Calls all ULibPlayerNameChanged hooks if a player changes their name.

Revisions

2.20Initial

getPlyByUID

function ULib.getPlyByUID(uid)

Parameters

uidThe uid to lookup.

Returns

The player that has the specified unique id, nil if none exists.

Revisions

v2.40Initial.

pcallError

function ULib.pcallError(...)

An adaptation of a function that used to exist before GM13, allows you to call functions safely and print errors (if it errors).

Parameters

...Arguments to pass to the function

Returns

The same thing regular pcall returns

Revisions

v2.50Initial.
function ULib.fileExists(f,
noMount)
Checks for the existence of a file by path.
function ULib.fileRead(f,
noMount)
Reads a file and returns the contents.
function ULib.fileWrite(f,
content)
Writes file content.
function ULib.fileAppend(f,
content)
Append to file content.
function ULib.fileCreateDir(f)
Create a directory.
function ULib.fileDelete(f)
Delete file contents.
function ULib.fileIsDir(f,
noMount)
Is file a directory?
function ULib.execFile(f,
queueName,
noMount)
Executes a file on the console.
function ULib.execString(f,
queueName)
Just like execFile, except acts on newline-delimited strings.
function ULib.execFileULib(f,
safeMode,
noMount)
Just like execFile, except only for ULib-defined commands.
function ULib.execStringULib(f,
safeMode)
Just like execString, except only for ULib-defined commands.
function ULib.serialize(v)
Serializes a variable.
function ULib.isSandbox()
Returns true if the current gamemode is sandbox or is derived from sandbox.
function ULib.filesInDir(dir,
recurse,
noMount,
root)
Returns files in directory.
function ULib.queueFunctionCall(fn,
...)
Adds a function call to the queue to be called.
function ULib.namedQueueFunctionCall(queueName,
fn,
...)
Exactly like queueFunctionCall(), but allows for separately running queues to exist.
function ULib.backupFile(f)
Copies a file to a backup file.
function ULib.nameCheck(data)
Calls all ULibPlayerNameChanged hooks if a player changes their name.
function ULib.getPlyByUID(uid)
function ULib.pcallError(...)
An adaptation of a function that used to exist before GM13, allows you to call functions safely and print errors (if it errors).
Close