diff --git a/CHANGELOG.md b/CHANGELOG.md index 22b407c..71b2e46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,11 +14,17 @@ As always, you can also check the commit history for a given version as well, an | Version | Date | Description | | ---|---|--- | +| [1.2.4](#1.2.4) | 2018-09-20 | | [1.2.3](#1.2.3) | 2018-08-15 | | | [1.2.2](#1.2.2) | 2018-08-15 | | ### Version History Notes +#### Version 1.2.4 +Instead of using `while` loops to wait for code to complete, proper usage of `coroutine.yield` and `coroutine.resume` have been implemented. This change reflects best practices on Roblox. Doing this was not possible before a recent Roblox update. The overall behavior of the code remains entirely the same. + +Some deprecated code has also been fixed in the TaskScheduler. The functionality remains entirely the same. + #### Version 1.2.3 Added the `WrapModule` method to both the AeroServer and AeroClient scripts. This method takes a `table` and will set its metatable to the same metatable used by other Aero-based modules/controllers/services. This allows you to easily integrate custom modules into the framework if needed. diff --git a/src/ReplicatedStorage/Aero/Shared/Event.modulescript.lua b/src/ReplicatedStorage/Aero/Shared/Event.modulescript.lua index 9063d19..4b5ca3d 100644 --- a/src/ReplicatedStorage/Aero/Shared/Event.modulescript.lua +++ b/src/ReplicatedStorage/Aero/Shared/Event.modulescript.lua @@ -57,16 +57,13 @@ end function Event:Wait() - local c, returnArgs = nil, nil - local be = Instance.new("BindableEvent") - c = self:Connect(function(...) - c:Disconnect() - returnArgs = {...} - be:Fire() + local thread = coroutine.running() + local connection + connection = self:Connect(function(...) + connection:Disconnect() + coroutine.resume(thread, ...) end) - be.Event:Wait() - be:Destroy() - return unpack(returnArgs) + return coroutine.yield() end diff --git a/src/ServerStorage/Aero/Modules/DataStoreCache.modulescript.lua b/src/ServerStorage/Aero/Modules/DataStoreCache.modulescript.lua index 8957d1e..847f092 100644 --- a/src/ServerStorage/Aero/Modules/DataStoreCache.modulescript.lua +++ b/src/ServerStorage/Aero/Modules/DataStoreCache.modulescript.lua @@ -97,6 +97,7 @@ end function Cache:FlushAllConcurrent() if (not self.DataStore) then return end + local thread = coroutine.running() local numData = 0 local numFlushed = 0 for key,_ in pairs(self.Data) do @@ -106,9 +107,12 @@ function Cache:FlushAllConcurrent() spawn(function() self:Flush(key, true) numFlushed = (numFlushed + 1) + if (numFlushed == numData) then + coroutine.resume(thread) + end end) end - while (numFlushed < numData) do wait() end + coroutine.yield() end diff --git a/src/ServerStorage/Aero/Services/DataService.modulescript.lua b/src/ServerStorage/Aero/Services/DataService.modulescript.lua index c7d55b7..2d0a6bf 100644 --- a/src/ServerStorage/Aero/Services/DataService.modulescript.lua +++ b/src/ServerStorage/Aero/Services/DataService.modulescript.lua @@ -212,6 +212,7 @@ end function DataService:FlushAllConcurrent() + local thread = coroutine.running() local numCaches = 0 local numFlushed = 0 for _ in pairs(playerCaches) do @@ -220,20 +221,26 @@ function DataService:FlushAllConcurrent() for _,cache in pairs(customCaches) do numCaches = (numCaches + 1) end + local function IncFlushed() + numFlushed = (numFlushed + 1) + if (numFlushed == numCaches) then + coroutine.resume(thread) + end + end for player,cache in pairs(playerCaches) do spawn(function() cache:FlushAllConcurrent() - numFlushed = (numFlushed + 1) + IncFlushed() end) end for _,cache in pairs(customCaches) do spawn(function() cache:FlushAll() - numFlushed = (numFlushed + 1) + IncFlushed() end) end globalCache:FlushAll() - while (numFlushed < numCaches) do wait() end + coroutine.yield() end @@ -261,17 +268,21 @@ function DataService:Start() self.GameClosing = false local function FireBoundToCloseCallbacks() + local thread = coroutine.running() local numBinded = #boundToCloseFuncs local numCompleted = 0 local maxWait = 20 local start = tick() for _,func in pairs(boundToCloseFuncs) do coroutine.wrap(function() - func() + pcall(func) numCompleted = (numCompleted + 1) + if (numCompleted == numBinded) then + coroutine.resume(thread) + end end)() end - while (numCompleted < numBinded and (tick() - start) < maxWait) do wait() end + coroutine.yield() end -- Flush cache: diff --git a/src/StarterPlayer/StarterPlayerScripts/Aero/Controllers/TaskScheduler.modulescript.lua b/src/StarterPlayer/StarterPlayerScripts/Aero/Controllers/TaskScheduler.modulescript.lua index b5586b8..ac9d66c 100644 --- a/src/StarterPlayer/StarterPlayerScripts/Aero/Controllers/TaskScheduler.modulescript.lua +++ b/src/StarterPlayer/StarterPlayerScripts/Aero/Controllers/TaskScheduler.modulescript.lua @@ -1,12 +1,11 @@ -- Author: EchoReaper --- ROBLOX Link: https://www.roblox.com/Task-Scheduler-item?id=348019935 +-- Roblox Link: https://www.roblox.com/Task-Scheduler-item?id=348019935 -- Publically released January 25, 2016 --- Optimizations and edits made by Crazyman32 --- Changes from EchoReaper's version: +-- Changes made from EchoReaper's version: -- GetCurrentFPS() method removed -- FPS is only tracked when the 'Loop' function is running for performance reasons - -- Styled code in consistency with the rest of the Team Crazy Game Framework codebase + -- Styled code in consistency with the rest of the AeroGameFramework codebase --[[ @@ -22,13 +21,13 @@ +local TaskScheduler = {} + local lastIteration local frameUpdateTable = {} local runService = game:GetService("RunService") -local TaskScheduler = {} - --[[ param targetFps Task scheduler won't run a task if it'd make the FPS drop below this amount (WARNING) this only holds true if it is used properly. If you try to complete 10 union operations @@ -60,7 +59,7 @@ local TaskScheduler = {} plugin:Union({partA, partB}):Destroy() totalOperations = totalOperations + 1 print("Times unioned:", totalOperations) - if totalOperations == 50 then + if (totalOperations == 50) then scheduler:Pause() paused = true end @@ -71,6 +70,8 @@ local TaskScheduler = {} wait(2) scheduler:Resume() --]] + + function TaskScheduler:CreateScheduler(targetFps) local scheduler = {} @@ -81,7 +82,7 @@ function TaskScheduler:CreateScheduler(targetFps) local updateFrameTableEvent = nil local start = tick() - runService.RenderStepped:wait() + runService.RenderStepped:Wait() local function UpdateFrameTable() lastIteration = tick() @@ -92,7 +93,7 @@ function TaskScheduler:CreateScheduler(targetFps) end local function Loop() - updateFrameTableEvent = runService.RenderStepped:connect(UpdateFrameTable) + updateFrameTableEvent = runService.RenderStepped:Connect(UpdateFrameTable) while (true) do if (sleeping) then break end local fps = (((tick() - start) >= 1 and #frameUpdateTable) or (#frameUpdateTable / (tick() - start))) @@ -105,10 +106,10 @@ function TaskScheduler:CreateScheduler(targetFps) break end else - runService.RenderStepped:wait() + runService.RenderStepped:Wait() end end - updateFrameTableEvent:disconnect() + updateFrameTableEvent:Disconnect() updateFrameTableEvent = nil end diff --git a/version.txt b/version.txt index a064add..b43a308 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -v1.2.3 \ No newline at end of file +v1.2.4 \ No newline at end of file