mirror of
				https://github.com/LDDestroier/CC/
				synced 2025-10-31 07:22:59 +00:00 
			
		
		
		
	Update mtape.lua
Added update mechanism, changed minor stuff
This commit is contained in:
		
							
								
								
									
										90
									
								
								mtape.lua
									
									
									
									
									
								
							
							
						
						
									
										90
									
								
								mtape.lua
									
									
									
									
									
								
							| @@ -2,6 +2,8 @@ | |||||||
| -- tape managing program | -- tape managing program | ||||||
| -- made by LDDestroier | -- made by LDDestroier | ||||||
|  |  | ||||||
|  | local _DEBUG = true | ||||||
|  |  | ||||||
| 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 | ||||||
| @@ -120,16 +123,31 @@ local function getHelp(specify) | |||||||
|             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), "r") |         file = fs.open(fs.combine(shell.dir(), path), "rb") | ||||||
|     end |     end | ||||||
|     if not file then |     if not file then | ||||||
|         return false, "" |         return false, "" | ||||||
| @@ -172,13 +190,25 @@ local function writeToTape(tape, contents, tapeName) | |||||||
|         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 | ||||||
| @@ -310,22 +355,27 @@ 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 | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 LDDestroier
					LDDestroier