Fixed compatibility for CC 1.41

Also, compatibility with os.loadAPI() has been added, in case you happen to have a version of CC that DOESN'T HAVE REQUIRE or DOESN'T LET DOFILE RETURN ANYTHING
This commit is contained in:
LDDestroier 2020-08-25 15:09:46 -04:00 committed by GitHub
parent 8546f2d807
commit 89e238d07b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 22 additions and 9 deletions

View File

@ -1,10 +1,10 @@
local disknet = {} local disknet = disknet or {}
local tArg = {...} local tArg = {...}
disknet.mainPath = "disk/DISKNET" -- path of shared file disknet.mainPath = "disk/DISKNET" -- path of shared file
local limitChannelsToModem = false -- if true, can only use number channels from 1 to 65535 local limitChannelsToModem = false -- if true, can only use number channels from 1 to 65535
local checkDelay = 0.05 -- amount of time (seconds) between checking the file -- if 0, checks super fast so don't do that local checkDelay = 0.05 -- amount of time (seconds) between checking the file -- if 0, checks super fast so don't do that
local maximumBufferSize = 64 -- largest amount of messages per channel buffered local maximumBufferSize = 64 -- largest amount of messages per channel buffered
local isUsingTweaked = false local isUsingTweaked = false
@ -17,15 +17,19 @@ end
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 uniqueID = math.random(1, 2^31 - 1) -- prevents receiving your own messages
local msgCheckList = {} -- makes sure duplicate messages aren't received disknet.msgCheckList = {} -- makes sure duplicate messages aren't received
local ageToToss = 0.005 -- amount of time before a message is removed local ageToToss = 0.005 -- amount of time before a message is removed
-- used for synching times between different emulators -- used for synching times between different emulators
disknet._timeMod = 0 disknet._timeMod = 0
-- do not think for one second that os.epoch("utc") would be a proper substitute -- 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) + disknet._timeMod if os.day then
return (os.time() + (-1 + os.day()) * 24) + disknet._timeMod
else
return os.time() + disknet._timeMod
end
end end
local function serialize(tbl) local function serialize(tbl)
@ -110,6 +114,7 @@ disknet.isOpen = function(channel)
error(grr) error(grr)
end end
end end
isOpen = disknet.isOpen
disknet.open = function(channel) disknet.open = function(channel)
local valid, grr = checkValidChannel(channel) local valid, grr = checkValidChannel(channel)
@ -120,6 +125,7 @@ disknet.open = function(channel)
error(grr) error(grr)
end end
end end
open = disknet.open
disknet.close = function(channel) disknet.close = function(channel)
local valid, grr = checkValidChannel(channel) local valid, grr = checkValidChannel(channel)
@ -135,15 +141,18 @@ disknet.close = function(channel)
error(grr) error(grr)
end end
end end
close = disknet.close
disknet.closeAll = function() disknet.closeAll = function()
openChannels = {} openChannels = {}
end end
closeAll = disknet.closeAll
disknet.send = function(channel, message, recipient) disknet.send = function(channel, message, recipient)
local valid, grr = checkValidChannel(channel) local valid, grr = checkValidChannel(channel)
if valid then if valid then
if not fs.exists(fs.combine(disknet.mainPath, tostring(channel))) then if not fs.exists(fs.combine(disknet.mainPath, tostring(channel))) then
fs.makeDir(disknet.mainPath)
fs.open(fs.combine(disknet.mainPath, tostring(channel)), "w").close() fs.open(fs.combine(disknet.mainPath, tostring(channel)), "w").close()
end end
local contents = textutils.unserialize(readFile(fs.combine(disknet.mainPath, tostring(channel)))) local contents = textutils.unserialize(readFile(fs.combine(disknet.mainPath, tostring(channel))))
@ -188,6 +197,7 @@ disknet.send = function(channel, message, recipient)
error(grr) error(grr)
end end
end end
send = disknet.send
local fList, pList, sList = {}, {}, {} local fList, pList, sList = {}, {}, {}
@ -204,6 +214,7 @@ local loadFList = function()
end end
end end
-- returns: string message, string/number channel, number senderID, number timeThatMessageWasSentAt
disknet.receive = function(channel, senderFilter) disknet.receive = function(channel, senderFilter)
local valid, grr = checkValidChannel(channel) local valid, grr = checkValidChannel(channel)
if valid or not channel then if valid or not channel then
@ -224,7 +235,7 @@ disknet.receive = function(channel, senderFilter)
if contents[1] then if contents[1] then
if not output then if not output then
for look = 1, #contents do for look = 1, #contents do
if (contents[look].uniqueID ~= uniqueID) and (not msgCheckList[contents[look].messageID]) then -- make sure you're not receiving messages that you sent if (contents[look].uniqueID ~= uniqueID) and (not disknet.msgCheckList[contents[look].messageID]) then -- make sure you're not receiving messages that you sent
if (not contents[look].recipient) or contents[look].recipient == yourID then -- make sure that messages intended for others aren't picked up if (not contents[look].recipient) or contents[look].recipient == yourID then -- make sure that messages intended for others aren't picked up
if (not channel) or channel == contents[look].channel then -- make sure that messages are the same channel as the filter, if any if (not channel) or channel == contents[look].channel then -- make sure that messages are the same channel as the filter, if any
if (not senderFilter) or senderFilter == contents[look].id then -- make sure that the sender is the same as the id filter, if any if (not senderFilter) or senderFilter == contents[look].id then -- make sure that the sender is the same as the id filter, if any
@ -234,7 +245,7 @@ disknet.receive = function(channel, senderFilter)
goWithIt = true goWithIt = true
end end
if cTime - (contents[look].time or 0) <= ageToToss or goWithIt then -- make sure the message isn't too old if cTime - (contents[look].time or 0) <= ageToToss or goWithIt then -- make sure the message isn't too old
msgCheckList[contents[look].messageID] = true disknet.msgCheckList[contents[look].messageID] = true
output = {} output = {}
for k,v in pairs(contents[look]) do for k,v in pairs(contents[look]) do
output[k] = v output[k] = v
@ -252,7 +263,7 @@ disknet.receive = function(channel, senderFilter)
doRewrite = false doRewrite = false
for t = #contents, 1, -1 do for t = #contents, 1, -1 do
if cTime - (contents[t].time or 0) > ageToToss or cTime - (contents[t].time or 0) < -1 then if cTime - (contents[t].time or 0) > ageToToss or cTime - (contents[t].time or 0) < -1 then
msgCheckList[contents[t].messageID] = nil disknet.msgCheckList[contents[t].messageID] = nil
table.remove(contents, t) table.remove(contents, t)
doRewrite = true doRewrite = true
end end
@ -300,6 +311,7 @@ disknet.receive = function(channel, senderFilter)
error(grr) error(grr)
end end
end end
receive = disknet.receive
-- not really needed if going between CCEmuX and another emulator, but may be needed between two separate CCEmuX daemons -- not really needed if going between CCEmuX and another emulator, but may be needed between two separate CCEmuX daemons
disknet.receive_TS = function(...) disknet.receive_TS = function(...)
@ -309,5 +321,6 @@ disknet.receive_TS = function(...)
end end
return message, channel, id, time return message, channel, id, time
end end
receive_TS = disknet.receive_TS
return disknet return disknet