### Create a basic library with LibStub Source: https://github.com/lua-wow/libstub/blob/master/README.md Initializes a new library and sets up internal state and frames if the library is not already loaded. ```lua local lib = LibStub:NewLibrary("MyLibrary-1.0", 1) if not lib then return -- already loaded and no upgrade necessary end lib.somearray = lib.somearray or {} if not lib.frame then lib.frame=CreateFrame("Frame") end function lib:SomeFunction() -- do stuff here end function lib:SomeOtherFunction() -- do other stuff here end local function OnUpdate() -- timing stuff here end lib.frame:SetScript("OnUpdate", OnUpdate); ``` -------------------------------- ### Download LibStub via Subversion Source: https://github.com/lua-wow/libstub/blob/master/README.md Command to checkout the LibStub repository using SVN. ```lua -- download from subversion svn checkout https://repos.curseforge.com/wow/libstub/trunk . ``` -------------------------------- ### Registering or Upgrading a Library Source: https://context7.com/lua-wow/libstub/llms.txt Use NewLibrary to register a library or perform an upgrade. The function returns the library table and the old minor version, or nil if a newer version is already present. ```lua -- Basic library registration local MAJOR, MINOR = "MyLibrary-1.0", 5 local lib, oldminor = LibStub:NewLibrary(MAJOR, MINOR) if not lib then return -- Already loaded and no upgrade necessary end -- Initialize or preserve existing data during upgrades lib.somedata = lib.somedata or {} lib.callbacks = lib.callbacks or {} -- Define library functions function lib:DoSomething(value) table.insert(self.somedata, value) return #self.somedata end function lib:GetAll() return self.somedata end -- Handle upgrades: oldminor is the previous version if upgrading if oldminor and oldminor < 3 then -- Migration from versions before 3 lib.somedata = lib.somedata or {} end -- Using SVN/Git revision tags for automatic versioning local lib2 = LibStub:NewLibrary("AnotherLib-1.0", "$Revision: 12345$") -- Extracts 12345 as the minor version number ``` -------------------------------- ### Handle library versioning with revision tags Source: https://github.com/lua-wow/libstub/blob/master/README.md Uses revision control tags for versioning, with a fallback mechanism to prevent version regression. ```lua local lib = LibStub:NewLibrary("MyLibrary-1.0", "$Revision: 12345$") ``` ```lua local lib = LibStub:NewLibrary("MyLibrary-1.0", 12345+tonumber(strmatch("%d+","$Revision: 2$")) ) ``` -------------------------------- ### Retrieving a Registered Library Source: https://context7.com/lua-wow/libstub/llms.txt Use GetLibrary or the shorthand call syntax to access a library. Set the silent parameter to true to return nil instead of throwing an error if the library is missing. ```lua -- Get a required library (throws error if not found) local AceEvent = LibStub:GetLibrary("AceEvent-3.0") AceEvent.RegisterEvent(frame, "PLAYER_LOGIN", OnLogin) -- Get an optional library (returns nil if not found) local AceDB = LibStub:GetLibrary("AceDB-3.0", true) if AceDB then local db = AceDB:New("MyAddonDB", defaults, true) end -- Shorthand syntax using __call metamethod local AceConsole = LibStub("AceConsole-3.0") local AceTimer = LibStub("AceTimer-3.0", true) -- silent mode -- Get library with version info local lib, version = LibStub:GetLibrary("MyLibrary-1.0") print("Loaded MyLibrary version:", version) -- Output: Loaded MyLibrary version: 5 ``` -------------------------------- ### Implement Mixin Pattern with LibStub Source: https://context7.com/lua-wow/libstub/llms.txt Defines a library with embedding support and demonstrates how to embed its methods into addon objects. Ensure the library is registered with LibStub before embedding. ```lua -- Library definition with embedding support local MAJOR, MINOR = "MyMixinLib-1.0", 1 local lib, oldminor = LibStub:NewLibrary(MAJOR, MINOR) if not lib then return end -- Track objects where this library has been embedded lib.mixinTargets = lib.mixinTargets or {} -- Define which methods to embed local mixins = {"Print", "Debug", "Error"} function lib:Print(msg) print("|cff00ff00[" .. (self.name or "Addon") .. "]|r " .. msg) end function lib:Debug(msg) if self.debug then print("|cff888888[DEBUG]|r " .. msg) end end function lib:Error(msg) print("|cffff0000[ERROR]|r " .. msg) end -- Embed library methods into target object function lib:Embed(target) for _, name in pairs(mixins) do target[name] = lib[name] end lib.mixinTargets[target] = true return target end -- Re-embed on library upgrades to update all existing targets for target in pairs(lib.mixinTargets) do lib:Embed(target) end -- Usage in an addon local MyAddon = {} MyAddon.name = "MyAddon" MyAddon.debug = true LibStub("MyMixinLib-1.0"):Embed(MyAddon) MyAddon:Print("Addon loaded!") -- [MyAddon] Addon loaded! MyAddon:Debug("Checking status") -- [DEBUG] Checking status MyAddon:Error("Something failed") -- [ERROR] Something failed ``` -------------------------------- ### Iterating Through Registered Libraries Source: https://context7.com/lua-wow/libstub/llms.txt Use IterateLibraries to loop through all currently registered libraries for debugging or introspection purposes. ```lua -- List all loaded libraries and their versions for name, lib in LibStub:IterateLibraries() do local _, minor = LibStub:GetLibrary(name) print(string.format("Library: %s (version %d)", name, minor)) end -- Output: -- Library: AceEvent-3.0 (version 4) -- Library: AceDB-3.0 (version 27) -- Library: MyLibrary-1.0 (version 5) -- Check if a specific library pattern is loaded local function FindLibraries(pattern) local found = {} for name, lib in LibStub:IterateLibraries() do if string.match(name, pattern) then table.insert(found, name) end end return found end local aceLibs = FindLibraries("^Ace") -- Returns: {"AceEvent-3.0", "AceDB-3.0", "AceConsole-3.0", ...} ``` -------------------------------- ### LibStub:NewLibrary(major, minor) Source: https://context7.com/lua-wow/libstub/llms.txt Registers a new library or upgrades an existing one by comparing version numbers. ```APIDOC ## LibStub:NewLibrary(major, minor) ### Description Registers a new library or upgrades an existing one. If a newer or equal version is already loaded, it returns nil. ### Parameters - **major** (string) - Required - The unique identifier for the library (e.g., "MyLibrary-1.0"). - **minor** (number/string) - Required - The version number of the library. ### Response - **lib** (table) - The library object. - **oldminor** (number/string) - The previous version number if an upgrade occurred. ``` -------------------------------- ### LibStub:GetLibrary(major, silent) Source: https://context7.com/lua-wow/libstub/llms.txt Retrieves a registered library by its major version string. ```APIDOC ## LibStub:GetLibrary(major, silent) ### Description Retrieves a registered library. If silent is false or omitted, it throws an error if the library is not found. If silent is true, it returns nil. ### Parameters - **major** (string) - Required - The unique identifier for the library. - **silent** (boolean) - Optional - If true, returns nil instead of throwing an error when the library is missing. ### Response - **lib** (table) - The library object if found, otherwise nil. ``` -------------------------------- ### LibStub:IterateLibraries() Source: https://context7.com/lua-wow/libstub/llms.txt Returns an iterator function for looping through all currently registered libraries. ```APIDOC ## LibStub:IterateLibraries() ### Description Returns an iterator function to loop through all libraries currently registered in the global registry. ### Response - **iterator** (function) - An iterator function that returns the library name and the library object. ``` -------------------------------- ### Implement library embedding and mixing Source: https://github.com/lua-wow/libstub/blob/master/README.md Defines an Embed function to mix library methods into target objects and handles re-embedding during library upgrades. ```lua lib.mixinTargets = lib.mixinTargets or {} local mixins = {"SomeFunction", "SomeOtherFunction" } function lib:Embed(target) for _,name in pairs(mixins) do target[name] = lib[name] end lib.mixinTargets[target] = true end ``` ```lua for target,_ in pairs(mixinTargets) do lib:Embed(target) end ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.