Skip to content

Commit

Permalink
Rev. B57
Browse files Browse the repository at this point in the history
  • Loading branch information
hoontee committed Mar 26, 2024
1 parent 321f178 commit 7182435
Showing 1 changed file with 113 additions and 81 deletions.
194 changes: 113 additions & 81 deletions Pronghorn/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 ║
║ ║
Expand All @@ -49,6 +49,8 @@
╚══════════════════════════════════════════════════════════════════════╝
]]

local Pronghorn = {}

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- Dependencies
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Expand All @@ -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
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Expand All @@ -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 --
Expand All @@ -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

0 comments on commit 7182435

Please sign in to comment.