2017-10-09 22:53:37 +08:00
|
|
|
local component=require("component")
|
2018-02-13 11:59:23 +08:00
|
|
|
local serialization = require("serialization")
|
2018-03-10 02:16:49 +08:00
|
|
|
local filesystem=require("filesystem")
|
|
|
|
|
|
|
|
function getWorldTimestamp()
|
|
|
|
local name=os.tmpname()
|
|
|
|
local f=io.open(name,"w")
|
|
|
|
f:close()
|
|
|
|
local ts=filesystem.lastModified(name)
|
|
|
|
filesystem.remove(name)
|
|
|
|
return ts
|
|
|
|
end
|
|
|
|
|
|
|
|
function getWorldTimeInfo(gmt)
|
|
|
|
if(gmt==nil) then gmt=0 end
|
|
|
|
local x=getWorldTimestamp()+gmt*3600*1000
|
|
|
|
local t=os.date("*t",math.ceil(x/1000))
|
|
|
|
t.msec=x%1000
|
|
|
|
t.unix=math.ceil(x/1000)
|
|
|
|
t.stamp=x
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
function getWorldDate(gmt)
|
|
|
|
local t=getWorldTimeInfo(gmt)
|
|
|
|
return t.year,t.month,t.day,t.hour,t.min,t.sec,t.msec
|
|
|
|
end
|
2018-02-13 11:59:23 +08:00
|
|
|
|
|
|
|
function serialize(value)
|
|
|
|
return serialization.serialize(value)
|
|
|
|
end
|
|
|
|
|
|
|
|
function unserialize(str)
|
|
|
|
return serialization.unserialize(str)
|
|
|
|
end
|
2017-10-09 22:53:37 +08:00
|
|
|
|
|
|
|
function getTableSize(t)
|
|
|
|
local cnt=0
|
|
|
|
for k in pairs(t) do
|
|
|
|
cnt=cnt+1
|
|
|
|
end
|
|
|
|
return cnt
|
|
|
|
end
|
|
|
|
|
|
|
|
function isTableEmpty(t)
|
|
|
|
return getTableSize(t) == 0
|
|
|
|
end
|
|
|
|
|
|
|
|
function rawproxy(id)
|
|
|
|
return component.proxy(id)
|
|
|
|
end
|
|
|
|
|
|
|
|
function proxy(componentType,beginWith)
|
|
|
|
local t=component.list(componentType)
|
|
|
|
local sz=getTableSize(t)
|
|
|
|
if(sz==0) then
|
2018-02-13 11:59:23 +08:00
|
|
|
print("proxy: Query List is Empty")
|
2017-10-09 22:53:37 +08:00
|
|
|
return nil
|
|
|
|
elseif(sz==1) then
|
|
|
|
for k in pairs(t) do
|
|
|
|
return rawproxy(k)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if(beginWith == nil) then
|
2018-02-13 11:59:23 +08:00
|
|
|
print("proxy: beginWith value required.")
|
2017-10-09 22:53:37 +08:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
if(type(beginWith) ~= "string") then
|
2018-02-13 11:59:23 +08:00
|
|
|
print("proxy: beginWith is not string")
|
2017-10-09 22:53:37 +08:00
|
|
|
return nil
|
|
|
|
end
|
|
|
|
local bsz=string.len(beginWith)
|
2017-10-25 14:37:45 +08:00
|
|
|
local traw
|
|
|
|
local cnt=0
|
2017-10-09 22:53:37 +08:00
|
|
|
for k in pairs(t) do
|
|
|
|
if(string.sub(k,1,bsz) == beginWith) then
|
2017-10-25 14:37:45 +08:00
|
|
|
if(cnt==0) then
|
|
|
|
traw=rawproxy(k)
|
|
|
|
cnt=1
|
|
|
|
else
|
2018-02-13 11:59:23 +08:00
|
|
|
print("proxy: Found more than 1 target.")
|
2017-10-25 14:37:45 +08:00
|
|
|
return nil
|
|
|
|
end
|
2017-10-09 22:53:37 +08:00
|
|
|
end
|
|
|
|
end
|
2017-10-25 14:37:45 +08:00
|
|
|
|
|
|
|
if(cnt==0) then
|
2018-02-13 11:59:23 +08:00
|
|
|
print("proxy: Not found with beginWith value")
|
2017-10-25 14:37:45 +08:00
|
|
|
return nil
|
|
|
|
else
|
|
|
|
return traw
|
|
|
|
end
|
2017-10-09 22:53:37 +08:00
|
|
|
end
|
|
|
|
end
|