Completely revamped the new version system to be consistent and consolidated among ULib and plugins.

This commit is contained in:
Nayruden 2015-12-13 18:35:35 -05:00
parent 64527a2dac
commit c4c3c99140
7 changed files with 157 additions and 139 deletions

View File

@ -50,7 +50,7 @@ v2.60 - *(00/00/00)*
* [ADD] CAMI support.
* [ADD] "noMount" parameter to file-related APIs.
* [ADD] ULibGetUser(s)CustomKeyword hooks (Thanks, LuaTenshi).
* [ADD] ULib.getVersion().
* [ADD] Dynamic and consistent versioning for ULib plugins with automatic update checking.
* [FIX] The usual random slew of Garry-breakages (Thanks, Fuzzik).
* [FIX] An assumption regarding player authentication that led to a player's group being reset to user sometimes.
* [FIX] Garry API change for ULib.findinDir (Thanks, ascentechit).

View File

@ -12,11 +12,10 @@ include( "ulib/client/cl_util.lua" )
include( "ulib/client/draw.lua" )
include( "ulib/shared/commands.lua" )
include( "ulib/shared/sh_ucl.lua" )
include( "ulib/shared/plugin.lua" )
include( "ulib/shared/cami_global.lua" )
include( "ulib/shared/cami_ulib.lua" )
Msg( string.format( "You are running ULib version %.2f.\n", ULib.VERSION ) )
--Shared modules
local files = file.Find( "ulib/modules/*.lua", "LUA" )
if #files > 0 then

View File

@ -24,35 +24,6 @@ end
net.Receive( "URPC", ULibRPC )
function ULib.getVersionData( build, workshop )
ULib.build = build
ULib.usingWorkshop = workshop
end
--[[
Function: getVersion
TODO
]]
function ULib.getVersion()
local versionStr
local build = ULib.build
local usingWorkshop = ULib.usingWorkshop
if ULib.RELEASE then
versionStr = string.format( "v%.02f", ULib.VERSION )
elseif usingWorkshop then
versionStr = string.format( "v%.02fw", ULib.VERSION )
elseif build then -- It's not release and it's not workshop
versionStr = string.format( "v%.02fd (%s)", ULib.VERSION, os.date( "%x", build ) )
else -- Not sure what this version is, but it's not a release
versionStr = string.format( "v%.02fd", ULib.VERSION )
end
return versionStr, ULib.VERSION, build, usingWorkshop
end
--[[
Function: umsgRcv

View File

@ -43,6 +43,8 @@ if not ULib then
include( "server/player_ext.lua" )
Msg( "// server/entity_ext.lua //\n" )
include( "server/entity_ext.lua" )
Msg( "// shared/plugin.lua //\n" )
include( "ulib/shared/plugin.lua" )
Msg( "// shared/cami_global.lua //\n" )
include( "shared/cami_global.lua" )
Msg( "// shared/cami_ulib.lua //\n" )

View File

@ -151,110 +151,6 @@ function ULib.getAllReadyPlayers()
end
--[[
Function: getVersion
TODO
]]
function ULib.getVersion()
local versionStr
local build = nil
local usingWorkshop = false
-- Get workshop information, if available
local addons = engine.GetAddons()
for i=1, #addons do
-- Ideally we'd use the "wsid" from this table
-- But, as of 19 Nov 2015, that is broken, so we'll work around it
if addons[i].file:find(tostring(ULib.WORKSHOPID)) then
usingWorkshop = true
end
end
-- If we have good build data, set it in "build"
if ULib.fileExists( "ulib.build" ) then
local buildStr = ULib.fileRead( "ulib.build" )
local buildNum = tonumber(buildStr)
-- Make sure the time is something reasonable -- between the year 2014 and 2128
if buildNum and buildNum > 1400000000 and buildNum < 5000000000 then
build = buildNum
end
end
if ULib.RELEASE then
versionStr = string.format( "v%.02f", ULib.VERSION )
elseif usingWorkshop then
versionStr = string.format( "v%.02fw", ULib.VERSION )
elseif build then -- It's not release and it's not workshop
versionStr = string.format( "v%.02fd (%s)", ULib.VERSION, os.date( "%x", build ) )
else -- Not sure what this version is, but it's not a release
versionStr = string.format( "v%.02fd", ULib.VERSION )
end
return versionStr, ULib.VERSION, build, usingWorkshop
end
local function sendVersionData( ply )
local _, _, build, workshop = ULib.getVersion()
ULib.clientRPC( ply, "ULib.getVersionData", build, workshop )
end
hook.Add( "PlayerInitialSpawn", "ULibSendVersionData", sendVersionData )
ULib.updateAvailable = false
local function advertiseNewVersion( ply )
if ply:IsAdmin() and ULib.updateAvailable and not ply.UlibUpdateAdvertised then
ULib.tsay( ply, "[ULib] There is an update available" )
ply.UlibUpdateAdvertised = true
end
end
hook.Add( ULib.HOOK_UCLAUTH, "ULibAdvertiseUpdate", advertiseNewVersion )
local function ulibUpdateCheck( body, len, headers, httpCode )
if httpCode ~= 200 then
return
end
timer.Remove( "ULibUpdateChecker" )
hook.Remove( "Initialize", "ULibUpdateChecker" )
local currentBuild = tonumber(body)
if not currentBuild then return end
local _, _, myBuild = ULib.getVersion()
if myBuild < currentBuild then
ULib.updateAvailable = true
Msg( "[ULib] There is an update available\n" )
local players = player.GetAll()
for i=1, #players do
advertiseNewVersion( players[ i ] )
end
end
end
local function ulibUpdateErr()
timer.Remove( "ULibUpdateChecker" )
hook.Remove( "Initialize", "ULibUpdateChecker" )
end
local function downloadForUlibUpdateCheck()
local _, _, myBuild, workshop = ULib.getVersion()
if not myBuild or workshop then
return
end
if ULib.RELEASE then
http.Fetch( "https://teamulysses.github.io/ulib/ulib.build", ulibUpdateCheck, ulibUpdateErr )
else
http.Fetch( "https://raw.githubusercontent.com/TeamUlysses/ulib/master/ulib.build", ulibUpdateCheck, ulibUpdateErr )
end
end
hook.Add( "Initialize", "ULibUpdateChecker", downloadForUlibUpdateCheck )
timer.Create( "ULibUpdateChecker", 9, 10, downloadForUlibUpdateCheck )
ULib.repcvars = ULib.repcvars or {} -- This is used for <ULib.replicatedWithWritableCvar> in order to keep track of valid cvars and access info.
local repcvars = ULib.repcvars
local repCvarServerChanged

View File

@ -6,8 +6,9 @@
ULib = ULib or {}
ULib.RELEASE = false -- Don't access these two directly, use ULib.getVersion()
ULib.RELEASE = false -- Don't access these two directly, use ULib.pluginVersionStr("ULib")
ULib.VERSION = 2.52
ULib.AUTOMATIC_UPDATE_CHECKS = true
ULib.ACCESS_ALL = "user"
ULib.ACCESS_OPERATOR = "operator"
@ -253,8 +254,6 @@ ULib.HOOK_GETUSERS_CUSTOM_KEYWORD = "ULibGetUsersCustomKeyword"
]]
ULib.HOOK_GETUSER_CUSTOM_KEYWORD = "ULibGetUserCustomKeyword"
ULib.WORKSHOPID = 557962238
--[[
Section: UCL Helpers

151
lua/ulib/shared/plugin.lua Normal file
View File

@ -0,0 +1,151 @@
--[[
Title: Plugin Helpers
Some useful functions for ULib plugins to use for doing plugin-type things.
]]
ULib.plugins = {} -- Any registered plugins go here
--[[
Function: registerPlugin
TODO
]]
function ULib.registerPlugin( name, pluginData )
if not ULib.plugins[ name ] then
ULib.plugins[ name ] = pluginData
else
table.Merge( ULib.plugins[ name ], pluginData )
pluginData = ULib.plugins[ name ]
end
--ULib.plugins[ name ] = { version=version, isRelease=isRelease, author=author,
-- url=url, workshopid=workshopid, build=build, hideBuild=hideBuild, buildURL=buildURL, buildCallback=buildCallback }
if pluginData.workshopid then
-- Get workshop information, if available
local addons = engine.GetAddons()
for i=1, #addons do
local addon = addons[i]
-- Ideally we'd use the "wsid" from this table
-- But, as of 19 Nov 2015, that is broken, so we'll work around it
if addon.mounted and addon.file:find(tostring(pluginData.workshopid)) then
ULib.plugins[ name ].usingWorkshop = true
end
end
end
if SERVER then
ULib.clientRPC( nil, "ULib.registerPlugin", name, pluginData )
end
end
if SERVER then
local function sendRegisteredPlugins( ply )
for name, pluginData in pairs (ULib.plugins) do
ULib.clientRPC( ply, "ULib.registerPlugin", name, pluginData )
end
end
hook.Add( "PlayerInitialSpawn", "ULibSendRegisteredPlugins", sendRegisteredPlugins )
end
local ulibDat = {
version = string.format( "%.2f", ULib.VERSION ),
isRelease = ULib.RELEASE,
author = "Team Ulysses",
url = "http://ulyssesmod.net",
workshopid = 557962238,
build = tonumber(ULib.fileRead( "ulib.build" )),
buildURL = ULib.RELEASE and "https://teamulysses.github.io/ulib/ulib.build" or "https://raw.githubusercontent.com/TeamUlysses/ulib/master/ulib.build",
--buildCallback = nil
}
ULib.registerPlugin( "ULib", ulibDat )
--[[
Function: pluginVersionStr
TODO
]]
function ULib.pluginVersionStr( name )
local dat = ULib.plugins[ name ]
if not dat then return nil end
if dat.isRelease then
return string.format( "v%s", dat.version )
elseif dat.usingWorkshop then
return string.format( "v%sw", dat.version )
elseif dat.build and not dat.hideBuild then -- It's not release and it's not workshop
local build = dat.build
if build > 1400000000 and build < 5000000000 then -- Probably a date -- between 2014 and 2128
build = os.date( "%x", build )
end
return string.format( "v%sd (%s)", dat.version, build )
else -- Not sure what this version is, but it's not a release
return string.format( "v%sd", dat.version )
end
end
local function receiverFor( plugin )
local function receiver( body, len, headers, httpCode )
local buildOnline = tonumber( body )
if not buildOnline then return end
plugin.buildOnline = buildOnline
if plugin.buildCallback then
plugin.buildCallback( plugin.build, buildOnline )
end
end
return receiver
end
--[[
Function: updateCheck
TODO
]]
function ULib.updateCheck( name, url )
local plugin = ULib.plugins[ name ]
if not plugin then return nil end
if plugin.buildOnline then return nil end
http.Fetch( url, receiverFor( plugin ) )
return true
end
local function httpCheck( body, len, headers, httpCode )
if httpCode ~= 200 then
return
end
timer.Remove( "ULibPluginUpdateChecker" )
hook.Remove( "Initialize", "ULibPluginUpdateChecker" )
-- Okay, the HTTP library is functional and we can reach out. Let's check for updates.
for name, plugin in pairs (ULib.plugins) do
if plugin.buildURL then
ULib.updateCheck( name, plugin.buildURL )
end
end
end
local function httpErr()
-- Assume major problem and give up
timer.Remove( "ULibPluginUpdateChecker" )
hook.Remove( "Initialize", "ULibPluginUpdateChecker" )
end
local function downloadForUlibUpdateCheck()
http.Fetch( "http://google.com", httpCheck, httpErr )
end
if ULib.AUTOMATIC_UPDATE_CHECKS then
hook.Add( "Initialize", "ULibPluginUpdateChecker", downloadForUlibUpdateCheck )
timer.Create( "ULibPluginUpdateChecker", 9, 10, downloadForUlibUpdateCheck )
end