dreamworkAPI
Ctrl K
GitHub
classDocs / Addon

Addon

classsharedmenusource
Object → Addon

The Garry's Mod addon (.gma) archive.

Parses and builds the same format as gmad, legacy LZMA compressed addons are decompressed on parse.

lua
local addon = Addon()
addon.title = "My Addon"
addon.type = "tool"
addon.tags = { "fun", "build" }
addon:addFile( "lua/autorun/test.lua", "print( 'hello' )" )

local data = addon:compile()
print( Addon( data ):readFile( "lua/autorun/test.lua" ) ) -- print( 'hello' )

Constructor

Addon(data: string?, ratio: number?): Addon

Fields

NameTypeDescription
format_versionintegerThe version of the .gma format, 3 by default.
steamidsteam.IdentifierThe Steam ID of the author, all zero bytes by default (gmad doesn't set it).
timestampintegerThe Unix timestamp of the creation in seconds.
required_contentstring[]The content required by the addon, unused by the game.
titlestringThe title of the addon.
descriptionstringThe description of the addon.
typestringThe type of the addon, one of AddonClass.Types.
tagsstring[]The tags of the addon, from 1 to 3 of AddonClass.Tags.
authorstringThe author of the addon, unused by the game.
versionintegerThe version of the addon, unused by the game.
filesAddon.File[]The files of the addon.
file_countintegerThe number of files.
sizeintegerThe size of the addon data in bytes without the trailing checksum, 0 until parsed or compiled.
checksumintegerThe CRC-32 checksum of the addon data, 0 until parsed or compiled.
legacybooleantrue if the parsed addon was LZMA compressed.
datastring | nilThe parsed addon data, the content of not yet read files.
file_indexestable<string, integer>The indexes of files in files by their paths.
SignatureintegerGMAD as little endian uint32.
VersionintegerThe latest supported format version.

Static functions

isAllowedPathChecks if the file path is allowed by the gmad whitelist.
isValidTagChecks if the tag is allowed by gmad.
isValidTypeChecks if the type is allowed by gmad.

#Addon.isAllowedPath

sharedmenunodiscardsource
function Addon.isAllowedPath(file_path: string): boolean

Checks if the file path is allowed by the gmad whitelist.

Parameters
NameTypeDescription
file_pathstringThe path of the file inside of the addon, e.g. lua/autorun/test.lua.
Returns
TypeNameDescription
booleanis_allowedtrue if the path is allowed.

#Addon.isValidTag

sharedmenunodiscardsource
function Addon.isValidTag(tag_name: string): boolean

Checks if the tag is allowed by gmad.

Parameters
NameTypeDescription
tag_namestringThe addon tag, e.g. fun.
Returns
TypeNameDescription
booleanis_validtrue if the tag is allowed.

#Addon.isValidType

sharedmenunodiscardsource
function Addon.isValidType(type_name: string): boolean

Checks if the type is allowed by gmad.

Parameters
NameTypeDescription
type_namestringThe addon type, e.g. tool.
Returns
TypeNameDescription
booleanis_validtrue if the type is allowed.

Methods

addFileAdds the file to the addon, replaces the existing file with the same path.
compileBuilds the .gma data of the addon, same as gmad create.
getFileReturns the file of the addon.
isAllowedChecks the addon against the gmad rules: an allowed type, 1 - 3 unique allowed tags and whitelisted file paths.
mountCompiles the addon into a temporary .gma in /garrysmod/data/dreamwork/tmp/ and mounts it into the game.
parseReplaces the addon with the parsed .gma data.
readFileReturns the content of the file of the addon.
removeFileRemoves the file from the addon.
verifyChecks the CRC-32 checksums of the parsed addon data and all files.

#Addon:addFile

sharedmenusource
function Addon:addFile(file_path: string, content: string): Addon.File

Adds the file to the addon, replaces the existing file with the same path.

The path is lowercased, same as gmad does.

Parameters
NameTypeDescription
file_pathstringThe path of the file inside of the addon, e.g. lua/autorun/test.lua.
contentstringThe file content.
Returns
TypeNameDescription
Addon.FilefileThe added file.

#Addon:compile

sharedmenunodiscardsource
function Addon:compile(): string

Builds the .gma data of the addon, same as gmad create.

Updates size and checksum of the addon.

Doesn't check type, tags and file paths, use validate for that.

Returns
TypeNameDescription
stringdataThe .gma data.

#Addon:getFile

sharedmenunodiscardsource
function Addon:getFile(file_path: string): Addon.File | nil

Returns the file of the addon.

Parameters
NameTypeDescription
file_pathstringThe path of the file inside of the addon.
Returns
TypeNameDescription
Addon.File | nilfileThe file, nil if there is no such file.

#Addon:isAllowed

sharedmenunodiscardsource
function Addon:isAllowed(): boolean, string | nil

Checks the addon against the gmad rules: an allowed type, 1 - 3 unique allowed tags and whitelisted file paths.

Returns
TypeNameDescription
booleanis_validtrue if the addon follows the rules.
string | nilreasonThe reason why the addon is invalid.

#Addon:mount

sharedmenuasyncsource
async function Addon:mount(): boolean, string[]

Compiles the addon into a temporary .gma in /garrysmod/data/dreamwork/tmp/ and mounts it into the game.

The file is written asynchronously when called from a coroutine. Mounted addons cannot be unmounted, so the file stays until the game is closed.

Returns
TypeNameDescription
booleansuccesstrue if the addon was mounted.
string[]filesIf successful, a list of files that have been mounted.

#Addon:parse

sharedmenuasyncsource
async function Addon:parse(data: string, ratio?: number)

Replaces the addon with the parsed .gma data.

Legacy LZMA compressed addons are decompressed first, on a separate thread when HolyLib is installed and the call is made from a coroutine.

File contents are not copied, they are read from the data on demand by readFile.

ratio limits how much a legacy addon may expand, see lzma.decompress; pass a value like 0.99 for untrusted data, since a few bytes of crafted lzma data can claim up to 4 GiB of output.

Parameters
NameTypeDescription
datastringThe .gma data.
ratiooptionalnumberThe maximum allowed compression ratio of legacy addons, no limit by default.

#Addon:readFile

sharedmenunodiscardsource
function Addon:readFile(file_path: string): string | nil

Returns the content of the file of the addon.

Parameters
NameTypeDescription
file_pathstringThe path of the file inside of the addon.
Returns
TypeNameDescription
string | nilcontentThe file content, nil if there is no such file.

#Addon:removeFile

sharedmenusource
function Addon:removeFile(file_path: string): boolean

Removes the file from the addon.

Parameters
NameTypeDescription
file_pathstringThe path of the file inside of the addon.
Returns
TypeNameDescription
booleanremovedtrue if the file was removed, false if there is no such file.

#Addon:verify

sharedmenunodiscardsource
function Addon:verify(): boolean, string | nil

Checks the CRC-32 checksums of the parsed addon data and all files.

Zero checksums are skipped, same as gmad.

Returns
TypeNameDescription
booleanis_validtrue if all checksums match.
string | nilfile_pathThe path of the first damaged file, nil if the addon data itself is damaged.
Generated from the dreamwork meta branch.