1 -- $Id: LibStub.lua 48018 2007-09-03 01:50:17Z mikk $
2 -- 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
4 -- Credits: Kaelten, Cladhaire, ckknight, Mikk, Ammo, Nevcairiel, joshborke
5 local LIBSTUB_MAJOR
, LIBSTUB_MINOR
= "LibStub", 2 -- NEVER MAKE THIS AN SVN REVISION! IT NEEDS TO BE USABLE IN ALL REPOS!
6 local LibStub
= _G
[LIBSTUB_MAJOR
]
8 -- Check to see is this version of the stub is obsolete
9 if not LibStub
or LibStub
.minor
< LIBSTUB_MINOR
then
10 LibStub
= LibStub
or {libs
= {}, minors
= {} }
11 _G
[LIBSTUB_MAJOR
] = LibStub
12 LibStub
.minor
= LIBSTUB_MINOR
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
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
20 function LibStub
:NewLibrary(major
, minor
)
21 assert(type(major
) == "string", "Bad argument #2 to `NewLibrary' (string expected)")
22 minor
= assert(tonumber(strmatch(minor
, "%d+")), "Minor version must either be a number or contain a number.")
24 local oldminor
= self
.minors
[major
]
25 if oldminor
and oldminor
>= minor
then return nil end
26 self
.minors
[major
], self
.libs
[major
] = minor
, self
.libs
[major
] or {}
27 return self
.libs
[major
], oldminor
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
34 -- throws an error if the library can not be found (except silent is set)
35 -- returns the library object if found
36 function LibStub
:GetLibrary(major
, silent
)
37 if not self
.libs
[major
] and not silent
then
38 error(("Cannot find a library instance of %q."):format(tostring(major
)), 2)
40 return self
.libs
[major
], self
.minors
[major
]
43 -- LibStub:IterateLibraries()
45 -- Returns an iterator for the currently registered libraries
46 function LibStub
:IterateLibraries()
47 return pairs(self
.libs
)
50 setmetatable(LibStub
, { __call
= LibStub
.GetLibrary
})