--[[ A package of utilities related to the Extension and its interaction with the server. ]]-- local ExtUtils = {} local cjson = require "cjson" function ExtUtils.slurpFile( name ) local f = assert( io.open( name, "rb" ) ) local content = f:read( "*all" ) f:close() return content end function ExtUtils.writeFile( name, content ) local f = assert( io.open( name, "w" ) ) local content = f:write( content ) f:close() end --[[ When an Extension is installed, the bundle is unpackaged and put in a directory under 'server.extensions.dir'. The GetArchDirFileName() function returns a path to a file in that directory. The Extension manifest is a JSON file with various bits of useful metadata that an Extension often needs, so this function gets it ready to use. ]]-- function getManifest() local fn = Helix.Core.Server.GetArchDirFileName( "manifest.json" ) return cjson.decode( ExtUtils.slurpFile( fn ) ) end -- Remove leading and trailing whitespace from a string. function trim( s ) return ( s:gsub( "^%s*(.-)%s*$", "%1" ) ) end --[[ Extensions have a single global configuration and one for each instance of the Extension. The GetInstanceConfigData() function returns a dictionary of the k/v pairs that are saved in the instance config that caused the Extension to be running. Examples of what might be in an instance config are the depot paths the Extension is activated for, or a list of users who the Extension is used on. ]]-- function getICfg() local cfg = {} for k, v in pairs( Helix.Core.Server.GetInstanceConfigData() ) do -- It's the Extension's responsibility to massage the data -- into the format it expects. Here, we make sure that there's -- no empty answers and that the whitespace has been trimmed off. if string.len( v ) > 0 then cfg[ k ] = trim( v ) end end cfg[ "ngroups" ] = 0 if cfg[ "groups" ] ~= nil then local groups = {} local n = 0 -- Create a table of groups whose members we target. for g in string.gmatch( cfg[ "groups" ], "%S+" ) do groups[ g ] = 1 n = n + 1 end cfg[ "groups" ] = groups cfg[ "ngroups" ] = n end return cfg end -- Return a string we can use to identify the Extension. function ExtUtils.getID() return ExtUtils.manifest[ "name" ] .. "/" .. ExtUtils.manifest[ "key" ] .. "/" .. ExtUtils.manifest[ "version_name" ] end function ExtUtils.msgHeader() return ExtUtils.getID() .. ": " end ExtUtils.manifest = {} ExtUtils.iCfgData = {} --[[ An Extension is loaded and can be executed multiple times on a single client connection, so in order to minimize the amount of work it does, we only initialize ourselves once at the beginning (read the manifest, etc). ]]-- function ExtUtils.init() if ExtUtils.iCfgData[ "specName" ] == nil then ExtUtils.iCfgData = getICfg() -- todo: split the user list into another table end if ExtUtils.manifest[ "key" ] == nil then ExtUtils.manifest = getManifest() end end return ExtUtils
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#1 | 30808 | C. Thomas Tyler |
Promoted from 2019.1/... to main/... |
||
//guest/perforce_software/extensions/2019.1/login_motd/ExtUtils.lua | |||||
#1 | 25601 | Jason Gibson |
Import example Extensions. p19.1@1797741 |