diff --git a/Pronghorn/init.lua b/Pronghorn/init.lua index ba4dcc9..6d72dc4 100644 --- a/Pronghorn/init.lua +++ b/Pronghorn/init.lua @@ -29,7 +29,7 @@ ║ ██████▀██▓▌▀▌ ▄ ▄▓▌▐▓█▌ ║ ║ ║ ║ ║ -║ Pronghorn Framework Rev. B56 ║ +║ Pronghorn Framework Rev. B57 ║ ║ https://github.com/Iron-Stag-Games/Pronghorn ║ ║ GNU Lesser General Public License v2.1 ║ ║ ║ @@ -49,6 +49,8 @@ ╚══════════════════════════════════════════════════════════════════════╝ ]] +local Pronghorn = {} + -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -- Dependencies -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- @@ -57,17 +59,26 @@ local Players = game:GetService("Players") -- Core +local Debug = require(script.Debug) local New = require(script.New) -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -- Helper Variables -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +-- Types type Module = { Object: ModuleScript; Return: any?; } +-- Defines +local imported = false +local startWaits = math.huge + +-- Objects +local deferredComplete = New.Event() + -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -- Helper Functions -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- @@ -93,6 +104,105 @@ local function addModules(allModules: {Module}, object: Instance) end end +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +-- Module Functions +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + +--- @todo +function Pronghorn:SetEnabledChannels(newEnabledChannels: {[string]: boolean}) + Debug:SetEnabledChannels(newEnabledChannels) +end + +--- @todo +function Pronghorn:Import(paths: {Instance}) + if imported then + error("Pronghorn:Import() cannot be called more than once", 0) + end + + local allModules: {Module} = {} + + for _, object in paths do + addModules(allModules, object) + end + + -- Init + for _, moduleTable in allModules do + if type(moduleTable.Return) == "table" and moduleTable.Return.Init then + local thread = task.spawn(moduleTable.Return.Init, moduleTable.Return) + if coroutine.status(thread) ~= "dead" then + error(`{moduleTable.Object:GetFullName()}: Yielded during Init function`, 0) + end + end + end + + -- Deferred + startWaits = 0 + for _, moduleTable in allModules do + if type(moduleTable.Return) == "table" and moduleTable.Return.Deferred then + startWaits += 1 + task.spawn(function() + local running = true + task.delay(5, function() + if running then + warn(`{moduleTable.Object:GetFullName()}: Infinite yield possible in Deferred function`) + end + end) + moduleTable.Return:Deferred() + running = false + startWaits -= 1 + if startWaits == 0 then + deferredComplete:Fire() + end + end) + end + end + + -- PlayerAdded + local function playerAdded(player: Player) + for _, moduleTable in allModules do + if type(moduleTable.Return) == "table" and moduleTable.Return.PlayerAdded then + task.spawn(moduleTable.Return.PlayerAdded, player) + end + end + for _, moduleTable in allModules do + if type(moduleTable.Return) == "table" and moduleTable.Return.PlayerAddedDeferred then + task.spawn(moduleTable.Return.PlayerAddedDeferred, player) + end + end + end + Players.PlayerAdded:Connect(playerAdded) + for _, player in Players:GetPlayers() do + playerAdded(player) + end + + -- PlayerRemoving + Players.PlayerRemoving:Connect(function(player: Player) + for _, moduleTable in allModules do + if type(moduleTable.Return) == "table" and moduleTable.Return.PlayerRemoving then + task.spawn(moduleTable.Return.PlayerRemoving, player) + end + end + for _, moduleTable in allModules do + if type(moduleTable.Return) == "table" and moduleTable.Return.PlayerRemovingDeferred then + task.spawn(moduleTable.Return.PlayerRemovingDeferred, player) + end + end + end) + + -- Wait for Deferred Functions to complete + Pronghorn:Wait() + + imported = true +end + +--- Yields until all imported module Deferred Functions have completed. +--- @yields +function Pronghorn:Wait() + while startWaits > 0 do + deferredComplete:Wait() + end +end + -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- -- Import Core Modules -- @@ -119,84 +229,6 @@ for _, coreModule in coreModules do end end -return { - SetEnabledChannels = coreModules.Debug.SetEnabledChannels; - - Import = function(_, paths: {Instance}) - local allModules: {Module} = {} - - for _, object in paths do - addModules(allModules, object) - end - - -- Init - for _, moduleTable in allModules do - if type(moduleTable.Return) == "table" and moduleTable.Return.Init then - local thread = task.spawn(moduleTable.Return.Init, moduleTable.Return) - if coroutine.status(thread) ~= "dead" then - error(`{moduleTable.Object:GetFullName()}: Yielded during Init function`, 0) - end - end - end - - -- Deferred - local deferredComplete = New.Event() - local startWaits = 0 - for _, moduleTable in allModules do - if type(moduleTable.Return) == "table" and moduleTable.Return.Deferred then - startWaits += 1 - task.spawn(function() - local running = true - task.delay(5, function() - if running then - warn(`{moduleTable.Object:GetFullName()}: Infinite yield possible in Deferred function`) - end - end) - moduleTable.Return:Deferred() - running = false - startWaits -= 1 - if startWaits == 0 then - deferredComplete:Fire() - end - end) - end - end - - -- PlayerAdded - local function playerAdded(player: Player) - for _, moduleTable in allModules do - if type(moduleTable.Return) == "table" and moduleTable.Return.PlayerAdded then - task.spawn(moduleTable.Return.PlayerAdded, player) - end - end - for _, moduleTable in allModules do - if type(moduleTable.Return) == "table" and moduleTable.Return.PlayerAddedDeferred then - task.spawn(moduleTable.Return.PlayerAddedDeferred, player) - end - end - end - Players.PlayerAdded:Connect(playerAdded) - for _, player in Players:GetPlayers() do - playerAdded(player) - end - - -- PlayerRemoving - Players.PlayerRemoving:Connect(function(player: Player) - for _, moduleTable in allModules do - if type(moduleTable.Return) == "table" and moduleTable.Return.PlayerRemoving then - task.spawn(moduleTable.Return.PlayerRemoving, player) - end - end - for _, moduleTable in allModules do - if type(moduleTable.Return) == "table" and moduleTable.Return.PlayerRemovingDeferred then - task.spawn(moduleTable.Return.PlayerRemovingDeferred, player) - end - end - end) +-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- - -- Wait for Deferred Functions to complete - while startWaits > 0 do - deferredComplete:Wait() - end - end; -} +return Pronghorn