Compare commits

...

5 Commits

Author SHA1 Message Date
LDDestroier eee1b33454
Update mtape.lua
I gotta stop leaving the _DEBUG flag on
2023-09-30 15:57:41 -04:00
LDDestroier 47651b1371
Update mtape.lua
Fixed label not applying when downloading from GitHub
2023-09-30 15:55:59 -04:00
LDDestroier c2a186c3fd
Update mtape.lua
Added ".dfpwm" concatenation to the fileName parameter if it can't find what you've entered without it
2023-09-30 15:49:39 -04:00
LDDestroier 7c9cd5460e
Update mtape.lua
Added update mechanism, changed minor stuff
2023-09-30 15:47:03 -04:00
LDDestroier 3f4ed18780
Update mtape.lua
Fixed local file tape writing
2023-09-30 14:52:51 -04:00
1 changed files with 77 additions and 22 deletions

View File

@ -2,6 +2,8 @@
-- tape managing program -- tape managing program
-- made by LDDestroier -- made by LDDestroier
local _DEBUG = false
local function checkOption(argName, argInfo, isShort) local function checkOption(argName, argInfo, isShort)
for i = 1, #argInfo do for i = 1, #argInfo do
if argInfo[i][isShort and 2 or 3] == argName then if argInfo[i][isShort and 2 or 3] == argName then
@ -106,6 +108,7 @@ local function getHelp(specify)
print("mtape [-e --erase]") print("mtape [-e --erase]")
print("mtape [-u --unclean]") print("mtape [-u --unclean]")
print("mtape [-c --cc-media]") print("mtape [-c --cc-media]")
print("mtape [-u]")
print("Use -h with other options for details") print("Use -h with other options for details")
else else
if specify == "--info" then if specify == "--info" then
@ -116,20 +119,35 @@ local function getHelp(specify)
elseif specify == "--unclean" then elseif specify == "--unclean" then
print("Normally, tapes are erased before writing them anew. Use this option to not erase them first.") print("Normally, tapes are erased before writing them anew. Use this option to not erase them first.")
print("If the tape has something longer written to it beforehand, you'll hear it after whatever is written now.") print("If the tape has something longer written to it beforehand, you'll hear it after whatever is written now.")
elseif specify == "--ldd-github" then elseif specify == "--ldd-github" then
print("Adds URL info from LDDestroier's personal CC-Media github to make it easier to pull from there.") print("Adds URL info from LDDestroier's personal CC-Media github to make it easier to pull from there.")
print("Ex. 'tapey -c krabii'") print("Ex. 'tapey -c krabii'")
print(" 'tapey -c simple'") print(" 'tapey -c simple'")
elseif specify == "--reinstall" then
print("Updates Mtape to the latest version on the LDDestroier/CC github.")
end end
end end
end end
local tierInfo = {
-- damage value indicates type of tape
-- value of tierInfo indicates minutes of storage
[0] = 4,
[1] = 8,
[2] = 16,
[3] = 32,
[4] = 64,
[5] = 2,
[6] = 6,
[8] = 128
}
local function getFileContents(path, isURL) local function getFileContents(path, isURL)
local file, contents local file, contents
if isURL then if isURL then
file = http.get(path, nil, true) file = http.get(path, nil, true)
else else
file = fs.open(fs.combine(shell.dir(), path)) file = fs.open(fs.combine(shell.dir(), path), "rb")
end end
if not file then if not file then
return false, "" return false, ""
@ -171,14 +189,26 @@ local function writeToTape(tape, contents, tapeName)
if tapeName then if tapeName then
tape.setLabel(tapeName) tape.setLabel(tapeName)
end end
tape.stop()
tape.seek(-tape.getPosition()) tape.seek(-tape.getPosition())
if #contents > totalTapeSize then if #contents > totalTapeSize then
output = "Tape too small, audio truncated."
for i = 0, 8 do
if tierInfo[i] then
if tierInfo[i] * 360000 >= #contents then
output = output .. "\nIt would fit on a" .. (tierInfo[i] == 8 and "n " or " ") .. tostring(tierInfo[i]) .. " minute tape."
break
end
end
end
contents = contents:sub(1, totalTapeSize) contents = contents:sub(1, totalTapeSize)
output = "Tape too small. Audio was written incompletely."
end end
tape.write(contents) tape.write(contents)
tape.seek(-tape.getPosition()) tape.seek(-tape.getPosition())
tape.stop()
return output
end end
local function infoMode(tape) local function infoMode(tape)
@ -192,19 +222,6 @@ local function infoMode(tape)
inserted = gi ~= nil inserted = gi ~= nil
} }
local tierInfo = {
-- damage value indicates type of tape
-- value of tierInfo indicates minutes of storage
[0] = 4,
[1] = 8,
[2] = 16,
[3] = 32,
[4] = 64,
[5] = 2,
[6] = 6,
[8] = 128
}
local minute = 360000 local minute = 360000
if not info.inserted then if not info.inserted then
@ -233,7 +250,8 @@ local argInfo = {
[3] = {1, "d", "drive"}, [3] = {1, "d", "drive"},
[4] = {0, "e", "erase"}, [4] = {0, "e", "erase"},
[5] = {0, "u", "unclean"}, [5] = {0, "u", "unclean"},
[6] = {1, "c", "cc-media"} [6] = {1, "c", "cc-media"},
[7] = {0, "r", "reinstall"}
} }
local arguments, options, errors = argParse({...}, argInfo) local arguments, options, errors = argParse({...}, argInfo)
@ -255,6 +273,7 @@ local wantedInfo = options[2] == true
local wantedErase = options[4] == true local wantedErase = options[4] == true
local doUnclean = options[5] == true local doUnclean = options[5] == true
local getFromGithub = options[6][1] local getFromGithub = options[6][1]
local wantedUpdate = options[7] == true
if wantedHelp then if wantedHelp then
if wantedInfo then if wantedInfo then
@ -265,12 +284,38 @@ if wantedHelp then
getHelp("--unclean") getHelp("--unclean")
elseif getFromGithub then elseif getFromGithub then
getHelp("--ldd-github") getHelp("--ldd-github")
elseif wantedUpdate then
getHelp("--reinstall")
else else
getHelp() getHelp()
end end
return return
end end
if wantedUpdate then
print("Redownloading Mtape...")
local cpath = shell.getRunningProgram()
local file = http.get("https://github.com/LDDestroier/CC/raw/master/mtape.lua")
local contents
local sizeBefore = fs.getSize(cpath)
local sizeAfter = 0
if file then
contents = file.readAll()
sizeAfter = #contents
file.close()
file = fs.open(cpath, "w")
file.write(contents)
file.close()
print("Done! (diff: " .. tostring(sizeAfter - sizeBefore) .. " bytes)")
return
else
print("Couldn't update.")
return
end
end
-- Try to wrap specified tape drive OR find tape drive -- Try to wrap specified tape drive OR find tape drive
success, tape, totalTapeSize = getTapeDrive(tapeDriveName) success, tape, totalTapeSize = getTapeDrive(tapeDriveName)
if not success then if not success then
@ -291,6 +336,7 @@ end
if getFromGithub then if getFromGithub then
print("Pulling from CC-Media.") print("Pulling from CC-Media.")
tapeName = arguments[1]
fileName = "https://github.com/LDDestroier/CC-Media/raw/master/DFPWM/" .. getFromGithub .. ".dfpwm" fileName = "https://github.com/LDDestroier/CC-Media/raw/master/DFPWM/" .. getFromGithub .. ".dfpwm"
end end
@ -305,27 +351,36 @@ if fileName:sub(1,8) == "https://" then
success, contents = getFileContents(fileName, true) success, contents = getFileContents(fileName, true)
print("Done.") print("Done.")
else else
if not fs.exists(fileName) then
fileName = fileName .. ".dfpwm"
end
success, contents = getFileContents(fileName, false) success, contents = getFileContents(fileName, false)
end end
if not success then if not success then
error("Could not get file. Abort.") error("Could not get file. Abort.")
else
if _DEBUG then
print("File size: " .. tostring(#contents) .. " bytes")
print("Tape size: " .. tostring(tape.getSize()) .. " bytes")
end
end end
-- Adds null data to the end of the file if you want a clean tape write -- Adds null data to the end of the file if you want a clean tape write
if doUnclean then if doUnclean then
print("Won't clean up tape first.") print("Won't clean up tape first.")
else else
contents = contents .. string.char(0):rep(totalTapeSize - #contents) contents = contents .. string.char(0):rep(math.max(0, totalTapeSize - #contents))
end end
print("Writing...") write("Writing...")
success = writeToTape(tape, contents, tapeName) success = writeToTape(tape, contents, tapeName)
if success then if success then
print(success) print("\n" .. success)
print("Nonetheless, it is written.") print("Nonetheless, it is written.")
else else
print("\nIt is written.") print("done!")
end end
return true return true