mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2025-12-28 12:05:21 +09:00
Spell Check and Lua Check
This commit is contained in:
@@ -17,7 +17,7 @@ local module_verbose = false --true|false
|
||||
-- @field tick an index for threads which will run every tick (contains uuids)
|
||||
-- @field timeout an index for threads which will timeout (contains uuids)
|
||||
-- @field events an index of threads based on event ids (contains uuids)
|
||||
-- @field paused an index of pasued threads (contains uuids)
|
||||
-- @field paused an index of paused threads (contains uuids)
|
||||
-- @field named a name index for thread uuids
|
||||
-- @field print_to contains players that event details will be printed to
|
||||
-- @field uuid contains the random number generator for the uuid system
|
||||
@@ -39,7 +39,7 @@ local global = global{
|
||||
-- @treturn string the new uuid
|
||||
Server.uuid = add_metatable({},function()
|
||||
-- when it is called as a function
|
||||
local uuid = 0
|
||||
local uuid
|
||||
if game then
|
||||
global.uuid=global.uuid+1
|
||||
uuid=global.uuid+pre_load_uuid
|
||||
@@ -68,8 +68,8 @@ Server.threads = setmetatable({},{
|
||||
__newindex=function(tbl,key,value) rawset(global.all,key,value) end,
|
||||
__pairs=function(tbl)
|
||||
local tbl = global.all
|
||||
local function next_pair(tbl,k)
|
||||
k, v = next(tbl, k)
|
||||
local function next_pair(tbl,key)
|
||||
local k, v = next(tbl, key)
|
||||
if type(v) ~= nil and k ~= '_n' then return k,v
|
||||
else return next(tbl, k) end
|
||||
end
|
||||
@@ -79,15 +79,15 @@ Server.threads = setmetatable({},{
|
||||
|
||||
--- Generates a new thread object
|
||||
-- @usage Server.new_thread{name='foo',data={}}
|
||||
-- @tparam table obj the atributes to give to the thread
|
||||
-- @tparam table obj the attributes to give to the thread
|
||||
-- @treturn Server._thread the new thread created
|
||||
function Server.new_thread(obj) return Server._thread:create(obj) end
|
||||
|
||||
--- Used to get a thread via uuid or name (if one is assied)
|
||||
--- Used to get a thread via uuid or name (if one is assigned)
|
||||
-- @usage Server.get_thread('decon') -- return thread
|
||||
-- @param mixed either a uuid or the name given to a thread
|
||||
-- @treturn[1] Server._thread the thread by that name or uuid
|
||||
-- @treturn[2] boolean if false is returned then no thread existes
|
||||
-- @treturn[2] boolean if false is returned then no thread exists
|
||||
function Server.get_thread(mixed)
|
||||
local threads = global
|
||||
if threads.named[mixed] then return threads.all[threads.named[mixed]]
|
||||
@@ -99,7 +99,7 @@ end
|
||||
--- Adds a thread into the resolve queue, can be used to lower lag
|
||||
-- @usage Server.queue_thread(thread) -- return true/false
|
||||
-- @tparam Server._thread thread_to_queue the thread to be added to the queue, must be open and have a on_resolve function
|
||||
-- @treturn boolean was it added successfuly
|
||||
-- @treturn boolean was it added successfully
|
||||
function Server.queue_thread(thread_to_queue)
|
||||
if not thread_to_queue and not thread_to_queue.valid and not thread_to_queue:valid() then return false end
|
||||
if not thread_to_queue._resolve then return false end
|
||||
@@ -109,15 +109,15 @@ end
|
||||
|
||||
--- Closes all active threads, can use force if it causes errors
|
||||
-- @usage Server.close_all_threads() -- asks all threads to close
|
||||
-- @usage Server.close_all_threads(true) -- forcefuly close all threads
|
||||
-- @tparam bolean with_force use force when closing
|
||||
-- @usage Server.close_all_threads(true) -- forcefully close all threads
|
||||
-- @tparam boolean with_force use force when closing
|
||||
function Server.close_all_threads(with_force)
|
||||
if not with_force then
|
||||
for uuid,thread in pairs(Server.threads) do thread:close() end
|
||||
else global.Server(true) end -- idk why you cant just use global even when global is defined at the top to be over ridren
|
||||
else global.Server(true) end -- idk why you cant just use global even when global is defined at the top to be overridden
|
||||
end
|
||||
|
||||
--- Runs all the theads which have opened with an on_tick event
|
||||
--- Runs all the threads which have opened with an on_tick event
|
||||
-- @usage Server.run_tick_threads()
|
||||
function Server.run_tick_threads()
|
||||
table.each(global.tick,function(uuid)
|
||||
@@ -146,8 +146,8 @@ end
|
||||
-- @tparam ?name|index event the event that info will be returned fo
|
||||
-- @tparam[opt=toggle] boolean state will info be returned, nil to toggle current state
|
||||
function Server._thread_debuger(player,event,state)
|
||||
local player = Game.get_player(player)
|
||||
local event = tonumber(event) or Manager.event.names[event]
|
||||
player = Game.get_player(player)
|
||||
event = tonumber(event) or Manager.event.names[event]
|
||||
local print_to = global.print_to
|
||||
print_to[player.index] = print_to[player.index] or {}
|
||||
if state then print_to[player.index][event] = state
|
||||
@@ -155,12 +155,12 @@ function Server._thread_debuger(player,event,state)
|
||||
else print_to[player.index][event] = true end
|
||||
end
|
||||
|
||||
--- Calles all threads on a certain game event (used with script.on_event)
|
||||
--- Calls all threads on a certain game event (used with script.on_event)
|
||||
-- @local Server._thread_handler
|
||||
-- @usage script.on_event(defines.events,Server._thread_handler) -- adds this handler
|
||||
-- @tparam table event the event that is called
|
||||
function Server._thread_handler(event)
|
||||
-- returns to players who have set _thread_debuger to trye
|
||||
-- returns to players who have set _thread_debuger to true
|
||||
table.each(global.print_to,function(print_to,player_index,event)
|
||||
if event.name == defines.events.on_tick then return true end
|
||||
if print_to[event.name] then
|
||||
@@ -189,7 +189,7 @@ end
|
||||
--- Adds a event handler to tell threads about events
|
||||
-- @usage Server.add_thread_handler(defines.event)
|
||||
-- @tparam number event the event to run the thread handler on`
|
||||
-- @treturn bolean if the handler was added
|
||||
-- @treturn boolean if the handler was added
|
||||
function Server.add_thread_handler(event)
|
||||
if not is_type(event,'number') then return false end
|
||||
local threads = global
|
||||
@@ -204,9 +204,9 @@ end
|
||||
|
||||
--- Acts as a bypass for running functions, can accept a string
|
||||
-- @usage Server.interface('local x = 1+1 print(x) return x') -- return 2
|
||||
-- @usage Server.interface('local x = 1+1 print(x)',true) -- will creat a thread to run as root (this is the bypass)
|
||||
-- @usage Server.interface('local x = 1+1 print(x)',true) -- will create a thread to run as root (this is the bypass)
|
||||
-- @tparam ?string|function callback function to be ran
|
||||
-- @tparam[opt] ?Server._thread|true use_thread run the command on a premade thread or let it make its own
|
||||
-- @tparam[opt] ?Server._thread|true use_thread run the command on a pre-made thread or let it make its own
|
||||
-- @tparam[opt] table env run the env to run the command in must have _env key as true to be
|
||||
-- @param[opt] ... any args you want to pass to the function
|
||||
-- @treturn[1] boolean if no thread then it will return a true for the success
|
||||
@@ -219,15 +219,15 @@ function Server.interface(callback,use_thread,env,...)
|
||||
if use_thread == true then use_thread = Server.new_thread{data={callback,env,...}} end
|
||||
-- creates the resolve function for the thread
|
||||
use_thread:on_event('resolve',function(thread)
|
||||
local callback = table.remove(thread.data,1)
|
||||
callback = is_type(callback,'function') and callback or loadstring(callback)
|
||||
local env = table.remove(thread.data,1)
|
||||
if is_type(env,'table') and not is_type(env.__self,'userdata') and env._env == true then
|
||||
local success, err = Manager.sandbox(callback,env,unpack(thread.data))
|
||||
local thread_callback = table.remove(thread.data,1)
|
||||
thread_callback = is_type(thread_callback,'function') and thread_callback or loadstring(thread_callback)
|
||||
local thread_env = table.remove(thread.data,1)
|
||||
if is_type(thread_env,'table') and not is_type(thread_env.__self,'userdata') and thread_env._env == true then
|
||||
local success, err = Manager.sandbox(thread_callback,thread_env,unpack(thread.data))
|
||||
if not success then error(err) end
|
||||
return err
|
||||
else
|
||||
local success, err = Manager.sandbox(callback,{},env,unpack(thread.data))
|
||||
local success, err = Manager.sandbox(thread_callback,{},env,unpack(thread.data))
|
||||
if not success then error(err) end
|
||||
return err
|
||||
end
|
||||
@@ -254,13 +254,13 @@ function Server.interface(callback,use_thread,env,...)
|
||||
end
|
||||
end
|
||||
|
||||
--- The class for the server threads, allows abbilty to run async function
|
||||
--- The class for the server threads, allows ability to run async function
|
||||
-- @type Thread
|
||||
-- @alias Server._thread
|
||||
-- @field name the name that is given to the thread, use for easy later indexing
|
||||
-- @field timeout the time in ticks that the thread will have before it times out
|
||||
-- @field reopen when true the thread will reopen itself untill set to false, combine with timeout to create a long on_nth_tick wait
|
||||
-- @field data any data that the thread will beable to access
|
||||
-- @field reopen when true the thread will reopen itself until set to false, combine with timeout to create a long on_nth_tick wait
|
||||
-- @field data any data that the thread will be able to access
|
||||
Server._thread = {}
|
||||
|
||||
local _env_metatable = {
|
||||
@@ -271,15 +271,15 @@ local _env_metatable = {
|
||||
-- @usage new_thread = thread:create()
|
||||
-- @tparam[opt={}] table obj all values are opt {timeout=int,name=str,data=any}
|
||||
-- @treturn Server._thread the new thread object
|
||||
function Server._thread:create(obj)
|
||||
local obj = obj or {}
|
||||
function Server._thread.create(obj)
|
||||
obj = obj or {}
|
||||
setmetatable(obj,{__index=Server._thread})
|
||||
obj.uuid = tostring(Server.uuid)
|
||||
-- creates a varible containg all upvalus for the thread (stops desyncs)
|
||||
-- creates a variable containing all upvalus for the thread (stops desyncs)
|
||||
obj._env = get_upvalues(2)
|
||||
obj._env._env = true
|
||||
obj._env._ENV = nil -- provents infinte recusion
|
||||
-- removes any modules from the _env to save space in global (less time to serizle)
|
||||
obj._env._ENV = nil -- prevents infinite recursion
|
||||
-- removes any modules from the _env to save space in global (less time to serialize)
|
||||
obj._env._modules = {}
|
||||
for name,value in pairs(obj._env) do if is_type(value,'table') and value._moduleName and loaded_modules[value._moduleName] == value then obj._env._modules[name] = value._moduleName obj._env[name] = nil end end
|
||||
setmetatable(obj._env,_env_metatable)
|
||||
@@ -288,21 +288,21 @@ function Server._thread:create(obj)
|
||||
return obj
|
||||
end
|
||||
|
||||
--- Opens and queses a thread
|
||||
--- Opens and queues a thread
|
||||
-- @usage Server._thread:queue() -- returns true/false
|
||||
-- @treturn boolean was the thread queued successfuly
|
||||
-- @treturn boolean was the thread queued successfully
|
||||
-- @see Server.queue_thread
|
||||
function Server._thread:queue()
|
||||
self:open()
|
||||
return Server.queue_thread(self)
|
||||
end
|
||||
|
||||
--- Test if the thread has all requied parts
|
||||
--- Test if the thread has all required parts
|
||||
-- @usage if thread:valid() then end -- basic test for valid
|
||||
-- @tparam[opt=false] bolean skip_location_check true to skip the location checking
|
||||
-- @tparam[opt=false] boolean skip_location_check true to skip the location checking
|
||||
-- @treturn boolean is the thread valid
|
||||
function Server._thread:valid(skip_location_check)
|
||||
local skip_location_check = skip_location_check or false
|
||||
skip_location_check = skip_location_check or false
|
||||
if is_type(self.uuid,'string') and
|
||||
skip_location_check or is_type(self.opened,'number') and
|
||||
skip_location_check or is_type(global.all[self.uuid],'table') and
|
||||
@@ -314,34 +314,34 @@ function Server._thread:valid(skip_location_check)
|
||||
is_type(self._resolve) or is_type(self._resolve,'function') and
|
||||
is_type(self._success) or is_type(self._success,'function') and
|
||||
is_type(self._error) or is_type(self._error,'function') then
|
||||
-- all above must be true to be vaild, must accept nil and function
|
||||
-- all above must be true to be valid, must accept nil and function
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
--- Opens the thread; indexs this thread in the global index
|
||||
--- Opens the thread; indexes this thread in the global index
|
||||
-- @usage thread:open() -- return true
|
||||
-- @treturn bolean if the thread was opened successfuly
|
||||
-- @treturn boolean if the thread was opened successfully
|
||||
function Server._thread:open()
|
||||
-- if the thread is valid and not already opended
|
||||
-- if the thread is valid and not already opened
|
||||
if not self:valid(true) or self.opened then return false end
|
||||
local uuid = self.uuid
|
||||
-- sets the thread to open, this is the tick it was opened
|
||||
self.opened = game.tick
|
||||
-- creats the global index
|
||||
-- creates the global index
|
||||
global.all[uuid] = global.all[uuid] or self
|
||||
global.all._n = global.all._n+1
|
||||
-- indexs the thread in other places if it has more function
|
||||
-- indexes the thread in other places if it has more function
|
||||
-- if it was paused before (ie did not run any events) then the index is removed from the paused index
|
||||
if global.paused[self.name] then global.paused[self.name] = nil end
|
||||
-- if it has a timeout or on_tick handler then it is indexed in those indexs
|
||||
-- if it has a timeout or on_tick handler then it is indexed in those indexes
|
||||
if is_type(self.timeout,'number') then table.insert(global.timeout,uuid) end
|
||||
if is_type(self._tick,'function') then table.insert(global.tick,uuid) end
|
||||
-- if the thread is given a name then a index from the name to uuid is made
|
||||
if is_type(self.name,'string') then global.named[self.name] = global.named[self.name] or self.uuid end
|
||||
-- if there are event handlers then it will loop over them and add them to the event index
|
||||
if is_type(self._events,'table') then
|
||||
if is_type(self._events,'table') then
|
||||
table.each(self._events,function(callback,event,global,uuid)
|
||||
-- cant be used V
|
||||
--Server.add_thread_handler(event)
|
||||
@@ -354,7 +354,7 @@ function Server._thread:open()
|
||||
return true
|
||||
end
|
||||
|
||||
--- Inverse of thread:open() - Removes all indexs to this thread, most cases this will cause it to become inassible
|
||||
--- Inverse of thread:open() - Removes all indexes to this thread, most cases this will cause it to become inaccessible
|
||||
-- @usage thread:close() -- return true
|
||||
-- @treturn boolean if the thread had a on_close function
|
||||
function Server._thread:close()
|
||||
@@ -363,17 +363,17 @@ function Server._thread:close()
|
||||
-- if there is a call to the threads on close event, will later return true
|
||||
if is_type(self._close,'function') then Manager.sandbox(self._close,self._env,self) _return = true end
|
||||
-- will search every possible location for this thread and remove it
|
||||
local value,key = table.find(global.queue,function(v,k,uuid) return v == uuid end,uuid)
|
||||
local _,key = table.find(global.queue,function(v,k,uuid) return v == uuid end,uuid)
|
||||
if key then table.remove(global.queue,key) end -- queue
|
||||
local value,key = table.find(global.timeout,function(v,k,uuid) return v == uuid end,uuid)
|
||||
_,key = table.find(global.timeout,function(v,k,uuid) return v == uuid end,uuid)
|
||||
if key then table.remove(global.timeout,key) end -- timeouts
|
||||
local value,key = table.find(global.tick,function(v,k,uuid) return v == uuid end,uuid)
|
||||
_,key = table.find(global.tick,function(v,k,uuid) return v == uuid end,uuid)
|
||||
if key then table.remove(global.tick,key) end -- on_tick
|
||||
-- then will look for it in the event handlers and remove it if found
|
||||
if is_type(self._events,'table') then
|
||||
table.each(self._events,function(callback,event)
|
||||
if global.events[event] then
|
||||
local value,key = table.find(global.events[event],function(v,k,uuid) return v == uuid end,uuid)
|
||||
local _,key = table.find(global.events[event],function(v,k,uuid) return v == uuid end,uuid)
|
||||
if key then table.remove(global.events[event],key) end
|
||||
-- cant be used V
|
||||
--if #global.events[event] == 0 then Event.remove(event,Server.game_event) global.events[event] = nil end
|
||||
@@ -382,9 +382,9 @@ function Server._thread:close()
|
||||
end
|
||||
-- sets the thread to closed
|
||||
self.opened=nil
|
||||
-- unless the thread has a name or is assied to be reopened
|
||||
-- unless the thread has a name or is assigned to be reopened
|
||||
if self.reopen == true then self:open() else
|
||||
-- if it has a name but not assied to reopen then it will become 'pasued'
|
||||
-- if it has a name but not assigned to reopen then it will become 'paused'
|
||||
if is_type(self.name,'string') then global.paused[self.name]=self.uuid
|
||||
-- else it will just be wiped from the global index
|
||||
else global.all[uuid] = nil global.all._n = global.all._n-1 end
|
||||
@@ -397,19 +397,19 @@ end
|
||||
--- Trigger the on_resolve function and closes the thread - error and success called based on result of pcall (useful for async)
|
||||
-- @usage thread:resolve(x,y,z) -- return true
|
||||
-- @param[opt] ... any arguments you want to pass to the resolve function
|
||||
-- @treturn bolean true if the thread called on_success or on_error
|
||||
-- @treturn boolean true if the thread called on_success or on_error
|
||||
function Server._thread:resolve(...)
|
||||
local _return = false
|
||||
-- checks if the resolve haddler is still present
|
||||
if is_type(self._resolve,'function') then
|
||||
-- checks if the resolve handler is still present
|
||||
if is_type(self._resolve,'function') then
|
||||
local success, err = Manager.sandbox(self._resolve,self._env,self,...)
|
||||
if success then
|
||||
-- if it was successful then it will attemp to call the success handler
|
||||
-- if it was successful then it will attempt to call the success handler
|
||||
if is_type(self._success,'function') then
|
||||
-- interface is used as a way to delay the success call till the next tick
|
||||
Server.interface(function(thread,err)
|
||||
local success, err = Manager.sandbox(thread._success,thread._env,thread,err)
|
||||
if not success then thread:error(err) end
|
||||
local _success, _err = Manager.sandbox(thread._success,thread._env,thread,err)
|
||||
if not _success then thread:error(_err) end
|
||||
end,true,self,err)
|
||||
-- later returns true if there was a call to the success handler
|
||||
_return = true
|
||||
@@ -422,9 +422,9 @@ function Server._thread:resolve(...)
|
||||
return _return
|
||||
end
|
||||
|
||||
--- Checks the timeout on a thread - if timed out then it calles on_timeout and closes
|
||||
--- Checks the timeout on a thread - if timed out then it calls on_timeout and closes
|
||||
-- @usage thread:check_timeout() -- return true
|
||||
-- @treturn bolean if the thread timed out
|
||||
-- @treturn boolean if the thread timed out
|
||||
function Server._thread:check_timeout()
|
||||
local _return = false
|
||||
-- makes sure the thread is still valid
|
||||
@@ -436,7 +436,7 @@ function Server._thread:check_timeout()
|
||||
Manager.sandbox(self._timeout,self._env,self)
|
||||
end
|
||||
_return = true
|
||||
-- closes the thread to provent any further event calls
|
||||
-- closes the thread to prevent any further event calls
|
||||
self:close()
|
||||
end
|
||||
return _return
|
||||
@@ -444,7 +444,7 @@ end
|
||||
|
||||
--- Used to check and raise the error handler of the thread, if not present it raises an error
|
||||
-- @usage thread:error(err) -- return true
|
||||
-- @tparam string err the err to be rasied
|
||||
-- @tparam string err the err to be raised
|
||||
-- @treturn boolean did the thread have an error handler
|
||||
function Server._thread:error(err)
|
||||
local _return = false
|
||||
@@ -453,12 +453,12 @@ function Server._thread:error(err)
|
||||
_return = true
|
||||
else
|
||||
self:close() -- no matter what happens next this thread will be closed
|
||||
error('Thread Error (no handler): '..err)
|
||||
error('Thread Error (no handler): '..err)
|
||||
end
|
||||
return _return
|
||||
end
|
||||
|
||||
--- Set function to run then an event is triggered, none of them are 'needed' but you are advised to have atleast one
|
||||
--- Set function to run then an event is triggered, none of them are 'needed' but you are advised to have at least one
|
||||
-- @usage thread:on_event('close',function) -- if event is not one below then a game event is used
|
||||
-- @usage thread_only_events = ['close','timeout','tick','resolve','success','error']
|
||||
-- @tparam ?string|index event the name of the event that the function should be called on
|
||||
@@ -466,7 +466,7 @@ end
|
||||
-- @treturn table returns self so that they can be chained together
|
||||
function Server._thread:on_event(event,callback)
|
||||
local events = {'close','timeout','tick','resolve','success','error'}
|
||||
-- seaches the above list for the event
|
||||
-- searches the above list for the event
|
||||
local value = table.find(events,function(v,k,find) return v == string.lower(find) end,event)
|
||||
if value and is_type(callback,'function') then
|
||||
-- if it is a thread_only_event then it will add it to its core values
|
||||
@@ -492,7 +492,7 @@ script.on_event(defines.events.on_tick,function(event)
|
||||
end)
|
||||
|
||||
script.on_load(function(event)
|
||||
-- sets up metatable again so that threads contiune to work
|
||||
-- sets up metatable again so that threads continue to work
|
||||
for uuid,thread in pairs(Server.threads) do
|
||||
setmetatable(thread,{__index=Server._thread})
|
||||
setmetatable(thread._env,_env_metatable)
|
||||
@@ -535,7 +535,7 @@ return Server
|
||||
end)
|
||||
thread:open()
|
||||
|
||||
all on_event functions can be chained from the thread creation rather than use varibles eg:
|
||||
all on_event functions can be chained from the thread creation rather than use variables eg:
|
||||
Server.new_thread{
|
||||
name='tree-decon',
|
||||
data={}
|
||||
|
||||
Reference in New Issue
Block a user