Better libevent

This commit is contained in:
Kirigaya Kazuto 2019-01-10 10:45:27 +08:00
parent ccf2d31831
commit 65c6a5a0f5

View File

@ -15,269 +15,299 @@ local function doInternalEventInit()
-- tb is a reference to event table. -- tb is a reference to event table.
local tb=internal_evtb local tb=internal_evtb
tb["component_added"]=function(raw_event,t) tb["component_added"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["address"]=raw_event[2] t["address"]=e[2]
t["componentType"]=raw_event[3] t["type"]=e[3]
t["componentType"]=e[3]
end end
tb["component_removed"]=tb["component_added"] tb["component_removed"]=tb["component_added"]
tb["component_available"]=function(raw_event,t) tb["component_available"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["componentType"]=raw_event[2] t["type"]=e[2]
t["componentType"]=e[2]
end end
tb["component_unavailable"]=tb["component_available"] tb["component_unavailable"]=tb["component_available"]
tb["term_available"]=function(raw_event,t) tb["term_available"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
end end
tb["term_unavailable"]=tb["term_available"] tb["term_unavailable"]=tb["term_available"]
tb["screen_resized"]=function(raw_event,t) tb["screen_resized"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["screenAddress"]=raw_event[2] t["address"]=e[2]
t["newWidth"]=raw_event[3] t["width"]=e[3]
t["newHeight"]=raw_event[4] t["height"]=e[4]
t["screenAddress"]=e[2]
t["newWidth"]=e[3]
t["newHeight"]=e[4]
end end
tb["touch"]=function(raw_event,t) tb["touch"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["screenAddress"]=raw_event[2] t["address"]=e[2]
t["x"]=raw_event[3] t["x"]=e[3]
t["y"]=raw_event[4] t["y"]=e[4]
t["button"]=raw_event[5] t["button"]=e[5]
t["playerName"]=raw_event[6] t["player"]=e[6]
t["screenAddress"]=e[2]
t["playerName"]=e[6]
end end
tb["drag"]=tb["touch"] tb["drag"]=tb["touch"]
tb["drop"]=tb["touch"] tb["drop"]=tb["touch"]
tb["scroll"]=function(raw_event,t) tb["scroll"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["screenAddress"]=raw_event[2] t["address"]=e[2]
t["x"]=raw_event[3] t["x"]=e[3]
t["y"]=raw_event[4] t["y"]=e[4]
t["direction"]=raw_event[5] t["direction"]=e[5]
t["playerName"]=raw_event[6] t["player"]=e[6]
t["screenAddress"]=e[2]
t["playerName"]=e[6]
end end
tb["walk"]=function(raw_event,t) tb["walk"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["screenAddress"]=raw_event[2] t["address"]=e[2]
t["x"]=raw_event[3] t["x"]=e[3]
t["y"]=raw_event[4] t["y"]=e[4]
t["playerName"]=raw_event[5] t["player"]=e[5]
t["screenAddress"]=e[2]
t["playerName"]=e[5]
end end
tb["key_down"]=function(raw_event,t) tb["key_down"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["keyboardAddress"]=raw_event[2] t["address"]=e[2]
t["char"]=raw_event[3] t["char"]=e[3]
t["code"]=raw_event[4] t["code"]=e[4]
t["playerName"]=raw_event[5] t["player"]=e[5]
t["keyboardAddress"]=e[2]
t["playerName"]=e[5]
end end
tb["key_up"]=tb["key_down"] tb["key_up"]=tb["key_down"]
tb["clipboard"]=function(raw_event,t) tb["clipboard"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["keyboardAddress"]=raw_event[2] t["address"]=e[2]
t["value"]=raw_event[3] t["value"]=e[3]
t["playerName"]=raw_event[4] t["player"]=e[4]
t["keyboardAddress"]=e[2]
t["playerName"]=e[4]
end end
tb["redstone_changed"]=function(raw_event,t) tb["redstone_changed"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["address"]=raw_event[2] t["address"]=e[2]
t["side"]=raw_event[3] t["side"]=e[3]
t["oldValue"]=raw_event[4] t["oldValue"]=e[4]
t["newValue"]=raw_event[5] t["newValue"]=e[5]
end end
tb["motion"]=function(raw_event,t) tb["motion"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["address"]=raw_event[2] t["address"]=e[2]
t["relativeX"]=raw_event[3] t["x"]=e[3]
t["relativeY"]=raw_event[4] t["y"]=e[4]
t["relativeZ"]=raw_event[5] t["z"]=e[5]
t["entityName"]=raw_event[6] t["entityName"]=e[6]
t["relativeX"]=e[3]
t["relativeY"]=e[4]
t["relativeZ"]=e[5]
end end
tb["modem_message"]=function(raw_event,t) --- Special tb["modem_message"]=function(e,t) --- Special
t["event"]=raw_event[1] t["event"]=e[1]
t["receiverAddress"]=raw_event[2] t["receiver"]=e[2]
t["senderAddress"]=raw_event[3] t["sender"]=e[3]
t["port"]=raw_event[4] t["port"]=e[4]
t["distance"]=raw_event[5] t["distance"]=e[5]
t["data"]={} t["data"]={}
for i=6,raw_event.n,1 do for i=6,e.n,1 do
t["data"][i-5]=raw_event[i] t["data"][i-5]=e[i]
end end
t["data"].n=raw_event.n-5 t["data"].n=e.n-5
t["receiverAddress"]=e[2]
t["senderAddress"]=e[3]
end end
tb["inventory_changed"]=function(raw_event,t) tb["inventory_changed"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["slot"]=raw_event[2] t["slot"]=e[2]
end end
tb["bus_message"]=function(raw_event,t) --- Can data or metadata be table? tb["bus_message"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["protocolId"]=raw_event[2] t["protocolId"]=e[2]
t["senderAddress"]=raw_event[3] t["senderAddress"]=e[3]
t["targetAddress"]=raw_event[4] t["targetAddress"]=e[4]
t["data"]=raw_event[5] t["data"]=e[5]
t["metadata"]=raw_event[6] t["metadata"]=e[6]
end end
tb["interrupted"]=function(raw_event,t) --- Should soft interrupt be a special event? tb["interrupted"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["uptime"]=raw_event[2] t["uptime"]=e[2]
end end
--- Computronics --- Computronics
tb["minecart"]=function(raw_event,t) tb["minecart"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["detectorAddress"]=raw_event[2] t["detectorAddress"]=e[2]
t["minecartType"]=raw_event[3] t["minecartType"]=e[3]
t["minecartName"]=raw_event[4] t["minecartName"]=e[4]
t["primaryColor"]=raw_event[5] t["primaryColor"]=e[5]
t["secondaryColor"]=raw_event[6] t["secondaryColor"]=e[6]
t["destination"]=raw_event[7] t["destination"]=e[7]
t["ownerName"]=raw_event[8] t["ownerName"]=e[8]
end end
tb["aspect_changed"]=function(raw_event,t) tb["aspect_changed"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["address"]=raw_event[2] t["address"]=e[2]
t["signalName"]=raw_event[3] t["signalName"]=e[3]
t["signalValue"]=raw_event[4] t["signalValue"]=e[4]
end end
tb["chat_message"]=function(raw_event,t) tb["chat_message"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["address"]=raw_event[2] t["address"]=e[2]
t["username"]=raw_event[3] t["username"]=e[3]
t["message"]=raw_event[4] t["message"]=e[4]
end end
tb["switch_flipped"]=function(raw_event,t) tb["switch_flipped"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["address"]=raw_event[2] t["address"]=e[2]
t["index"]=raw_event[3] t["index"]=e[3]
t["newState"]=raw_event[4] t["newState"]=e[4]
end end
--- OpenSecurity --- OpenSecurity
tb["magData"]=function(raw_event,t) tb["magData"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["address"]=raw_event[2] t["address"]=e[2]
t["user"]=raw_event[3] t["user"]=e[3]
t["content"]=raw_event[4] t["content"]=e[4]
t["uuid"]=raw_event[5] t["uuid"]=e[5]
t["locked"]=raw_event[6] t["locked"]=e[6]
t["side"]=raw_event[7] t["side"]=e[7]
end end
tb["cardInsert"]=function(raw_event,t) tb["cardInsert"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["address"]=raw_event[2] t["address"]=e[2]
end end
tb["cardRemove"]=tb["cardInsert"] tb["cardRemove"]=tb["cardInsert"]
tb["keypad"]=function(raw_event,t) tb["keypad"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["address"]=raw_event[2] t["address"]=e[2]
t["id"]=raw_event[3] t["id"]=e[3]
t["label"]=raw_event[4] t["label"]=e[4]
end end
tb["bioReader"]=function(raw_event,t) tb["bioReader"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["address"]=raw_event[2] t["address"]=e[2]
t["uuid"]=raw_event[3] t["uuid"]=e[3]
end end
-- OpenGlasses -- OpenGlasses
tb["glasses_on"]=function(raw_event,t) tb["glasses_on"]=function(e,t)
t["user"]=raw_event[1] t["user"]=e[1]
end end
tb["glasses_off"]=tb["glasses_on"] tb["glasses_off"]=tb["glasses_on"]
tb["interact_world_left"]=function(raw_event,t) tb["interact_world_left"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["address"]=raw_event[2] t["address"]=e[2]
t["user"]=raw_event[3] t["user"]=e[3]
t["pos"]={ t["pos"]={
["x"]=raw_event[4], ["x"]=e[4],
["y"]=raw_event[5], ["y"]=e[5],
["z"]=raw_event[6] ["z"]=e[6]
} }
t["look"]={ t["look"]={
["x"]=raw_event[7], ["x"]=e[7],
["y"]=raw_event[8], ["y"]=e[8],
["z"]=raw_event[9] ["z"]=e[9]
} }
t["eyeHeight"]=raw_event[10] t["eyeHeight"]=e[10]
end end
tb["interact_world_right"]=tb["interact_world_left"] tb["interact_world_right"]=tb["interact_world_left"]
tb["interact_overlay"]=function(raw_event,t) tb["interact_overlay"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["address"]=raw_event[2] t["address"]=e[2]
t["user"]=raw_event[3] t["user"]=e[3]
t["button"]=raw_event[4] t["button"]=e[4]
t["x"]=raw_event[5] t["x"]=e[5]
t["y"]=raw_event[6] t["y"]=e[6]
t["width"]=raw_event[7] t["width"]=e[7]
t["height"]=raw_event[8] t["height"]=e[8]
end end
--- LibNetBox --- LibNetBox
tb["net_message"]=function(raw_event,t) tb["net_message"]=function(e,t)
t["event"]=raw_event[1] t["event"]=e[1]
t["receiverAddress"]=raw_event[2] t["receiverAddress"]=e[2]
t["senderAddress"]=raw_event[3] t["senderAddress"]=e[3]
t["port"]=raw_event[4] t["port"]=e[4]
t["data"]={} t["data"]={}
for i=5,raw_event.n,1 do for i=5,e.n,1 do
t["data"][i-4]=raw_event[i] t["data"][i-4]=e[i]
end end
t["data"].n=raw_event.n-4; t["data"].n=e.n-4;
end end
-- Mark as inited. -- Mark as inited.
_hasInited=true _hasInited=true
end end
local function TranslateEvent(raw_event) local function TranslateEvent(e)
if(raw_event==nil) then return nil end if(e==nil) then return nil end
local t={} local t={}
local name=raw_event[1] local name=e[1]
if(name==nil) then return nil end if(name==nil) then return nil end
t["event"]=name t["event"]=name
-- Standard Events -- Standard Events
if(internal_evtb[name]~=nil) then if(internal_evtb[name]~=nil) then
internal_evtb[name](raw_event,t) internal_evtb[name](e,t)
-- External Events -- External Events
elseif(internal_evtbex[name]~=nil) then elseif(internal_evtbex[name]~=nil) then
internal_evtbex[name](raw_event,t) internal_evtbex[name](e,t)
-- Unknown Events. Args is packed into t.data (instead of returning the list) -- Unknown Events. Args is packed into t.data (instead of returning the list)
else else
t["data"]=table.pack(raw_event,2) t["data"]=table.pack(e,2)
end end
setmetatable(t,{__index=function(xt,xk) setmetatable(t,{__index=function(xt,xk)
@ -302,8 +332,8 @@ function AddEventListener(EventName,CallbackFunction)
checkfunction(CallbackFunction) checkfunction(CallbackFunction)
return event.listen(EventName, return event.listen(EventName,
function(...) function(...)
local raw_event=table.pack(...) local e=table.pack(...)
local rt=table.pack(TranslateEvent(raw_event)) local rt=table.pack(TranslateEvent(e))
if(type(rt[1])=="table") then if(type(rt[1])=="table") then
return CallbackFunction(rt[1]) return CallbackFunction(rt[1])
else else