Nsk (lib)
Shared core library for Nosink's World of Warcraft addons: event bus, layered SavedVariables database, localization, read-only enums, a settings store and a vertical settings panel.
Nsk is a library registered with LibStub as Nsk-1.0. It works both as a standalone addon and as a copy embedded inside each addon (libs/Nsk/): LibStub keeps the highest revision loaded, so an addon works even when the standalone Nsk is not installed.
Supports every flavor listed in Nsk.toc (## Interface).
Features
- Event bus for native WoW events and internal addon events, with safe (
pcall) handlers and duplicate-safe secure hooks. - Layered SavedVariables database with per-character and account-wide defaults.
- Localization that falls back to the key when a translation is missing.
- Read-only enums, preloaded with class IDs, unit reactions and expansion levels.
- Settings store that writes settings and notifies every change.
- Vertical settings panel on Blizzard's modern
SettingsAPI: sections, check boxes, sliders, drop downs, dependent options and custom rows. - Works standalone or embedded in each addon; LibStub keeps the highest revision loaded.
Usage
<Addon>.toc:
## OptionalDeps: Nsk
## X-Embeds: Nsk
# Nsk
libs/Nsk/Nsk.xml
addon/nsk.lua
addon/defaults.lua
...
addon.lua
## OptionalDeps: Nskloads the standalone Nsk first when it is installed, without making the addon depend on it.## X-Embeds: Nsktellsbuild.ps1to copy Nsk into<Addon>/libs/Nsk/inside the zip. The copy only exists in the package, never in the addon's repository.libs/Nsk/Nsk.xmlloads the embedded copy. During development the folder does not exist and the junctioned standalone Nsk is used instead.
addon/nsk.lua must be the first file of the addon:
local name, ns = ...
LibStub("Nsk-1.0"):Embed(name, ns)
After that call the namespace exposes:
| Field | Module |
|---|---|
ns.expansion |
Expansion level of the running client (ns.enums.expansion) |
ns.enums, ns.RegisterEnum(name, values) |
Read-only enums, preloaded with classID, reaction, expansion |
ns.bus |
RegisterEvent(event, fn) (returns an unsubscribe function), RegisterEventOnce(event, fn), TriggerEvent(event, ...), for native and internal events |
ns.HookSecureFunc, ns.HookScript |
Duplicate-safe hooks |
ns.database.Register, ns.database.Load, ns.db |
SavedVariables <Addon>DB / <Addon>PCDB with defaults cascade |
ns.L, ns.locale |
Translations, falling back to the key |
ns.settingsStore |
Get, Set (write and notify), Write, GetDefault, CHANGED_EVENT |
ns.settingsPanel |
Vertical layout settings panel on the modern Settings API |
ns.settingsCategory |
Set when a panel registers |
Vertical settings panel
local panel = ns.settingsPanel.Create(L["NAME"])
panel:AddSection(L["SECTION"])
panel:AddCheckBox(L["ENABLED"], "enabled", L["ENABLED_TOOLTIP"])
panel:AddSlider(L["SCALE"], "scale", L["SCALE_TOOLTIP"], { min = 0.5, max = 2, step = 0.1, format = formatScale })
panel:DependsOn("enabled")
panel:AddDropDown(L["MODE"], "mode", L["MODE_TOOLTIP"], { { value = "A", text = L["MODE_A"] } })
panel:DependsOn("enabled")
panel:Register()
Every key needs a registered default. DependsOn(parentKey) applies to the setting added just before it: it is indented under the boolean setting parentKey (added earlier) and disabled while that setting is off. Register() returns the category, or nil when the client has no modern Settings API.
For controls the list does not offer, register the settings without a control and add your own row (Nsk 1.3.0+):
panel:AddSetting(L["SHOW_BAGS"], "showBags")
panel:AddInitializer(function(category)
return Settings.CreateElementInitializer("MyAddonRowTemplate", { name = L["ROW"] })
end)
AddSetting(label, key) registers the setting in the category, so the page's Defaults button resets it, but draws nothing. AddInitializer(createInitializer) calls createInitializer(category) while the panel registers and adds the returned initializer to the page at that position; returning nil skips it. The row's frame template must be defined in XML.
LibStub("Nsk-1.0") returns the library and its revision. It errors when Nsk is not loaded; LibStub("Nsk-1.0", true) returns nil instead.