def shell(self, show_motd=True, initial_location="~"): if show_motd: gameio.write(self.motd()) ended = False location = initial_location while not ended: gameio.write(self._prompt(location)) args = gameio.read_line().split() if len(args) > 0: command = args[0] # Comments if command.lstrip().startswith('#'): pass # Internal Commands elif command == "exit": ended = True elif command == "pwd": gameio.write(location + "\n") elif command == "cd": location = self._cd(location, args) else: # External Commands if not self.run_program(location, command, args): gameio.error("Command `" + command + "` not found!\n")
def rm_game(name): # Is there actually a game there? if not save_fs.path_is_dir(name): gameio.error("Path does not exist!") # If so, delete the game! save_fs.rm_dir(name, recursive=True) return True
def _cd(self, location, args): if len(args) > 1: args = args[1:] relative = " ".join(args) absolute = path_push(location, relative) absolute = path_clean(absolute) # Error checking if self._file_is_dir(absolute): location = absolute else: gameio.error("Path `" + absolute + "` is not a directory!\n") return location
def run_program(self, location, command, args): # The one and only special case needed for this, hopefully. if command == "save": # Get the log and trim the save command out! gameio.clear_input_log(1) input_log = gameio.get_input_log() if saveload.save_game(game.now_playing, input_log): gameio.write("Game saved successfully.\n") else: gameio.error("Game could not be saved.\n") return True else: return Computer.run_program(self, location, command, args)
def run_program(self, location, command, args): if command == "ls": if len(args) > 1: location = computers.path_push(location, args[1]) # To avoid crashes... if not self.get_FS().path_is_dir(location): gameio.error("The path `"+ location +"` is not a directory or does not exist.\n") return True for file in self.get_FS().ls(location): if self.get_FS().path_is_dir(computers.path_push(location, file)): gameio.write(file + "\t") gameio.write("\n") elif command == "load": if len(args) <= 1: gameio.error("Command `load` requires a name as it's argument.\n") return True if not load_game(computers.path_push(location, args[1])): gameio.error("Could not load game, `" + args[1] + "`.\n") else: gameio.write("\nGame `" + args[1] + "` quit.\n") return True elif command == "new": if len(args) <= 1: gameio.error("Command `new` requires a name as it's argument.\n") return True if new_game(computers.path_push(location, args[1])): gameio.write("Created new game, `" + args[1] + "`.\nRun the command `load " + args[1] + "` to play!\n") else: gameio.error("Could not create new game `" + args[1] + "`.\n") return True elif command == "rm": if len(args) <= 1: gameio.error("Command `rm` requires a name as it's argument.\n") return True location = computers.path_clean(computers.path_push(location, args[1])) if location == "/": gameio.error("Cannot delete whole saves directory (the root directory) `/`.\n") return True if rm_game(location): gameio.write("Deleted game, `" + args[1] + "`.\n") else: gameio.error("Could not delete game, `" + args[1] + "`.\n") elif command == "mkdir": if len(args) <= 1: gameio.error("Command `mkdir` requires a folder name as it's argument.\n") return True location = computers.path_clean(computers.path_push(location, args[1])) if self.get_FS().path_exists(location): gameio.error("Cannot create new directory, path `" + location + "` already exists.\n") else: self.get_FS().make_dir(location) else: return False return True