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