Fixed System Threaded Queues!
Faster Added older versions Other bug fixes More examples
This commit is contained in:
parent
ef1cf06416
commit
eaaf5d80a7
130
README.html
130
README.html
File diff suppressed because one or more lines are too long
121
README.md
121
README.md
@ -1,7 +1,7 @@
|
||||
# multi Version: 1.8.5 (System Threaded Execute) Looking into some bugs...
|
||||
# multi Version: 1.8.6 (System Threaded Job Queues gets an update!) Also On the github you can check out the old versions of this library! It stems back from 2012 see rambling for more info...
|
||||
**Note: The changes section has information on how to use the new features as they come out. Why put the infomation twice on the readme?** Also I added a Testing Branch. That Branch will have the latest updates, but those updates may be unstable. I like to keep the master as bug free as possible</br>
|
||||
|
||||
# Note SystemThreadedTable's behavior is not stable! I am looking into this.
|
||||
# Note SystemThreadedTable's behavior is not stable! I have found the cause of the problem and am currently figuring out a solution... The next update will probably include the fix!
|
||||
|
||||
In Changes you'll find documentation for(In Order):
|
||||
- System Threaded Job Queues
|
||||
@ -801,8 +801,52 @@ We did it! 1 2 3</br>
|
||||
|
||||
Changes
|
||||
-------
|
||||
Updated from 1.8.4 to 1.8.5
|
||||
---------------------------
|
||||
Update: 1.8.6
|
||||
-------------
|
||||
Added:
|
||||
- jobQueue:doToAll(function)
|
||||
- jobQueue:start() is now required Call this after all calls to registerJob()'s. Calling it afterwards will not guarantee your next push job with that job will work. Not calling this will make pushing jobs impossible!
|
||||
- Fixed a bug with love2d Threaded Queue
|
||||
- Fixed some bugs
|
||||
|
||||
This will run said function in every thread.
|
||||
```lua
|
||||
-- Going to use love2d code this time, almost the same as last time... See ramblings
|
||||
require("core.Library")
|
||||
GLOBAL,sThread=require("multi.integration.loveManager").init() -- load the love2d version of the lanesManager and requires the entire multi library
|
||||
require("core.GuiManager")
|
||||
gui.ff.Color=Color.Black
|
||||
jQueue=multi:newSystemThreadedJobQueue()
|
||||
jQueue:registerJob("TEST_JOB",function(a,s)
|
||||
math.randomseed(s)
|
||||
TEST_JOB2()
|
||||
return math.random(0,255)
|
||||
end)
|
||||
jQueue:registerJob("TEST_JOB2",function()
|
||||
print("Test Works!")
|
||||
end)
|
||||
-- 1.8.6 EXAMPLE Change
|
||||
jQueue:start() -- This is now needed!
|
||||
--
|
||||
jQueue:doToAll(function()
|
||||
print("Doing this 2? times!")
|
||||
end)
|
||||
tableOfOrder={}
|
||||
jQueue.OnJobCompleted(function(JOBID,n)
|
||||
tableOfOrder[JOBID]=n
|
||||
if #tableOfOrder==10 then
|
||||
t.text="We got all of the pieces!"
|
||||
end
|
||||
end)
|
||||
for i=1,10 do -- Job Name of registered function, ... varargs
|
||||
jQueue:pushJob("TEST_JOB","This is a test!",math.random(1,1000000))
|
||||
end
|
||||
t=gui:newTextLabel("no done yet!",0,0,300,100)
|
||||
t:centerX()
|
||||
t:centerY()
|
||||
```
|
||||
Update: 1.8.5
|
||||
-------------
|
||||
Added:
|
||||
- SystemThreadedExecute(cmd)
|
||||
|
||||
@ -818,8 +862,8 @@ multi:newTLoop(function()
|
||||
end,1)
|
||||
multi:mainloop()
|
||||
```
|
||||
Updated from 1.8.3 to 1.8.4
|
||||
---------------------------
|
||||
Update: 1.8.4
|
||||
-------------
|
||||
Added:
|
||||
- multi:newSystemThreadedJobQueue()
|
||||
- Improved stability of the library
|
||||
@ -863,8 +907,8 @@ multi:mainloop() -- Start the main loop :D
|
||||
|
||||
Thats it from this version!
|
||||
|
||||
Updated from 1.8.2 to 1.8.3
|
||||
---------------------------
|
||||
Update: 1.8.3
|
||||
-------------
|
||||
Added:</br>
|
||||
**New Mainloop functions** Below you can see the slight differences... Function overhead is not too bad in lua, but has a real difference. multi:mainloop() and multi:unprotectedMainloop() use the same algorithm yet the dedicated unprotected one is slightly faster due to having less function overhead.
|
||||
- multi:mainloop()\* -- Bench: 16830003 Steps in 3 second(s)!
|
||||
@ -881,8 +925,8 @@ However there is a work around! You can use processes to run multiobjs as well a
|
||||
|
||||
I may make a full comparison between each method and which is faster, but for now trust that the dedicated ones with less function overhead are infact faster. Not by much but still faster. :D
|
||||
|
||||
Updated from 1.8.1 to 1.8.2
|
||||
---------------------------
|
||||
Update: 1.8.2
|
||||
-------------
|
||||
Added:</br>
|
||||
- multi:newsystemThreadedTable(name) NOTE: Metatables are not supported in transfers. However there is a work around obj:init() that you see does this. Take a look in the multi/integration/shared/shared.lua files to see how I did it!
|
||||
- Modified the GLOBAL metatable to sync before doing its tests
|
||||
@ -945,8 +989,8 @@ t:centerX()
|
||||
t:centerY()
|
||||
```
|
||||
|
||||
Updated from 1.8.0 to 1.8.1
|
||||
---------------------------
|
||||
Update: 1.8.1
|
||||
-------------
|
||||
No real change!</br>
|
||||
Changed the structure of the library. Combined the coroutine based threads into the core!</br>
|
||||
Only compat and integrations are not part of the core and never will be by nature.</br>
|
||||
@ -1073,8 +1117,8 @@ multi:newThread("test!",function() -- this is a lua thread
|
||||
end)
|
||||
multi:mainloop()
|
||||
```
|
||||
Updated from 1.7.5 to 1.7.6
|
||||
---------------------------
|
||||
Update: 1.7.6
|
||||
-------------
|
||||
Fixed:
|
||||
Typos like always
|
||||
Added:</br>
|
||||
@ -1086,8 +1130,9 @@ The old way still works and is more convient to be honest, but I felt a method t
|
||||
Updated:
|
||||
some example files to reflect changes to the core. Changes allow for less typing</br>
|
||||
loveManager to require the compat if used so you don't need 2 require line to retrieve the library</br>
|
||||
Updated from 1.7.4 to 1.7.5
|
||||
---------------------------
|
||||
|
||||
Update: 1.7.5
|
||||
-------------
|
||||
Fixed some typos in the readme... (I am sure there are more there are always more)</br>
|
||||
Added more features for module support</br>
|
||||
TODO:</br>
|
||||
@ -1095,8 +1140,8 @@ Work on performance of the library... I see 3 places where I can make this thing
|
||||
|
||||
I'll show case some old versions of the multitasking library eventually so you can see its changes in days past!</br>
|
||||
|
||||
Updated from 1.7.3 to 1.7.4
|
||||
---------------------------
|
||||
Update: 1.7.4
|
||||
-------------
|
||||
Added: the example folder which will be populated with more examples in the near future!</br>
|
||||
The loveManager integration that mimics the lanesManager integration almost exactly to keep coding in both enviroments as close to possible. This is done mostly for library creation support!</br>
|
||||
An example of the loveManager in action using almost the same code as the lanesintergreationtest2.lua</br>
|
||||
@ -1173,8 +1218,8 @@ t=gui:newTextLabel("no done yet!",0,0,300,100)
|
||||
t:centerX()
|
||||
t:centerY()
|
||||
```
|
||||
Updated from 1.7.2 to 1.7.3
|
||||
---------------------------
|
||||
Update: 1.7.3
|
||||
-------------
|
||||
Changed how requiring the library works!
|
||||
`require("multi.all")` Will still work as expected; however, with the exception of threading, compat, and integrations everything else has been moved into the core of the library.
|
||||
```lua
|
||||
@ -1190,15 +1235,15 @@ require("multi.task")
|
||||
-- ^ they are all part of the core now
|
||||
```
|
||||
|
||||
Updated from 1.7.1 to 1.7.2
|
||||
---------------------------
|
||||
Update: 1.7.2
|
||||
-------------
|
||||
Moved updaters, loops, and alarms into the init.lua file. I consider them core features and they are referenced in the init.lua file so they need to exist there. Threaded versions are still separate though. Added another example file
|
||||
|
||||
Updated from 1.7.0 to 1.7.1 Bug fixes only
|
||||
---------------------------
|
||||
Update: 1.7.1 Bug Fixes Only
|
||||
-------------
|
||||
|
||||
Updated from 1.6.0 to 1.7.0
|
||||
---------------------------
|
||||
Update: 1.7.0
|
||||
-------------
|
||||
Modified: multi.integration.lanesManager.lua
|
||||
It is now in a stable and simple state Works with the latest lanes version! Tested with version 3.11 I cannot promise that everything will work with eariler versions. Future versions are good though.</br>
|
||||
Example Usage:</br>
|
||||
@ -1251,8 +1296,8 @@ end)
|
||||
multi:mainloop()
|
||||
```
|
||||
|
||||
Updated from 1.5.0 to 1.6.0
|
||||
---------------------------
|
||||
Update: 1.6.0
|
||||
-------------
|
||||
Changed: steps and loops
|
||||
```lua
|
||||
-- Was
|
||||
@ -1275,21 +1320,31 @@ Reasoning I wanted to keep objects consistant, but a lot of my older libraries u
|
||||
require("multi.all")
|
||||
require("multi.compat.backwards[1,5,0]") -- allows for the use of features that were scrapped/changed in 1.6.0+
|
||||
```
|
||||
Updated from 1.4.1 to 1.5.0
|
||||
---------------------------
|
||||
Update: 1.5.0
|
||||
-------------
|
||||
Added:
|
||||
- An easy way to manage timeouts
|
||||
- Small bug fixes
|
||||
|
||||
1.4.1 - First Public release of the library
|
||||
---------------------------
|
||||
Update: 1.4.1 - First Public release of the library
|
||||
-------------
|
||||
|
||||
IMPORTANT:</br>
|
||||
Every update I make aims to make things simpler more efficent and just better, but a lot of old code, which can be really big, uses a lot of older features. I know the pain of having to rewrite everything. My promise to my library users is that I will always have backwards support for older features! New ways may exist that are quicker and eaiser, but the old features/methods will be supported.</br>
|
||||
Rambling
|
||||
--------
|
||||
Love2d: Sleeping reduces the cpu time making my load detection think the system is under more load, thus preventing it from sleeping... I will look into other means. As of right now it will not eat all of your cpu if threads are active. For now I suggest killing threads that aren't needed anymore. On lanes threads at idle use 0% cpu and it is amazing. A state machine may solve what I need though. One state being idle state that sleeps and only goes into the active state if a job request or data is sent to it... after some time of not being under load it wil switch back into the idle state... We'll see what happens.
|
||||
Love2d Sleeping reduces the cpu time making my load detection think the system is under more load, thus preventing it from sleeping... I will look into other means. As of right now it will not eat all of your cpu if threads are active. For now I suggest killing threads that aren't needed anymore. On lanes threads at idle use 0% cpu and it is amazing. A state machine may solve what I need though. One state being idle state that sleeps and only goes into the active state if a job request or data is sent to it... after some time of not being under load it wil switch back into the idle state... We'll see what happens.
|
||||
|
||||
Love2d doesn't like to send functions through channels. By defualt it does not support this. I achieve this by dumping the function and loadstring it on the thread. This however is slow. For the System Threaded Job Queue I had to change my original idea of sending functions as jobs. The current way you do it now is register a job functions once and then call that job across the thread through a queue. Each worker thread pops from the queue and returns the job. The Job ID is automatically updated and allows you to keep track of the order that the data comes in. A table with # indexes can be used to originze the data...
|
||||
|
||||
In regards to benchmarking. If you see my bench marks and are wondering they are 10x better its because I am using luajit for my tests. I highly recommend using luajit for my library, but lua 5.1 will work just as well, but not as fast.
|
||||
|
||||
So while working on the jobQueue:doToAll() method I figured out why love2d's threaded tables were acting up when more than 1 thread was sharing the table. It turns out 1 thread was eating all of the pops from the queue and starved all of the other queues... Ill need to use the same trick I did with GLOBAL to fix the problem... However at the rate I am going threading in love will become way slower. I might use the regualr GLOBAL to manage data internally for threadedtables...
|
||||
|
||||
It has been awhile since I had to bring out the Multi Functions... Syncing within threads are a pain! I had no idea what a task it would be to get something as simple as syncing data was going to be... I will probably add a SystemThreadedSyncer in the future because it will make life eaiser for you guys as well. SystemThreadedTables are still not going to work on love2d, but will work fine on lanes... I have a solution and it is being worked on... Depending on when I pust the next update to this library the second half of this ramble won't apply anymore
|
||||
|
||||
I have been using this (EventManager --> MultiManager --> now multi) for my own purposes and started making this when I first started learning lua. You are able to see how the code changed and evolved throughout the years. I tried to include all the versions that still existed on my HDD.
|
||||
|
||||
I added my old versions to this library... It started out as the EventManager and was kinda crappy but it was the start to this library. It kept getting better and better until it became what it is today. There are some features that nolonger exist in the latest version, but they were remove because they were useless... I added these files to the github so for those interested can see into my mind in a sense and see how I developed the library before I used github.
|
||||
|
||||
The first version of the EventManager was function based not object based and benched at about 2000 steps per second... Yeah that was bad... I used loadstring and it was a mess... Take a look and see how it grew throughout the years I think it may intrest some of you guys!
|
||||
@ -11,6 +11,7 @@ end)
|
||||
jQueue:registerJob("TEST_JOB2",function()
|
||||
print("Test Works!") -- this is called from the job since it is registered on the same queue
|
||||
end)
|
||||
jQueue:start()
|
||||
tableOfOrder={} -- This is how we will keep order of our completed jobs. There is no guarantee that the order will be correct
|
||||
jQueue.OnJobCompleted(function(JOBID,n) -- whenever a job is completed you hook to the event that is called. This passes the JOBID folled by the returns of the job
|
||||
-- JOBID is the completed job, starts at 1 and counts up by 1.
|
||||
|
||||
26
examples/lanesintegratetest9.lua
Normal file
26
examples/lanesintegratetest9.lua
Normal file
@ -0,0 +1,26 @@
|
||||
package.path="?/init.lua;"..package.path
|
||||
local GLOBAL,sThread=require("multi.integration.lanesManager").init()
|
||||
jQueue=multi:newSystemThreadedJobQueue(n)
|
||||
jQueue:registerJob("TEST_JOB",function(a,s)
|
||||
math.randomseed(s)
|
||||
TEST_JOB2()
|
||||
return math.random(0,255)
|
||||
end)
|
||||
jQueue:registerJob("TEST_JOB2",function()
|
||||
print("Test Works!")
|
||||
end)
|
||||
jQueue:start()
|
||||
jQueue:doToAll(function()
|
||||
print("Doing this 6? times!")
|
||||
end)
|
||||
for i=1,10 do -- Job Name of registered function, ... varargs
|
||||
jQueue:pushJob("TEST_JOB","This is a test!",math.random(1,1000000))
|
||||
end
|
||||
tableOfOrder={}
|
||||
jQueue.OnJobCompleted(function(JOBID,n)
|
||||
tableOfOrder[JOBID]=n
|
||||
if #tableOfOrder==10 then
|
||||
print("We got all of the pieces!")
|
||||
end
|
||||
end)
|
||||
multi:mainloop()
|
||||
@ -13,6 +13,7 @@ end)
|
||||
jQueue:registerJob("TEST_JOB2",function(a,s)
|
||||
print("Test Works!")
|
||||
end)
|
||||
jQueue:start()
|
||||
tableOfOrder={}
|
||||
jQueue.OnJobCompleted(function(JOBID,n)
|
||||
-- JOBID is the completed job, starts at 1 and counts up by 1.
|
||||
|
||||
@ -45,8 +45,8 @@ function print(...)
|
||||
end
|
||||
end
|
||||
multi = {}
|
||||
multi.Version="1.8.5"
|
||||
multi._VERSION="1.8.5"
|
||||
multi.Version="1.8.6"
|
||||
multi._VERSION="1.8.6"
|
||||
multi.stage='mostly-stable'
|
||||
multi.__index = multi
|
||||
multi.Mainloop={}
|
||||
@ -607,7 +607,11 @@ function multi:hold(task)
|
||||
timer:Start()
|
||||
while timer:Get()<task do
|
||||
if love then
|
||||
if love.thread then
|
||||
self.Parent:Do_Order()
|
||||
else
|
||||
self.Parent:lManager()
|
||||
end
|
||||
else
|
||||
self.Parent:Do_Order()
|
||||
end
|
||||
|
||||
@ -129,5 +129,5 @@ print("Integrated Lanes!")
|
||||
multi.integration={} -- for module creators
|
||||
multi.integration.GLOBAL=GLOBAL
|
||||
multi.integration.THREAD=THREAD
|
||||
require("multi.integration.shared.shared")
|
||||
require("multi.integration.shared")
|
||||
return {init=function() return GLOBAL,THREAD end}
|
||||
|
||||
@ -340,7 +340,7 @@ updater:OnUpdate(function(self)
|
||||
data=multi.integration.love2d.mainChannel:pop()
|
||||
end
|
||||
end)
|
||||
require("multi.integration.shared.shared")
|
||||
require("multi.integration.shared")
|
||||
print("Integrated Love2d!")
|
||||
return {
|
||||
init=function(t)
|
||||
|
||||
@ -41,12 +41,15 @@ function multi:newSystemThreadedQueue(name) -- in love2d this will spawn a chann
|
||||
end
|
||||
function self:pop() -- pop from the channel
|
||||
local tab=self.chan:pop()
|
||||
--print(tab)
|
||||
if not tab then return end
|
||||
return resolveType(tab[1],tab[2])
|
||||
end
|
||||
function self:peek()
|
||||
local tp,d=unpack{self.chan:peek()}
|
||||
return resolveType(tp,d)
|
||||
local tab=self.chan:peek()
|
||||
--print(tab)
|
||||
if not tab then return end
|
||||
return resolveType(tab[1],tab[2])
|
||||
end
|
||||
GLOBAL[self.name]=self -- send the object to the thread through the global interface
|
||||
return self -- return the object
|
||||
@ -117,9 +120,11 @@ function multi:systemThreadedBenchmark(n,p)
|
||||
end)
|
||||
return c
|
||||
end
|
||||
function multi:newSystemThreadedTable(name)
|
||||
function multi:newSystemThreadedTable(name,n)
|
||||
local c={} -- where we will store our object
|
||||
c.name=name -- set the name this is important for the love2d side
|
||||
c.cores=n
|
||||
c.hasT={}
|
||||
if love then -- check love
|
||||
if love.thread then -- make sure we can use the threading module
|
||||
function c:init() -- create an init function so we can mimic on bith love2d and lanes
|
||||
@ -130,19 +135,25 @@ function multi:newSystemThreadedTable(name)
|
||||
return self[name]
|
||||
end
|
||||
function self:sync()
|
||||
local data=self.chan:pop()
|
||||
while data do
|
||||
if type(data)=="string" then
|
||||
local data=self.chan:peek()
|
||||
if data then
|
||||
local cmd,tp,name,d=data:match("(%S-) (%S-) (%S-) (.+)")
|
||||
if not self.hasT[name] then
|
||||
if type(data)=="string" then
|
||||
if cmd=="SYNC" then
|
||||
self.tab[name]=resolveType(tp,d) -- this is defined in the loveManager.lua file
|
||||
self.hasT[name]=true
|
||||
end
|
||||
else
|
||||
self.tab[name]=data
|
||||
end
|
||||
data=self.chan:pop()
|
||||
self.chan:pop()
|
||||
end
|
||||
end
|
||||
end
|
||||
function self:reset(name)
|
||||
self.hasT[core]=nil
|
||||
end
|
||||
setmetatable(self,{
|
||||
__index=function(t,k)
|
||||
self:sync()
|
||||
@ -154,8 +165,10 @@ function multi:newSystemThreadedTable(name)
|
||||
if type(v)=="userdata" then
|
||||
self.chan:push(v)
|
||||
else
|
||||
for i=1,self.cores do
|
||||
self.chan:push("SYNC "..type(v).." "..k.." "..resolveData(v)) -- this is defined in the loveManager.lua file
|
||||
end
|
||||
end
|
||||
end,
|
||||
})
|
||||
GLOBAL[self.name]=self -- send the object to the thread through the global interface
|
||||
@ -197,11 +210,14 @@ function multi:newSystemThreadedJobQueue(numOfCores)
|
||||
c.cores=numOfCores or multi.integration.THREAD.getCores()
|
||||
c.queueIN=multi:newSystemThreadedQueue("THREADED_JQ"):init()
|
||||
c.queueOUT=multi:newSystemThreadedQueue("THREADED_JQO"):init()
|
||||
c.REG=multi:newSystemThreadedTable("THREADED_JQ_F_REG"):init()
|
||||
c.queueALL=multi:newSystemThreadedQueue("THREADED_QALL"):init()
|
||||
c.REG=multi:newSystemThreadedQueue("THREADED_JQ_F_REG"):init()
|
||||
-- registerJob(name,func)
|
||||
-- pushJob(...)
|
||||
function c:registerJob(name,func)
|
||||
self.REG[name]=func
|
||||
for i=1,self.cores do
|
||||
self.REG:push({name,func})
|
||||
end
|
||||
end
|
||||
function c:pushJob(name,...)
|
||||
self.queueOUT:push({self.jobnum,name,...})
|
||||
@ -209,6 +225,18 @@ function multi:newSystemThreadedJobQueue(numOfCores)
|
||||
end
|
||||
local GLOBAL=multi.integration.GLOBAL -- set up locals incase we are using lanes
|
||||
local sThread=multi.integration.THREAD -- set up locals incase we are using lanes
|
||||
function c:doToAll(func)
|
||||
local TaskName=multi.randomString(16)
|
||||
for i=1,self.cores do
|
||||
self.queueALL:push({TaskName,func})
|
||||
end
|
||||
end
|
||||
function c:start()
|
||||
self:doToAll(function()
|
||||
_G["__started__"]=true
|
||||
SFunc()
|
||||
end)
|
||||
end
|
||||
GLOBAL["__JQ_COUNT__"]=c.cores
|
||||
for i=1,c.cores do
|
||||
multi:newSystemThread("System Threaded Job Queue Worker Thread #"..i,function()
|
||||
@ -221,81 +249,71 @@ function multi:newSystemThreadedJobQueue(numOfCores)
|
||||
end
|
||||
JQI=sThread.waitFor("THREADED_JQO"):init() -- Grab it
|
||||
JQO=sThread.waitFor("THREADED_JQ"):init() -- Grab it
|
||||
FGLOBAL=sThread.waitFor("THREADED_JQ_F_REG"):init() -- Grab it
|
||||
REG=sThread.waitFor("THREADED_JQ_F_REG"):init() -- Grab it
|
||||
QALL=sThread.waitFor("THREADED_QALL"):init() -- Grab it
|
||||
sThread.sleep(.1) -- lets wait for things to work out
|
||||
setmetatable(_G,{
|
||||
__index=FGLOBAL
|
||||
})
|
||||
GLOBAL["THREADED_JQ"]=nil -- remove it
|
||||
GLOBAL["THREADED_JQO"]=nil -- remove it
|
||||
GLOBAL["THREADED_JQ_F_REG"]=nil -- remove it
|
||||
QALLT={}
|
||||
FUNCS={}
|
||||
SFunc=multi:newFunction(function(self)
|
||||
MainLoop:Pause()
|
||||
self:hold(.1)
|
||||
MainLoop:Resume()
|
||||
self:Pause()
|
||||
end)
|
||||
multi:newLoop(function()
|
||||
sThread.sleep(__sleep__) -- lets allow cpu time for other processes on our system!
|
||||
local job=JQI:pop()
|
||||
if job then
|
||||
local ID=table.remove(job,1) -- return and remove
|
||||
local name=table.remove(job,1) -- return and remove
|
||||
local ret={FGLOBAL:waitFor(name)(unpack(job))} -- unpack the rest
|
||||
JQO:push({ID,ret})
|
||||
local rd=REG:peek()
|
||||
if rd then
|
||||
if not FUNCS[rd[1]] then
|
||||
FUNCS[rd[1]]=rd[2]
|
||||
rd=nil -- lets clean up
|
||||
REG:pop()
|
||||
end
|
||||
end
|
||||
local d=QALL:peek()
|
||||
if d then
|
||||
if not QALLT[d[1]] then
|
||||
QALLT[d[1]]=true
|
||||
d[2]()
|
||||
d=nil -- lets clean up
|
||||
QALL:pop()
|
||||
end
|
||||
end
|
||||
end)
|
||||
multi:mainloop()
|
||||
end)
|
||||
end
|
||||
c.OnJobCompleted=multi:newConnection()
|
||||
c.updater=multi:newLoop(function(self)
|
||||
local data=self.link.queueIN:pop()
|
||||
while data do
|
||||
if data then
|
||||
self.link.OnJobCompleted:Fire(unpack(data))
|
||||
end
|
||||
data=self.link.queueIN:pop()
|
||||
end
|
||||
end)
|
||||
c.updater.link=c
|
||||
return c
|
||||
end
|
||||
if love then
|
||||
if love.thread then
|
||||
function multi:newSystemThreadedJobQueue(numOfCores)
|
||||
local c={}
|
||||
c.jobnum=1
|
||||
c.cores=numOfCores or multi.integration.THREAD.getCores()
|
||||
c.queueIN=multi:newSystemThreadedQueue("THREADED_JQ"):init()
|
||||
c.queueOUT=multi:newSystemThreadedQueue("THREADED_JQO"):init()
|
||||
function c:registerJob(name,func)
|
||||
GLOBAL["__TJQ__"..name.."__"]=func
|
||||
end
|
||||
function c:pushJob(name,...)
|
||||
self.queueOUT:push({self.jobnum,name,...})
|
||||
self.jobnum=self.jobnum+1
|
||||
end
|
||||
local GLOBAL=multi.integration.GLOBAL
|
||||
local sThread=multi.integration.THREAD
|
||||
GLOBAL["__JQ_COUNT__"]=c.cores
|
||||
for i=1,c.cores do
|
||||
multi:newSystemThread("System Threaded Job Queue Worker Thread #"..i,function()
|
||||
GLOBAL=_G.GLOBAL
|
||||
sThread=_G.sThread
|
||||
local JQI=sThread.waitFor("THREADED_JQO"):init() -- Grab it
|
||||
local JQO=sThread.waitFor("THREADED_JQ"):init() -- Grab it
|
||||
sThread.sleep(.1) -- lets wait for things to work out
|
||||
setmetatable(_G,{
|
||||
__index=function(t,k,v)
|
||||
return GLOBAL["__TJQ__"..k.."__"]
|
||||
__index=function(t,k)
|
||||
return FUNCS[k]
|
||||
end
|
||||
})
|
||||
GLOBAL["THREADED_JQ"]=nil -- remove it
|
||||
GLOBAL["THREADED_JQO"]=nil -- remove it
|
||||
multi:newLoop(function()
|
||||
MainLoop=multi:newLoop(function(self)
|
||||
if __started__ then
|
||||
local job=JQI:pop()
|
||||
if job then
|
||||
local d=QALL:peek()
|
||||
if d then
|
||||
if not QALLT[d[1]] then
|
||||
QALLT[d[1]]=true
|
||||
d[2]()
|
||||
d=nil -- lets clean up
|
||||
QALL:pop()
|
||||
end
|
||||
end
|
||||
local ID=table.remove(job,1) -- return and remove
|
||||
local name=table.remove(job,1) -- return and remove
|
||||
local ret={sThread.waitFor("__TJQ__"..name.."__")(unpack(job))} -- unpack the rest
|
||||
JQO:push({ID,ret})
|
||||
if FUNCS[name] then
|
||||
JQO:push({ID,FUNCS[name](unpack(job))})
|
||||
else
|
||||
self:hold(function() return FUNCS[name] end)
|
||||
JQO:push({ID,FUNCS[name](unpack(job))})
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
if not love then
|
||||
multi:mainloop()
|
||||
end
|
||||
end)
|
||||
end
|
||||
c.OnJobCompleted=multi:newConnection()
|
||||
@ -310,8 +328,6 @@ if love then
|
||||
end)
|
||||
c.updater.link=c
|
||||
return c
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:newSystemThreadedExecute(cmd)
|
||||
local c={}
|
||||
@ -45,8 +45,8 @@ function print(...)
|
||||
end
|
||||
end
|
||||
multi = {}
|
||||
multi.Version="1.8.5"
|
||||
multi._VERSION="1.8.5"
|
||||
multi.Version="1.8.6"
|
||||
multi._VERSION="1.8.6"
|
||||
multi.stage='mostly-stable'
|
||||
multi.__index = multi
|
||||
multi.Mainloop={}
|
||||
@ -607,7 +607,11 @@ function multi:hold(task)
|
||||
timer:Start()
|
||||
while timer:Get()<task do
|
||||
if love then
|
||||
if love.thread then
|
||||
self.Parent:Do_Order()
|
||||
else
|
||||
self.Parent:lManager()
|
||||
end
|
||||
else
|
||||
self.Parent:Do_Order()
|
||||
end
|
||||
|
||||
@ -129,5 +129,5 @@ print("Integrated Lanes!")
|
||||
multi.integration={} -- for module creators
|
||||
multi.integration.GLOBAL=GLOBAL
|
||||
multi.integration.THREAD=THREAD
|
||||
require("multi.integration.shared.shared")
|
||||
require("multi.integration.shared")
|
||||
return {init=function() return GLOBAL,THREAD end}
|
||||
|
||||
@ -340,7 +340,7 @@ updater:OnUpdate(function(self)
|
||||
data=multi.integration.love2d.mainChannel:pop()
|
||||
end
|
||||
end)
|
||||
require("multi.integration.shared.shared")
|
||||
require("multi.integration.shared")
|
||||
print("Integrated Love2d!")
|
||||
return {
|
||||
init=function(t)
|
||||
|
||||
@ -41,12 +41,15 @@ function multi:newSystemThreadedQueue(name) -- in love2d this will spawn a chann
|
||||
end
|
||||
function self:pop() -- pop from the channel
|
||||
local tab=self.chan:pop()
|
||||
--print(tab)
|
||||
if not tab then return end
|
||||
return resolveType(tab[1],tab[2])
|
||||
end
|
||||
function self:peek()
|
||||
local tp,d=unpack{self.chan:peek()}
|
||||
return resolveType(tp,d)
|
||||
local tab=self.chan:peek()
|
||||
--print(tab)
|
||||
if not tab then return end
|
||||
return resolveType(tab[1],tab[2])
|
||||
end
|
||||
GLOBAL[self.name]=self -- send the object to the thread through the global interface
|
||||
return self -- return the object
|
||||
@ -117,9 +120,11 @@ function multi:systemThreadedBenchmark(n,p)
|
||||
end)
|
||||
return c
|
||||
end
|
||||
function multi:newSystemThreadedTable(name)
|
||||
function multi:newSystemThreadedTable(name,n)
|
||||
local c={} -- where we will store our object
|
||||
c.name=name -- set the name this is important for the love2d side
|
||||
c.cores=n
|
||||
c.hasT={}
|
||||
if love then -- check love
|
||||
if love.thread then -- make sure we can use the threading module
|
||||
function c:init() -- create an init function so we can mimic on bith love2d and lanes
|
||||
@ -130,19 +135,25 @@ function multi:newSystemThreadedTable(name)
|
||||
return self[name]
|
||||
end
|
||||
function self:sync()
|
||||
local data=self.chan:pop()
|
||||
while data do
|
||||
if type(data)=="string" then
|
||||
local data=self.chan:peek()
|
||||
if data then
|
||||
local cmd,tp,name,d=data:match("(%S-) (%S-) (%S-) (.+)")
|
||||
if not self.hasT[name] then
|
||||
if type(data)=="string" then
|
||||
if cmd=="SYNC" then
|
||||
self.tab[name]=resolveType(tp,d) -- this is defined in the loveManager.lua file
|
||||
self.hasT[name]=true
|
||||
end
|
||||
else
|
||||
self.tab[name]=data
|
||||
end
|
||||
data=self.chan:pop()
|
||||
self.chan:pop()
|
||||
end
|
||||
end
|
||||
end
|
||||
function self:reset(name)
|
||||
self.hasT[core]=nil
|
||||
end
|
||||
setmetatable(self,{
|
||||
__index=function(t,k)
|
||||
self:sync()
|
||||
@ -154,8 +165,10 @@ function multi:newSystemThreadedTable(name)
|
||||
if type(v)=="userdata" then
|
||||
self.chan:push(v)
|
||||
else
|
||||
for i=1,self.cores do
|
||||
self.chan:push("SYNC "..type(v).." "..k.." "..resolveData(v)) -- this is defined in the loveManager.lua file
|
||||
end
|
||||
end
|
||||
end,
|
||||
})
|
||||
GLOBAL[self.name]=self -- send the object to the thread through the global interface
|
||||
@ -197,11 +210,14 @@ function multi:newSystemThreadedJobQueue(numOfCores)
|
||||
c.cores=numOfCores or multi.integration.THREAD.getCores()
|
||||
c.queueIN=multi:newSystemThreadedQueue("THREADED_JQ"):init()
|
||||
c.queueOUT=multi:newSystemThreadedQueue("THREADED_JQO"):init()
|
||||
c.REG=multi:newSystemThreadedTable("THREADED_JQ_F_REG"):init()
|
||||
c.queueALL=multi:newSystemThreadedQueue("THREADED_QALL"):init()
|
||||
c.REG=multi:newSystemThreadedQueue("THREADED_JQ_F_REG"):init()
|
||||
-- registerJob(name,func)
|
||||
-- pushJob(...)
|
||||
function c:registerJob(name,func)
|
||||
self.REG[name]=func
|
||||
for i=1,self.cores do
|
||||
self.REG:push({name,func})
|
||||
end
|
||||
end
|
||||
function c:pushJob(name,...)
|
||||
self.queueOUT:push({self.jobnum,name,...})
|
||||
@ -209,6 +225,18 @@ function multi:newSystemThreadedJobQueue(numOfCores)
|
||||
end
|
||||
local GLOBAL=multi.integration.GLOBAL -- set up locals incase we are using lanes
|
||||
local sThread=multi.integration.THREAD -- set up locals incase we are using lanes
|
||||
function c:doToAll(func)
|
||||
local TaskName=multi.randomString(16)
|
||||
for i=1,self.cores do
|
||||
self.queueALL:push({TaskName,func})
|
||||
end
|
||||
end
|
||||
function c:start()
|
||||
self:doToAll(function()
|
||||
_G["__started__"]=true
|
||||
SFunc()
|
||||
end)
|
||||
end
|
||||
GLOBAL["__JQ_COUNT__"]=c.cores
|
||||
for i=1,c.cores do
|
||||
multi:newSystemThread("System Threaded Job Queue Worker Thread #"..i,function()
|
||||
@ -221,81 +249,71 @@ function multi:newSystemThreadedJobQueue(numOfCores)
|
||||
end
|
||||
JQI=sThread.waitFor("THREADED_JQO"):init() -- Grab it
|
||||
JQO=sThread.waitFor("THREADED_JQ"):init() -- Grab it
|
||||
FGLOBAL=sThread.waitFor("THREADED_JQ_F_REG"):init() -- Grab it
|
||||
REG=sThread.waitFor("THREADED_JQ_F_REG"):init() -- Grab it
|
||||
QALL=sThread.waitFor("THREADED_QALL"):init() -- Grab it
|
||||
sThread.sleep(.1) -- lets wait for things to work out
|
||||
setmetatable(_G,{
|
||||
__index=FGLOBAL
|
||||
})
|
||||
GLOBAL["THREADED_JQ"]=nil -- remove it
|
||||
GLOBAL["THREADED_JQO"]=nil -- remove it
|
||||
GLOBAL["THREADED_JQ_F_REG"]=nil -- remove it
|
||||
QALLT={}
|
||||
FUNCS={}
|
||||
SFunc=multi:newFunction(function(self)
|
||||
MainLoop:Pause()
|
||||
self:hold(.1)
|
||||
MainLoop:Resume()
|
||||
self:Pause()
|
||||
end)
|
||||
multi:newLoop(function()
|
||||
sThread.sleep(__sleep__) -- lets allow cpu time for other processes on our system!
|
||||
local job=JQI:pop()
|
||||
if job then
|
||||
local ID=table.remove(job,1) -- return and remove
|
||||
local name=table.remove(job,1) -- return and remove
|
||||
local ret={FGLOBAL:waitFor(name)(unpack(job))} -- unpack the rest
|
||||
JQO:push({ID,ret})
|
||||
local rd=REG:peek()
|
||||
if rd then
|
||||
if not FUNCS[rd[1]] then
|
||||
FUNCS[rd[1]]=rd[2]
|
||||
rd=nil -- lets clean up
|
||||
REG:pop()
|
||||
end
|
||||
end
|
||||
local d=QALL:peek()
|
||||
if d then
|
||||
if not QALLT[d[1]] then
|
||||
QALLT[d[1]]=true
|
||||
d[2]()
|
||||
d=nil -- lets clean up
|
||||
QALL:pop()
|
||||
end
|
||||
end
|
||||
end)
|
||||
multi:mainloop()
|
||||
end)
|
||||
end
|
||||
c.OnJobCompleted=multi:newConnection()
|
||||
c.updater=multi:newLoop(function(self)
|
||||
local data=self.link.queueIN:pop()
|
||||
while data do
|
||||
if data then
|
||||
self.link.OnJobCompleted:Fire(unpack(data))
|
||||
end
|
||||
data=self.link.queueIN:pop()
|
||||
end
|
||||
end)
|
||||
c.updater.link=c
|
||||
return c
|
||||
end
|
||||
if love then
|
||||
if love.thread then
|
||||
function multi:newSystemThreadedJobQueue(numOfCores)
|
||||
local c={}
|
||||
c.jobnum=1
|
||||
c.cores=numOfCores or multi.integration.THREAD.getCores()
|
||||
c.queueIN=multi:newSystemThreadedQueue("THREADED_JQ"):init()
|
||||
c.queueOUT=multi:newSystemThreadedQueue("THREADED_JQO"):init()
|
||||
function c:registerJob(name,func)
|
||||
GLOBAL["__TJQ__"..name.."__"]=func
|
||||
end
|
||||
function c:pushJob(name,...)
|
||||
self.queueOUT:push({self.jobnum,name,...})
|
||||
self.jobnum=self.jobnum+1
|
||||
end
|
||||
local GLOBAL=multi.integration.GLOBAL
|
||||
local sThread=multi.integration.THREAD
|
||||
GLOBAL["__JQ_COUNT__"]=c.cores
|
||||
for i=1,c.cores do
|
||||
multi:newSystemThread("System Threaded Job Queue Worker Thread #"..i,function()
|
||||
GLOBAL=_G.GLOBAL
|
||||
sThread=_G.sThread
|
||||
local JQI=sThread.waitFor("THREADED_JQO"):init() -- Grab it
|
||||
local JQO=sThread.waitFor("THREADED_JQ"):init() -- Grab it
|
||||
sThread.sleep(.1) -- lets wait for things to work out
|
||||
setmetatable(_G,{
|
||||
__index=function(t,k,v)
|
||||
return GLOBAL["__TJQ__"..k.."__"]
|
||||
__index=function(t,k)
|
||||
return FUNCS[k]
|
||||
end
|
||||
})
|
||||
GLOBAL["THREADED_JQ"]=nil -- remove it
|
||||
GLOBAL["THREADED_JQO"]=nil -- remove it
|
||||
multi:newLoop(function()
|
||||
MainLoop=multi:newLoop(function(self)
|
||||
if __started__ then
|
||||
local job=JQI:pop()
|
||||
if job then
|
||||
local d=QALL:peek()
|
||||
if d then
|
||||
if not QALLT[d[1]] then
|
||||
QALLT[d[1]]=true
|
||||
d[2]()
|
||||
d=nil -- lets clean up
|
||||
QALL:pop()
|
||||
end
|
||||
end
|
||||
local ID=table.remove(job,1) -- return and remove
|
||||
local name=table.remove(job,1) -- return and remove
|
||||
local ret={sThread.waitFor("__TJQ__"..name.."__")(unpack(job))} -- unpack the rest
|
||||
JQO:push({ID,ret})
|
||||
if FUNCS[name] then
|
||||
JQO:push({ID,FUNCS[name](unpack(job))})
|
||||
else
|
||||
self:hold(function() return FUNCS[name] end)
|
||||
JQO:push({ID,FUNCS[name](unpack(job))})
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
if not love then
|
||||
multi:mainloop()
|
||||
end
|
||||
end)
|
||||
end
|
||||
c.OnJobCompleted=multi:newConnection()
|
||||
@ -310,8 +328,6 @@ if love then
|
||||
end)
|
||||
c.updater.link=c
|
||||
return c
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:newSystemThreadedExecute(cmd)
|
||||
local c={}
|
||||
53
oldversions/EventManager(0.0.1).lua
Normal file
53
oldversions/EventManager(0.0.1).lua
Normal file
@ -0,0 +1,53 @@
|
||||
IsEvent=true
|
||||
event={
|
||||
Active=true,
|
||||
CT=false,
|
||||
tag={},
|
||||
Events={},
|
||||
EventTracker={},
|
||||
Steps={},
|
||||
setAlarm=function(self,tag,set) event:new("Alarm_"..tag.."(\""..tag.."\")",[[GetCounter(event:getTracker("_Alarm_]]..tag..[["))>=]]..set,[[event:removeAlarm("]]..tag..[[")]]) event:addTracker("_Alarm_"..tag,SetCounter()) end,
|
||||
setEvent=function(self,fname,condition,also) if not(string.find(fname,"(",1,true)) and not(string.find(fname,")",1,true)) then fname=fname.."()" end event:new("Event_"..fname,condition,also) end,
|
||||
removeAlarm=function(self,tag) event:destroy("Alarm_"..tag) event:removeTracker("_Alarm_"..tag) end,
|
||||
updateAlarm=function(self,tag,set) event:removeAlarm(tag) event:setAlarm(tag,set) end,
|
||||
addTracker=function(self,varname,var) event.EventTracker[varname]=var end,
|
||||
getTracker=function(self,var) return event.EventTracker[var] end,
|
||||
removeTracker=function(self,var) event.EventTracker[var]=nil end,
|
||||
trackerExist=function(self,tag) return event.EventTracker[tag]~=nil end,
|
||||
updateTracker=function(self,tag,val) if event:trackerExist(tag) then event.EventTracker[tag]=val end end,
|
||||
alarmExist=function(self,tag) return (event:getTracker("_Alarm_"..tag)~=nil) end,
|
||||
new=function(self,fname,condition,also) if not(also) then also="" end table.insert(self.Events,"if "..condition.." then "..also.." "..fname.." end") table.insert(event.tag,fname) end,
|
||||
eventExist=function(self,tag) for j=1,#event.tag do if event.tag[j]==tag then return true end end return false end,
|
||||
destroy=function(self,tag) for j=1,#event.tag do if event.tag[j]==tag then table.remove(event.tag,j) table.remove(event.Events,j) end end end,
|
||||
Stop=function() event.Active=false end,
|
||||
OnCreate=function() end,
|
||||
OnUpdate=function() end,
|
||||
OnClose=function() end,
|
||||
getActive=function() return event.tag end,
|
||||
Manager=function() event.OnCreate() while event.Active==true do for d=1,#event.Events do if event.Active==true then loadstring(event.Events[d])() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end event.OnClose() end,
|
||||
CManager=function() for d=1,#event.Events do if event.Active==true then loadstring(event.Events[d])() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end,
|
||||
UManager=function() if event.CT==false then event.CT=true event.OnCreate() end for d=1,#event.Events do if event.Active==true then loadstring(event.Events[d])() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end,
|
||||
createStep=function(self,tag,reset,endc)
|
||||
if not(endc) then endc=false end
|
||||
if not(reset) then reset=0 end
|
||||
temp={Name=tag,pos=1,endAt=reset,active=true,endc=endc,
|
||||
Step=function(self) if self~=nil then _G.__CStep__=self if self.active==true then loadstring("Step_"..tag.."("..self.pos..",__CStep__)")() self.pos=self.pos+1 end end if self.endAt+1<=self.pos then self:Reset() if endc==true then loadstring("Step_"..self.Name.."_End(__CStep__)")() end end end,
|
||||
Remove=function(self) for as=1,#event.Steps do if event.Steps[as].Name==self.Name then table.remove(event.Steps,as) end end end,
|
||||
Reset=function(self) self.pos=1 end,
|
||||
Set=function(self,amt) self.pos=amt end,
|
||||
Pause=function(self) self.active=false end,
|
||||
Resume=function(self) self.active=true end,
|
||||
Stop=function(self) self:Reset() self:Pause() end,
|
||||
Start=function(self) self:Resume() end,
|
||||
End=function(self) self.pos=self.EndAt self.endc=true end,
|
||||
}
|
||||
table.insert(event.Steps,temp) return temp end,
|
||||
stepExist=function(self,tag)
|
||||
for a_s=1,#event.Steps do
|
||||
if event.Steps[a_s].Name==tag then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end,
|
||||
}
|
||||
55
oldversions/EventManager(0.9.0).lua
Normal file
55
oldversions/EventManager(0.9.0).lua
Normal file
@ -0,0 +1,55 @@
|
||||
IsEvent=true
|
||||
event={
|
||||
Active=true,
|
||||
CT=false,
|
||||
tag={},
|
||||
Events={},
|
||||
EventTracker={},
|
||||
Steps={},
|
||||
--addAlarm=function(self,tag) end,
|
||||
setAlarm=function(self,tag,set) event:new("Alarm_"..tag.."(\""..tag.."\")",[[GetCounter(event:getTracker("_Alarm_]]..tag..[["))>=]]..set,[[event:removeAlarm("]]..tag..[[")]]) event:addTracker("_Alarm_"..tag,SetCounter()) assert(loadstring("if Alarm_"..tag.."==nil then function Alarm_"..tag.."() print('No function \"Alarm_"..tag.."()\" exists make sure you created it') end end"))() end,
|
||||
setEvent=function(self,fname,condition,also) if not(string.find(fname,"(",1,true)) and not(string.find(fname,")",1,true)) then fname=fname.."()" end a=string.find(fname,"(",1,true) tempstr=string.sub(fname,1,a-1) event:new("Event_"..fname,condition,also) assert(loadstring("if Event_"..tempstr.."==nil then function Event_"..tempstr.."() print('No function \"Event_"..tempstr.."()\" exists make sure you created it') end end"))() end,
|
||||
removeAlarm=function(self,tag) event:destroy("Alarm_"..tag) event:removeTracker("_Alarm_"..tag) end,
|
||||
updateAlarm=function(self,tag,set) event:removeAlarm(tag) event:setAlarm(tag,set) end,
|
||||
addTracker=function(self,varname,var) event.EventTracker[varname]=var end,
|
||||
getTracker=function(self,var) return event.EventTracker[var] end,
|
||||
removeTracker=function(self,var) event.EventTracker[var]=nil end,
|
||||
trackerExist=function(self,tag) return event.EventTracker[tag]~=nil end,
|
||||
updateTracker=function(self,tag,val) if event:trackerExist(tag) then event.EventTracker[tag]=val end end,
|
||||
alarmExist=function(self,tag) return (event:getTracker("_Alarm_"..tag)~=nil) end,
|
||||
new=function(self,fname,condition,also) if not(also) then also="" end table.insert(self.Events,"if "..condition.." then "..also.." "..fname.." end") table.insert(event.tag,fname) end,
|
||||
eventExist=function(self,tag) for j=1,#event.tag do if event.tag[j]==tag then return true end end return false end,
|
||||
destroy=function(self,tag) for j=1,#event.tag do if event.tag[j]==tag then table.remove(event.tag,j) table.remove(event.Events,j) end end end,
|
||||
Stop=function() event.Active=false end,
|
||||
OnCreate=function() end,
|
||||
OnUpdate=function() end,
|
||||
OnClose=function() end,
|
||||
getActive=function() return event.tag end,
|
||||
Manager=function() event.OnCreate() while event.Active==true do for d=1,#event.Events do if event.Active==true then assert(loadstring(event.Events[d]))() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end event.OnClose() end,
|
||||
CManager=function() for d=1,#event.Events do if event.Active==true then assert(loadstring(event.Events[d]))() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end,
|
||||
UManager=function() if event.CT==false then event.CT=true event.OnCreate() end for d=1,#event.Events do if event.Active==true then assert(loadstring(event.Events[d]))() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end,
|
||||
RManager=function() event.OnCreate() while event.Active==true do event.OnUpdate() for d=1,#event.Events do if event.Active==true then assert(loadstring(event.Events[d]))() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end end event.OnClose() end,
|
||||
createStep=function(self,tag,reset,endc)
|
||||
if not(endc) then endc=false end
|
||||
if not(reset) then reset=0 end
|
||||
temp={Name=tag,pos=1,endAt=reset,active=true,endc=endc,
|
||||
Step=function(self) if self~=nil then _G.__CStep__=self if self.active==true then assert(loadstring("Step_"..tag.."("..self.pos..",__CStep__)"))() self.pos=self.pos+1 end end if self.endAt+1<=self.pos then self:Reset() if endc==true then assert(loadstring("Step_"..self.Name.."_End(__CStep__)"))() end end end,
|
||||
Remove=function(self) for as=1,#event.Steps do if event.Steps[as].Name==self.Name then table.remove(event.Steps,as) end end end,
|
||||
Reset=function(self) self.pos=1 end,
|
||||
Set=function(self,amt) self.pos=amt end,
|
||||
Pause=function(self) self.active=false end,
|
||||
Resume=function(self) self.active=true end,
|
||||
Stop=function(self) self:Reset() self:Pause() end,
|
||||
Start=function(self) self:Resume() end,
|
||||
End=function(self) self.pos=self.EndAt self.endc=true end,
|
||||
}
|
||||
table.insert(event.Steps,temp) return temp end,
|
||||
stepExist=function(self,tag)
|
||||
for a_s=1,#event.Steps do
|
||||
if event.Steps[a_s].Name==tag then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end,
|
||||
}
|
||||
55
oldversions/EventManager(1.0.0).lua
Normal file
55
oldversions/EventManager(1.0.0).lua
Normal file
@ -0,0 +1,55 @@
|
||||
IsEvent=true
|
||||
event={
|
||||
Active=true,
|
||||
CT=false,
|
||||
tag={},
|
||||
Events={},
|
||||
EventTracker={},
|
||||
Steps={},
|
||||
--addAlarm=function(self,tag) end,
|
||||
setAlarm=function(self,tag,set) event:new("Alarm_"..tag.."(\""..tag.."\")",[[GetCounter(event:getTracker("_Alarm_]]..tag..[["))>=]]..set,[[event:removeAlarm("]]..tag..[[")]]) event:addTracker("_Alarm_"..tag,SetCounter()) assert(loadstring("if Alarm_"..tag.."==nil then function Alarm_"..tag.."() print('No function \"Alarm_"..tag.."()\" exists make sure you created it') end end"))() end,
|
||||
setEvent=function(self,fname,condition,also) if not(string.find(fname,"(",1,true)) and not(string.find(fname,")",1,true)) then fname=fname.."()" end a=string.find(fname,"(",1,true) tempstr=string.sub(fname,1,a-1) event:new("Event_"..fname,condition,also) assert(loadstring("if Event_"..tempstr.."==nil then function Event_"..tempstr.."() print('No function \"Event_"..tempstr.."()\" exists make sure you created it') end end"))() end,
|
||||
removeAlarm=function(self,tag) event:destroy("Alarm_"..tag) event:removeTracker("_Alarm_"..tag) end,
|
||||
updateAlarm=function(self,tag,set) event:removeAlarm(tag) event:setAlarm(tag,set) end,
|
||||
addTracker=function(self,varname,var) event.EventTracker[varname]=var end,
|
||||
getTracker=function(self,var) return event.EventTracker[var] end,
|
||||
removeTracker=function(self,var) event.EventTracker[var]=nil end,
|
||||
trackerExist=function(self,tag) return event.EventTracker[tag]~=nil end,
|
||||
updateTracker=function(self,tag,val) if event:trackerExist(tag) then event.EventTracker[tag]=val end end,
|
||||
alarmExist=function(self,tag) return (event:getTracker("_Alarm_"..tag)~=nil) end,
|
||||
new=function(self,fname,condition,also) if not(also) then also="" end table.insert(self.Events,"if "..condition.." then "..also.." "..fname.." end") table.insert(event.tag,fname) end,
|
||||
eventExist=function(self,tag) for j=1,#event.tag do if event.tag[j]==tag then return true end end return false end,
|
||||
destroy=function(self,tag) for j=1,#event.tag do if event.tag[j]==tag then table.remove(event.tag,j) table.remove(event.Events,j) end end end,
|
||||
Stop=function() event.Active=false end,
|
||||
OnCreate=function() end,
|
||||
OnUpdate=function() end,
|
||||
OnClose=function() end,
|
||||
getActive=function() return event.tag end,
|
||||
Manager=function() event.OnCreate() while event.Active==true do for d=1,#event.Events do if event.Active==true then assert(loadstring(event.Events[d]))() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end event.OnClose() end,
|
||||
CManager=function() for d=1,#event.Events do if event.Active==true then assert(loadstring(event.Events[d]))() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end,
|
||||
UManager=function() if event.CT==false then event.CT=true event.OnCreate() end for d=1,#event.Events do if event.Active==true then assert(loadstring(event.Events[d]))() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end,
|
||||
RManager=function() event.OnCreate() while event.Active==true do event.OnUpdate() for d=1,#event.Events do if event.Active==true then assert(loadstring(event.Events[d]))() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end end event.OnClose() end,
|
||||
createStep=function(self,tag,reset,endc)
|
||||
if not(endc) then endc=false end
|
||||
if not(reset) then reset=0 end
|
||||
temp={Name=tag,pos=1,endAt=reset,active=true,endc=endc,
|
||||
Step=function(self) if self~=nil then _G.__CStep__=self if self.active==true then assert(loadstring("Step_"..tag.."("..self.pos..",__CStep__)"))() self.pos=self.pos+1 end end if self.endAt+1<=self.pos then self:Reset() if endc==true then assert(loadstring("Step_"..self.Name.."_End(__CStep__)"))() end end end,
|
||||
Remove=function(self) for as=1,#event.Steps do if event.Steps[as].Name==self.Name then table.remove(event.Steps,as) end end end,
|
||||
Reset=function(self) self.pos=1 end,
|
||||
Set=function(self,amt) self.pos=amt end,
|
||||
Pause=function(self) self.active=false end,
|
||||
Resume=function(self) self.active=true end,
|
||||
Stop=function(self) self:Reset() self:Pause() end,
|
||||
Start=function(self) self:Resume() end,
|
||||
End=function(self) self.pos=self.EndAt self.endc=true end,
|
||||
}
|
||||
table.insert(event.Steps,temp) return temp end,
|
||||
stepExist=function(self,tag)
|
||||
for a_s=1,#event.Steps do
|
||||
if event.Steps[a_s].Name==tag then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end,
|
||||
}
|
||||
98
oldversions/EventManager(1.1.0).lua
Normal file
98
oldversions/EventManager(1.1.0).lua
Normal file
@ -0,0 +1,98 @@
|
||||
require("Data/BasicCommands")
|
||||
IsEvent=true
|
||||
event={
|
||||
Active=true,
|
||||
CT=false,
|
||||
tag={},
|
||||
Events={},
|
||||
EventTracker={},
|
||||
Steps={},
|
||||
TSteps={},
|
||||
LoadOrder="/E/S/U",-- types = ESU,EUS,USE,UES,SEU,SUE
|
||||
setLoadOrder=function(self,str) event.LoadOrder=string.upper(str) end,
|
||||
DO_Order=function() LoadOrder=event.LoadOrder LoadOrder=LoadOrder:gsub("/E", "event.RUN_EVENTS(); ") LoadOrder=LoadOrder:gsub("/S", "event.RUN_STEPS(); ") LoadOrder=LoadOrder:gsub("/U", "event.RUN_UPDATES(); ") assert(loadstring(LoadOrder))() end,
|
||||
RUN_EVENTS=function() for d=1,#event.Events do assert(loadstring(event.Events[d]))() end end,
|
||||
RUN_STEPS=function() for s_g=1,#event.Steps do event.Steps[s_g]:Step() end end,
|
||||
RUN_UPDATES=function() event.OnUpdate() end,
|
||||
setAlarm=function(self,tag,set) event:new("Alarm_"..tag.."(\""..tag.."\")",[[GetCounter(event:getTracker("_Alarm_]]..tag..[["))>=]]..set,[[event:removeAlarm("]]..tag..[[")]]) event:addTracker("_Alarm_"..tag,SetCounter()) assert(loadstring("if Alarm_"..tag.."==nil then function Alarm_"..tag.."() print('No function \"Alarm_"..tag.."()\" exists make sure you created it') end end"))() end,
|
||||
setEvent=function(self,fname,condition,also) if not(string.find(fname,"(",1,true)) and not(string.find(fname,")",1,true)) then fname=fname.."()" end a=string.find(fname,"(",1,true) tempstr=string.sub(fname,1,a-1) event:new("Event_"..fname,condition,also) assert(loadstring("if Event_"..tempstr.."==nil then function Event_"..tempstr.."() print('No function \"Event_"..tempstr.."()\" exists make sure you created it') end end"))() end,
|
||||
removeAlarm=function(self,tag) event:destroy("Alarm_"..tag) event:removeTracker("_Alarm_"..tag) end,
|
||||
updateAlarm=function(self,tag,set) event:removeAlarm(tag) event:setAlarm(tag,set) end,
|
||||
addTracker=function(self,varname,var) event.EventTracker[varname]=var end,
|
||||
getTracker=function(self,varname) return event.EventTracker[varname] end,
|
||||
removeTracker=function(self,var) event.EventTracker[var]=nil end,
|
||||
trackerExist=function(self,tag) return event.EventTracker[tag]~=nil end,
|
||||
updateTracker=function(self,tag,val) if event:trackerExist(tag) then event.EventTracker[tag]=val end end,
|
||||
alarmExist=function(self,tag) return (event:getTracker("_Alarm_"..tag)~=nil) end,
|
||||
new=function(self,fname,condition,also) if not(also) then also="" end table.insert(self.Events,"if "..condition.." then "..also.." "..fname.." end") table.insert(event.tag,fname) end,
|
||||
eventExist=function(self,tag) for j=1,#event.tag do if event.tag[j]==tag then return true end end return false end,
|
||||
destroy=function(self,tag) for j=1,#event.tag do if event.tag[j]==tag then table.remove(event.tag,j) table.remove(event.Events,j) end end end,
|
||||
Stop=function() event.Active=false end,
|
||||
OnCreate=function() end,
|
||||
OnUpdate=function() end,
|
||||
OnClose=function() end,
|
||||
getActive=function() return event.tag end,
|
||||
Manager=function() event.OnCreate() while event.Active==true do event.DO_Order() end event.OnClose() end,
|
||||
--Manager=function() event.OnCreate() while event.Active==true do for d=1,#event.Events do if event.Active==true then assert(loadstring(event.Events[d]))() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end event.OnClose() end,
|
||||
CManager=function() for d=1,#event.Events do if event.Active==true then assert(loadstring(event.Events[d]))() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end,
|
||||
UManager=function() if event.CT==false then event.CT=true event.OnCreate() end for d=1,#event.Events do if event.Active==true then assert(loadstring(event.Events[d]))() end end for s_g=1,#event.Steps do event.Steps[s_g]:Step() end event.OnUpdate() end,
|
||||
createStep=function(self,tag,reset,skip,endc)
|
||||
if not(endc) then endc=false end
|
||||
temp=
|
||||
{
|
||||
Name=tag,
|
||||
pos=1,
|
||||
endAt=reset or math.huge,
|
||||
active=true,
|
||||
endc=endc,
|
||||
skip=skip or 0,
|
||||
spos=0,
|
||||
Step=function(self) if self~=nil then if self.spos==0 then _G.__CStep__=self if self.active==true then assert(loadstring("Step_"..tag.."("..self.pos..",__CStep__)"))() self.pos=self.pos+1 end end if self.endAt+1<=self.pos then self:Reset() if endc==true then assert(loadstring("Step_"..self.Name.."_End(__CStep__)"))() end end end self.spos=self.spos+1 if self.spos>=self.skip then self.spos=0 end end,
|
||||
FStep=function(self) if self~=nil then if self.spos==0 then _G.__CStep__=self assert(loadstring("Step_"..tag.."("..self.pos..",__CStep__)"))() self.pos=self.pos+1 end end if self.endAt+1<=self.pos then self:Reset() if endc==true then assert(loadstring("Step_"..self.Name.."_End(__CStep__)"))() end end self.spos=self.spos+1 if self.spos>=self.skip then self.spos=0 end end,
|
||||
Remove=function(self) for as=1,#event.Steps do if event.Steps[as].Name==self.Name then table.remove(event.Steps,as) end end end,
|
||||
Reset=function(self) self.pos=1 end,
|
||||
Set=function(self,amt) self.pos=amt end,
|
||||
Pause=function(self) self.active=false end,
|
||||
Resume=function(self) self.active=true end,
|
||||
Stop=function(self) self:Reset() self:Pause() end,
|
||||
Start=function(self) self:Resume() end,
|
||||
End=function(self) self.pos=self.EndAt self.endc=true end,
|
||||
}
|
||||
table.insert(event.Steps,temp) return temp end,
|
||||
createTStep=function(self,tag,reset,timer,endc)
|
||||
if not(endc) then endc=false end
|
||||
timer=timer or 1
|
||||
temp=
|
||||
{
|
||||
Name=tag,
|
||||
pos=1,
|
||||
endAt=reset or math.huge,
|
||||
active=true,
|
||||
endc=endc,
|
||||
skip= 0,
|
||||
spos=0,
|
||||
Step=function(self) if self~=nil then if self.spos==0 then _G.__CStep__=self if self.active==true then assert(loadstring("TStep_"..tag.."("..self.pos..",__CStep__)"))() self.pos=self.pos+1 end end if self.endAt+1<=self.pos then self:Reset() if endc==true then assert(loadstring("TStep_"..self.Name.."_End(__CStep__)"))() end end end self.spos=self.spos+1 if self.spos>=self.skip then self.spos=0 end end,
|
||||
FStep=function(self) if self~=nil then if self.spos==0 then _G.__CStep__=self assert(loadstring("TStep_"..tag.."("..self.pos..",__CStep__)"))() self.pos=self.pos+1 end end if self.endAt+1<=self.pos then self:Reset() if endc==true then assert(loadstring("TStep_"..self.Name.."_End(__CStep__)"))() end end self.spos=self.spos+1 if self.spos>=self.skip then self.spos=0 end end,
|
||||
Remove=function(self) for as=1,#event.Steps do if event.Steps[as].Name==self.Name then table.remove(event.Steps,as) end end end,
|
||||
Reset=function(self) self.pos=1 end,
|
||||
Set=function(self,amt) self.pos=amt end,
|
||||
Pause=function(self) self.active=false end,
|
||||
Resume=function(self) self.active=true end,
|
||||
Stop=function(self) self:Reset() self:Pause() end,
|
||||
Start=function(self) self:Resume() end,
|
||||
End=function(self) self.pos=self.EndAt self.endc=true end,
|
||||
}
|
||||
event:setAlarm("TStep_"..tag,timer)
|
||||
event:addTracker("_TStep_"..tag,temp)
|
||||
table.insert(event.TSteps,temp)
|
||||
assert(loadstring("function Alarm_TStep_"..tag.."(alarm) event:getTracker(\"_\"..alarm):Step() event:setAlarm(alarm,"..timer..") end"))()
|
||||
return temp end,
|
||||
stepExist=function(self,tag)
|
||||
for a_s=1,#event.Steps do
|
||||
if event.Steps[a_s].Name==tag then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end,
|
||||
}
|
||||
203
oldversions/EventManager(1.2.0).lua
Normal file
203
oldversions/EventManager(1.2.0).lua
Normal file
@ -0,0 +1,203 @@
|
||||
function dump(t,indent)
|
||||
local names = {}
|
||||
if not indent then indent = "" end
|
||||
for n,g in pairs(t) do
|
||||
table.insert(names,n)
|
||||
end
|
||||
table.sort(names)
|
||||
for i,n in pairs(names) do
|
||||
local v = t[n]
|
||||
if type(v) == "table" then
|
||||
if(v==t) then -- prevent endless loop if table contains reference to itself
|
||||
print(indent..tostring(n)..": <-")
|
||||
else
|
||||
print(indent..tostring(n)..":")
|
||||
dump(v,indent.." ")
|
||||
end
|
||||
else
|
||||
if type(v) == "function" then
|
||||
print(indent..tostring(n).."()")
|
||||
else
|
||||
print(indent..tostring(n)..": "..tostring(v))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function SetCounter()
|
||||
return os.clock()
|
||||
end
|
||||
----------------------------------------------------------------------------------------------------
|
||||
function GetCounter(count)
|
||||
if count~=nil then
|
||||
return os.clock()-count
|
||||
else
|
||||
return 0
|
||||
end
|
||||
end
|
||||
----------------------------------------------------------------------------------------------------
|
||||
clock=os.clock
|
||||
event={
|
||||
Active=true,
|
||||
CT=false,
|
||||
tag={},
|
||||
Events={},
|
||||
Alarms={},
|
||||
EventTracker={},
|
||||
Steps={},
|
||||
TSteps={},
|
||||
CTask="",
|
||||
LoadOrder="/E/S/A/U",
|
||||
UpdateObj=
|
||||
{
|
||||
LoadOrder="",
|
||||
Resume=function(self) event.LoadOrder=self.LoadOrder end,
|
||||
Pause=function(self) self.LoadOrder=event.LoadOrder event.LoadOrder=event.LoadOrder:gsub("/U", "") end,
|
||||
},
|
||||
setLoadOrder=function(self,str) event.LoadOrder=string.upper(str) end,
|
||||
DO_Order=function() LoadOrder=event.LoadOrder LoadOrder=LoadOrder:gsub("/A", "event.RUN_ALARMS(); ") LoadOrder=LoadOrder:gsub("/E", "event.RUN_EVENTS(); ") LoadOrder=LoadOrder:gsub("/S", "event.RUN_STEPS(); ") LoadOrder=LoadOrder:gsub("/U", "event.RUN_UPDATES(); ") assert(loadstring(LoadOrder))() end,
|
||||
RUN_EVENTS=function() event.CTask="events" for d=1,#event.Events do assert(loadstring(event.Events[d]))() end end,
|
||||
RUN_STEPS=function() event.CTask="steps" for s_g=1,#event.Steps do event.Steps[s_g]:Step() end end,
|
||||
RUN_UPDATES=function() _G.__CAction__=event.UpdateObj event.CTask="updates" event.OnUpdate() end,
|
||||
RUN_ALARMS=function() for i=1,#event.Alarms do event.Alarms[i]:Tick() end end,
|
||||
--System Used Functions
|
||||
Hold=function(self,task) -- as many conditions and times that you want can be used
|
||||
local action=__CAction__
|
||||
action:Pause()
|
||||
if type(task)=="number" then
|
||||
local func=function() end
|
||||
local alarm=event:newAlarm(task,func,true)
|
||||
while alarm.active==true do
|
||||
event.CManager()
|
||||
end
|
||||
alarm:Destroy()
|
||||
action:Resume()
|
||||
elseif type(task)=="string" then
|
||||
assert(loadstring("while not("..task..") do event.CManager() end"))()
|
||||
action:Resume()
|
||||
end
|
||||
end,
|
||||
newAlarm=function(self,set,func,start)
|
||||
if not(start) then timer=0 active=false else timer=clock() active=true end
|
||||
if not(func) then func=(function() end) end
|
||||
Alarm=
|
||||
{
|
||||
active=active,
|
||||
timer=timer,
|
||||
set=set or 0,
|
||||
func=func,
|
||||
Tick=function(self) _G.__CAction__=self if self.active==true then if clock()-self.timer>=self.set then self:Pause() self:Ring() end end end,
|
||||
Pause=function(self) self.active=false end,
|
||||
Set=function(self,amt) self.set=amt self.timer=clock() self:Resume() end,
|
||||
OnRing=function(self,func) self.func=func end,
|
||||
Ring=function(self) self:func(self) end,
|
||||
Reset=function(self) self.timer=clock() self:Resume() end,
|
||||
Resume=function(self) self.active=true end,
|
||||
Destroy=function(self) for i=1,#event.Alarms do if tostring(event.Alarms[i])==tostring(self) then table.remove(event.Alarms,i) end end end,
|
||||
}
|
||||
table.insert(event.Alarms,Alarm)
|
||||
return Alarm
|
||||
end,
|
||||
setAlarm=function(self,tag,set)
|
||||
if event:eventExist("Alarm_"..tag.."(\""..tag.."\")")==false then
|
||||
event:new("Alarm_"..tag.."(\""..tag.."\")",[[GetCounter(event:getTracker("_Alarm_]]..tag..[["))>=]]..set,[[event:removeAlarm("]]..tag..[[")]])
|
||||
event:addTracker("_Alarm_"..tag,SetCounter())
|
||||
assert(loadstring("if Alarm_"..tag.."==nil then function Alarm_"..tag.."() print('No function \"Alarm_"..tag.."()\" exists make sure you created it') end end"))()
|
||||
else
|
||||
event:addTracker("_Alarm_"..tag,SetCounter())
|
||||
end
|
||||
end,
|
||||
setEvent=function(self,fname,condition,also) if not(string.find(fname,"(",1,true)) and not(string.find(fname,")",1,true)) then fname=fname.."()" end a=string.find(fname,"(",1,true) tempstr=string.sub(fname,1,a-1) event:new("Event_"..fname,condition,also) assert(loadstring("if Event_"..tempstr.."==nil then function Event_"..tempstr.."() print('No function \"Event_"..tempstr.."()\" exists make sure you created it') end end"))() end,
|
||||
removeAlarm=function(self,tag) event:destroyEvent("Alarm_"..tag) event:removeTracker("_Alarm_"..tag) end,
|
||||
updateAlarm=function(self,tag,set) event:removeAlarm(tag) event:setAlarm(tag,set) end,
|
||||
addTracker=function(self,varname,var) event.EventTracker[varname]=var end,
|
||||
getTracker=function(self,varname) return event.EventTracker[varname] end,
|
||||
listTrackers=function(self) return event.EventTracker end,
|
||||
removeTracker=function(self,var) event.EventTracker[var]=nil end,
|
||||
trackerExist=function(self,tag) return event.EventTracker[tag]~=nil end,
|
||||
updateTracker=function(self,tag,val) if event:trackerExist(tag) then event.EventTracker[tag]=val end end,
|
||||
alarmExist=function(self,tag) return (event:getTracker("_Alarm_"..tag)~=nil) end,
|
||||
new=function(self,fname,condition,also) if not(also) then also="" end table.insert(self.Events,"if "..condition.." then "..also.." "..fname.." end") table.insert(event.tag,fname) end,
|
||||
eventExist=function(self,tag) for j=1,#event.tag do if event.tag[j]==tag then return true end end return false end,
|
||||
destroyEvent=function(self,tag) for j=1,#event.tag do if event.tag[j]==tag then table.remove(event.tag,j) table.remove(event.Events,j) end end end,
|
||||
Stop=function() event.Active=false end,
|
||||
OnCreate=function() end,
|
||||
OnUpdate=function() end,
|
||||
OnClose=function() end,
|
||||
getActive=function() return event.tag end,
|
||||
Manager=function() event.OnCreate() while event.Active==true do event.DO_Order() end event.OnClose() end,
|
||||
CManager=function() if event.Active==true then event.DO_Order() end end,
|
||||
UManager=function() if event.CT==false then event.CT=true event.OnCreate() end if event.Active==true then event.DO_Order() end end,
|
||||
createStep=function(self,tag,reset,skip,endc)
|
||||
if not(endc) then
|
||||
endc=false
|
||||
end
|
||||
temp=
|
||||
{
|
||||
Name=tag,
|
||||
pos=1,
|
||||
endAt=reset or math.huge,
|
||||
active=true,
|
||||
endc=endc,
|
||||
skip=skip or 0,
|
||||
spos=0,
|
||||
Step=function(self) if self~=nil then if self.spos==0 then _G.__CAction__=self if self.active==true then assert(loadstring("Step_"..tag.."("..self.pos..",__CAction__)"))() self.pos=self.pos+1 end end if self.endAt+1<=self.pos then self:Reset() if endc==true then assert(loadstring("Step_"..self.Name.."_End(__CAction__)"))() end end end self.spos=self.spos+1 if self.spos>=self.skip then self.spos=0 end end,
|
||||
FStep=function(self) if self~=nil then if self.spos==0 then _G.__CAction__=self assert(loadstring("Step_"..tag.."("..self.pos..",__CAction__)"))() self.pos=self.pos+1 end end if self.endAt+1<=self.pos then self:Reset() if endc==true then assert(loadstring("Step_"..self.Name.."_End(__CAction__)"))() end end self.spos=self.spos+1 if self.spos>=self.skip then self.spos=0 end end,
|
||||
Remove=function(self) for as=1,#event.Steps do if event.Steps[as].Name==self.Name then table.remove(event.Steps,as) end end end,
|
||||
Reset=function(self) self.pos=1 end,
|
||||
Set=function(self,amt) self.pos=amt end,
|
||||
Pause=function(self) self.active=false end,
|
||||
Resume=function(self) self.active=true end,
|
||||
Stop=function(self) self:Reset() self:Pause() end,
|
||||
Start=function(self) self:Resume() end,
|
||||
End=function(self) _G.__CAction__=self assert(loadstring("Step_"..self.Name.."_End(__CAction__)"))() self:Reset() end,
|
||||
}
|
||||
table.insert(event.Steps,temp) return temp
|
||||
end,
|
||||
createTStep=function(self,tag,reset,timer,endc)
|
||||
if not(endc) then
|
||||
endc=false
|
||||
end
|
||||
timer=timer or 1
|
||||
temp=
|
||||
{
|
||||
Name=tag,
|
||||
pos=1,
|
||||
endAt=reset or math.huge,
|
||||
active=true,
|
||||
endc=endc,
|
||||
skip= 0,
|
||||
spos=0,
|
||||
Step=function(self) if self~=nil then if self.spos==0 then _G.__CAction__=self if self.active==true then assert(loadstring("TStep_"..tag.."("..self.pos..",__CAction__)"))() self.pos=self.pos+1 end end if self.endAt+1<=self.pos then self:Reset() if endc==true then assert(loadstring("TStep_"..self.Name.."_End(__CAction__)"))() end end end self.spos=self.spos+1 if self.spos>=self.skip then self.spos=0 end end,
|
||||
FStep=function(self) if self~=nil then if self.spos==0 then _G.__CAction__=self assert(loadstring("TStep_"..tag.."("..self.pos..",__CAction__)"))() self.pos=self.pos+1 end end if self.endAt+1<=self.pos then self:Reset() if endc==true then assert(loadstring("TStep_"..self.Name.."_End(__CAction__)"))() end end self.spos=self.spos+1 if self.spos>=self.skip then self.spos=0 end end,
|
||||
Remove=function(self) for as=1,#event.Steps do if event.Steps[as].Name==self.Name then table.remove(event.Steps,as) end end end,
|
||||
Reset=function(self) self.pos=1 end,
|
||||
Set=function(self,amt) self.pos=amt end,
|
||||
Pause=function(self) self.active=false end,
|
||||
Resume=function(self) self.active=true end,
|
||||
Stop=function(self) self:Reset() self:Pause() end,
|
||||
Start=function(self) self:Resume() end,
|
||||
End=function(self) self.pos=self.EndAt self.endc=true end,
|
||||
}
|
||||
event:setAlarm("TStep_"..tag,timer)
|
||||
event:addTracker("_TStep_"..tag,temp)
|
||||
table.insert(event.TSteps,temp)
|
||||
assert(loadstring("function Alarm_TStep_"..tag.."(alarm) event:getTracker(\"_\"..alarm):Step() event:updateAlarm(alarm,"..timer..") end"))()
|
||||
return temp
|
||||
end,
|
||||
stepExist=function(self,tag)
|
||||
for a_s=1,#event.Steps do
|
||||
if event.Steps[a_s].Name==tag then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end,
|
||||
tstepExist=function(self,tag)
|
||||
for a_s=1,#event.TSteps do
|
||||
if event.TSteps[a_s].Name==tag then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end,
|
||||
}
|
||||
728
oldversions/EventManager(1.3.0).lua
Normal file
728
oldversions/EventManager(1.3.0).lua
Normal file
@ -0,0 +1,728 @@
|
||||
function readonlytable(table)
|
||||
return setmetatable({}, {
|
||||
__index = table,
|
||||
__newindex = function(table, key, value)
|
||||
error("Attempt to modify read-only table")
|
||||
end,
|
||||
__metatable = false
|
||||
});
|
||||
end
|
||||
local EventRef=
|
||||
readonlytable{
|
||||
Pause=function(self)
|
||||
self.active=false
|
||||
if not(event.isPaused(self)) then
|
||||
table.insert(event.Paused,self)
|
||||
for _j=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[_j])==tostring(self) then
|
||||
table.remove(event.Mainloop,_j)
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
Resume=function(self)
|
||||
self.active=true
|
||||
if event.isPaused(self) then
|
||||
table.insert(event.Mainloop,self)
|
||||
for _j=1,#event.Paused do
|
||||
if tostring(event.Paused[_j])==tostring(self) then
|
||||
table.remove(event.Paused,_j)
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
Stop=function(self)
|
||||
self.active=nil
|
||||
end,
|
||||
}
|
||||
|
||||
local StepRef=
|
||||
readonlytable{
|
||||
Step=function(self)
|
||||
if self~=nil then
|
||||
if self.spos==0 then
|
||||
_G.__CAction__=self
|
||||
if self.active==true then
|
||||
for i=1,#self.steps do
|
||||
self.steps[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
end
|
||||
end
|
||||
if self.endAt+self.count<=self.pos and self.endAt>self.start then
|
||||
self:Reset()
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
elseif self.pos<=0 then
|
||||
self:Reset()
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
self.spos=self.spos+1
|
||||
if self.spos>=self.skip then
|
||||
self.spos=0
|
||||
end
|
||||
end,
|
||||
FStep=function(self)
|
||||
if self~=nil then
|
||||
if self.spos==0 then
|
||||
_G.__CAction__=self
|
||||
for i=1,#self.steps do
|
||||
self.steps[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
end
|
||||
if self.endAt+self.count<=self.pos and self.endAt>self.start then
|
||||
self:Reset()
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
elseif self.pos==0 then
|
||||
self:Reset()
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
self.spos=self.spos+1
|
||||
if self.spos>=self.skip then
|
||||
self.spos=0
|
||||
end
|
||||
end,
|
||||
Remove=function(self)
|
||||
if self~=_G.__CAction__ then
|
||||
for as=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[as])==tostring(self) then
|
||||
table.remove(event.Mainloop,as)
|
||||
end
|
||||
end
|
||||
else
|
||||
table.insert(event.garbage,self)
|
||||
end
|
||||
end,
|
||||
Reset=function(self)
|
||||
self.pos=self.start
|
||||
end,
|
||||
Set=function(self,amt)
|
||||
self.pos=amt
|
||||
end,
|
||||
Pause=EventRef.Pause,
|
||||
Resume=EventRef.Resume,
|
||||
Stop=function(self)
|
||||
self:Reset()
|
||||
self.active=nil
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
end,
|
||||
End=function(self)
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
self:Reset()
|
||||
end,
|
||||
Update=function(self,start,reset,count,skip)
|
||||
self.start=start or self.start
|
||||
self.endAt=reset or self.endAt
|
||||
self.skip=skip or self.skip
|
||||
self.count=count or self.count
|
||||
self:Resume()
|
||||
end,
|
||||
OnEnd=function(self,func)
|
||||
table.insert(self.funcs,func)
|
||||
end,
|
||||
OnStep=function(self,func)
|
||||
table.insert(self.steps,func)
|
||||
end,
|
||||
FreeConnections=function(self)
|
||||
self.funcs={}
|
||||
self.steps={}
|
||||
end,
|
||||
}
|
||||
--thread and run setup
|
||||
if love then
|
||||
function love.run()
|
||||
if love.math then
|
||||
love.math.setRandomSeed(os.time())
|
||||
end
|
||||
if love.event then
|
||||
love.event.pump()
|
||||
end
|
||||
if love.load then love.load(arg) end
|
||||
if love.timer then love.timer.step() end
|
||||
local dt = 0
|
||||
while true do
|
||||
-- Process events.
|
||||
if love.event then
|
||||
love.event.pump()
|
||||
for e,a,b,c,d in love.event.poll() do
|
||||
if e == "quit" then
|
||||
if not love.quit or not love.quit() then
|
||||
if love.audio then
|
||||
love.audio.stop()
|
||||
end
|
||||
return
|
||||
end
|
||||
end
|
||||
love.handlers[e](a,b,c,d)
|
||||
end
|
||||
end
|
||||
if love.timer then
|
||||
love.timer.step()
|
||||
dt = love.timer.getDelta()
|
||||
end
|
||||
if love.update then love.update(dt) end
|
||||
event.uManager(dt)
|
||||
if love.window and love.graphics and love.window.isCreated() then
|
||||
love.graphics.clear()
|
||||
love.graphics.origin()
|
||||
if love.draw then love.draw() end
|
||||
event.dManager()
|
||||
love.graphics.setColor(255,255,255,255)
|
||||
if event.draw then event.draw() end
|
||||
love.graphics.present()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
eThreads={
|
||||
send=function() end,
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
function RunTasks()
|
||||
for i=1,#event.DoTasks do
|
||||
event.DoTasks[i]()
|
||||
end
|
||||
end
|
||||
event={
|
||||
VERSION="1.0.0 (Build Version: 5.7.3)",
|
||||
Priority_Core=1,
|
||||
Priority_High=2,
|
||||
Priority_Above_Normal=4,
|
||||
Priority_Normal=16,
|
||||
Priority_Low=256,
|
||||
Priority_Idle=65536,
|
||||
Start=0,
|
||||
Active=true,
|
||||
CT=false,
|
||||
Tasks={},
|
||||
DoTasks={},
|
||||
garbage={},
|
||||
Paused={},
|
||||
last={},
|
||||
func={},
|
||||
drawF={},
|
||||
pump=false,
|
||||
pumpvar=0,
|
||||
Mainloop={},
|
||||
PEnabled=true,
|
||||
PCount=1,
|
||||
Triggers={},
|
||||
oneTimeObj=
|
||||
{
|
||||
last={},
|
||||
Resume=function(self) end,
|
||||
Pause=function(self) end,
|
||||
},
|
||||
RemoveAll=function()
|
||||
event.Mainloop={}
|
||||
end,
|
||||
GarbageObj=
|
||||
{
|
||||
Resume=function() end,
|
||||
Pause=function() end
|
||||
},
|
||||
isPaused=function(obj)
|
||||
for _j=1,#event.Paused do
|
||||
if tostring(event.Paused[_j])==tostring(obj) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end,
|
||||
DO_Order=function()
|
||||
event.oneTime(RunTasks)
|
||||
event.PCount=event.PCount+1
|
||||
for i=1,#event.Mainloop do
|
||||
if event.Mainloop[i]~=nil then
|
||||
local obj = event.Mainloop[i]
|
||||
if event.PCount%obj.Priority==0 and event.PEnabled then
|
||||
obj:Act()
|
||||
elseif event.PEnabled==false then
|
||||
obj:Act()
|
||||
end
|
||||
if event.PCount>event.Priority_Idle then
|
||||
event.PCount=event.Priority_Core
|
||||
end
|
||||
end
|
||||
end
|
||||
event.MANAGE_GARBAGE()
|
||||
end,
|
||||
MANAGE_GARBAGE=function()
|
||||
_G.__CAction__=event.GarbageObj
|
||||
for _i=1,#event.garbage do
|
||||
event.garbage[_i]:Remove()
|
||||
table.remove(event.garbage,_i)
|
||||
end
|
||||
end,
|
||||
oneTime=function(func)
|
||||
event.oneTimeObj.last=_G.__CAction__
|
||||
_G.__CAction__=event.oneTimeObj
|
||||
for _k=1,#event.Tasks do
|
||||
if event.Tasks[_k]==func then
|
||||
_G.__CAction__=event.oneTimeObj.last
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(event.Tasks,func)
|
||||
func()
|
||||
_G.__CAction__=event.oneTimeObj.last
|
||||
return true
|
||||
end,
|
||||
oneETime=function(func)
|
||||
for _k=1,#event.Tasks do
|
||||
if event.Tasks[_k]==string.dump(func) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(event.Tasks,string.dump(func))
|
||||
func()
|
||||
return true
|
||||
end,
|
||||
hold=function(task) -- as many conditions and times that you want can be used
|
||||
local action=__CAction__
|
||||
action:Pause()
|
||||
if type(task)=="number" then
|
||||
local alarm=event.newAlarm(task,function() end,true)
|
||||
while alarm.active==true do
|
||||
if love then
|
||||
event.lManager()
|
||||
else
|
||||
event.cManager()
|
||||
end
|
||||
end
|
||||
alarm:Remove()
|
||||
action:Resume()
|
||||
elseif type(task)=="function" then
|
||||
local env=event.newEvent(task,function(envt) envt:Pause() envt:Stop() end)
|
||||
while env.active do
|
||||
if love then
|
||||
event.lManager()
|
||||
else
|
||||
event.cManager()
|
||||
end
|
||||
end
|
||||
env:Remove()
|
||||
action:Resume()
|
||||
else
|
||||
print("Error Data Type!!!")
|
||||
end
|
||||
end,
|
||||
waitFor=function(obj)
|
||||
local obj=obj
|
||||
event.hold(function() return not(obj.active) end)
|
||||
end,
|
||||
getType=function(obj)
|
||||
if obj.Type~=nil then
|
||||
return obj.Type
|
||||
end
|
||||
end,
|
||||
newEvent=function(test,task)
|
||||
temp=
|
||||
{
|
||||
Priority=event.Priority_Normal,
|
||||
_Priority=0,
|
||||
Parent=event.Events,
|
||||
Type="Event",
|
||||
active=true,
|
||||
test=test or (function() end),
|
||||
task={task} or {},
|
||||
Act=function(self)
|
||||
_G.__CAction__=self
|
||||
if self:test(self)==true then
|
||||
self:Pause()
|
||||
self:Stop()
|
||||
for i=1,#self.task do
|
||||
self.task[i](self)
|
||||
end
|
||||
end
|
||||
end,
|
||||
Reset=function(self) self:Resume() end,
|
||||
Stop=EventRef.Stop,
|
||||
Pause=EventRef.Pause,
|
||||
Resume=EventRef.Resume,
|
||||
OnEvent=function(self,func)
|
||||
table.insert(self.task,func)
|
||||
end,
|
||||
FreeConnections=function(self)
|
||||
self.task={}
|
||||
end,
|
||||
Remove=function(self)
|
||||
if self~=_G.__CAction__ then
|
||||
for i=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[i])==tostring(self) then
|
||||
table.remove(event.Mainloop,i)
|
||||
end
|
||||
end
|
||||
else
|
||||
table.insert(event.garbage,self)
|
||||
end
|
||||
end,
|
||||
}
|
||||
table.insert(event.Mainloop,temp)
|
||||
event.last=temp
|
||||
return temp
|
||||
end,
|
||||
newAlarm=function(set,func,start)
|
||||
if not(start) then
|
||||
timer=0
|
||||
active=false
|
||||
else
|
||||
timer=os.clock()
|
||||
active=true
|
||||
end
|
||||
if not(func) then
|
||||
func=(function() end)
|
||||
end
|
||||
Alarm=
|
||||
{
|
||||
Priority=event.Priority_Normal,
|
||||
_Priority=0,
|
||||
Parent=event.Alarms,
|
||||
Type="Alarm",
|
||||
active=active,
|
||||
timer=timer,
|
||||
set=set or 0,
|
||||
func={func},
|
||||
Act=function(self)
|
||||
_G.__CAction__=self
|
||||
if self.active==true then
|
||||
if os.clock()-self.timer>=self.set then
|
||||
self:Pause()
|
||||
self:Stop()
|
||||
self:Ring()
|
||||
end
|
||||
end
|
||||
end,
|
||||
FreeConnections=function(self)
|
||||
self.func={}
|
||||
end,
|
||||
Stop=EventRef.Stop,
|
||||
Pause=EventRef.Pause,
|
||||
Set=function(self,amt)
|
||||
self.set=amt
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end,
|
||||
OnRing=function(self,func)
|
||||
table.insert(self.func,func)
|
||||
end,
|
||||
Ring=function(self)
|
||||
for i=1,#self.func do
|
||||
self.func[i](self)
|
||||
end
|
||||
end,
|
||||
Reset=function(self)
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end,
|
||||
Resume=EventRef.Resume,
|
||||
Remove=function(self)
|
||||
if self~=_G.__CAction__ then
|
||||
for i=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[i])==tostring(self) then
|
||||
table.remove(event.Mainloop,i)
|
||||
end
|
||||
end
|
||||
else
|
||||
table.insert(event.garbage,self)
|
||||
end
|
||||
end,
|
||||
}
|
||||
table.insert(event.Mainloop,Alarm)
|
||||
event.last=temp
|
||||
return Alarm
|
||||
end,
|
||||
newTask=function(func)
|
||||
table.insert(event.DoTasks,func)
|
||||
end,
|
||||
createLoop=function()
|
||||
temp=
|
||||
{
|
||||
Priority=event.Priority_Normal,
|
||||
_Priority=0,
|
||||
Parent=event.Loops,
|
||||
Type="Loop",
|
||||
active=true,
|
||||
func={},
|
||||
OnLoop=function(self,func)
|
||||
table.insert(self.func,func)
|
||||
end,
|
||||
Stop=EventRef.Stop,
|
||||
Pause=EventRef.Pause,
|
||||
Resume=EventRef.Resume,
|
||||
Act=function(self)
|
||||
_G.__CAction__=self
|
||||
for i=1,#self.func do
|
||||
self.func[i](os.clock()-event.Start,self)
|
||||
end
|
||||
end,
|
||||
Remove=function(self)
|
||||
if self~=_G.__CAction__ then
|
||||
for i=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[i])==tostring(self) then
|
||||
table.remove(event.Mainloop,i)
|
||||
end
|
||||
end
|
||||
else
|
||||
table.insert(event.garbage,self)
|
||||
end
|
||||
end,
|
||||
FreeConnections=function(self)
|
||||
self.func={}
|
||||
end,
|
||||
}
|
||||
table.insert(event.Mainloop,temp)
|
||||
event.last=temp
|
||||
return temp
|
||||
end,
|
||||
createStep=function(start,reset,count,skip)
|
||||
think=1
|
||||
if start~=nil and reset~=nil then
|
||||
if start>reset then
|
||||
think=-1
|
||||
end
|
||||
end
|
||||
if not(endc) then
|
||||
endc=false
|
||||
end
|
||||
temp=
|
||||
{
|
||||
Priority=event.Priority_Normal,
|
||||
_Priority=0,
|
||||
start=start or 1,
|
||||
Parent=event.Steps,
|
||||
Type="Step",
|
||||
pos=start or 1,
|
||||
endAt=reset or math.huge,
|
||||
active=true,
|
||||
skip=skip or 0,
|
||||
spos=0,
|
||||
count=count or 1*think,
|
||||
funcs={},
|
||||
steps={},
|
||||
Act=StepRef.Step,
|
||||
FAct=StepRef.FStep,
|
||||
Remove=StepRef.Remove,
|
||||
Reset=StepRef.Reset,
|
||||
Set=StepRef.Set,
|
||||
Pause=StepRef.Pause,
|
||||
Resume=StepRef.Resume,
|
||||
Stop=StepRef.Stop,
|
||||
End=StepRef.End,
|
||||
Update=StepRef.Update,
|
||||
OnEnd=StepRef.OnEnd,
|
||||
OnStep=StepRef.OnStep,
|
||||
FreeConnections=StepRef.FreeConnections
|
||||
}
|
||||
table.insert(event.Mainloop,temp)
|
||||
event.last=temp
|
||||
return temp
|
||||
end,
|
||||
createTStep=function(start,reset,timer,count)
|
||||
think=1
|
||||
if start~=nil and reset~=nil then
|
||||
if start>reset then
|
||||
think=-1
|
||||
end
|
||||
end
|
||||
if not(endc) then
|
||||
endc=false
|
||||
end
|
||||
timer=timer or 1
|
||||
local _alarm=event.newAlarm(timer,function(alarm) alarm.Link:Act() alarm:Reset() end,true)
|
||||
temp=
|
||||
{
|
||||
Priority=event.Priority_Normal,
|
||||
_Priority=0,
|
||||
start=start or 1,
|
||||
Parent=event.TSteps,
|
||||
Type="TStep",
|
||||
pos=start or 1,
|
||||
endAt=reset or math.huge,
|
||||
active=true,
|
||||
skip= 0,
|
||||
spos=0,
|
||||
count=count or 1*think,
|
||||
funcs={},
|
||||
steps={},
|
||||
alarm=_alarm,
|
||||
Act=StepRef.Step,
|
||||
FAct=StepRef.FStep,
|
||||
Remove=function(self)
|
||||
if self~=_G.__CAction__ then
|
||||
for as=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[as])==tostring(self) then
|
||||
table.remove(event.Mainloop,as)
|
||||
end
|
||||
end
|
||||
self.alarm:Remove()
|
||||
else
|
||||
table.insert(event.garbage,self)
|
||||
end
|
||||
end,
|
||||
Reset=StepRef.Reset,
|
||||
Set=StepRef.Set,
|
||||
Pause=StepRef.Pause,
|
||||
Resume=StepRef.Resume,
|
||||
Stop=StepRef.Stop,
|
||||
End=StepRef.End,
|
||||
Update=function(self,start,reset,timer,count)
|
||||
if start~=nil and reset~=nil then
|
||||
if start>reset then
|
||||
if not(count<0) then
|
||||
print("less")
|
||||
count=-count
|
||||
end
|
||||
end
|
||||
end
|
||||
self.start=start or self.start
|
||||
self.endAt=reset or self.endAt
|
||||
if timer~=nil then
|
||||
self.alarm:Set(timer)
|
||||
end
|
||||
self.count=count or self.count
|
||||
self.pos=self.start
|
||||
self:Resume()
|
||||
end,
|
||||
OnEnd=StepRef.OnEnd,
|
||||
OnStep=StepRef.OnStep,
|
||||
FreeConnections=StepRef.FreeConnections
|
||||
}
|
||||
_alarm.Link=temp
|
||||
event.last=temp
|
||||
return temp
|
||||
end,
|
||||
createTrigger=function(func)
|
||||
temp={
|
||||
active=true,
|
||||
trigfunc=func,
|
||||
Remove=function(self)
|
||||
for i=1,#event.Triggers do
|
||||
if event.Triggers[i]==self then
|
||||
table.remove(event.Triggers,i)
|
||||
end
|
||||
end
|
||||
end,
|
||||
Pause=function(self) self.active=false end,
|
||||
Resume=function(self) self.active=true end,
|
||||
Fire=function(self,...)
|
||||
if self.active==true then
|
||||
local tempA=__CAction__
|
||||
__CAction__=self
|
||||
self:trigfunc(...)
|
||||
__CAction__=tempA
|
||||
end
|
||||
end,
|
||||
}
|
||||
table.insert(event.Triggers,temp)
|
||||
return temp
|
||||
end,
|
||||
stop=function()
|
||||
event.Active=false
|
||||
end,
|
||||
onStart=function() end,
|
||||
onUpdate=function(func)
|
||||
local temp=event.createLoop()
|
||||
temp:OnLoop(func)
|
||||
temp.Priority=1
|
||||
end,
|
||||
onDraw=function(func)
|
||||
table.insert(event.drawF,func)
|
||||
end,
|
||||
onClose=function() end,
|
||||
manager=function()
|
||||
if not(love) then
|
||||
event.onStart()
|
||||
event.Start=os.clock()
|
||||
while event.Active==true do
|
||||
event.DO_Order()
|
||||
end
|
||||
event.onClose()
|
||||
return os.clock()-event.Start
|
||||
else
|
||||
return false
|
||||
end
|
||||
end,
|
||||
cManager=function()
|
||||
if event.Active==true then
|
||||
event.DO_Order()
|
||||
end
|
||||
end,
|
||||
uManager=function(dt)
|
||||
if event.CT==false then
|
||||
if dt then
|
||||
event.pump=true
|
||||
end
|
||||
event.CT=true
|
||||
event.onStart()
|
||||
event.Start=os.clock()
|
||||
end
|
||||
event.pumpvar=dt
|
||||
if event.Active==true then
|
||||
event.DO_Order()
|
||||
end
|
||||
end,
|
||||
dManager=function()
|
||||
for ii=1,#event.drawF do
|
||||
event.drawF[ii]()
|
||||
end
|
||||
end,
|
||||
lManager=function()
|
||||
if love.event then
|
||||
love.event.pump()
|
||||
for e,a,b,c,d in love.event.poll() do
|
||||
if e == "quit" then
|
||||
if not love.quit or not love.quit() then
|
||||
if love.audio then
|
||||
love.audio.stop()
|
||||
end
|
||||
return
|
||||
end
|
||||
end
|
||||
love.handlers[e](a,b,c,d)
|
||||
end
|
||||
end
|
||||
if love.timer then
|
||||
love.timer.step()
|
||||
dt = love.timer.getDelta()
|
||||
end
|
||||
if love.update then love.update(dt) end
|
||||
event.uManager(dt)
|
||||
if love.window and love.graphics and love.window.isCreated() then
|
||||
love.graphics.clear()
|
||||
love.graphics.origin()
|
||||
if love.draw then love.draw() end
|
||||
event.dManager()
|
||||
love.graphics.present()
|
||||
end
|
||||
end,
|
||||
benchMark=function(sec,p)
|
||||
p=p or event.Priority_Normal
|
||||
local temp=event.createStep(10)
|
||||
temp.CC=0
|
||||
temp:OnStep(function(pos,step) step.CC=step.CC+1 end)
|
||||
local Loud=event.newAlarm(sec,nil,true)
|
||||
Loud.Link=temp
|
||||
Loud:OnRing(function(alarm) print((alarm.Link.CC).." steps in "..alarm.set.." second(s)") end)
|
||||
temp.Priority=p
|
||||
Loud.Priority=p
|
||||
return Loud
|
||||
end,
|
||||
}
|
||||
728
oldversions/EventManager(5.7.3).lua
Normal file
728
oldversions/EventManager(5.7.3).lua
Normal file
@ -0,0 +1,728 @@
|
||||
function readonlytable(table)
|
||||
return setmetatable({}, {
|
||||
__index = table,
|
||||
__newindex = function(table, key, value)
|
||||
error("Attempt to modify read-only table")
|
||||
end,
|
||||
__metatable = false
|
||||
});
|
||||
end
|
||||
local EventRef=
|
||||
readonlytable{
|
||||
Pause=function(self)
|
||||
self.active=false
|
||||
if not(event.isPaused(self)) then
|
||||
table.insert(event.Paused,self)
|
||||
for _j=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[_j])==tostring(self) then
|
||||
table.remove(event.Mainloop,_j)
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
Resume=function(self)
|
||||
self.active=true
|
||||
if event.isPaused(self) then
|
||||
table.insert(event.Mainloop,self)
|
||||
for _j=1,#event.Paused do
|
||||
if tostring(event.Paused[_j])==tostring(self) then
|
||||
table.remove(event.Paused,_j)
|
||||
end
|
||||
end
|
||||
end
|
||||
end,
|
||||
Stop=function(self)
|
||||
self.active=nil
|
||||
end,
|
||||
}
|
||||
|
||||
local StepRef=
|
||||
readonlytable{
|
||||
Step=function(self)
|
||||
if self~=nil then
|
||||
if self.spos==0 then
|
||||
_G.__CAction__=self
|
||||
if self.active==true then
|
||||
for i=1,#self.steps do
|
||||
self.steps[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
end
|
||||
end
|
||||
if self.endAt+self.count<=self.pos and self.endAt>self.start then
|
||||
self:Reset()
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
elseif self.pos<=0 then
|
||||
self:Reset()
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
self.spos=self.spos+1
|
||||
if self.spos>=self.skip then
|
||||
self.spos=0
|
||||
end
|
||||
end,
|
||||
FStep=function(self)
|
||||
if self~=nil then
|
||||
if self.spos==0 then
|
||||
_G.__CAction__=self
|
||||
for i=1,#self.steps do
|
||||
self.steps[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
end
|
||||
if self.endAt+self.count<=self.pos and self.endAt>self.start then
|
||||
self:Reset()
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
elseif self.pos==0 then
|
||||
self:Reset()
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
self.spos=self.spos+1
|
||||
if self.spos>=self.skip then
|
||||
self.spos=0
|
||||
end
|
||||
end,
|
||||
Remove=function(self)
|
||||
if self~=_G.__CAction__ then
|
||||
for as=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[as])==tostring(self) then
|
||||
table.remove(event.Mainloop,as)
|
||||
end
|
||||
end
|
||||
else
|
||||
table.insert(event.garbage,self)
|
||||
end
|
||||
end,
|
||||
Reset=function(self)
|
||||
self.pos=self.start
|
||||
end,
|
||||
Set=function(self,amt)
|
||||
self.pos=amt
|
||||
end,
|
||||
Pause=EventRef.Pause,
|
||||
Resume=EventRef.Resume,
|
||||
Stop=function(self)
|
||||
self:Reset()
|
||||
self.active=nil
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
end,
|
||||
End=function(self)
|
||||
for i=1,#self.funcs do
|
||||
self.funcs[i](self)
|
||||
end
|
||||
self:Reset()
|
||||
end,
|
||||
Update=function(self,start,reset,count,skip)
|
||||
self.start=start or self.start
|
||||
self.endAt=reset or self.endAt
|
||||
self.skip=skip or self.skip
|
||||
self.count=count or self.count
|
||||
self:Resume()
|
||||
end,
|
||||
OnEnd=function(self,func)
|
||||
table.insert(self.funcs,func)
|
||||
end,
|
||||
OnStep=function(self,func)
|
||||
table.insert(self.steps,func)
|
||||
end,
|
||||
FreeConnections=function(self)
|
||||
self.funcs={}
|
||||
self.steps={}
|
||||
end,
|
||||
}
|
||||
--thread and run setup
|
||||
if love then
|
||||
function love.run()
|
||||
if love.math then
|
||||
love.math.setRandomSeed(os.time())
|
||||
end
|
||||
if love.event then
|
||||
love.event.pump()
|
||||
end
|
||||
if love.load then love.load(arg) end
|
||||
if love.timer then love.timer.step() end
|
||||
local dt = 0
|
||||
while true do
|
||||
-- Process events.
|
||||
if love.event then
|
||||
love.event.pump()
|
||||
for e,a,b,c,d in love.event.poll() do
|
||||
if e == "quit" then
|
||||
if not love.quit or not love.quit() then
|
||||
if love.audio then
|
||||
love.audio.stop()
|
||||
end
|
||||
return
|
||||
end
|
||||
end
|
||||
love.handlers[e](a,b,c,d)
|
||||
end
|
||||
end
|
||||
if love.timer then
|
||||
love.timer.step()
|
||||
dt = love.timer.getDelta()
|
||||
end
|
||||
if love.update then love.update(dt) end
|
||||
event.uManager(dt)
|
||||
if love.window and love.graphics and love.window.isCreated() then
|
||||
love.graphics.clear()
|
||||
love.graphics.origin()
|
||||
if love.draw then love.draw() end
|
||||
event.dManager()
|
||||
love.graphics.setColor(255,255,255,255)
|
||||
if event.draw then event.draw() end
|
||||
love.graphics.present()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
eThreads={
|
||||
send=function() end,
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
function RunTasks()
|
||||
for i=1,#event.DoTasks do
|
||||
event.DoTasks[i]()
|
||||
end
|
||||
end
|
||||
event={
|
||||
VERSION="1.0.0 (Build Version: 5.7.3)",
|
||||
Priority_Core=1,
|
||||
Priority_High=2,
|
||||
Priority_Above_Normal=4,
|
||||
Priority_Normal=16,
|
||||
Priority_Low=256,
|
||||
Priority_Idle=65536,
|
||||
Start=0,
|
||||
Active=true,
|
||||
CT=false,
|
||||
Tasks={},
|
||||
DoTasks={},
|
||||
garbage={},
|
||||
Paused={},
|
||||
last={},
|
||||
func={},
|
||||
drawF={},
|
||||
pump=false,
|
||||
pumpvar=0,
|
||||
Mainloop={},
|
||||
PEnabled=true,
|
||||
PCount=1,
|
||||
Triggers={},
|
||||
oneTimeObj=
|
||||
{
|
||||
last={},
|
||||
Resume=function(self) end,
|
||||
Pause=function(self) end,
|
||||
},
|
||||
RemoveAll=function()
|
||||
event.Mainloop={}
|
||||
end,
|
||||
GarbageObj=
|
||||
{
|
||||
Resume=function() end,
|
||||
Pause=function() end
|
||||
},
|
||||
isPaused=function(obj)
|
||||
for _j=1,#event.Paused do
|
||||
if tostring(event.Paused[_j])==tostring(obj) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
return false
|
||||
end,
|
||||
DO_Order=function()
|
||||
event.oneTime(RunTasks)
|
||||
event.PCount=event.PCount+1
|
||||
for i=1,#event.Mainloop do
|
||||
if event.Mainloop[i]~=nil then
|
||||
local obj = event.Mainloop[i]
|
||||
if event.PCount%obj.Priority==0 and event.PEnabled then
|
||||
obj:Act()
|
||||
elseif event.PEnabled==false then
|
||||
obj:Act()
|
||||
end
|
||||
if event.PCount>event.Priority_Idle then
|
||||
event.PCount=event.Priority_Core
|
||||
end
|
||||
end
|
||||
end
|
||||
event.MANAGE_GARBAGE()
|
||||
end,
|
||||
MANAGE_GARBAGE=function()
|
||||
_G.__CAction__=event.GarbageObj
|
||||
for _i=1,#event.garbage do
|
||||
event.garbage[_i]:Remove()
|
||||
table.remove(event.garbage,_i)
|
||||
end
|
||||
end,
|
||||
oneTime=function(func)
|
||||
event.oneTimeObj.last=_G.__CAction__
|
||||
_G.__CAction__=event.oneTimeObj
|
||||
for _k=1,#event.Tasks do
|
||||
if event.Tasks[_k]==func then
|
||||
_G.__CAction__=event.oneTimeObj.last
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(event.Tasks,func)
|
||||
func()
|
||||
_G.__CAction__=event.oneTimeObj.last
|
||||
return true
|
||||
end,
|
||||
oneETime=function(func)
|
||||
for _k=1,#event.Tasks do
|
||||
if event.Tasks[_k]==string.dump(func) then
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(event.Tasks,string.dump(func))
|
||||
func()
|
||||
return true
|
||||
end,
|
||||
hold=function(task) -- as many conditions and times that you want can be used
|
||||
local action=__CAction__
|
||||
action:Pause()
|
||||
if type(task)=="number" then
|
||||
local alarm=event.newAlarm(task,function() end,true)
|
||||
while alarm.active==true do
|
||||
if love then
|
||||
event.lManager()
|
||||
else
|
||||
event.cManager()
|
||||
end
|
||||
end
|
||||
alarm:Remove()
|
||||
action:Resume()
|
||||
elseif type(task)=="function" then
|
||||
local env=event.newEvent(task,function(envt) envt:Pause() envt:Stop() end)
|
||||
while env.active do
|
||||
if love then
|
||||
event.lManager()
|
||||
else
|
||||
event.cManager()
|
||||
end
|
||||
end
|
||||
env:Remove()
|
||||
action:Resume()
|
||||
else
|
||||
print("Error Data Type!!!")
|
||||
end
|
||||
end,
|
||||
waitFor=function(obj)
|
||||
local obj=obj
|
||||
event.hold(function() return not(obj.active) end)
|
||||
end,
|
||||
getType=function(obj)
|
||||
if obj.Type~=nil then
|
||||
return obj.Type
|
||||
end
|
||||
end,
|
||||
newEvent=function(test,task)
|
||||
temp=
|
||||
{
|
||||
Priority=event.Priority_Normal,
|
||||
_Priority=0,
|
||||
Parent=event.Events,
|
||||
Type="Event",
|
||||
active=true,
|
||||
test=test or (function() end),
|
||||
task={task} or {},
|
||||
Act=function(self)
|
||||
_G.__CAction__=self
|
||||
if self:test(self)==true then
|
||||
self:Pause()
|
||||
self:Stop()
|
||||
for i=1,#self.task do
|
||||
self.task[i](self)
|
||||
end
|
||||
end
|
||||
end,
|
||||
Reset=function(self) self:Resume() end,
|
||||
Stop=EventRef.Stop,
|
||||
Pause=EventRef.Pause,
|
||||
Resume=EventRef.Resume,
|
||||
OnEvent=function(self,func)
|
||||
table.insert(self.task,func)
|
||||
end,
|
||||
FreeConnections=function(self)
|
||||
self.task={}
|
||||
end,
|
||||
Remove=function(self)
|
||||
if self~=_G.__CAction__ then
|
||||
for i=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[i])==tostring(self) then
|
||||
table.remove(event.Mainloop,i)
|
||||
end
|
||||
end
|
||||
else
|
||||
table.insert(event.garbage,self)
|
||||
end
|
||||
end,
|
||||
}
|
||||
table.insert(event.Mainloop,temp)
|
||||
event.last=temp
|
||||
return temp
|
||||
end,
|
||||
newAlarm=function(set,func,start)
|
||||
if not(start) then
|
||||
timer=0
|
||||
active=false
|
||||
else
|
||||
timer=os.clock()
|
||||
active=true
|
||||
end
|
||||
if not(func) then
|
||||
func=(function() end)
|
||||
end
|
||||
Alarm=
|
||||
{
|
||||
Priority=event.Priority_Normal,
|
||||
_Priority=0,
|
||||
Parent=event.Alarms,
|
||||
Type="Alarm",
|
||||
active=active,
|
||||
timer=timer,
|
||||
set=set or 0,
|
||||
func={func},
|
||||
Act=function(self)
|
||||
_G.__CAction__=self
|
||||
if self.active==true then
|
||||
if os.clock()-self.timer>=self.set then
|
||||
self:Pause()
|
||||
self:Stop()
|
||||
self:Ring()
|
||||
end
|
||||
end
|
||||
end,
|
||||
FreeConnections=function(self)
|
||||
self.func={}
|
||||
end,
|
||||
Stop=EventRef.Stop,
|
||||
Pause=EventRef.Pause,
|
||||
Set=function(self,amt)
|
||||
self.set=amt
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end,
|
||||
OnRing=function(self,func)
|
||||
table.insert(self.func,func)
|
||||
end,
|
||||
Ring=function(self)
|
||||
for i=1,#self.func do
|
||||
self.func[i](self)
|
||||
end
|
||||
end,
|
||||
Reset=function(self)
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end,
|
||||
Resume=EventRef.Resume,
|
||||
Remove=function(self)
|
||||
if self~=_G.__CAction__ then
|
||||
for i=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[i])==tostring(self) then
|
||||
table.remove(event.Mainloop,i)
|
||||
end
|
||||
end
|
||||
else
|
||||
table.insert(event.garbage,self)
|
||||
end
|
||||
end,
|
||||
}
|
||||
table.insert(event.Mainloop,Alarm)
|
||||
event.last=temp
|
||||
return Alarm
|
||||
end,
|
||||
newTask=function(func)
|
||||
table.insert(event.DoTasks,func)
|
||||
end,
|
||||
createLoop=function()
|
||||
temp=
|
||||
{
|
||||
Priority=event.Priority_Normal,
|
||||
_Priority=0,
|
||||
Parent=event.Loops,
|
||||
Type="Loop",
|
||||
active=true,
|
||||
func={},
|
||||
OnLoop=function(self,func)
|
||||
table.insert(self.func,func)
|
||||
end,
|
||||
Stop=EventRef.Stop,
|
||||
Pause=EventRef.Pause,
|
||||
Resume=EventRef.Resume,
|
||||
Act=function(self)
|
||||
_G.__CAction__=self
|
||||
for i=1,#self.func do
|
||||
self.func[i](os.clock()-event.Start,self)
|
||||
end
|
||||
end,
|
||||
Remove=function(self)
|
||||
if self~=_G.__CAction__ then
|
||||
for i=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[i])==tostring(self) then
|
||||
table.remove(event.Mainloop,i)
|
||||
end
|
||||
end
|
||||
else
|
||||
table.insert(event.garbage,self)
|
||||
end
|
||||
end,
|
||||
FreeConnections=function(self)
|
||||
self.func={}
|
||||
end,
|
||||
}
|
||||
table.insert(event.Mainloop,temp)
|
||||
event.last=temp
|
||||
return temp
|
||||
end,
|
||||
createStep=function(start,reset,count,skip)
|
||||
think=1
|
||||
if start~=nil and reset~=nil then
|
||||
if start>reset then
|
||||
think=-1
|
||||
end
|
||||
end
|
||||
if not(endc) then
|
||||
endc=false
|
||||
end
|
||||
temp=
|
||||
{
|
||||
Priority=event.Priority_Normal,
|
||||
_Priority=0,
|
||||
start=start or 1,
|
||||
Parent=event.Steps,
|
||||
Type="Step",
|
||||
pos=start or 1,
|
||||
endAt=reset or math.huge,
|
||||
active=true,
|
||||
skip=skip or 0,
|
||||
spos=0,
|
||||
count=count or 1*think,
|
||||
funcs={},
|
||||
steps={},
|
||||
Act=StepRef.Step,
|
||||
FAct=StepRef.FStep,
|
||||
Remove=StepRef.Remove,
|
||||
Reset=StepRef.Reset,
|
||||
Set=StepRef.Set,
|
||||
Pause=StepRef.Pause,
|
||||
Resume=StepRef.Resume,
|
||||
Stop=StepRef.Stop,
|
||||
End=StepRef.End,
|
||||
Update=StepRef.Update,
|
||||
OnEnd=StepRef.OnEnd,
|
||||
OnStep=StepRef.OnStep,
|
||||
FreeConnections=StepRef.FreeConnections
|
||||
}
|
||||
table.insert(event.Mainloop,temp)
|
||||
event.last=temp
|
||||
return temp
|
||||
end,
|
||||
createTStep=function(start,reset,timer,count)
|
||||
think=1
|
||||
if start~=nil and reset~=nil then
|
||||
if start>reset then
|
||||
think=-1
|
||||
end
|
||||
end
|
||||
if not(endc) then
|
||||
endc=false
|
||||
end
|
||||
timer=timer or 1
|
||||
local _alarm=event.newAlarm(timer,function(alarm) alarm.Link:Act() alarm:Reset() end,true)
|
||||
temp=
|
||||
{
|
||||
Priority=event.Priority_Normal,
|
||||
_Priority=0,
|
||||
start=start or 1,
|
||||
Parent=event.TSteps,
|
||||
Type="TStep",
|
||||
pos=start or 1,
|
||||
endAt=reset or math.huge,
|
||||
active=true,
|
||||
skip= 0,
|
||||
spos=0,
|
||||
count=count or 1*think,
|
||||
funcs={},
|
||||
steps={},
|
||||
alarm=_alarm,
|
||||
Act=StepRef.Step,
|
||||
FAct=StepRef.FStep,
|
||||
Remove=function(self)
|
||||
if self~=_G.__CAction__ then
|
||||
for as=1,#event.Mainloop do
|
||||
if tostring(event.Mainloop[as])==tostring(self) then
|
||||
table.remove(event.Mainloop,as)
|
||||
end
|
||||
end
|
||||
self.alarm:Remove()
|
||||
else
|
||||
table.insert(event.garbage,self)
|
||||
end
|
||||
end,
|
||||
Reset=StepRef.Reset,
|
||||
Set=StepRef.Set,
|
||||
Pause=StepRef.Pause,
|
||||
Resume=StepRef.Resume,
|
||||
Stop=StepRef.Stop,
|
||||
End=StepRef.End,
|
||||
Update=function(self,start,reset,timer,count)
|
||||
if start~=nil and reset~=nil then
|
||||
if start>reset then
|
||||
if not(count<0) then
|
||||
print("less")
|
||||
count=-count
|
||||
end
|
||||
end
|
||||
end
|
||||
self.start=start or self.start
|
||||
self.endAt=reset or self.endAt
|
||||
if timer~=nil then
|
||||
self.alarm:Set(timer)
|
||||
end
|
||||
self.count=count or self.count
|
||||
self.pos=self.start
|
||||
self:Resume()
|
||||
end,
|
||||
OnEnd=StepRef.OnEnd,
|
||||
OnStep=StepRef.OnStep,
|
||||
FreeConnections=StepRef.FreeConnections
|
||||
}
|
||||
_alarm.Link=temp
|
||||
event.last=temp
|
||||
return temp
|
||||
end,
|
||||
createTrigger=function(func)
|
||||
temp={
|
||||
active=true,
|
||||
trigfunc=func,
|
||||
Remove=function(self)
|
||||
for i=1,#event.Triggers do
|
||||
if event.Triggers[i]==self then
|
||||
table.remove(event.Triggers,i)
|
||||
end
|
||||
end
|
||||
end,
|
||||
Pause=function(self) self.active=false end,
|
||||
Resume=function(self) self.active=true end,
|
||||
Fire=function(self,...)
|
||||
if self.active==true then
|
||||
local tempA=__CAction__
|
||||
__CAction__=self
|
||||
self:trigfunc(...)
|
||||
__CAction__=tempA
|
||||
end
|
||||
end,
|
||||
}
|
||||
table.insert(event.Triggers,temp)
|
||||
return temp
|
||||
end,
|
||||
stop=function()
|
||||
event.Active=false
|
||||
end,
|
||||
onStart=function() end,
|
||||
onUpdate=function(func)
|
||||
local temp=event.createLoop()
|
||||
temp:OnLoop(func)
|
||||
temp.Priority=1
|
||||
end,
|
||||
onDraw=function(func)
|
||||
table.insert(event.drawF,func)
|
||||
end,
|
||||
onClose=function() end,
|
||||
manager=function()
|
||||
if not(love) then
|
||||
event.onStart()
|
||||
event.Start=os.clock()
|
||||
while event.Active==true do
|
||||
event.DO_Order()
|
||||
end
|
||||
event.onClose()
|
||||
return os.clock()-event.Start
|
||||
else
|
||||
return false
|
||||
end
|
||||
end,
|
||||
cManager=function()
|
||||
if event.Active==true then
|
||||
event.DO_Order()
|
||||
end
|
||||
end,
|
||||
uManager=function(dt)
|
||||
if event.CT==false then
|
||||
if dt then
|
||||
event.pump=true
|
||||
end
|
||||
event.CT=true
|
||||
event.onStart()
|
||||
event.Start=os.clock()
|
||||
end
|
||||
event.pumpvar=dt
|
||||
if event.Active==true then
|
||||
event.DO_Order()
|
||||
end
|
||||
end,
|
||||
dManager=function()
|
||||
for ii=1,#event.drawF do
|
||||
event.drawF[ii]()
|
||||
end
|
||||
end,
|
||||
lManager=function()
|
||||
if love.event then
|
||||
love.event.pump()
|
||||
for e,a,b,c,d in love.event.poll() do
|
||||
if e == "quit" then
|
||||
if not love.quit or not love.quit() then
|
||||
if love.audio then
|
||||
love.audio.stop()
|
||||
end
|
||||
return
|
||||
end
|
||||
end
|
||||
love.handlers[e](a,b,c,d)
|
||||
end
|
||||
end
|
||||
if love.timer then
|
||||
love.timer.step()
|
||||
dt = love.timer.getDelta()
|
||||
end
|
||||
if love.update then love.update(dt) end
|
||||
event.uManager(dt)
|
||||
if love.window and love.graphics and love.window.isCreated() then
|
||||
love.graphics.clear()
|
||||
love.graphics.origin()
|
||||
if love.draw then love.draw() end
|
||||
event.dManager()
|
||||
love.graphics.present()
|
||||
end
|
||||
end,
|
||||
benchMark=function(sec,p)
|
||||
p=p or event.Priority_Normal
|
||||
local temp=event.createStep(10)
|
||||
temp.CC=0
|
||||
temp:OnStep(function(pos,step) step.CC=step.CC+1 end)
|
||||
local Loud=event.newAlarm(sec,nil,true)
|
||||
Loud.Link=temp
|
||||
Loud:OnRing(function(alarm) print((alarm.Link.CC).." steps in "..alarm.set.." second(s)") end)
|
||||
temp.Priority=p
|
||||
Loud.Priority=p
|
||||
return Loud
|
||||
end,
|
||||
}
|
||||
365
oldversions/MultiManager(0.3.0).lua
Normal file
365
oldversions/MultiManager(0.3.0).lua
Normal file
@ -0,0 +1,365 @@
|
||||
multi = {}
|
||||
multi.__index = multi
|
||||
multi.Mainloop={}
|
||||
multi.Tasks={}
|
||||
multi.Tasks2={}
|
||||
multi.Garbage={}
|
||||
multi.Children={}
|
||||
multi.Paused={}
|
||||
multi.MasterId=0
|
||||
multi.Active=true
|
||||
multi.Id=-1
|
||||
-- System
|
||||
function multi:newBase(ins)
|
||||
local c = {}
|
||||
setmetatable(c, multi)
|
||||
c.Parent=self
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.Id=0
|
||||
c.Act=function() end
|
||||
if ins then
|
||||
table.insert(multi.Mainloop,ins,c)
|
||||
else
|
||||
table.insert(multi.Mainloop,c)
|
||||
end
|
||||
multi.MasterId=multi.MasterId+1
|
||||
return c
|
||||
end
|
||||
function multi:reboot(r)
|
||||
multi.Mainloop={}
|
||||
multi.Tasks={}
|
||||
multi.Tasks2={}
|
||||
multi.Garbage={}
|
||||
multi.Children={}
|
||||
multi.Paused={}
|
||||
multi.MasterId=0
|
||||
multi.Active=true
|
||||
multi.Id=-1
|
||||
if r then
|
||||
for i,v in pairs(_G) do
|
||||
if type(i)=="table" then
|
||||
if i.Parent and i.Id and i.Act then
|
||||
i={}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
--Processor
|
||||
function multi.Do_Order()
|
||||
for _D=#multi.Mainloop,1,-1 do
|
||||
if multi.Mainloop[_D]~=nil then
|
||||
multi.Mainloop[_D].Id=_D
|
||||
multi.Mainloop[_D]:Act()
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:benchMark(sec)
|
||||
local temp=multi:newStep(2)
|
||||
temp.CC=0
|
||||
temp:OnStep(function(pos,step) step.CC=step.CC+1 end)
|
||||
local Loud=multi:newAlarm(sec)
|
||||
Loud.Link=temp
|
||||
Loud:OnRing(function(alarm) alarm.Link.CC=alarm.Link.CC print((alarm.Link.CC).." steps in "..alarm.set.." second(s)") alarm.bench=alarm.Link.CC alarm.Link:Destroy() alarm:Destroy() end)
|
||||
return Loud
|
||||
end
|
||||
--Helpers
|
||||
function multi:FreeMainEvent()
|
||||
self.func={}
|
||||
end
|
||||
function multi:isPaused()
|
||||
return not(self.Active)
|
||||
end
|
||||
function multi:Pause(n)
|
||||
if not(n) then
|
||||
self.Active=false
|
||||
table.remove(multi.Mainloop,self.Id)
|
||||
table.insert(multi.Paused,self)
|
||||
else
|
||||
self:hold(n)
|
||||
end
|
||||
end
|
||||
function multi:Resume()
|
||||
if self:isPaused() then
|
||||
self.Active=true
|
||||
table.remove(multi.Paused,self.Id)
|
||||
table.insert(multi.Mainloop,self)
|
||||
end
|
||||
end
|
||||
function multi:Remove()
|
||||
self:Pause()
|
||||
self:Destroy()
|
||||
end
|
||||
function multi:Destroy()
|
||||
self:Pause()
|
||||
if self:isPaused() then
|
||||
for i=1,#multi.Paused do
|
||||
if multi.Paused[i]==self then
|
||||
table.remove(multi.Paused,i)
|
||||
return
|
||||
end
|
||||
end
|
||||
else
|
||||
table.remove(multi.Mainloop,self.Id)
|
||||
end
|
||||
self.Act=function() end
|
||||
end
|
||||
function multi:hold(task)
|
||||
self:Pause()
|
||||
if type(task)=="number" then
|
||||
local alarm=multi:newAlarm(task)
|
||||
while alarm.Active==true do
|
||||
if love then
|
||||
multi.lManager()
|
||||
else
|
||||
multi.Do_Order()
|
||||
end
|
||||
end
|
||||
alarm:Destroy()
|
||||
self:Resume()
|
||||
elseif type(task)=="function" then
|
||||
local env=multi:newEvent(task)
|
||||
env:OnEvent(function(envt) envt:Pause() envt:Stop() end)
|
||||
while env.Active do
|
||||
if love then
|
||||
multi.lManager()
|
||||
else
|
||||
multi.Do_Order()
|
||||
end
|
||||
end
|
||||
env:Destroy()
|
||||
self:Resume()
|
||||
else
|
||||
print("Error Data Type!!!")
|
||||
end
|
||||
end
|
||||
function multi:oneTime(func,...)
|
||||
for _k=1,#multi.Tasks2 do
|
||||
if multi.Tasks2[_k]==func then
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(multi.Tasks2,func)
|
||||
func(...)
|
||||
return true
|
||||
end
|
||||
--Constructors
|
||||
function multi:newEvent(task)
|
||||
local c=multi:newBase()
|
||||
c.Type="Event"
|
||||
c.Task=task or function() end
|
||||
function c:Act()
|
||||
if self.Task(self) and self.Active==true then
|
||||
self:Pause()
|
||||
for _E=1,#self.func do
|
||||
self.func[_E](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnEvent(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newAlarm(set)
|
||||
local c=multi:newBase()
|
||||
c.Type="Alarm"
|
||||
c.timer=os.clock()
|
||||
c.set=set or 0
|
||||
function c:Act()
|
||||
if self.Active==true then
|
||||
if os.clock()-self.timer>=self.set then
|
||||
self:Pause()
|
||||
for i=1,#self.func do
|
||||
self.func[i](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:Reset(n)
|
||||
if n then self.set=n end
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end
|
||||
function c:OnRing(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTask(func)
|
||||
table.insert(multi.Tasks,func)
|
||||
end
|
||||
function multi:newLoop()
|
||||
local c=multi:newBase()
|
||||
c.Type="Loop"
|
||||
function c:Act()
|
||||
if self.Active==true then
|
||||
for i=1,#self.func do
|
||||
self.func[i](os.clock()-multi.Start,self)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnLoop(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newStep(start,reset,count,skip)
|
||||
local c=multi:newBase()
|
||||
think=1
|
||||
c.Type="Step"
|
||||
c.pos=start or 1
|
||||
c.endAt=reset or math.huge
|
||||
c.skip=skip or 0
|
||||
c.spos=0
|
||||
c.count=count or 1*think
|
||||
c.funcE={}
|
||||
c.start=start or 1
|
||||
if start~=nil and reset~=nil then
|
||||
if start>reset then
|
||||
think=-1
|
||||
end
|
||||
end
|
||||
function c:Act()
|
||||
if self~=nil then
|
||||
if self.spos==0 then
|
||||
if self.Active==true then
|
||||
for i=1,#self.func do
|
||||
self.func[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
end
|
||||
end
|
||||
end
|
||||
self.spos=self.spos+1
|
||||
if self.spos>=self.skip then
|
||||
self.spos=0
|
||||
end
|
||||
end
|
||||
function c:OnStep(func)
|
||||
table.insert(self.func,1,func)
|
||||
end
|
||||
function c:OnEnd(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
function c:Update(start,reset,count,skip)
|
||||
self.start=start or self.start
|
||||
self.endAt=reset or self.endAt
|
||||
self.skip=skip or self.skip
|
||||
self.count=count or self.count
|
||||
self:Resume()
|
||||
end
|
||||
c:OnStep(function(p,s)
|
||||
if s.count>0 and s.endAt==p then
|
||||
for fe=1,#s.funcE do
|
||||
s.funcE[fe](s)
|
||||
end
|
||||
s.pos=s.start-1
|
||||
elseif s.count<0 and s.endAt==p then
|
||||
for fe=1,#s.funcE do
|
||||
s.funcE[fe](s)
|
||||
end
|
||||
s.pos=s.start-1
|
||||
end
|
||||
end)
|
||||
return c
|
||||
end
|
||||
function multi:newTStep(start,reset,count,set)
|
||||
local c=multi:newBase()
|
||||
think=1
|
||||
c.Type="TStep"
|
||||
c.start=start or 1
|
||||
local reset = reset or math.huge
|
||||
c.endAt=reset
|
||||
c.pos=start or 1
|
||||
c.skip=skip or 0
|
||||
c.count=count or 1*think
|
||||
c.funcE={}
|
||||
c.timer=os.clock()
|
||||
c.set=set or 1
|
||||
function c:Update(start,reset,count,set)
|
||||
self.start=start or self.start
|
||||
self.pos=start
|
||||
self.endAt=reset or self.endAt
|
||||
self.set=set or self.set
|
||||
self.count=count or self.count or 1
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end
|
||||
function c:Act()
|
||||
if self.Active then
|
||||
if os.clock()-self.timer>=self.set then
|
||||
self:Reset()
|
||||
for i=1,#self.func do
|
||||
self.func[i](self.pos,self)
|
||||
end
|
||||
if self.endAt==self.pos then
|
||||
for fe=1,#self.funcE do
|
||||
self.funcE[fe](self)
|
||||
end
|
||||
self.pos=self.start-1
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnEnd(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
function c:Reset(n)
|
||||
if n then self.set=n end
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end
|
||||
function c:OnStep(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
|
||||
function multi:inQueue(func)
|
||||
if self.Id==-1 then
|
||||
print("Error: Can't queue the multi object")
|
||||
return
|
||||
end
|
||||
local c=multi:newBase(self.Id)
|
||||
self.Id=self.Id-1
|
||||
c.Type="Queue"
|
||||
c.Task=func
|
||||
c.Link=self
|
||||
function c:Act()
|
||||
self.Task(self.Link)
|
||||
self:Destroy()
|
||||
end
|
||||
end
|
||||
function multi:newTrigger(func)
|
||||
local c=multi:newBase()
|
||||
c.Type="Trigger"
|
||||
c.trigfunc=func or function() end
|
||||
function c:Fire(...)
|
||||
self:trigfunc(self,...)
|
||||
end
|
||||
return c
|
||||
end
|
||||
--Managers
|
||||
function multi:mainloop()
|
||||
for i=1,#multi.Tasks do
|
||||
multi.Tasks[i]()
|
||||
end
|
||||
multi.Start=os.clock()
|
||||
while self.Active do
|
||||
multi.Do_Order()
|
||||
end
|
||||
end
|
||||
function multi._tFunc(dt)
|
||||
if dt then
|
||||
multi.pump=true
|
||||
end
|
||||
multi.pumpvar=dt
|
||||
multi.Start=os.clock()
|
||||
end
|
||||
function multi:uManager(dt)
|
||||
multi:oneTime(multi._tFunc,dt)
|
||||
multi.Do_Order()
|
||||
end
|
||||
506
oldversions/MultiManager(0.4.0-1).lua
Normal file
506
oldversions/MultiManager(0.4.0-1).lua
Normal file
@ -0,0 +1,506 @@
|
||||
multi = {}
|
||||
multi.Version="4.0.1"
|
||||
multi.__index = multi
|
||||
multi.Mainloop={}
|
||||
multi.Tasks={}
|
||||
multi.Tasks2={}
|
||||
multi.Garbage={}
|
||||
multi.Children={}
|
||||
multi.Paused={}
|
||||
multi.MasterId=0
|
||||
multi.Active=true
|
||||
multi.Id=-1
|
||||
multi.Type="mainint"
|
||||
multi.Rest=0
|
||||
multi._type=type
|
||||
--[[function type(v)
|
||||
local t={}
|
||||
if multi._type(v)=="table" then
|
||||
t=getmetatable(v)
|
||||
if v.Type~=nil then
|
||||
if multi._type(v.Type)=="string" then
|
||||
return v.Type
|
||||
end
|
||||
end
|
||||
end
|
||||
if t.__type~=nil then
|
||||
return t.__type
|
||||
else
|
||||
return multi._type(v)
|
||||
end
|
||||
end]]
|
||||
-- System
|
||||
function os.getOS()
|
||||
if package.config:sub(1,1)=="\\" then
|
||||
return "windows"
|
||||
else
|
||||
return "unix"
|
||||
end
|
||||
end
|
||||
if os.getOS()=="windows" then
|
||||
function os.sleep(n)
|
||||
if n > 0 then os.execute("ping -n " .. tonumber(n+1) .. " localhost > NUL") end
|
||||
end
|
||||
else
|
||||
function os.sleep(n)
|
||||
os.execute("sleep " .. tonumber(n))
|
||||
end
|
||||
end
|
||||
function multi:newBase(ins)
|
||||
if not(self.Type=="mainint" or self.Type=="int") then error("Can only create an object on multi or an interface obj") return false end
|
||||
local c = {}
|
||||
if self.Type=="int" then
|
||||
setmetatable(c, self.Parent)
|
||||
else
|
||||
setmetatable(c, self)
|
||||
end
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.Id=0
|
||||
c.PId=0
|
||||
c.Act=function() end
|
||||
c.Parent=self
|
||||
if ins then
|
||||
table.insert(self.Mainloop,ins,c)
|
||||
else
|
||||
table.insert(self.Mainloop,c)
|
||||
end
|
||||
self.MasterId=self.MasterId+1
|
||||
return c
|
||||
end
|
||||
function multi:reboot(r)
|
||||
self.Mainloop={}
|
||||
self.Tasks={}
|
||||
self.Tasks2={}
|
||||
self.Garbage={}
|
||||
self.Children={}
|
||||
self.Paused={}
|
||||
self.MasterId=0
|
||||
self.Active=true
|
||||
self.Id=-1
|
||||
if r then
|
||||
for i,v in pairs(_G) do
|
||||
if type(i)=="table" then
|
||||
if i.Parent and i.Id and i.Act then
|
||||
i={}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:getChildren()
|
||||
return self.Mainloop
|
||||
end
|
||||
--Processor
|
||||
function multi:Do_Order()
|
||||
for _D=#self.Mainloop,1,-1 do
|
||||
if self.Mainloop[_D]~=nil then
|
||||
self.Mainloop[_D].Id=_D
|
||||
self.Mainloop[_D]:Act()
|
||||
end
|
||||
if self.Mainloop[_D]~=nil then
|
||||
if self.Mainloop[_D].rem~=nil then
|
||||
table.remove(self.Mainloop,_D)
|
||||
end
|
||||
end
|
||||
end
|
||||
if self.Rest>0 then
|
||||
os.sleep(self.Rest)
|
||||
end
|
||||
end
|
||||
function multi:benchMark(sec)
|
||||
local temp=self:newLoop(function(t,self)
|
||||
if os.clock()-self.init>self.sec then
|
||||
print(self.c.." steps in "..self.sec.." second(s)")
|
||||
self.tt(self.sec)
|
||||
self:Destroy()
|
||||
else
|
||||
self.c=self.c+1
|
||||
end
|
||||
end)
|
||||
function temp:OnBench(func)
|
||||
self.tt=func
|
||||
end
|
||||
self.tt=function() end
|
||||
temp.sec=sec
|
||||
temp.init=os.clock()
|
||||
temp.c=0
|
||||
return temp
|
||||
end
|
||||
function multi:newInterface()
|
||||
if not(self.Type=="mainint") then error("Can only create an interface on the multi obj") return false end
|
||||
local c = {}
|
||||
setmetatable(c, self)
|
||||
c.Parent=self
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.Id=0
|
||||
c.Type="int"
|
||||
c.Mainloop={}
|
||||
c.Tasks={}
|
||||
c.Tasks2={}
|
||||
c.Garbage={}
|
||||
c.Children={}
|
||||
c.Paused={}
|
||||
c.MasterId=0
|
||||
c.Active=true
|
||||
c.Id=-1
|
||||
c.Rest=0
|
||||
function c:Start()
|
||||
if self.l then
|
||||
self.l:Resume()
|
||||
else
|
||||
self.l=self.Parent:newLoop(function(dt) c:uManager(dt) end)
|
||||
end
|
||||
end
|
||||
function c:Stop()
|
||||
if self.l then
|
||||
self.l:Pause()
|
||||
end
|
||||
end
|
||||
function c:Remove()
|
||||
self:Destroy()
|
||||
self.l:Destroy()
|
||||
end
|
||||
return c
|
||||
end
|
||||
--Helpers
|
||||
function multi:FreeMainEvent()
|
||||
self.func={}
|
||||
end
|
||||
function multi:isPaused()
|
||||
return not(self.Active)
|
||||
end
|
||||
function multi:Pause(n)
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
self.Active=false
|
||||
if not(n) then
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Pause()
|
||||
end
|
||||
else
|
||||
self:hold(n)
|
||||
end
|
||||
else
|
||||
if n==nil then
|
||||
self.Active=false
|
||||
if self.Parent.Mainloop[self.Id]~=nil then
|
||||
table.remove(self.Parent.Mainloop,self.Id)
|
||||
table.insert(self.Parent.Paused,self)
|
||||
self.PId=#self.Parent.Paused
|
||||
end
|
||||
else
|
||||
self:hold(n)
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:Resume()
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
self.Active=true
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Resume()
|
||||
end
|
||||
else
|
||||
if self:isPaused() then
|
||||
table.remove(self.Parent.Paused,self.PId)
|
||||
table.insert(self.Parent.Mainloop,self)
|
||||
self.Id=#self.Parent.Mainloop
|
||||
self.Active=true
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:Destroy()
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Destroy()
|
||||
end
|
||||
else
|
||||
self.rem=true
|
||||
self.Active=false
|
||||
end
|
||||
end
|
||||
function multi:hold(task)
|
||||
self:Pause()
|
||||
if type(task)=="number" then
|
||||
local alarm=self.Parent:newAlarm(task)
|
||||
while alarm.Active==true do
|
||||
if love then
|
||||
self.Parent:lManager()
|
||||
else
|
||||
self.Parent:Do_Order()
|
||||
end
|
||||
end
|
||||
alarm:Destroy()
|
||||
self:Resume()
|
||||
elseif type(task)=="function" then
|
||||
local env=self.Parent:newEvent(task)
|
||||
env:OnEvent(function(envt) envt:Pause() envt:Stop() end)
|
||||
while env.Active do
|
||||
if love then
|
||||
self.Parent:lManager()
|
||||
else
|
||||
self.Parent:Do_Order()
|
||||
end
|
||||
end
|
||||
env:Destroy()
|
||||
self:Resume()
|
||||
else
|
||||
print("Error Data Type!!!")
|
||||
end
|
||||
end
|
||||
function multi:oneTime(func,...)
|
||||
if not(self.Type=="mainint" or self.Type=="int") then
|
||||
for _k=1,#self.Parent.Tasks2 do
|
||||
if self.Parent.Tasks2[_k]==func then
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(self.Parent.Tasks2,func)
|
||||
func(...)
|
||||
return true
|
||||
else
|
||||
for _k=1,#self.Tasks2 do
|
||||
if self.Tasks2[_k]==func then
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(self.Tasks2,func)
|
||||
func(...)
|
||||
return true
|
||||
end
|
||||
end
|
||||
--Constructors
|
||||
function multi:newEvent(task)
|
||||
local c=self:newBase()
|
||||
c.Type="event"
|
||||
c.Task=task or function() end
|
||||
function c:Act()
|
||||
if self.Task(self) and self.Active==true then
|
||||
self:Pause()
|
||||
for _E=1,#self.func do
|
||||
self.func[_E](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnEvent(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newAlarm(set)
|
||||
local c=self:newBase()
|
||||
c.Type="alarm"
|
||||
c.timer=os.clock()
|
||||
c.set=set or 0
|
||||
function c:Act()
|
||||
if self.Active==true then
|
||||
if os.clock()-self.timer>=self.set then
|
||||
self:Pause()
|
||||
for i=1,#self.func do
|
||||
self.func[i](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:Reset(n)
|
||||
if n then self.set=n end
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
self.Active=true
|
||||
end
|
||||
function c:OnRing(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTask(func)
|
||||
table.insert(self.Tasks,func)
|
||||
end
|
||||
function multi:newLoop(func)
|
||||
local c=self:newBase()
|
||||
c.Type="loop"
|
||||
if func then
|
||||
c.func={func}
|
||||
end
|
||||
function c:Act()
|
||||
if self.Active==true then
|
||||
for i=1,#self.func do
|
||||
self.func[i](os.clock()-self.Parent.Start,self)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnLoop(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
function c:Break()
|
||||
self.Active=nil
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newStep(start,reset,count,skip)
|
||||
local c=self:newBase()
|
||||
think=1
|
||||
c.Type="step"
|
||||
c.pos=start or 1
|
||||
c.endAt=reset or math.huge
|
||||
c.skip=skip or 0
|
||||
c.spos=0
|
||||
c.count=count or 1*think
|
||||
c.funcE={}
|
||||
c.funcS={}
|
||||
c.start=start or 1
|
||||
if start~=nil and reset~=nil then
|
||||
if start>reset then
|
||||
think=-1
|
||||
end
|
||||
end
|
||||
function c:Act()
|
||||
if self~=nil then
|
||||
if self.spos==0 then
|
||||
if self.Active==true then
|
||||
if self.pos==self.start then
|
||||
for fe=1,#self.funcS do
|
||||
self.funcS[fe](self)
|
||||
end
|
||||
end
|
||||
for i=1,#self.func do
|
||||
self.func[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
if self.pos-self.count==self.endAt then
|
||||
for fe=1,#self.funcE do
|
||||
self.funcE[fe](self)
|
||||
end
|
||||
self.pos=self.start
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
self.spos=self.spos+1
|
||||
if self.spos>=self.skip then
|
||||
self.spos=0
|
||||
end
|
||||
end
|
||||
function c:OnStart(func)
|
||||
table.insert(self.funcS,func)
|
||||
end
|
||||
function c:OnStep(func)
|
||||
table.insert(self.func,1,func)
|
||||
end
|
||||
function c:OnEnd(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
function c:Break()
|
||||
self.Active=nil
|
||||
end
|
||||
function c:Update(start,reset,count,skip)
|
||||
self.start=start or self.start
|
||||
self.endAt=reset or self.endAt
|
||||
self.skip=skip or self.skip
|
||||
self.count=count or self.count
|
||||
self:Resume()
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTStep(start,reset,count,set)
|
||||
local c=self:newBase()
|
||||
think=1
|
||||
c.Type="tstep"
|
||||
c.start=start or 1
|
||||
local reset = reset or math.huge
|
||||
c.endAt=reset
|
||||
c.pos=start or 1
|
||||
c.skip=skip or 0
|
||||
c.count=count or 1*think
|
||||
c.funcE={}
|
||||
c.timer=os.clock()
|
||||
c.set=set or 1
|
||||
c.funcS={}
|
||||
function c:Update(start,reset,count,set)
|
||||
self.start=start or self.start
|
||||
self.pos=start
|
||||
self.endAt=reset or self.endAt
|
||||
self.set=set or self.set
|
||||
self.count=count or self.count or 1
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end
|
||||
function c:Act()
|
||||
if self.Active then
|
||||
if os.clock()-self.timer>=self.set then
|
||||
self:Reset()
|
||||
if self.pos==self.start then
|
||||
for fe=1,#self.funcS do
|
||||
self.funcS[fe](self)
|
||||
end
|
||||
end
|
||||
for i=1,#self.func do
|
||||
self.func[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
if self.pos-self.count==self.endAt then
|
||||
for fe=1,#self.funcE do
|
||||
self.funcE[fe](self)
|
||||
end
|
||||
self.pos=self.start
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnStart(func)
|
||||
table.insert(self.funcS,func)
|
||||
end
|
||||
function c:OnStep(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
function c:OnEnd(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
function c:Break()
|
||||
self.Active=nil
|
||||
end
|
||||
function c:Reset(n)
|
||||
if n then self.set=n end
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTrigger(func)
|
||||
local c=self:newBase()
|
||||
c.Type="trigger"
|
||||
c.trigfunc=func or function() end
|
||||
function c:Fire(...)
|
||||
self:trigfunc(self,...)
|
||||
end
|
||||
return c
|
||||
end
|
||||
--Managers
|
||||
function multi:mainloop()
|
||||
for i=1,#self.Tasks do
|
||||
self.Tasks[i](self)
|
||||
end
|
||||
self.Start=os.clock()
|
||||
while self.Active do
|
||||
self:Do_Order()
|
||||
end
|
||||
end
|
||||
function multi._tFunc(self,dt)
|
||||
for i=1,#self.Tasks do
|
||||
self.Tasks[i](self)
|
||||
end
|
||||
if dt then
|
||||
self.pump=true
|
||||
end
|
||||
self.pumpvar=dt
|
||||
self.Start=os.clock()
|
||||
end
|
||||
function multi:uManager(dt)
|
||||
if self.Active then
|
||||
self:oneTime(self._tFunc,self,dt)
|
||||
self:Do_Order()
|
||||
end
|
||||
end
|
||||
596
oldversions/MultiManager(0.4.0-5).lua
Normal file
596
oldversions/MultiManager(0.4.0-5).lua
Normal file
@ -0,0 +1,596 @@
|
||||
multi = {}
|
||||
multi.Version="4.0.5"
|
||||
multi.__index = multi
|
||||
multi.Mainloop={}
|
||||
multi.Tasks={}
|
||||
multi.Tasks2={}
|
||||
multi.Garbage={}
|
||||
multi.Children={}
|
||||
multi.Paused={}
|
||||
multi.MasterId=0
|
||||
multi.Active=true
|
||||
multi.Id=-1
|
||||
multi.Type="mainint"
|
||||
multi.Rest=0
|
||||
multi._type=type
|
||||
multi.Jobs={}
|
||||
multi.queue={}
|
||||
multi.jobUS=2
|
||||
--[[function type(v)
|
||||
local t={}
|
||||
if multi._type(v)=="table" then
|
||||
t=getmetatable(v)
|
||||
if v.Type~=nil then
|
||||
if multi._type(v.Type)=="string" then
|
||||
return v.Type
|
||||
end
|
||||
end
|
||||
end
|
||||
if t.__type~=nil then
|
||||
return t.__type
|
||||
else
|
||||
return multi._type(v)
|
||||
end
|
||||
end]]
|
||||
-- System
|
||||
function os.getOS()
|
||||
if package.config:sub(1,1)=="\\" then
|
||||
return "windows"
|
||||
else
|
||||
return "unix"
|
||||
end
|
||||
end
|
||||
if os.getOS()=="windows" then
|
||||
function os.sleep(n)
|
||||
if n > 0 then os.execute("ping -n " .. tonumber(n+1) .. " localhost > NUL") end
|
||||
end
|
||||
else
|
||||
function os.sleep(n)
|
||||
os.execute("sleep " .. tonumber(n))
|
||||
end
|
||||
end
|
||||
function multi:newBase(ins)
|
||||
if not(self.Type=="mainint" or self.Type=="int") then error("Can only create an object on multi or an interface obj") return false end
|
||||
local c = {}
|
||||
if self.Type=="int" then
|
||||
setmetatable(c, self.Parent)
|
||||
else
|
||||
setmetatable(c, self)
|
||||
end
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.Id=0
|
||||
c.PId=0
|
||||
c.Act=function() end
|
||||
c.Parent=self
|
||||
if ins then
|
||||
table.insert(self.Mainloop,ins,c)
|
||||
else
|
||||
table.insert(self.Mainloop,c)
|
||||
end
|
||||
self.MasterId=self.MasterId+1
|
||||
return c
|
||||
end
|
||||
function multi:reboot(r)
|
||||
self.Mainloop={}
|
||||
self.Tasks={}
|
||||
self.Tasks2={}
|
||||
self.Garbage={}
|
||||
self.Children={}
|
||||
self.Paused={}
|
||||
self.MasterId=0
|
||||
self.Active=true
|
||||
self.Id=-1
|
||||
if r then
|
||||
for i,v in pairs(_G) do
|
||||
if type(i)=="table" then
|
||||
if i.Parent and i.Id and i.Act then
|
||||
i={}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:getChildren()
|
||||
return self.Mainloop
|
||||
end
|
||||
--Processor
|
||||
function multi:Do_Order()
|
||||
for _D=#self.Mainloop,1,-1 do
|
||||
if self.Mainloop[_D]~=nil then
|
||||
self.Mainloop[_D].Id=_D
|
||||
self.Mainloop[_D]:Act()
|
||||
end
|
||||
if self.Mainloop[_D]~=nil then
|
||||
if self.Mainloop[_D].rem~=nil then
|
||||
table.remove(self.Mainloop,_D)
|
||||
end
|
||||
end
|
||||
end
|
||||
if self.Rest~=0 then
|
||||
os.sleep(self.Rest)
|
||||
end
|
||||
end
|
||||
function multi:benchMark(sec)
|
||||
local temp=self:newLoop(function(t,self)
|
||||
if os.clock()-self.init>self.sec then
|
||||
print(self.c.." steps in "..self.sec.." second(s)")
|
||||
self.tt(self.sec)
|
||||
self:Destroy()
|
||||
else
|
||||
self.c=self.c+1
|
||||
end
|
||||
end)
|
||||
function temp:OnBench(func)
|
||||
self.tt=func
|
||||
end
|
||||
self.tt=function() end
|
||||
temp.sec=sec
|
||||
temp.init=os.clock()
|
||||
temp.c=0
|
||||
return temp
|
||||
end
|
||||
function multi:newInterface()
|
||||
if not(self.Type=="mainint") then error("Can only create an interface on the multi obj") return false end
|
||||
local c = {}
|
||||
setmetatable(c, self)
|
||||
c.Parent=self
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.Id=0
|
||||
c.Type="int"
|
||||
c.Mainloop={}
|
||||
c.Tasks={}
|
||||
c.Tasks2={}
|
||||
c.Garbage={}
|
||||
c.Children={}
|
||||
c.Paused={}
|
||||
c.MasterId=0
|
||||
c.Active=true
|
||||
c.Id=-1
|
||||
c.Rest=0
|
||||
c.Jobs={}
|
||||
c.queue={}
|
||||
c.jobUS=2
|
||||
function c:Start()
|
||||
if self.l then
|
||||
self.l:Resume()
|
||||
else
|
||||
self.l=self.Parent:newLoop(function(dt) c:uManager(dt) end)
|
||||
end
|
||||
end
|
||||
function c:Stop()
|
||||
if self.l then
|
||||
self.l:Pause()
|
||||
end
|
||||
end
|
||||
function c:Remove()
|
||||
self:Destroy()
|
||||
self.l:Destroy()
|
||||
end
|
||||
return c
|
||||
end
|
||||
--Helpers
|
||||
function multi:FreeMainEvent()
|
||||
self.func={}
|
||||
end
|
||||
function multi:isPaused()
|
||||
return not(self.Active)
|
||||
end
|
||||
function multi:Pause(n)
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
self.Active=false
|
||||
if not(n) then
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Pause()
|
||||
end
|
||||
else
|
||||
self:hold(n)
|
||||
end
|
||||
else
|
||||
if n==nil then
|
||||
self.Active=false
|
||||
if self.Parent.Mainloop[self.Id]~=nil then
|
||||
table.remove(self.Parent.Mainloop,self.Id)
|
||||
table.insert(self.Parent.Paused,self)
|
||||
self.PId=#self.Parent.Paused
|
||||
end
|
||||
else
|
||||
self:hold(n)
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:Resume()
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
self.Active=true
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Resume()
|
||||
end
|
||||
else
|
||||
if self:isPaused() then
|
||||
table.remove(self.Parent.Paused,self.PId)
|
||||
table.insert(self.Parent.Mainloop,self)
|
||||
self.Id=#self.Parent.Mainloop
|
||||
self.Active=true
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:Destroy()
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Destroy()
|
||||
end
|
||||
else
|
||||
self.rem=true
|
||||
self.Active=false
|
||||
end
|
||||
end
|
||||
function multi:hold(task)
|
||||
self:Pause()
|
||||
if type(task)=="number" then
|
||||
local alarm=self.Parent:newAlarm(task)
|
||||
while alarm.Active==true do
|
||||
if love then
|
||||
self.Parent:lManager()
|
||||
else
|
||||
self.Parent:Do_Order()
|
||||
end
|
||||
end
|
||||
alarm:Destroy()
|
||||
self:Resume()
|
||||
elseif type(task)=="function" then
|
||||
local env=self.Parent:newEvent(task)
|
||||
env:OnEvent(function(envt) envt:Pause() envt:Stop() end)
|
||||
while env.Active do
|
||||
if love then
|
||||
self.Parent:lManager()
|
||||
else
|
||||
self.Parent:Do_Order()
|
||||
end
|
||||
end
|
||||
env:Destroy()
|
||||
self:Resume()
|
||||
else
|
||||
print("Error Data Type!!!")
|
||||
end
|
||||
end
|
||||
function multi:oneTime(func,...)
|
||||
if not(self.Type=="mainint" or self.Type=="int") then
|
||||
for _k=1,#self.Parent.Tasks2 do
|
||||
if self.Parent.Tasks2[_k]==func then
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(self.Parent.Tasks2,func)
|
||||
func(...)
|
||||
return true
|
||||
else
|
||||
for _k=1,#self.Tasks2 do
|
||||
if self.Tasks2[_k]==func then
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(self.Tasks2,func)
|
||||
func(...)
|
||||
return true
|
||||
end
|
||||
end
|
||||
--Constructors
|
||||
function multi:newEvent(task)
|
||||
local c=self:newBase()
|
||||
c.Type="event"
|
||||
c.Task=task or function() end
|
||||
function c:Act()
|
||||
if self.Task(self) and self.Active==true then
|
||||
self:Pause()
|
||||
for _E=1,#self.func do
|
||||
self.func[_E](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnEvent(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newAlarm(set)
|
||||
local c=self:newBase()
|
||||
c.Type="alarm"
|
||||
c.timer=os.clock()
|
||||
c.set=set or 0
|
||||
function c:Act()
|
||||
if self.Active==true then
|
||||
if os.clock()-self.timer>=self.set then
|
||||
self:Pause()
|
||||
for i=1,#self.func do
|
||||
self.func[i](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:Reset(n)
|
||||
if n then self.set=n end
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
self.Active=true
|
||||
end
|
||||
function c:OnRing(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTask(func)
|
||||
table.insert(self.Tasks,func)
|
||||
end
|
||||
function multi:newLoop(func)
|
||||
local c=self:newBase()
|
||||
c.Type="loop"
|
||||
if func then
|
||||
c.func={func}
|
||||
end
|
||||
function c:Act()
|
||||
if self.Active==true then
|
||||
for i=1,#self.func do
|
||||
self.func[i](os.clock()-self.Parent.Start,self)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnLoop(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
function c:Break()
|
||||
self.Active=nil
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newStep(start,reset,count,skip)
|
||||
local c=self:newBase()
|
||||
think=1
|
||||
c.Type="step"
|
||||
c.pos=start or 1
|
||||
c.endAt=reset or math.huge
|
||||
c.skip=skip or 0
|
||||
c.spos=0
|
||||
c.count=count or 1*think
|
||||
c.funcE={}
|
||||
c.funcS={}
|
||||
c.start=start or 1
|
||||
if start~=nil and reset~=nil then
|
||||
if start>reset then
|
||||
think=-1
|
||||
end
|
||||
end
|
||||
function c:Act()
|
||||
if self~=nil then
|
||||
if self.spos==0 then
|
||||
if self.Active==true then
|
||||
if self.pos==self.start then
|
||||
for fe=1,#self.funcS do
|
||||
self.funcS[fe](self)
|
||||
end
|
||||
end
|
||||
for i=1,#self.func do
|
||||
self.func[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
if self.pos-self.count==self.endAt then
|
||||
for fe=1,#self.funcE do
|
||||
self.funcE[fe](self)
|
||||
end
|
||||
self.pos=self.start
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
self.spos=self.spos+1
|
||||
if self.spos>=self.skip then
|
||||
self.spos=0
|
||||
end
|
||||
end
|
||||
function c:OnStart(func)
|
||||
table.insert(self.funcS,func)
|
||||
end
|
||||
function c:OnStep(func)
|
||||
table.insert(self.func,1,func)
|
||||
end
|
||||
function c:OnEnd(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
function c:Break()
|
||||
self.Active=nil
|
||||
end
|
||||
function c:Update(start,reset,count,skip)
|
||||
self.start=start or self.start
|
||||
self.endAt=reset or self.endAt
|
||||
self.skip=skip or self.skip
|
||||
self.count=count or self.count
|
||||
self:Resume()
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTStep(start,reset,count,set)
|
||||
local c=self:newBase()
|
||||
think=1
|
||||
c.Type="tstep"
|
||||
c.start=start or 1
|
||||
local reset = reset or math.huge
|
||||
c.endAt=reset
|
||||
c.pos=start or 1
|
||||
c.skip=skip or 0
|
||||
c.count=count or 1*think
|
||||
c.funcE={}
|
||||
c.timer=os.clock()
|
||||
c.set=set or 1
|
||||
c.funcS={}
|
||||
function c:Update(start,reset,count,set)
|
||||
self.start=start or self.start
|
||||
self.pos=start
|
||||
self.endAt=reset or self.endAt
|
||||
self.set=set or self.set
|
||||
self.count=count or self.count or 1
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end
|
||||
function c:Act()
|
||||
if self.Active then
|
||||
if os.clock()-self.timer>=self.set then
|
||||
self:Reset()
|
||||
if self.pos==self.start then
|
||||
for fe=1,#self.funcS do
|
||||
self.funcS[fe](self)
|
||||
end
|
||||
end
|
||||
for i=1,#self.func do
|
||||
self.func[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
if self.pos-self.count==self.endAt then
|
||||
for fe=1,#self.funcE do
|
||||
self.funcE[fe](self)
|
||||
end
|
||||
self.pos=self.start
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnStart(func)
|
||||
table.insert(self.funcS,func)
|
||||
end
|
||||
function c:OnStep(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
function c:OnEnd(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
function c:Break()
|
||||
self.Active=nil
|
||||
end
|
||||
function c:Reset(n)
|
||||
if n then self.set=n end
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTrigger(func)
|
||||
local c={}--self:newBase()
|
||||
c.Type="trigger"
|
||||
c.trigfunc=func or function() end
|
||||
function c:Fire(...)
|
||||
self:trigfunc(self,...)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newConnection()
|
||||
local c={}
|
||||
c.Type="connector"
|
||||
c.func={}
|
||||
function c:Fire(...)
|
||||
for i=1,#self.func do
|
||||
t,e=pcall(self.func[i],...)
|
||||
if not(t) then
|
||||
print(e)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:connect(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newJob(func,name)
|
||||
if not(self.Type=="mainint" or self.Type=="int") then error("Can only create an object on multi or an interface obj") return false end
|
||||
local c = {}
|
||||
if self.Type=="int" then
|
||||
setmetatable(c, self.Parent)
|
||||
else
|
||||
setmetatable(c, self)
|
||||
end
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.Id=0
|
||||
c.PId=0
|
||||
c.Parent=self
|
||||
c.Type="job"
|
||||
c.trigfunc=func or function() end
|
||||
function c:Act()
|
||||
self:trigfunc(self)
|
||||
end
|
||||
table.insert(self.Jobs,{c,name})
|
||||
if self.JobRunner==nil then
|
||||
self.JobRunner=self:newAlarm(self.jobUS)
|
||||
self.JobRunner:OnRing(function(self)
|
||||
if #self.Parent.Jobs>0 then
|
||||
if self.Parent.Jobs[1] then
|
||||
self.Parent.Jobs[1][1]:Act()
|
||||
table.remove(self.Parent.Jobs,1)
|
||||
end
|
||||
end
|
||||
self:Reset(self.Parent.jobUS)
|
||||
end)
|
||||
end
|
||||
end
|
||||
function multi:setJobSpeed(n)
|
||||
self.jobUS=n
|
||||
end
|
||||
function multi:hasJobs()
|
||||
return #self.Jobs>0,#self.Jobs
|
||||
end
|
||||
function multi:getJobs()
|
||||
return #self.Jobs
|
||||
end
|
||||
function multi:removeJob(name)
|
||||
for i=#self.Jobs,1,-1 do
|
||||
if self.Jobs[i][2]==name then
|
||||
table.remove(self.Jobs,i)
|
||||
end
|
||||
end
|
||||
end
|
||||
--Incomplete
|
||||
function multi:addToQueue(name,job)
|
||||
if self.queue[name]~=nil then
|
||||
table.insert(self.queue[name],job)
|
||||
else
|
||||
self.queue[name]={}
|
||||
end
|
||||
if self.QRunner==nil then
|
||||
self.QRunner=self:newAlarm(.5)
|
||||
self.QRunner:OnRing(function(self)
|
||||
if #self.Parent.queue>0 then
|
||||
local w=math.random(1,#self.Parent.Jobs)
|
||||
self.Parent.Jobs[w]:Act()
|
||||
table.remove(self.Parent.Jobs,w)
|
||||
end
|
||||
self:Reset()
|
||||
end)
|
||||
end
|
||||
end
|
||||
--Managers
|
||||
function multi:mainloop()
|
||||
for i=1,#self.Tasks do
|
||||
self.Tasks[i](self)
|
||||
end
|
||||
self.Start=os.clock()
|
||||
while self.Active do
|
||||
self:Do_Order()
|
||||
end
|
||||
end
|
||||
function multi._tFunc(self,dt)
|
||||
for i=1,#self.Tasks do
|
||||
self.Tasks[i](self)
|
||||
end
|
||||
if dt then
|
||||
self.pump=true
|
||||
end
|
||||
self.pumpvar=dt
|
||||
self.Start=os.clock()
|
||||
end
|
||||
function multi:uManager(dt)
|
||||
if self.Active then
|
||||
self:oneTime(self._tFunc,self,dt)
|
||||
self:Do_Order()
|
||||
end
|
||||
end
|
||||
767
oldversions/MultiManager(0.5.1-6).lua
Normal file
767
oldversions/MultiManager(0.5.1-6).lua
Normal file
@ -0,0 +1,767 @@
|
||||
multi = {}
|
||||
multi.Version="5.1.6"
|
||||
multi.__index = multi
|
||||
multi.Mainloop={}
|
||||
multi.Tasks={}
|
||||
multi.Tasks2={}
|
||||
multi.Garbage={}
|
||||
multi.Children={}
|
||||
multi.Paused={}
|
||||
multi.MasterId=0
|
||||
multi.Active=true
|
||||
multi.Id=-1
|
||||
multi.Type="mainint"
|
||||
multi.Rest=0
|
||||
multi._type=type
|
||||
multi.Jobs={}
|
||||
multi.queue={}
|
||||
multi.jobUS=2
|
||||
-- System
|
||||
function multi:Stop()
|
||||
self.Active=false
|
||||
end
|
||||
function os.getOS()
|
||||
if package.config:sub(1,1)=="\\" then
|
||||
return "windows"
|
||||
else
|
||||
return "unix"
|
||||
end
|
||||
end
|
||||
if os.getOS()=="windows" then
|
||||
function os.sleep(n)
|
||||
if n > 0 then os.execute("ping -n " .. tonumber(n+1) .. " localhost > NUL") end
|
||||
end
|
||||
else
|
||||
function os.sleep(n)
|
||||
os.execute("sleep " .. tonumber(n))
|
||||
end
|
||||
end
|
||||
function multi:newBase(ins)
|
||||
if not(self.Type=="mainint" or self.Type=="int" or self.Type=="stack") then error("Can only create an object on multi or an interface obj") return false end
|
||||
local c = {}
|
||||
if self.Type=="int" or self.Type=="stack" then
|
||||
setmetatable(c, self.Parent)
|
||||
else
|
||||
setmetatable(c, self)
|
||||
end
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.ender={}
|
||||
c.Id=0
|
||||
c.PId=0
|
||||
c.Act=function() end
|
||||
c.Parent=self
|
||||
if ins then
|
||||
table.insert(self.Mainloop,ins,c)
|
||||
else
|
||||
table.insert(self.Mainloop,c)
|
||||
end
|
||||
self.MasterId=self.MasterId+1
|
||||
return c
|
||||
end
|
||||
function multi:reboot(r)
|
||||
self.Mainloop={}
|
||||
self.Tasks={}
|
||||
self.Tasks2={}
|
||||
self.Garbage={}
|
||||
self.Children={}
|
||||
self.Paused={}
|
||||
self.MasterId=0
|
||||
self.Active=true
|
||||
self.Id=-1
|
||||
if r then
|
||||
for i,v in pairs(_G) do
|
||||
if type(i)=="table" then
|
||||
if i.Parent and i.Id and i.Act then
|
||||
i={}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:getChildren()
|
||||
return self.Mainloop
|
||||
end
|
||||
--Processor
|
||||
function multi:getError()
|
||||
if self.error then
|
||||
return self.error
|
||||
end
|
||||
end
|
||||
function multi:Do_Order()
|
||||
for _D=#self.Mainloop,1,-1 do
|
||||
if self.Mainloop[_D]~=nil then
|
||||
self.Mainloop[_D].Id=_D
|
||||
self.Mainloop[_D]:Act()
|
||||
end
|
||||
if self.Mainloop[_D]~=nil then
|
||||
if self.Mainloop[_D].rem~=nil then
|
||||
table.remove(self.Mainloop,_D)
|
||||
end
|
||||
end
|
||||
end
|
||||
if self.Rest~=0 then
|
||||
os.sleep(self.Rest)
|
||||
end
|
||||
end
|
||||
function multi:benchMark(sec)
|
||||
local temp=self:newLoop(function(t,self)
|
||||
if os.clock()-self.init>self.sec then
|
||||
print(self.c.." steps in "..self.sec.." second(s)")
|
||||
self.tt(self.sec)
|
||||
self:Destroy()
|
||||
else
|
||||
self.c=self.c+1
|
||||
end
|
||||
end)
|
||||
function temp:OnBench(func)
|
||||
self.tt=func
|
||||
end
|
||||
self.tt=function() end
|
||||
temp.sec=sec
|
||||
temp.init=os.clock()
|
||||
temp.c=0
|
||||
return temp
|
||||
end
|
||||
function multi:newInterface()
|
||||
if not(self.Type=="mainint") then error("Can only create an interface on the multi obj") return false end
|
||||
local c = {}
|
||||
setmetatable(c, self)
|
||||
c.Parent=self
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.Id=0
|
||||
c.Type="int"
|
||||
c.Mainloop={}
|
||||
c.Tasks={}
|
||||
c.Tasks2={}
|
||||
c.Garbage={}
|
||||
c.Children={}
|
||||
c.Paused={}
|
||||
c.MasterId=0
|
||||
c.Active=true
|
||||
c.Id=-1
|
||||
c.Rest=0
|
||||
c.Jobs={}
|
||||
c.queue={}
|
||||
c.jobUS=2
|
||||
function c:Start()
|
||||
if self.l then
|
||||
self.l:Resume()
|
||||
else
|
||||
self.l=self.Parent:newLoop(function(dt) c:uManager(dt) end)
|
||||
end
|
||||
end
|
||||
function c:Stop()
|
||||
if self.l then
|
||||
self.l:Pause()
|
||||
end
|
||||
end
|
||||
function c:Remove()
|
||||
self:Destroy()
|
||||
self.l:Destroy()
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newStack(file)
|
||||
local c=self:newInterface()
|
||||
c.Type="stack"
|
||||
stack=c
|
||||
c.last={}
|
||||
c.funcE={}
|
||||
if file then
|
||||
dofile(file)
|
||||
end
|
||||
function c:OnStackCompleted(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
--Helpers
|
||||
function multi:protect()
|
||||
function self:Do_Order()
|
||||
for _D=#self.Mainloop,1,-1 do
|
||||
if self.Mainloop[_D]~=nil then
|
||||
self.Mainloop[_D].Id=_D
|
||||
local status, err=pcall(self.Mainloop[_D].Act,self.Mainloop[_D])
|
||||
if err and not(self.Mainloop[_D].error) then
|
||||
self.Mainloop[_D].error=err
|
||||
print(err..": Ingoring error continuing...")
|
||||
end
|
||||
end
|
||||
if self.Mainloop[_D]~=nil then
|
||||
if self.Mainloop[_D].rem~=nil then
|
||||
table.remove(self.Mainloop,_D)
|
||||
end
|
||||
end
|
||||
end
|
||||
if self.Rest~=0 then
|
||||
os.sleep(self.Rest)
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:unProtect()
|
||||
function self:Do_Order()
|
||||
for _D=#self.Mainloop,1,-1 do
|
||||
if self.Mainloop[_D]~=nil then
|
||||
self.Mainloop[_D].Id=_D
|
||||
self.Mainloop[_D]:Act()
|
||||
end
|
||||
if self.Mainloop[_D]~=nil then
|
||||
if self.Mainloop[_D].rem~=nil then
|
||||
table.remove(self.Mainloop,_D)
|
||||
end
|
||||
end
|
||||
end
|
||||
if self.Rest~=0 then
|
||||
os.sleep(self.Rest)
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:setJobSpeed(n)
|
||||
self.jobUS=n
|
||||
end
|
||||
function multi:hasJobs()
|
||||
return #self.Jobs>0,#self.Jobs
|
||||
end
|
||||
function multi:getJobs()
|
||||
return #self.Jobs
|
||||
end
|
||||
function multi:removeJob(name)
|
||||
for i=#self.Jobs,1,-1 do
|
||||
if self.Jobs[i][2]==name then
|
||||
table.remove(self.Jobs,i)
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:FreeMainEvent()
|
||||
self.func={}
|
||||
end
|
||||
function multi:connectFinal(func)
|
||||
if self.Type=="event" then
|
||||
self:OnEvent(func)
|
||||
elseif self.Type=="alarm" then
|
||||
self:OnRing(func)
|
||||
elseif self.Type=="step" or self.Type=="tstep" then
|
||||
self:OnEnd(func)
|
||||
elseif self.Type=="loop" then
|
||||
self:OnBreak(func)
|
||||
else
|
||||
error("No final event exists for: "..self.Type)
|
||||
end
|
||||
end
|
||||
function multi:Break()
|
||||
self:Pause()
|
||||
self.Active=nil
|
||||
for i=1,#self.ender do
|
||||
self.ender[i](self)
|
||||
end
|
||||
end
|
||||
function multi:OnBreak(func)
|
||||
table.insert(self.ender,func)
|
||||
end
|
||||
function multi:isPaused()
|
||||
return not(self.Active)
|
||||
end
|
||||
function multi:Pause(n)
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
self.Active=false
|
||||
if not(n) then
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Pause()
|
||||
end
|
||||
else
|
||||
self:hold(n)
|
||||
end
|
||||
else
|
||||
if n==nil then
|
||||
self.Active=false
|
||||
if self.Parent.Mainloop[self.Id]~=nil then
|
||||
table.remove(self.Parent.Mainloop,self.Id)
|
||||
table.insert(self.Parent.Paused,self)
|
||||
self.PId=#self.Parent.Paused
|
||||
end
|
||||
else
|
||||
self:hold(n)
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:Resume()
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
self.Active=true
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Resume()
|
||||
end
|
||||
else
|
||||
if self:isPaused() then
|
||||
table.remove(self.Parent.Paused,self.PId)
|
||||
table.insert(self.Parent.Mainloop,self)
|
||||
self.Id=#self.Parent.Mainloop
|
||||
self.Active=true
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:Destroy()
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Destroy()
|
||||
end
|
||||
else
|
||||
self.rem=true
|
||||
self.Active=false
|
||||
end
|
||||
end
|
||||
function multi:hold(task)
|
||||
self:Pause()
|
||||
if type(task)=="number" then
|
||||
local alarm=self.Parent:newAlarm(task)
|
||||
while alarm.Active==true do
|
||||
if love then
|
||||
self.Parent:lManager()
|
||||
else
|
||||
self.Parent:Do_Order()
|
||||
end
|
||||
end
|
||||
alarm:Destroy()
|
||||
self:Resume()
|
||||
elseif type(task)=="function" then
|
||||
local env=self.Parent:newEvent(task)
|
||||
env:OnEvent(function(envt) envt:Pause() envt:Stop() end)
|
||||
while env.Active do
|
||||
if love then
|
||||
self.Parent:lManager()
|
||||
else
|
||||
self.Parent:Do_Order()
|
||||
end
|
||||
end
|
||||
env:Destroy()
|
||||
self:Resume()
|
||||
else
|
||||
print("Error Data Type!!!")
|
||||
end
|
||||
end
|
||||
function multi:oneTime(func,...)
|
||||
if not(self.Type=="mainint" or self.Type=="int") then
|
||||
for _k=1,#self.Parent.Tasks2 do
|
||||
if self.Parent.Tasks2[_k]==func then
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(self.Parent.Tasks2,func)
|
||||
func(...)
|
||||
return true
|
||||
else
|
||||
for _k=1,#self.Tasks2 do
|
||||
if self.Tasks2[_k]==func then
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(self.Tasks2,func)
|
||||
func(...)
|
||||
return true
|
||||
end
|
||||
end
|
||||
function multi:Reset(n)
|
||||
self:Resume()
|
||||
end
|
||||
function multi:isDone()
|
||||
return self.Active~=true
|
||||
end
|
||||
--Constructors
|
||||
function multi:newEvent(task)
|
||||
local c={}
|
||||
if self.Type=="stack" then
|
||||
c=self:newBase(1)
|
||||
self.last=c
|
||||
else
|
||||
c=self:newBase()
|
||||
end
|
||||
c.Type="event"
|
||||
c.Task=task or function() end
|
||||
function c:Act()
|
||||
if self.Task(self) and self.Active==true then
|
||||
self:Pause()
|
||||
for _E=1,#self.func do
|
||||
self.func[_E](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnEvent(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
if self.Type=="stack" then
|
||||
if #self.Mainloop>1 then
|
||||
c:Pause()
|
||||
end
|
||||
c:connectFinal(function(self)
|
||||
if self.Parent.last==self then
|
||||
for i=1,#self.Parent.funcE do
|
||||
self.Parent.funcE[i](self)
|
||||
end
|
||||
self.Parent:Remove()
|
||||
end
|
||||
self:Destroy()
|
||||
self.Parent.Mainloop[#self.Parent.Mainloop]:Resume()
|
||||
end)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newAlarm(set)
|
||||
local c={}
|
||||
if self.Type=="stack" then
|
||||
c=self:newBase(1)
|
||||
self.last=c
|
||||
else
|
||||
c=self:newBase()
|
||||
end
|
||||
c.Type="alarm"
|
||||
c.timer=os.clock()
|
||||
c.set=set or 0
|
||||
function c:Act()
|
||||
if self.Active==true then
|
||||
if os.clock()-self.timer>=self.set then
|
||||
self:Pause()
|
||||
self.Active=false
|
||||
for i=1,#self.func do
|
||||
self.func[i](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:Resume()
|
||||
self.Parent.Resume(self)
|
||||
self.timer=os.clock()
|
||||
self.Active=true
|
||||
end
|
||||
function c:Reset(n)
|
||||
if n then self.set=n end
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
self.Active=true
|
||||
end
|
||||
function c:OnRing(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
if self.Type=="stack" then
|
||||
if #self.Mainloop>1 then
|
||||
c:Pause()
|
||||
end
|
||||
c:connectFinal(function(self)
|
||||
if self.Parent.last==self then
|
||||
for i=1,#self.Parent.funcE do
|
||||
self.Parent.funcE[i](self)
|
||||
end
|
||||
self.Parent:Remove()
|
||||
end
|
||||
table.remove(self.Parent.Mainloop,#self.Parent.Mainloop)
|
||||
self.Parent.Mainloop[#self.Parent.Mainloop]:Resume()
|
||||
end)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTask(func)
|
||||
table.insert(self.Tasks,func)
|
||||
end
|
||||
function multi:newLoop(func)
|
||||
local c={}
|
||||
if self.Type=="stack" then
|
||||
c=self:newBase(1)
|
||||
self.last=c
|
||||
else
|
||||
c=self:newBase()
|
||||
end
|
||||
c.Type="loop"
|
||||
c.Start=os.clock()
|
||||
if func then
|
||||
c.func={func}
|
||||
end
|
||||
function c:Act()
|
||||
if self.Active==true then
|
||||
for i=1,#self.func do
|
||||
self.func[i](os.clock()-self.Start,self)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnLoop(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
if self.Type=="stack" then
|
||||
if #self.Mainloop>1 then
|
||||
c:Pause()
|
||||
end
|
||||
c:connectFinal(function(self)
|
||||
if self.Parent.last==self then
|
||||
for i=1,#self.Parent.funcE do
|
||||
self.Parent.funcE[i](self)
|
||||
end
|
||||
self.Parent:Remove()
|
||||
end
|
||||
self:Destroy()
|
||||
self.Parent.Mainloop[#self.Parent.Mainloop]:Resume()
|
||||
end)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newStep(start,reset,count,skip)
|
||||
local c={}
|
||||
if self.Type=="stack" then
|
||||
c=self:newBase(1)
|
||||
self.last=c
|
||||
else
|
||||
c=self:newBase()
|
||||
end
|
||||
think=1
|
||||
c.Type="step"
|
||||
c.pos=start or 1
|
||||
c.endAt=reset or math.huge
|
||||
c.skip=skip or 0
|
||||
c.spos=0
|
||||
c.count=count or 1*think
|
||||
c.funcE={}
|
||||
c.funcS={}
|
||||
c.start=start or 1
|
||||
if start~=nil and reset~=nil then
|
||||
if start>reset then
|
||||
think=-1
|
||||
end
|
||||
end
|
||||
function c:Act()
|
||||
if self~=nil then
|
||||
if self.spos==0 then
|
||||
if self.Active==true then
|
||||
if self.pos==self.start then
|
||||
for fe=1,#self.funcS do
|
||||
self.funcS[fe](self)
|
||||
end
|
||||
end
|
||||
for i=1,#self.func do
|
||||
self.func[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
if self.pos-self.count==self.endAt then
|
||||
self:Pause()
|
||||
for fe=1,#self.funcE do
|
||||
self.funcE[fe](self)
|
||||
end
|
||||
self.pos=self.start
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
self.spos=self.spos+1
|
||||
if self.spos>=self.skip then
|
||||
self.spos=0
|
||||
end
|
||||
end
|
||||
function c:OnStart(func)
|
||||
table.insert(self.funcS,func)
|
||||
end
|
||||
function c:OnStep(func)
|
||||
table.insert(self.func,1,func)
|
||||
end
|
||||
function c:OnEnd(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
function c:Break()
|
||||
self.Active=nil
|
||||
end
|
||||
function c:Update(start,reset,count,skip)
|
||||
self.start=start or self.start
|
||||
self.endAt=reset or self.endAt
|
||||
self.skip=skip or self.skip
|
||||
self.count=count or self.count
|
||||
self:Resume()
|
||||
end
|
||||
if self.Type=="stack" then
|
||||
if #self.Mainloop>1 then
|
||||
c:Pause()
|
||||
end
|
||||
c:connectFinal(function(self)
|
||||
if self.Parent.last==self then
|
||||
for i=1,#self.Parent.funcE do
|
||||
self.Parent.funcE[i](self)
|
||||
end
|
||||
self.Parent:Remove()
|
||||
end
|
||||
self:Destroy()
|
||||
self.Parent.Mainloop[#self.Parent.Mainloop]:Resume()
|
||||
end)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTStep(start,reset,count,set)
|
||||
local c={}
|
||||
if self.Type=="stack" then
|
||||
c=self:newBase(1)
|
||||
self.last=c
|
||||
else
|
||||
c=self:newBase()
|
||||
end
|
||||
think=1
|
||||
c.Type="tstep"
|
||||
c.start=start or 1
|
||||
local reset = reset or math.huge
|
||||
c.endAt=reset
|
||||
c.pos=start or 1
|
||||
c.skip=skip or 0
|
||||
c.count=count or 1*think
|
||||
c.funcE={}
|
||||
c.timer=os.clock()
|
||||
c.set=set or 1
|
||||
c.funcS={}
|
||||
function c:Update(start,reset,count,set)
|
||||
self.start=start or self.start
|
||||
self.pos=start
|
||||
self.endAt=reset or self.endAt
|
||||
self.set=set or self.set
|
||||
self.count=count or self.count or 1
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end
|
||||
function c:Act()
|
||||
if self.Active then
|
||||
if os.clock()-self.timer>=self.set then
|
||||
self:Reset()
|
||||
if self.pos==self.start then
|
||||
for fe=1,#self.funcS do
|
||||
self.funcS[fe](self)
|
||||
end
|
||||
end
|
||||
for i=1,#self.func do
|
||||
self.func[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
if self.pos-self.count==self.endAt then
|
||||
self:Pause()
|
||||
for fe=1,#self.funcE do
|
||||
self.funcE[fe](self)
|
||||
end
|
||||
self.pos=self.start
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnStart(func)
|
||||
table.insert(self.funcS,func)
|
||||
end
|
||||
function c:OnStep(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
function c:OnEnd(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
function c:Break()
|
||||
self.Active=nil
|
||||
end
|
||||
function c:Reset(n)
|
||||
if n then self.set=n end
|
||||
self.timer=os.clock()
|
||||
self:Resume()
|
||||
end
|
||||
if self.Type=="stack" then
|
||||
if #self.Mainloop>1 then
|
||||
c:Pause()
|
||||
end
|
||||
c:connectFinal(function(self)
|
||||
if self.Parent.last==self then
|
||||
for i=1,#self.Parent.funcE do
|
||||
self.Parent.funcE[i](self)
|
||||
end
|
||||
self.Parent:Remove()
|
||||
end
|
||||
self:Destroy()
|
||||
self.Parent.Mainloop[#self.Parent.Mainloop]:Resume()
|
||||
end)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTrigger(func)
|
||||
local c={}
|
||||
c.Type="trigger"
|
||||
c.trigfunc=func or function() end
|
||||
function c:Fire(...)
|
||||
self:trigfunc(self,...)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newConnection()
|
||||
local c={}
|
||||
c.Type="connector"
|
||||
c.func={}
|
||||
function c:Fire(...)
|
||||
for i=1,#self.func do
|
||||
t,e=pcall(self.func[i],...)
|
||||
if not(t) then
|
||||
print(e)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:bind(t)
|
||||
self.func=t
|
||||
end
|
||||
function c:connect(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newJob(func,name)
|
||||
if not(self.Type=="mainint" or self.Type=="int") then error("Can only create an object on multi or an interface obj") return false end
|
||||
local c = {}
|
||||
if self.Type=="int" then
|
||||
setmetatable(c, self.Parent)
|
||||
else
|
||||
setmetatable(c, self)
|
||||
end
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.Id=0
|
||||
c.PId=0
|
||||
c.Parent=self
|
||||
c.Type="job"
|
||||
c.trigfunc=func or function() end
|
||||
function c:Act()
|
||||
self:trigfunc(self)
|
||||
end
|
||||
table.insert(self.Jobs,{c,name})
|
||||
if self.JobRunner==nil then
|
||||
self.JobRunner=self:newAlarm(self.jobUS)
|
||||
self.JobRunner:OnRing(function(self)
|
||||
if #self.Parent.Jobs>0 then
|
||||
if self.Parent.Jobs[1] then
|
||||
self.Parent.Jobs[1][1]:Act()
|
||||
table.remove(self.Parent.Jobs,1)
|
||||
end
|
||||
end
|
||||
self:Reset(self.Parent.jobUS)
|
||||
end)
|
||||
end
|
||||
end
|
||||
--Managers
|
||||
function multi:mainloop()
|
||||
for i=1,#self.Tasks do
|
||||
self.Tasks[i](self)
|
||||
end
|
||||
self.Start=os.clock()
|
||||
while self.Active do
|
||||
self:Do_Order()
|
||||
end
|
||||
end
|
||||
function multi._tFunc(self,dt)
|
||||
for i=1,#self.Tasks do
|
||||
self.Tasks[i](self)
|
||||
end
|
||||
if dt then
|
||||
self.pump=true
|
||||
end
|
||||
self.pumpvar=dt
|
||||
self.Start=os.clock()
|
||||
end
|
||||
function multi:uManager(dt)
|
||||
if self.Active then
|
||||
self:oneTime(self._tFunc,self,dt)
|
||||
self:Do_Order()
|
||||
end
|
||||
end
|
||||
929
oldversions/MultiManager(0.6.1-6).lua
Normal file
929
oldversions/MultiManager(0.6.1-6).lua
Normal file
@ -0,0 +1,929 @@
|
||||
multi = {}
|
||||
multi.Version={6,1,6}-- History: EventManager,EventManager+,MultiManager <-- Current
|
||||
multi.stage="stable"
|
||||
multi.Features=multi.Version[1].."."..multi.Version[2].."."..multi.Version[3].." "..multi.stage..[[
|
||||
Objects:
|
||||
Event
|
||||
Alarm
|
||||
Loop
|
||||
Step
|
||||
TStep
|
||||
Trigger
|
||||
Task
|
||||
Connection
|
||||
Timer
|
||||
Job
|
||||
]]
|
||||
multi.__index = multi
|
||||
multi.Mainloop={}
|
||||
multi.Tasks={}
|
||||
multi.Tasks2={}
|
||||
multi.Garbage={}
|
||||
multi.Children={}
|
||||
multi.Paused={}
|
||||
multi.Active=true
|
||||
multi.Id=-1
|
||||
multi.Type="mainint"
|
||||
multi.Rest=0
|
||||
multi._type=type
|
||||
multi.Jobs={}
|
||||
multi.queue={}
|
||||
multi.jobUS=2
|
||||
multi.clock=os.clock
|
||||
multi.time=os.time
|
||||
-- System
|
||||
function multi:Stop()
|
||||
self.Active=false
|
||||
end
|
||||
function os.getOS()
|
||||
if package.config:sub(1,1)=="\\" then
|
||||
return "windows"
|
||||
else
|
||||
return "unix"
|
||||
end
|
||||
end
|
||||
if os.getOS()=="windows" then
|
||||
function os.sleep(n)
|
||||
if n > 0 then os.execute("ping -n " .. tonumber(n+1) .. " localhost > NUL") end
|
||||
end
|
||||
else
|
||||
function os.sleep(n)
|
||||
os.execute("sleep " .. tonumber(n))
|
||||
end
|
||||
end
|
||||
function multi.executeFunction(name,...)
|
||||
if type(_G[name])=="function" then
|
||||
_G[name](...)
|
||||
else
|
||||
print("Error: Not a function")
|
||||
end
|
||||
end
|
||||
function multi:newBase(ins)
|
||||
if not(self.Type=="mainint" or self.Type=="int" or self.Type=="stack") then error("Can only create an object on multi or an interface obj") return false end
|
||||
local c = {}
|
||||
if self.Type=="int" or self.Type=="stack" then
|
||||
setmetatable(c, self.Parent)
|
||||
else
|
||||
setmetatable(c, self)
|
||||
end
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.ender={}
|
||||
c.Id=0
|
||||
c.PId=0
|
||||
c.Act=function() end
|
||||
c.Parent=self
|
||||
if ins then
|
||||
table.insert(self.Mainloop,ins,c)
|
||||
else
|
||||
table.insert(self.Mainloop,c)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:reboot(r)
|
||||
local before=collectgarbage("count")
|
||||
self.Mainloop={}
|
||||
self.Tasks={}
|
||||
self.Tasks2={}
|
||||
self.Garbage={}
|
||||
self.Children={}
|
||||
self.Paused={}
|
||||
self.Active=true
|
||||
self.Id=-1
|
||||
if r then
|
||||
for i,v in pairs(_G) do
|
||||
if type(i)=="table" then
|
||||
if i.Parent and i.Id and i.Act then
|
||||
i={}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
collectgarbage()
|
||||
local after=collectgarbage("count")
|
||||
print([[Before rebooting total Ram used was ]]..before..[[Kb
|
||||
After rebooting total Ram used is ]]..after..[[ Kb
|
||||
A total of ]]..(before-after)..[[Kb was cleaned up]])
|
||||
end
|
||||
function multi:getChildren()
|
||||
return self.Mainloop
|
||||
end
|
||||
--Processor
|
||||
function multi:getError()
|
||||
if self.error then
|
||||
return self.error
|
||||
end
|
||||
end
|
||||
function multi:Do_Order()
|
||||
local Loop=self.Mainloop
|
||||
for _D=#Loop,1,-1 do
|
||||
if Loop[_D]~=nil then
|
||||
Loop[_D].Id=_D
|
||||
Loop[_D]:Act()
|
||||
end
|
||||
end
|
||||
if self.Rest~=0 then
|
||||
os.sleep(self.Rest)
|
||||
end
|
||||
end
|
||||
function multi:fromfile(path,int)
|
||||
int=int or multi
|
||||
local test2={}
|
||||
local test=bin.load(path)
|
||||
local tp=test:getBlock("s")
|
||||
if tp=="event" then
|
||||
test2=int:newEvent(test:getBlock("f"))
|
||||
local t=test:getBlock("t")
|
||||
for i=1,#t do
|
||||
test2:OnEvent(t[i])
|
||||
end
|
||||
elseif tp=="alarm" then
|
||||
test2=int:newAlarm(test:getBlock("n"))
|
||||
elseif tp=="loop" then
|
||||
test2=int:newLoop(test:getBlock("t")[1])
|
||||
elseif tp=="step" or tp=="tstep" then
|
||||
local func=test:getBlock("t")
|
||||
local funcE=test:getBlock("t")
|
||||
local funcS=test:getBlock("t")
|
||||
local tab=test:getBlock("t")
|
||||
test2=int:newStep()
|
||||
table.merge(test2,tab)
|
||||
test2.funcE=funcE
|
||||
test2.funcS=funcS
|
||||
test2.func=func
|
||||
elseif tp=="trigger" then
|
||||
test2=int:newTrigger(test:getBlock("f"))
|
||||
elseif tp=="connector" then
|
||||
test2=int:newConnection()
|
||||
test2.func=test:getBlock("t")
|
||||
elseif tp=="timer" then
|
||||
test2=int:newTimer()
|
||||
test2.count=tonumber(test:getBlock("n"))
|
||||
else
|
||||
print("Error: The file you selected is not a valid multi file object!")
|
||||
return false
|
||||
end
|
||||
return test2
|
||||
end
|
||||
function multi:benchMark(sec)
|
||||
local temp=self:newLoop(function(t,self)
|
||||
if multi.clock()-self.init>self.sec then
|
||||
print(self.c.." steps in "..self.sec.." second(s)")
|
||||
self.tt(self.sec)
|
||||
self:Destroy()
|
||||
else
|
||||
self.c=self.c+1
|
||||
end
|
||||
end)
|
||||
function temp:OnBench(func)
|
||||
self.tt=func
|
||||
end
|
||||
self.tt=function() end
|
||||
temp.sec=sec
|
||||
temp.init=multi.clock()
|
||||
temp.c=0
|
||||
return temp
|
||||
end
|
||||
function multi:tofile(path)
|
||||
local items=self:getChildren()
|
||||
io.mkDir(io.getName(path))
|
||||
for i=1,#items do
|
||||
items[i]:tofile(io.getName(path).."\\item"..item[i]..".dat")
|
||||
end
|
||||
local int=bin.new()
|
||||
int:addBlock("int")
|
||||
int:addBlock(io.getName(path))
|
||||
int:addBlock(#self.Mainloop)
|
||||
int:addBlock(self.Active)
|
||||
int:addBlock(self.Rest)
|
||||
int:addBlock(self.Jobs)
|
||||
int:tofile()
|
||||
end
|
||||
function multi:newInterface(file)
|
||||
if not(self.Type=="mainint") then error("Can only create an interface on the multi obj") return false end
|
||||
local c = {}
|
||||
setmetatable(c, self)
|
||||
c.Parent=self
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.Id=0
|
||||
c.Type="int"
|
||||
c.Mainloop={}
|
||||
c.Tasks={}
|
||||
c.Tasks2={}
|
||||
c.Garbage={}
|
||||
c.Children={}
|
||||
c.Paused={}
|
||||
c.Active=true
|
||||
c.Id=-1
|
||||
c.Rest=0
|
||||
c.Jobs={}
|
||||
c.queue={}
|
||||
c.jobUS=2
|
||||
function c:Start()
|
||||
if self.l then
|
||||
self.l:Resume()
|
||||
else
|
||||
self.l=self.Parent:newLoop(function(dt) c:uManager(dt) end)
|
||||
end
|
||||
end
|
||||
function c:Stop()
|
||||
if self.l then
|
||||
self.l:Pause()
|
||||
end
|
||||
end
|
||||
function c:Remove()
|
||||
self:Destroy()
|
||||
self.l:Destroy()
|
||||
end
|
||||
if file then
|
||||
multi.Cself=c
|
||||
loadstring("interface=multi.Cself "..io.open(file,"rb"):read("*all"))()
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newStack(file)
|
||||
local c=self:newInterface()
|
||||
c.Type="stack"
|
||||
c.last={}
|
||||
c.funcE={}
|
||||
if file then
|
||||
multi.Cself=c
|
||||
loadstring("stack=multi.Cself "..io.open(file,"rb"):read("*all"))()
|
||||
end
|
||||
function c:OnStackCompleted(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
return c
|
||||
end
|
||||
--Helpers
|
||||
function multi:protect()
|
||||
function self:Do_Order()
|
||||
local Loop=self.Mainloop
|
||||
for _D=#Loop,1,-1 do
|
||||
if Loop[_D]~=nil then
|
||||
Loop[_D].Id=_D
|
||||
local status, err=pcall(Loop[_D].Act,Loop[_D])
|
||||
if err and not(Loop[_D].error) then
|
||||
Loop[_D].error=err
|
||||
print(err..": Ingoring error continuing...")
|
||||
end
|
||||
end
|
||||
end
|
||||
if self.Rest~=0 then
|
||||
os.sleep(self.Rest)
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:unProtect()
|
||||
function self:Do_Order()
|
||||
for _D=#Loop,1,-1 do
|
||||
if Loop[_D]~=nil then
|
||||
Loop[_D].Id=_D
|
||||
Loop[_D]:Act()
|
||||
end
|
||||
end
|
||||
if self.Rest~=0 then
|
||||
os.sleep(self.Rest)
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:setJobSpeed(n)
|
||||
self.jobUS=n
|
||||
end
|
||||
function multi:hasJobs()
|
||||
return #self.Jobs>0,#self.Jobs
|
||||
end
|
||||
function multi:getJobs()
|
||||
return #self.Jobs
|
||||
end
|
||||
function multi:removeJob(name)
|
||||
for i=#self.Jobs,1,-1 do
|
||||
if self.Jobs[i][2]==name then
|
||||
table.remove(self.Jobs,i)
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:FreeMainEvent()
|
||||
self.func={}
|
||||
end
|
||||
function multi:connectFinal(func)
|
||||
if self.Type=="event" then
|
||||
self:OnEvent(func)
|
||||
elseif self.Type=="alarm" then
|
||||
self:OnRing(func)
|
||||
elseif self.Type=="step" or self.Type=="tstep" then
|
||||
self:OnEnd(func)
|
||||
elseif self.Type=="loop" then
|
||||
self:OnBreak(func)
|
||||
else
|
||||
error("No final event exists for: "..self.Type)
|
||||
end
|
||||
end
|
||||
function multi:Break()
|
||||
self:Pause()
|
||||
self.Active=nil
|
||||
for i=1,#self.ender do
|
||||
self.ender[i](self)
|
||||
end
|
||||
end
|
||||
function multi:OnBreak(func)
|
||||
table.insert(self.ender,func)
|
||||
end
|
||||
function multi:isPaused()
|
||||
return not(self.Active)
|
||||
end
|
||||
function multi:Pause(n)
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
self.Active=false
|
||||
if not(n) then
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Pause()
|
||||
end
|
||||
else
|
||||
self:hold(n)
|
||||
end
|
||||
else
|
||||
if n==nil then
|
||||
self.Active=false
|
||||
if self.Parent.Mainloop[self.Id]~=nil then
|
||||
table.remove(self.Parent.Mainloop,self.Id)
|
||||
table.insert(self.Parent.Paused,self)
|
||||
self.PId=#self.Parent.Paused
|
||||
end
|
||||
else
|
||||
self:hold(n)
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:Resume()
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
self.Active=true
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Resume()
|
||||
end
|
||||
else
|
||||
if self:isPaused() then
|
||||
table.remove(self.Parent.Paused,self.PId)
|
||||
table.insert(self.Parent.Mainloop,self)
|
||||
self.Id=#self.Parent.Mainloop
|
||||
self.Active=true
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:resurrect()
|
||||
table.insert(self.Parent.Mainloop,self)
|
||||
self.Active=true
|
||||
end
|
||||
function multi:Destroy()
|
||||
if self.Type=="int" or self.Type=="mainint" then
|
||||
local c=self:getChildren()
|
||||
for i=1,#c do
|
||||
c[i]:Destroy()
|
||||
end
|
||||
else
|
||||
for i=1,#self.Parent.Mainloop do
|
||||
if self.Parent.Mainloop[i]==self then
|
||||
table.remove(self.Parent.Mainloop,i)
|
||||
break
|
||||
end
|
||||
end
|
||||
self.Active=false
|
||||
end
|
||||
end
|
||||
function multi:hold(task)
|
||||
self:Pause()
|
||||
if type(task)=="number" then
|
||||
local alarm=self.Parent:newAlarm(task)
|
||||
while alarm.Active==true do
|
||||
if love then
|
||||
self.Parent:lManager()
|
||||
else
|
||||
self.Parent:Do_Order()
|
||||
end
|
||||
end
|
||||
alarm:Destroy()
|
||||
self:Resume()
|
||||
elseif type(task)=="function" then
|
||||
local env=self.Parent:newEvent(task)
|
||||
env:OnEvent(function(envt) envt:Pause() envt:Stop() end)
|
||||
while env.Active do
|
||||
if love then
|
||||
self.Parent:lManager()
|
||||
else
|
||||
self.Parent:Do_Order()
|
||||
end
|
||||
end
|
||||
env:Destroy()
|
||||
self:Resume()
|
||||
else
|
||||
print("Error Data Type!!!")
|
||||
end
|
||||
end
|
||||
function multi:oneTime(func,...)
|
||||
if not(self.Type=="mainint" or self.Type=="int") then
|
||||
for _k=1,#self.Parent.Tasks2 do
|
||||
if self.Parent.Tasks2[_k]==func then
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(self.Parent.Tasks2,func)
|
||||
func(...)
|
||||
return true
|
||||
else
|
||||
for _k=1,#self.Tasks2 do
|
||||
if self.Tasks2[_k]==func then
|
||||
return false
|
||||
end
|
||||
end
|
||||
table.insert(self.Tasks2,func)
|
||||
func(...)
|
||||
return true
|
||||
end
|
||||
end
|
||||
function multi:Reset(n)
|
||||
self:Resume()
|
||||
end
|
||||
function multi:isDone()
|
||||
return self.Active~=true
|
||||
end
|
||||
--Constructors
|
||||
function multi:newEvent(task)
|
||||
local c={}
|
||||
if self.Type=="stack" then
|
||||
c=self:newBase(1)
|
||||
self.last=c
|
||||
else
|
||||
c=self:newBase()
|
||||
end
|
||||
c.Type="event"
|
||||
c.Task=task or function() end
|
||||
function c:Act()
|
||||
if self.Task(self) and self.Active==true then
|
||||
self:Pause()
|
||||
for _E=1,#self.func do
|
||||
self.func[_E](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnEvent(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
function c:tofile(path)
|
||||
local m=bin.new()
|
||||
m:addBlock(self.Type)
|
||||
m:addBlock(self.Task)
|
||||
m:addBlock(self.func)
|
||||
m:addBlock(self.Active)
|
||||
m:tofile(path)
|
||||
end
|
||||
if self.Type=="stack" then
|
||||
if #self.Mainloop>1 then
|
||||
c:Pause()
|
||||
end
|
||||
c:connectFinal(function(self)
|
||||
if self.Parent.last==self then
|
||||
for i=1,#self.Parent.funcE do
|
||||
self.Parent.funcE[i](self)
|
||||
end
|
||||
self.Parent:Remove()
|
||||
end
|
||||
self:Destroy()
|
||||
self.Parent.Mainloop[#self.Parent.Mainloop]:Resume()
|
||||
end)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newAlarm(set)
|
||||
local c={}
|
||||
if self.Type=="stack" then
|
||||
c=self:newBase(1)
|
||||
self.last=c
|
||||
else
|
||||
c=self:newBase()
|
||||
end
|
||||
c.Type="alarm"
|
||||
c.timer=self:newTimer()
|
||||
c.set=set or 0
|
||||
function c:tofile(path)
|
||||
local m=bin.new()
|
||||
m:addBlock(self.Type)
|
||||
m:addBlock(self.set)
|
||||
m:addBlock(self.Active)
|
||||
m:tofile(path)
|
||||
end
|
||||
function c:Act()
|
||||
if self.Active==true then
|
||||
if self.timer:Get()>=self.set then
|
||||
self:Pause()
|
||||
self.Active=false
|
||||
for i=1,#self.func do
|
||||
self.func[i](self)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:Resume()
|
||||
self.Parent.Resume(self)
|
||||
self.timer:Resume()
|
||||
end
|
||||
function c:Reset(n)
|
||||
if n then self.set=n end
|
||||
self:Resume()
|
||||
self.timer:Reset()
|
||||
end
|
||||
function c:OnRing(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
function c:Pause()
|
||||
self.timer:Pause()
|
||||
self.Parent.Pause(self)
|
||||
end
|
||||
if self.Type=="stack" then
|
||||
c:Pause()
|
||||
c:connectFinal(function(self)
|
||||
if self.Parent.last==self then
|
||||
for i=1,#self.Parent.funcE do
|
||||
self.Parent.funcE[i](self)
|
||||
end
|
||||
self.Parent:Remove()
|
||||
end
|
||||
table.remove(self.Parent.Mainloop,#self.Parent.Mainloop)
|
||||
self.Parent.Mainloop[#self.Parent.Mainloop]:Resume()
|
||||
end)
|
||||
else
|
||||
c.timer:Start()
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTimer()
|
||||
local c={}
|
||||
c.Type="timer"
|
||||
c.time=0
|
||||
c.count=0
|
||||
function c:Start()
|
||||
self.time=multi.clock()
|
||||
end
|
||||
function c:Get()
|
||||
return (multi.clock()-self.time)+self.count
|
||||
end
|
||||
c.Reset=c.Start
|
||||
function c:Pause()
|
||||
self.time=self:Get()
|
||||
end
|
||||
function c:Resume()
|
||||
self.time=multi.clock()-self.time
|
||||
end
|
||||
function c:tofile(path)
|
||||
local m=bin.new()
|
||||
self.count=self.count+self:Get()
|
||||
m:addBlock(self.Type)
|
||||
m:addBlock(self.count)
|
||||
m:tofile(path)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTask(func)
|
||||
table.insert(self.Tasks,func)
|
||||
end
|
||||
function multi:newLoop(func)
|
||||
local c={}
|
||||
if self.Type=="stack" then
|
||||
c=self:newBase(1)
|
||||
self.last=c
|
||||
else
|
||||
c=self:newBase()
|
||||
end
|
||||
c.Type="loop"
|
||||
c.Start=multi.clock()
|
||||
if func then
|
||||
c.func={func}
|
||||
end
|
||||
function c:tofile(path)
|
||||
local m=bin.new()
|
||||
m:addBlock(self.Type)
|
||||
m:addBlock(self.func)
|
||||
m:addBlock(self.Active)
|
||||
m:tofile(path)
|
||||
end
|
||||
function c:Act()
|
||||
if self.Active==true then
|
||||
for i=1,#self.func do
|
||||
self.func[i](multi.clock()-self.Start,self)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnLoop(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
if self.Type=="stack" then
|
||||
if #self.Mainloop>1 then
|
||||
c:Pause()
|
||||
end
|
||||
c:connectFinal(function(self)
|
||||
if self.Parent.last==self then
|
||||
for i=1,#self.Parent.funcE do
|
||||
self.Parent.funcE[i](self)
|
||||
end
|
||||
self.Parent:Remove()
|
||||
end
|
||||
self:Destroy()
|
||||
self.Parent.Mainloop[#self.Parent.Mainloop]:Resume()
|
||||
end)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newStep(start,reset,count,skip)
|
||||
local c={}
|
||||
if self.Type=="stack" then
|
||||
c=self:newBase(1)
|
||||
self.last=c
|
||||
else
|
||||
c=self:newBase()
|
||||
end
|
||||
think=1
|
||||
c.Type="step"
|
||||
c.pos=start or 1
|
||||
c.endAt=reset or math.huge
|
||||
c.skip=skip or 0
|
||||
c.spos=0
|
||||
c.count=count or 1*think
|
||||
c.funcE={}
|
||||
c.funcS={}
|
||||
c.start=start or 1
|
||||
if start~=nil and reset~=nil then
|
||||
if start>reset then
|
||||
think=-1
|
||||
end
|
||||
end
|
||||
function c:tofile(path)
|
||||
local m=bin.new()
|
||||
m:addBlock(self.Type)
|
||||
m:addBlock(self.func)
|
||||
m:addBlock(self.funcE)
|
||||
m:addBlock(self.funcS)
|
||||
m:addBlock({pos=self.pos,endAt=self.endAt,skip=self.skip,spos=self.spos,count=self.count,start=self.start})
|
||||
m:addBlock(self.Active)
|
||||
m:tofile(path)
|
||||
end
|
||||
function c:Act()
|
||||
if self~=nil then
|
||||
if self.spos==0 then
|
||||
if self.Active==true then
|
||||
if self.pos==self.start then
|
||||
for fe=1,#self.funcS do
|
||||
self.funcS[fe](self)
|
||||
end
|
||||
end
|
||||
for i=1,#self.func do
|
||||
self.func[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
if self.pos-self.count==self.endAt then
|
||||
self:Pause()
|
||||
for fe=1,#self.funcE do
|
||||
self.funcE[fe](self)
|
||||
end
|
||||
self.pos=self.start
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
self.spos=self.spos+1
|
||||
if self.spos>=self.skip then
|
||||
self.spos=0
|
||||
end
|
||||
end
|
||||
function c:OnStart(func)
|
||||
table.insert(self.funcS,func)
|
||||
end
|
||||
function c:OnStep(func)
|
||||
table.insert(self.func,1,func)
|
||||
end
|
||||
function c:OnEnd(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
function c:Break()
|
||||
self.Active=nil
|
||||
end
|
||||
function c:Update(start,reset,count,skip)
|
||||
self.start=start or self.start
|
||||
self.endAt=reset or self.endAt
|
||||
self.skip=skip or self.skip
|
||||
self.count=count or self.count
|
||||
self:Resume()
|
||||
end
|
||||
if self.Type=="stack" then
|
||||
if #self.Mainloop>1 then
|
||||
c:Pause()
|
||||
end
|
||||
c:connectFinal(function(self)
|
||||
if self.Parent.last==self then
|
||||
for i=1,#self.Parent.funcE do
|
||||
self.Parent.funcE[i](self)
|
||||
end
|
||||
self.Parent:Remove()
|
||||
end
|
||||
self:Destroy()
|
||||
self.Parent.Mainloop[#self.Parent.Mainloop]:Resume()
|
||||
end)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTStep(start,reset,count,set)
|
||||
local c={}
|
||||
if self.Type=="stack" then
|
||||
c=self:newBase(1)
|
||||
self.last=c
|
||||
else
|
||||
c=self:newBase()
|
||||
end
|
||||
think=1
|
||||
c.Type="tstep"
|
||||
c.start=start or 1
|
||||
local reset = reset or math.huge
|
||||
c.endAt=reset
|
||||
c.pos=start or 1
|
||||
c.skip=skip or 0
|
||||
c.count=count or 1*think
|
||||
c.funcE={}
|
||||
c.timer=multi.clock()
|
||||
c.set=set or 1
|
||||
c.funcS={}
|
||||
function c:Update(start,reset,count,set)
|
||||
self.start=start or self.start
|
||||
self.pos=start
|
||||
self.endAt=reset or self.endAt
|
||||
self.set=set or self.set
|
||||
self.count=count or self.count or 1
|
||||
self.timer=multi.clock()
|
||||
self:Resume()
|
||||
end
|
||||
function c:tofile(path)
|
||||
local m=bin.new()
|
||||
m:addBlock(self.Type)
|
||||
m:addBlock(self.func)
|
||||
m:addBlock(self.funcE)
|
||||
m:addBlock(self.funcS)
|
||||
m:addBlock({pos=self.pos,endAt=self.endAt,skip=self.skip,timer=self.timer,count=self.count,start=self.start,set=self.set})
|
||||
m:addBlock(self.Active)
|
||||
m:tofile(path)
|
||||
end
|
||||
function c:Act()
|
||||
if self.Active then
|
||||
if multi.clock()-self.timer>=self.set then
|
||||
self:Reset()
|
||||
if self.pos==self.start then
|
||||
for fe=1,#self.funcS do
|
||||
self.funcS[fe](self)
|
||||
end
|
||||
end
|
||||
for i=1,#self.func do
|
||||
self.func[i](self.pos,self)
|
||||
end
|
||||
self.pos=self.pos+self.count
|
||||
if self.pos-self.count==self.endAt then
|
||||
self:Pause()
|
||||
for fe=1,#self.funcE do
|
||||
self.funcE[fe](self)
|
||||
end
|
||||
self.pos=self.start
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:OnStart(func)
|
||||
table.insert(self.funcS,func)
|
||||
end
|
||||
function c:OnStep(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
function c:OnEnd(func)
|
||||
table.insert(self.funcE,func)
|
||||
end
|
||||
function c:Break()
|
||||
self.Active=nil
|
||||
end
|
||||
function c:Reset(n)
|
||||
if n then self.set=n end
|
||||
self.timer=multi.clock()
|
||||
self:Resume()
|
||||
end
|
||||
if self.Type=="stack" then
|
||||
if #self.Mainloop>1 then
|
||||
c:Pause()
|
||||
end
|
||||
c:connectFinal(function(self)
|
||||
if self.Parent.last==self then
|
||||
for i=1,#self.Parent.funcE do
|
||||
self.Parent.funcE[i](self)
|
||||
end
|
||||
self.Parent:Remove()
|
||||
end
|
||||
self:Destroy()
|
||||
self.Parent.Mainloop[#self.Parent.Mainloop]:Resume()
|
||||
end)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newTrigger(func)
|
||||
local c={}
|
||||
c.Type="trigger"
|
||||
c.trigfunc=func or function() end
|
||||
function c:Fire(...)
|
||||
self:trigfunc(self,...)
|
||||
end
|
||||
function c:tofile(path)
|
||||
local m=bin.new()
|
||||
m:addBlock(self.Type)
|
||||
m:addBlock(self.trigfunc)
|
||||
m:tofile(path)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newConnection()
|
||||
local c={}
|
||||
c.Type="connector"
|
||||
c.func={}
|
||||
function c:Fire(...)
|
||||
for i=1,#self.func do
|
||||
t,e=pcall(self.func[i],...)
|
||||
if not(t) then
|
||||
print(e)
|
||||
end
|
||||
end
|
||||
end
|
||||
function c:bind(t)
|
||||
self.func=t
|
||||
end
|
||||
function c:connect(func)
|
||||
table.insert(self.func,func)
|
||||
end
|
||||
function c:tofile(path)
|
||||
local m=bin.new()
|
||||
m:addBlock(self.Type)
|
||||
m:addBlock(self.func)
|
||||
m:tofile(path)
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:newJob(func,name)
|
||||
if not(self.Type=="mainint" or self.Type=="int") then error("Can only create an object on multi or an interface obj") return false end
|
||||
local c = {}
|
||||
if self.Type=="int" then
|
||||
setmetatable(c, self.Parent)
|
||||
else
|
||||
setmetatable(c, self)
|
||||
end
|
||||
c.Active=true
|
||||
c.func={}
|
||||
c.Id=0
|
||||
c.PId=0
|
||||
c.Parent=self
|
||||
c.Type="job"
|
||||
c.trigfunc=func or function() end
|
||||
function c:Act()
|
||||
self:trigfunc(self)
|
||||
end
|
||||
table.insert(self.Jobs,{c,name})
|
||||
if self.JobRunner==nil then
|
||||
self.JobRunner=self:newAlarm(self.jobUS)
|
||||
self.JobRunner:OnRing(function(self)
|
||||
if #self.Parent.Jobs>0 then
|
||||
if self.Parent.Jobs[1] then
|
||||
self.Parent.Jobs[1][1]:Act()
|
||||
table.remove(self.Parent.Jobs,1)
|
||||
end
|
||||
end
|
||||
self:Reset(self.Parent.jobUS)
|
||||
end)
|
||||
end
|
||||
end
|
||||
--Managers
|
||||
function multi:mainloop()
|
||||
for i=1,#self.Tasks do
|
||||
self.Tasks[i](self)
|
||||
end
|
||||
rawset(self,"Start",multi.clock())
|
||||
while self.Active do
|
||||
self:Do_Order()
|
||||
end
|
||||
end
|
||||
function multi._tFunc(self,dt)
|
||||
for i=1,#self.Tasks do
|
||||
self.Tasks[i](self)
|
||||
end
|
||||
if dt then
|
||||
self.pump=true
|
||||
end
|
||||
self.pumpvar=dt
|
||||
rawset(self,"Start",multi.clock())
|
||||
end
|
||||
function multi:uManager(dt)
|
||||
if self.Active then
|
||||
self:oneTime(self._tFunc,self,dt)
|
||||
self:Do_Order()
|
||||
end
|
||||
end
|
||||
1056
oldversions/MultiManager(0.6.2).lua
Normal file
1056
oldversions/MultiManager(0.6.2).lua
Normal file
File diff suppressed because it is too large
Load Diff
1112
oldversions/MultiManager(0.6.3).lua
Normal file
1112
oldversions/MultiManager(0.6.3).lua
Normal file
File diff suppressed because it is too large
Load Diff
1375
oldversions/MultiManager(1.0.0).lua
Normal file
1375
oldversions/MultiManager(1.0.0).lua
Normal file
File diff suppressed because it is too large
Load Diff
1391
oldversions/MultiManager(1.0.1).lua
Normal file
1391
oldversions/MultiManager(1.0.1).lua
Normal file
File diff suppressed because it is too large
Load Diff
1434
oldversions/MultiManager(1.2.0).lua
Normal file
1434
oldversions/MultiManager(1.2.0).lua
Normal file
File diff suppressed because it is too large
Load Diff
1460
oldversions/MultiManager(1.3.0).lua
Normal file
1460
oldversions/MultiManager(1.3.0).lua
Normal file
File diff suppressed because it is too large
Load Diff
1518
oldversions/MultiManager(1.4.1).lua
Normal file
1518
oldversions/MultiManager(1.4.1).lua
Normal file
File diff suppressed because it is too large
Load Diff
30
rockspecs/multi-1.8-6.rockspec
Normal file
30
rockspecs/multi-1.8-6.rockspec
Normal file
@ -0,0 +1,30 @@
|
||||
package = "multi"
|
||||
version = "1.8-6"
|
||||
source = {
|
||||
url = "git://github.com/rayaman/multi.git",
|
||||
tag = "v1.8.6",
|
||||
}
|
||||
description = {
|
||||
summary = "Lua Multi tasking library",
|
||||
detailed = [[
|
||||
This library contains many methods for multi tasking. From simple side by side code using multiobjs, to using coroutine based Threads and System threads(When you have lua lanes installed or are using love2d. Optional) The core of the library works on lua 5.1+ however the systemthreading features are limited to 5.1 due to love2d and lua lanes being lua 5.1 only!
|
||||
]],
|
||||
homepage = "https://github.com/rayaman/multi",
|
||||
license = "MIT"
|
||||
}
|
||||
dependencies = {
|
||||
"lua >= 5.1, < 5.2"
|
||||
}
|
||||
build = {
|
||||
type = "builtin",
|
||||
modules = {
|
||||
-- Note the required Lua syntax when listing submodules as keys
|
||||
["multi.init"] = "multi/init.lua",
|
||||
["multi.all"] = "multi/all.lua",
|
||||
["multi.compat.backwards[1,5,0]"] = "multi/compat/backwards[1,5,0].lua",
|
||||
["multi.compat.love2d"] = "multi/compat/love2d.lua",
|
||||
["multi.integration.lanesManager"] = "multi/integration/lanesManager.lua",
|
||||
["multi.integration.loveManager"] = "multi/integration/loveManager.lua",
|
||||
["multi.integration.shared"] = "multi/integration/shared.lua"
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user