1
0
mirror of https://github.com/LDDestroier/CC/ synced 2025-01-31 11:19:11 +00:00

Fixed it up real good

This commit is contained in:
LDDestroier 2019-05-02 00:04:12 -04:00 committed by GitHub
parent 9f8df17943
commit 72ec4a3a6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,15 +1,35 @@
local disknet = {} local disknet = {}
disknet.mainPath = ({...})[1] or "disk/DISKNET" local tArg = {...}
disknet.mainPath = tArg[1] or "disk/DISKNET"
local limitChannelsToModem = false local limitChannelsToModem = false
local maximumBufferSize = 32
local openChannels = {} local openChannels = {}
local yourID = os.getComputerID() local yourID = os.getComputerID()
local uniqueID = math.random(1, 2^31 - 1) -- prevents receiving your own messages
local msgCheckList = {} -- makes sure duplicate messages aren't received
-- do not think for one second that os.epoch("utc") would be a proper substitute
local getTime = function() local getTime = function()
return os.time() + (-1 + os.day()) * 24 return os.time() + (-1 + os.day()) * 24
end end
local readFile = function(path)
local file = fs.open(path, "r")
local contents = file.readAll()
file.close()
return contents
end
local writeFile = function(path, contents)
local file = fs.open(path, "w")
file.write(contents)
file.close()
end
-- if 'limitChannelsToModem', then will make sure that channel is a number between 0 and 65535
local checkValidChannel = function(channel) local checkValidChannel = function(channel)
if limitChannelsToModem then if limitChannelsToModem then
if type(channel) == "number" then if type(channel) == "number" then
@ -73,19 +93,35 @@ disknet.closeAll = function()
openChannels = {} openChannels = {}
end end
disknet.send = function(channel, message)
disknet.transmit = function(channel, message)
local valid, grr = checkValidChannel(channel) local valid, grr = checkValidChannel(channel)
if valid then if valid then
local contents = textutils.unserialize(readFile(fs.combine(disknet.mainPath, tostring(channel))))
if disknet.isOpen(channel) then if disknet.isOpen(channel) then
local file = fs.open(fs.combine(disknet.mainPath, tostring(channel)), "a") local file = fs.open(fs.combine(disknet.mainPath, tostring(channel)), "w")
file.write(textutils.serialize({ if contents then
time = getTime(), contents[#contents + 1] = {
id = yourID, time = getTime(),
channel = channel, id = yourID,
message = message, uniqueID = uniqueID,
})) messageID = math.random(1, 2^31 - 1),
channel = channel,
message = message,
}
if #contents > maximumBufferSize then
table.remove(contents, 1)
end
file.write(textutils.serialize(contents))
else
file.write(textutils.serialize({{
time = getTime(),
id = yourID,
uniqueID = uniqueID,
messageID = math.random(1, 2^31 - 1),
channel = channel,
message = message,
}}))
end
file.close() file.close()
return true return true
else else
@ -96,50 +132,78 @@ disknet.transmit = function(channel, message)
end end
end end
local fList, pList = {}, {}
local loadFList = function()
fList, pList = {}, {}
if channel then
fList = {fs.open(fs.combine(disknet.mainPath, tostring(channel)), "r")}
pList = {fs.combine(disknet.mainPath, tostring(channel))}
else
for i = 1, #openChannels do
fList[i] = fs.open(fs.combine(disknet.mainPath, tostring(openChannels[i])), "r")
pList[i] = fs.combine(disknet.mainPath, tostring(openChannels[i]))
end
end
end
disknet.receive = function(channel) disknet.receive = function(channel)
local valid, grr = checkValidChannel(channel) local valid, grr = checkValidChannel(channel)
if valid or not channel then if valid or not channel then
local fList, contents = {} local output, contents
local doRewrite = false
-- clear files
if channel then
if openChannels[channel] then
file = fs.open(fs.combine(disknet.mainPath, tostring(channel)), "w")
file.close()
fList[1] = fs.open(fs.combine(disknet.mainPath, tostring(channel)), "r")
end
else
for i = 1, #openChannels do
file = fs.open(fs.combine(disknet.mainPath, tostring(openChannels[i])), "w")
file.close()
fList[i] = fs.open(fs.combine(disknet.mainPath, tostring(openChannels[i])), "r")
end
end
-- constantly check channel files
local returnChannel
while true do while true do
loadFList()
for i = 1, #fList do for i = 1, #fList do
contents = fList[i].readAll() contents = fList[i].readAll()
if contents ~= "" then if contents ~= "" then
returnChannel = channel or openChannels[i] contents = textutils.unserialize(contents)
break if type(contents) == "table" then
if contents[1] then
if (not output) and (contents[1].uniqueID ~= uniqueID) and (not msgCheckList[contents[1].messageID]) then
if getTime() - (contents[1].time or 0) <= 0.01 then
msgCheckList[contents[1].messageID] = true
output = {}
for k,v in pairs(contents[1]) do
output[k] = v
end
end
end
doRewrite = false
for t = #contents, 1, -1 do
if getTime() - (contents[t].time or 0) > 0.01 then
table.remove(contents, t)
doRewrite = true
end
end
if doRewrite then
writeFile(pList[i], textutils.serialize(contents))
end
if output then
for i = 1, #fList do
fList[i].close()
end
break
end
end
end
end end
end end
if returnChannel then if output then
break break
else else
for i = 1, #fList do
fList[i].close()
end
os.queueEvent("") os.queueEvent("")
os.pullEvent("") os.pullEvent("")
end end
end end
for i = 1, #fList do
fList[i].close()
end
contents = textutils.unserialize(contents)
if contents then if contents then
return contents.message, returnChannel, contents.id, contents.time return output.message, output.channel, output.id, output.time
else else
return nil return nil
end end