annotate Libs/LibStub/LibStub.lua @ 25:ac501d71c890 tip

Added tag v8.2.0.024 for changeset 389dcaeebc47
author Tercioo
date Fri, 28 Jun 2019 20:07:27 -0300
parents 3596dadf9a90
children
rev   line source
Tercioo@22 1 -- $Id: LibStub.lua 103 2014-10-16 03:02:50Z mikk $
Tercioo@22 2 -- LibStub is a simple versioning stub meant for use in Libraries. http://www.wowace.com/addons/libstub/ for more info
Tercioo@22 3 -- LibStub is hereby placed in the Public Domain
Tercioo@22 4 -- Credits: Kaelten, Cladhaire, ckknight, Mikk, Ammo, Nevcairiel, joshborke
tercio@0 5 local LIBSTUB_MAJOR, LIBSTUB_MINOR = "LibStub", 2 -- NEVER MAKE THIS AN SVN REVISION! IT NEEDS TO BE USABLE IN ALL REPOS!
tercio@0 6 local LibStub = _G[LIBSTUB_MAJOR]
tercio@0 7
Tercioo@22 8 -- Check to see is this version of the stub is obsolete
tercio@0 9 if not LibStub or LibStub.minor < LIBSTUB_MINOR then
tercio@0 10 LibStub = LibStub or {libs = {}, minors = {} }
tercio@0 11 _G[LIBSTUB_MAJOR] = LibStub
tercio@0 12 LibStub.minor = LIBSTUB_MINOR
tercio@0 13
Tercioo@22 14 -- LibStub:NewLibrary(major, minor)
Tercioo@22 15 -- major (string) - the major version of the library
Tercioo@22 16 -- minor (string or number ) - the minor version of the library
Tercioo@22 17 --
Tercioo@22 18 -- returns nil if a newer or same version of the lib is already present
Tercioo@22 19 -- returns empty library object or old library object if upgrade is needed
tercio@0 20 function LibStub:NewLibrary(major, minor)
tercio@0 21 assert(type(major) == "string", "Bad argument #2 to `NewLibrary' (string expected)")
tercio@0 22 minor = assert(tonumber(strmatch(minor, "%d+")), "Minor version must either be a number or contain a number.")
tercio@0 23
tercio@0 24 local oldminor = self.minors[major]
tercio@0 25 if oldminor and oldminor >= minor then return nil end
tercio@0 26 self.minors[major], self.libs[major] = minor, self.libs[major] or {}
tercio@0 27 return self.libs[major], oldminor
tercio@0 28 end
tercio@0 29
Tercioo@22 30 -- LibStub:GetLibrary(major, [silent])
Tercioo@22 31 -- major (string) - the major version of the library
Tercioo@22 32 -- silent (boolean) - if true, library is optional, silently return nil if its not found
Tercioo@22 33 --
Tercioo@22 34 -- throws an error if the library can not be found (except silent is set)
Tercioo@22 35 -- returns the library object if found
tercio@0 36 function LibStub:GetLibrary(major, silent)
tercio@0 37 if not self.libs[major] and not silent then
tercio@0 38 error(("Cannot find a library instance of %q."):format(tostring(major)), 2)
tercio@0 39 end
tercio@0 40 return self.libs[major], self.minors[major]
tercio@0 41 end
tercio@0 42
Tercioo@22 43 -- LibStub:IterateLibraries()
Tercioo@22 44 --
Tercioo@22 45 -- Returns an iterator for the currently registered libraries
Tercioo@22 46 function LibStub:IterateLibraries()
Tercioo@22 47 return pairs(self.libs)
Tercioo@22 48 end
Tercioo@22 49
tercio@0 50 setmetatable(LibStub, { __call = LibStub.GetLibrary })
tercio@0 51 end