comparison Libs/LibStub/LibStub.lua @ 22:3596dadf9a90 v8.1.0.022

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