Tercioo@22: -- $Id: LibStub.lua 103 2014-10-16 03:02:50Z mikk $ Tercioo@22: -- LibStub is a simple versioning stub meant for use in Libraries. http://www.wowace.com/addons/libstub/ for more info Tercioo@22: -- LibStub is hereby placed in the Public Domain Tercioo@22: -- Credits: Kaelten, Cladhaire, ckknight, Mikk, Ammo, Nevcairiel, joshborke tercio@0: local LIBSTUB_MAJOR, LIBSTUB_MINOR = "LibStub", 2 -- NEVER MAKE THIS AN SVN REVISION! IT NEEDS TO BE USABLE IN ALL REPOS! tercio@0: local LibStub = _G[LIBSTUB_MAJOR] tercio@0: Tercioo@22: -- Check to see is this version of the stub is obsolete tercio@0: if not LibStub or LibStub.minor < LIBSTUB_MINOR then tercio@0: LibStub = LibStub or {libs = {}, minors = {} } tercio@0: _G[LIBSTUB_MAJOR] = LibStub tercio@0: LibStub.minor = LIBSTUB_MINOR tercio@0: Tercioo@22: -- LibStub:NewLibrary(major, minor) Tercioo@22: -- major (string) - the major version of the library Tercioo@22: -- minor (string or number ) - the minor version of the library Tercioo@22: -- Tercioo@22: -- returns nil if a newer or same version of the lib is already present Tercioo@22: -- returns empty library object or old library object if upgrade is needed tercio@0: function LibStub:NewLibrary(major, minor) tercio@0: assert(type(major) == "string", "Bad argument #2 to `NewLibrary' (string expected)") tercio@0: minor = assert(tonumber(strmatch(minor, "%d+")), "Minor version must either be a number or contain a number.") tercio@0: tercio@0: local oldminor = self.minors[major] tercio@0: if oldminor and oldminor >= minor then return nil end tercio@0: self.minors[major], self.libs[major] = minor, self.libs[major] or {} tercio@0: return self.libs[major], oldminor tercio@0: end tercio@0: Tercioo@22: -- LibStub:GetLibrary(major, [silent]) Tercioo@22: -- major (string) - the major version of the library Tercioo@22: -- silent (boolean) - if true, library is optional, silently return nil if its not found Tercioo@22: -- Tercioo@22: -- throws an error if the library can not be found (except silent is set) Tercioo@22: -- returns the library object if found tercio@0: function LibStub:GetLibrary(major, silent) tercio@0: if not self.libs[major] and not silent then tercio@0: error(("Cannot find a library instance of %q."):format(tostring(major)), 2) tercio@0: end tercio@0: return self.libs[major], self.minors[major] tercio@0: end tercio@0: Tercioo@22: -- LibStub:IterateLibraries() Tercioo@22: -- Tercioo@22: -- Returns an iterator for the currently registered libraries Tercioo@22: function LibStub:IterateLibraries() Tercioo@22: return pairs(self.libs) Tercioo@22: end Tercioo@22: tercio@0: setmetatable(LibStub, { __call = LibStub.GetLibrary }) tercio@0: end