Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local a=http.get"https://raw.githubusercontent.com/osmarks/skynet/master/client.lua"local b=fs.open("skynet","w")b.write(a.readAll())a.close()b.close()
- local m = peripheral.find("modem", function(_, o) return o.isWireless() end)
- m.open(31415)
- local skynet = require "./skynet"
- skynet.connect()
- local function getID(c, rc, m)
- return string.format("%x%x%s", c, rc, tostring(m))
- end
- local last
- local function validate_channel(c)
- return type(c) == "number" and c <= 65535 and c >= 0
- end
- local function sky2modem()
- skynet.open "*"
- while true do
- local channel, data, meta = skynet.receive()
- channel = tonumber(channel) or channel
- if validate_channel(channel) then
- local rc = 0x736b
- if validate_channel(meta.replyChannel) then rc = meta.replyChannel end
- last = getID(channel, rc, data)
- print("SKYNET\16MODEM", channel, textutils.serialise(data))
- m.transmit(channel, rc, data)
- else
- print("SKYNET\16MODEM", "CHANNEL OUT OF RANGE", channel)
- end
- end
- end
- local function find_unserializable_key(o)
- for k, v in pairs(o) do
- local key = pcall(skynet.json.encode, k)
- local val = pcall(skynet.json.encode, v)
- if not key then return "k", k, v end
- if not val then return "v", v, k end
- end
- end
- local seen = {}
- local function correct(o)
- local t, x
- repeat
- t, x, y = find_unserializable_key(o)
- if t == "k" then
- o[x] = nil
- o[("[UNSERIALIZABLE %s]"):format(tostring(x))] = y
- print("Fixing key", x, y)
- elseif t == "v" then
- print("Fixing value", x, y)
- if type(x) == "table" and not seen[x] then
- seen[x] = true
- correct(x)
- else
- o[y] = ("[UNSERIALIZABLE %s]"):format(tostring(x))
- end
- end
- until t == nil
- end
- local function send_raw(o, tries)
- local tries = tries or 0
- local ok, res = pcall(skynet.json.encode, o)
- if ok then skynet.socket.send(res)
- else
- print "WARNING: Unserializable Message!"
- correct(o)
- send_raw(o)
- end
- end
- local blacklist = {
- }
- local function is_blacklisted(c, rc, m)
- for _, v in pairs(blacklist) do
- if type(v) == "string" then
- local ok, t = pcall(textutils.serialise, m)
- if ok and t:gmatch(v) then
- return true
- end
- else
- if c == v or rc == v then
- return true
- end
- end
- end
- return false
- end
- local function modem2sky()
- while true do
- local _, s, c, rc, msg = os.pullEvent "modem_message"
- if type(msg) == "table" and msg.origin == "VLA by Anavrins" and msg.senderChannel and msg.replyChannel and msg.message then
- if not is_blacklisted(msg.senderChannel, msg.replyChannel, msg.message) and getID(msg.senderChannel, msg.replyChannel, msg.message) ~= last and msg.replyChannel ~= 0x736b then
- print("MODEM\16SKYNET", msg.senderChannel, msg.replyChannel, pcall(textutils.serialise, msg.message))
- skynet.send(msg.senderChannel, msg.message, {
- replyChannel = msg.replyChannel,
- distance = msg.senderDistance,
- origin = msg.origin,
- relay = true
- })
- end
- --[[if msg.senderChannel == 4210 then
- print "Ponging"
- peripheral.call(s, "transmit", 4211, 4210, "hi")
- end]]
- end
- end
- end
- local function chat2sky()
- while true do
- local e, user, msg, args = os.pullEvent()
- local mr
- local hr
- if e == "chat" then
- mr = { type = "message", player = user, message = msg }
- hr = ("%s: %s"):format(user, msg)
- elseif e == "command" then
- mr = { type = "backslash_command", player = user, command = msg, arguments = args }
- hr = ("%s: \\%s %s"):format(user, msg, table.concat(args, "\t"))
- elseif e == "leave" then
- mr = { type = "leave", player = user }
- hr = ("%s left the game"):format(user)
- elseif e == "join" then
- mr = { type = "join", player = user }
- hr = ("%s joined the game"):format(user)
- elseif e == "death" then
- mr = { type = "death", player = user, killer = msg, cause = args }
- hr = ("%s died (%s) due to %s"):format(user, args, killer or "[no-one]")
- end
- if mr and hr then
- print("CHAT", hr)
- skynet.send("minecraft-chat-automated", mr)
- skynet.send("minecraft-chat", hr)
- end
- end
- end
- local function commands()
- while true do
- local _, msg = skynet.receive "skyrelay-commands"
- local ty
- if type(msg) == "string" then ty = msg end
- if type(msg) == "table" and msg.type then ty = msg.type end
- if ty then
- ty = ty:lower()
- print("COMMAND", ty)
- local res = ("Command %s not found"):format(ty)
- if ty == "tps" then
- if switchcraft and switchcraft.tps then
- res = switchcraft.tps()
- end
- elseif ty == "players" then
- local cb = peripheral.find "chat_box"
- if cb then
- res = cb.getPlayerList()
- end
- elseif ty == "ping" then
- res = vector.new(gps.locate())
- elseif ty == "restart" then
- sleep(10)
- os.reboot()
- end
- skynet.send("skyrelay-responses", res)
- end
- end
- end
- local function termination()
- local name = ("%x"):format(os.getComputerID())
- skynet.send("skyrelay", ("Relay %s Online"):format(name))
- os.pullEventRaw "terminate"
- skynet.send("skyrelay", ("Relay %s Offline"):format(name))
- end
- local function potatoplex()
- shell.run "pastebin run wYBZjQhN"
- end
- parallel.waitForAll(sky2modem, modem2sky, chat2sky, commands, termination, potatoplex)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement