diff --git a/typings.lua b/typings.lua index 866c7fd..5e901c2 100644 --- a/typings.lua +++ b/typings.lua @@ -127,20 +127,6 @@ dw = 0 --- The display height. dh = 0 ---- The Uranium Template table! Mostly callback-related stuff goes here. -uranium = {} - ---- A callback for initialization. Called on `OnCommand`. -uranium.init = function() end ---- A callback for updates. Called every frame. Draw stuff here! -uranium.update = function() end - ----@param event string ----@param ... any ----@return any ---- Call a defined callback. -function uranium:call(event, ...) end - --- Equivalent to a modfile-sandboxed `_G`, similar to Mirin's `xero`. You shouldn't need this; and if you do, *what are you doing?* oat = _G diff --git a/uranium/actors.lua b/uranium/actors.lua index 3450205..604b123 100644 --- a/uranium/actors.lua +++ b/uranium/actors.lua @@ -26,7 +26,7 @@ end function setShader(actor, shader) if not shader.__raw then - function uranium.init() setShader(actor, shader) end + uranium.on('init', function() setShader(actor, shader) end) else actor:SetShader(shader.__raw) end @@ -34,7 +34,7 @@ end function setShaderfuck(shader) if not shader.__raw then - function uranium.init() setShaderfuck(shader) end + uranium.on('init', function() setShaderfuck(shader) end) else DISPLAY:ShaderFuck(shader.__raw) end @@ -81,7 +81,7 @@ end oat._globalQueue = {} -- for resetting function reset(actor) - if not actorsInitialized then error('uranium: cannot reset an actor during initialization', 2) end + if not oat._actorsInitialized then error('uranium: cannot reset an actor during initialization', 2) end for _, q in ipairs(oat._globalQueue) do local queueActor = q[1] if queueActor == actor.__raw then diff --git a/uranium/events.lua b/uranium/events.lua index 8789643..ddbd2f2 100644 --- a/uranium/events.lua +++ b/uranium/events.lua @@ -1,18 +1,23 @@ useProfiler = false profilerInfo = {} -local uraniumFunc = {} +local callbacks = {} local debugCache = {} -function uraniumFunc:call(event, ...) - if self._callbacks[event] then + +---@param event string +---@param ... any +---@return any +--- Call a defined callback. +function uranium.call(event, ...) + if callbacks[event] then profilerInfo[event] = {} - for _, callback in ipairs(self._callbacks[event]) do + for _, callback in ipairs(callbacks[event]) do local start = os.clock() local res = callback(unpack(arg)) local dur = os.clock() - start - if oat.useProfiler then + if useProfiler then if not debugCache[callback] then debugCache[callback] = debug.getinfo(callback, 'Sl') -- cached cus debug.getinfo is EXPENSIVE end @@ -29,16 +34,12 @@ function uraniumFunc:call(event, ...) end end -local uraniumMeta = {} - -function uraniumMeta:__newindex(key, value) - if self._callbacks[key] then - table.insert(self._callbacks[key], value) - else - self._callbacks[key] = {value} +---@param event string +---@param f function +--- Register a callback handler. +function uranium.on(event, f) + if not callbacks[event] then + callbacks[event] = {} end -end - -uraniumMeta.__index = uraniumFunc - -uranium = setmetatable({_callbacks = {}}, uraniumMeta) \ No newline at end of file + table.insert(callbacks[event], f) +end \ No newline at end of file diff --git a/uranium/main.lua b/uranium/main.lua index 0522644..c41ec9c 100644 --- a/uranium/main.lua +++ b/uranium/main.lua @@ -28,6 +28,7 @@ dh = DISPLAY:GetDisplayHeight() local resetOnFrameStartCfg = false local resetOnFrameStartActors = {} +--- The Uranium Template table! All template-related functionality is stored here. uranium = {} require 'uranium.events' @@ -36,7 +37,7 @@ local hasExited = false local function exit() if hasExited then return end hasExited = true - uranium:call('exit') + uranium.call('exit') -- good templates clean up after themselves uranium = nil _G.oat = nil @@ -66,7 +67,7 @@ local function onCommand(self) resetOnFrameStartActors_[k.__raw] = v end resetOnFrameStartActors = resetOnFrameStartActors_ - uranium:call('init') + uranium.call('init') end -- runs once during ScreenReadyCommand, before the user code is loaded @@ -141,7 +142,7 @@ local function screenReadyCommand(self) firstrun = false dt = 0 self:GetChildren()[2]:hidden(1) - uranium:call('ready') + uranium.call('ready') end drawfunctionArguments = {} @@ -165,9 +166,9 @@ local function screenReadyCommand(self) end end - uranium:call('preUpdate', dt) - uranium:call('update', dt) - uranium:call('postUpdate', dt) + uranium.call('preUpdate', dt) + uranium.call('update', dt) + uranium.call('postUpdate', dt) errored = false @@ -189,7 +190,7 @@ if success then print('---') - actorsInitializing = true + oat._actorsInitializing = true oat._transformQueueToTree() --Trace(fullDump(oat._actorTree)) oat._currentPath = oat._actorTree @@ -199,10 +200,10 @@ if success then _main:addcommand('Off', exit) _main:addcommand('SaltyReset', exit) _main:addcommand('WindowFocus', function() - uranium:call('focus', true) + uranium.call('focus', true) end) _main:addcommand('WindowFocusLost', function() - uranium:call('focus', false) + uranium.call('focus', false) end) _main:queuecommand('Ready') else