1
0
mirror of https://github.com/LDDestroier/CC/ synced 2024-11-08 19:09:58 +00:00

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

View File

@ -1,10 +1,10 @@
local disknet = {}
local disknet = disknet or {}
local tArg = {...}
disknet.mainPath = "disk/DISKNET" -- path of shared file
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 isUsingTweaked = false
@ -17,15 +17,19 @@ end
local openChannels = {}
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
local ageToToss = 0.005 -- amount of time before a message is removed
disknet.msgCheckList = {} -- makes sure duplicate messages aren't received
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
-- do not think for one second that os.epoch("utc") would be a proper substitute
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
local function serialize(tbl)
@ -110,6 +114,7 @@ disknet.isOpen = function(channel)
error(grr)
end
end
isOpen = disknet.isOpen
disknet.open = function(channel)
local valid, grr = checkValidChannel(channel)
@ -120,6 +125,7 @@ disknet.open = function(channel)
error(grr)
end
end
open = disknet.open
disknet.close = function(channel)
local valid, grr = checkValidChannel(channel)
@ -135,15 +141,18 @@ disknet.close = function(channel)
error(grr)
end
end
close = disknet.close
disknet.closeAll = function()
openChannels = {}
end
closeAll = disknet.closeAll
disknet.send = function(channel, message, recipient)
local valid, grr = checkValidChannel(channel)
if valid 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()
end
local contents = textutils.unserialize(readFile(fs.combine(disknet.mainPath, tostring(channel))))
@ -188,6 +197,7 @@ disknet.send = function(channel, message, recipient)
error(grr)
end
end
send = disknet.send
local fList, pList, sList = {}, {}, {}
@ -204,6 +214,7 @@ local loadFList = function()
end
end
-- returns: string message, string/number channel, number senderID, number timeThatMessageWasSentAt
disknet.receive = function(channel, senderFilter)
local valid, grr = checkValidChannel(channel)
if valid or not channel then
@ -224,7 +235,7 @@ disknet.receive = function(channel, senderFilter)
if contents[1] then
if not output then
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 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
@ -234,7 +245,7 @@ disknet.receive = function(channel, senderFilter)
goWithIt = true
end
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 = {}
for k,v in pairs(contents[look]) do
output[k] = v
@ -252,7 +263,7 @@ disknet.receive = function(channel, senderFilter)
doRewrite = false
for t = #contents, 1, -1 do
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)
doRewrite = true
end
@ -300,6 +311,7 @@ disknet.receive = function(channel, senderFilter)
error(grr)
end
end
receive = disknet.receive
-- not really needed if going between CCEmuX and another emulator, but may be needed between two separate CCEmuX daemons
disknet.receive_TS = function(...)
@ -309,5 +321,6 @@ disknet.receive_TS = function(...)
end
return message, channel, id, time
end
receive_TS = disknet.receive_TS
return disknet