Files

disk.src
  • MAX_FILE_CONTENT = 160000 // limited by the game
  • MAX_NESTED_FOLDERS = 16 // limited by the game
  • MAX_FILES = 249 // limited by the game
  • FOLDER_PER_BLOCK = 2
  • Block = {}
  • Block.init = function(disk, folder)
  • self.disk = disk
  • self.folder = folder
  • self.name = folder.name
  • self.path = folder.path
  • self.full_path = self.path + "/" + self.name
  • self.depth = self.path.split("/").len - 1
  • self.is_at_max_depth = self.depth == MAX_NESTED_FOLDERS
  • self.is_at_max_folders = self.folder.get_folders.len == FOLDER_PER_BLOCK
  • if self.folder.get_files.len == 0 then self.new_file
  • end function
  • Block.last_file = function()
  • return self.folder.get_files[-1]
  • end function
  • Block.new_file = function()
  • blob_name = "0"
  • if self.disk.blobs.len > 0 then blob_name = str(self.disk.blobs[-1].name.to_int + 1)
  • self.disk.computer.touch(self.path, blob_name)
  • self.disk.setup_blocks
  • end function
  • Block.is_full_of_folders = function()
  • if self.is_at_max_depth then
  • return true
  • else
  • if self.is_at_max_folders then
  • for f in self.folder.get_folders
  • n_block = new Block
  • n_block.init(self.disk, f)
  • if n_block.is_full_of_folders == false then return false
  • end for
  • return true
  • end if
  • return false
  • end if
  • end function
  • Disk = {}
  • Disk.init = function(data_folder_path = null, data_folder_name = ".disk")
  • self.shell = get_shell
  • self.computer = self.shell.host_computer
  • self.data_folder_path = data_folder_path
  • if self.data_folder_path == null then self.data_folder_path = home_dir
  • self.data_folder_name = data_folder_name
  • self.data_folder_full_path = self.data_folder_path + "/" + self.data_folder_name
  • self.computer.create_folder(self.data_folder_path, self.data_folder_name)
  • self.data_folder = self.computer.File(self.data_folder_full_path)
  • self.setup_blocks
  • self.tape = ""
  • end function
  • Disk.setup_blocks = function()
  • self.blocks = []
  • self.blobs = []
  • self.computer.create_folder(self.data_folder_full_path, "blocks")
  • first_block_folder = self.computer.File(self.data_folder_full_path + "/blocks")
  • new_block = new Block
  • new_block.init(self, first_block_folder)
  • self.load_blocks(new_block)
  • end function
  • // this wont check for blocks already loaded and will load them again
  • Disk.load_blocks = function(block)
  • self.blocks.push(block)
  • for f in block.folder.get_files
  • self.blobs.push(f)
  • end for
  • for f in block.folder.get_folders
  • new_block = new Block
  • new_block.init(self, f)
  • self.load_blocks(new_block)
  • end for
  • end function
  • Disk.create_new_block = function(block = null)
  • if block == null then block = self.blocks[0]
  • count = self.blocks[-1].name.to_int + 1
  • if self.blocks[-1].name == "blocks" then count = 1
  • if block.folder.get_folders.len == 0 then
  • self.computer.create_folder(block.path, str(count))
  • self.setup_blocks
  • return 1
  • else
  • for f in block.folder.get_folders
  • n_block = new Block
  • n_block.init(block.disk, f)
  • if n_block.is_full_of_folders then continue
  • self.create_new_block(n_block)
  • self.setup_blocks
  • return 1
  • end for
  • self.computer.create_folder(block.path, str(count))
  • self.setup_blocks
  • return 1
  • end if
  • end function
  • Disk.last_block = function()
  • b = self.blocks[-1]
  • if MAX_FILES - b.folder.get_files.len < MAX_FILES then
  • return b
  • end if
  • if b.folder.get_files[-1].get_content.len == MAX_FILE_CONTENT then
  • self.create_new_block
  • return self.blocks[-1]
  • end if
  • end function
  • Disk.last_blob = function()
  • b = self.last_block
  • if b.last_file.get_content.len == MAX_FILE_CONTENT then
  • b.new_file
  • return b.last_file
  • else
  • return b.last_file
  • end if
  • end function
  • Disk.write = function(string)
  • while true
  • available_chars = MAX_FILE_CONTENT - self.last_blob.get_content.len
  • if available_chars > string.len then
  • chars_to_write = string.len
  • else
  • chars_to_write = available_chars
  • end if
  • self.last_blob.set_content(self.last_blob.get_content + string[:chars_to_write])
  • //return
  • if string.len > chars_to_write then
  • string = string[(string.len - chars_to_write) * -1:]
  • else
  • return
  • end if
  • end while
  • last_blob = self.last_blob
  • available_chars = MAX_FILE_CONTENT - last_blob.get_content.len
  • if string.len <= available_chars then
  • last_blob.set_content(last_blob.get_content + string)
  • else
  • last_blob.set_content(last_blob.get_content + string[:available_chars])
  • string = string[(string.len - available_chars) * -1:]
  • self.write(string)
  • end if
  • end function
  • Disk.update = function(offset, string)
  • end function
  • Disk.init()
  • //for i in range(19)
  • // Disk.create_new_block
  • //end for
  • //Disk.create_new_block
  • //Disk.last_block
  • //Disk.last_blob
  • char_100 = "1" * 100
  • char_10000 = char_100 * 100
  • char_100000 = char_10000 * 10
  • char_100000 = char_100000 + char_10000 * 6
  • char_100000 = char_100000 + "yes"
  • Disk.write(char_100000)
  • for i in Disk.blobs
  • print(i.name)
  • print(i.get_content.len)
  • end for